Java Programming: Understanding Types, Variables, and Data Structures, Slides of Object Oriented Programming

An overview of various topics in java programming, including object-oriented concepts, abstract data types, generics, java collections, reasoning about complex problems, analyzing algorithms, implementing algorithms, testing, data structures such as linked lists, trees, graphs, recursion, algorithmic complexity, and parallelism. It also covers obtaining java and using eclipse ide for development.

Typology: Slides

2013/2014

Uploaded on 01/29/2014

sundar
sundar 🇮🇳

4.7

(9)

104 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Overview and intro to types
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Java Programming: Understanding Types, Variables, and Data Structures and more Slides Object Oriented Programming in PDF only on Docsity!

Overview and intro to types

Welcome

Learning about:

  • OO, abstract data types, generics, Java Collections, …
  • Reasoning about complex problems, analyzing algorithms we create to solve them, and implementing algorithms with elegant, easy-to-understand, correct code
  • Testing; Reasoning about correctness
  • Data structures: linked lists, trees, graphs, etc.
  • Recursion
  • Algorithmic complexity
  • Parallelism —threads of execution

Obtaining Java

  • Follow instructions on our Resources web page
    • Make sure you have Java JDK 1.7, if not download and install. We explain how on the web page.
    • Then download and install the Eclipse Juno « IDE » for Java developers from Eclipse IDE for Java Developers
  • Test it out: launch Eclipse and click “new>Java Project”
    • This is one of a few ways Java can be used
    • When program runs, output is visible in a little console window

Eclipse IDE

  • IDE: Integrated Development Environment
    • Helps you write your code
    • Protects against many common mistakes
    • At runtime, helps with debugging
  • Follow Resources link to download and install “In my country of Kazakhstan everyone is use Eclipse and Java! Java 1.7 is best for hack American web site and steal credit card.”

Academic Integrity… Trust but verify!

  • We use artificial intelligence tools to check each homework assignment - The software is very accurate! - It tests your code and also notices similarities between code written by different people
  • Sure, you can fool this software
    • … but it’s easier to just do the assignments
    • … and if you try to fool it and screw up, you might fail the assignment or even the whole course.

Types in Java

References in text and in JavaSummary type: A.14 slide 4 variable: A.13 slide 7 variable declaration: A.15 slide 7 Primitive types, A.16, back inside cover slide 5 Constants, A.17 slide 20 Assignment, A.18-A.20 slide 8 Casting, A.21 slide 6 Expressions: A.22-A. Precedences: A.24, back inside cover Unicode character codes, back inside cover

Type: Set of values

together with operations on them.

Matlab and Python are weakly typed : One variable can contain at different times a number, a string, an array, etc. One isn’t so concerned with types. Valid Python sequence: x= 100 ; x= ‘Hello World’; x= ( 1 , 2 , 3 , 4 , 5 ); Corresponding Java int x; x= 100 ; x= “Hello”; Java strongly typed : A variable must be declared before it is used and can contain only values of the type with which it is declared Declaration of x: x can contain only values of type int Illegal assignment: “Hello” is not an int

Weakly typed versus strongly typed

Weakly typed : Shorter programs, generally. Programmer has more freedom, language is more liberal in applying operations to values. S trongly typed : Programmer has to be more disciplined. Declarations provide a place for comments about variables. More errors caught at compile-time (e.g. it’s a syntax error to assign a string to an int variable). Note: weak and strong typing not well defined; literature has several definitions

About ‘primitive’ type int

int : values: – 2 31 .. 2 31

  • 1, i.e. operations: +, – , *, /, %, unary – Inside back cover, A-6.. Java Principle: A basic operation of type int must produce an int
  • 2 31 : - 2147483648 WRAP-AROUND Integer.MAX_VALUE: name for max int value: 2 31
  • 1: 2147483647 Integer.MAX_VALUE + 1 is

Primitive number types

Integer types: byte short int long 1 byte 2 bytes 4 bytes 8 bytes Real types: float double – 22.51E 4 bytes 8 bytes 24. Use these to save space. Have an array of 1,000,000 integers in range 0..7? Use a byte array rather than an int array usual operators usual operators Don’t worry about this in next 7 - 8 weeks. Use int and double. Inside back cover, A- 6 .. 7

Char is a number type! Page A- 9 , inside back cover

16 char is a number type: ( int ) 'V' ( char ) 86 Unicode repr. in decimal: 86 'V' Unicode: 16-bit char repr. Encodes chars in just about all languages. In java, use hexadecimal (base 16) char literals: '\u 0041 ' is 'A' '\u 0042 ' is 'B' '\u 0056 ' is 'V' '\u 0024 ' is ‘$' See www.unicode.org '\u 0950 ' is 'ॐ’ —Om, the sound of the universe '\u 5927 ' is '大' —大衛 is (I think) a transliteration '\u 885 b' is '衛' of David into Chinese (Da Wei)

Basic Variable Declaration Page A- 6

17 Declaration : gives name of variable, type of value it can contain int x; (^) Declaration of x, can contain an int value area 20. double double area; Declaration of area, can contain a double value x 5 int int [] a; Declaration of a, can contain a pointer to an int array. We explain arrays much later a int []

Assignment statement type restriction

19 Every expression has a type, which depends on its operators and the types of its operands in a natural way. Rule: In x= e; type of e has to be same as or narrower than type of x. Reason: To avoid possibly losing info without the programmer realizing it. The value of 5 + 1 is automatically cast from type int to type double. double y= 5 + 1; int x= ( int ) (75.5 + 1 ); You can cast to int explicitly. 76 will be stored in x. int x= 75.5 + 1 ; Illegal: The exp value is of type double.

A function in Matlab, Python, and Java

20 function s = sum(a, b) % Return sum of a and b s= a + b; /** return sum of a and b */ public static double sum( double a, double b) { return a + b; } def sum(a, b): """ return sum of a and b""" return a + b Matlab Python Declarations of return type parameters a and b Specification: in comment before function