powerpoint presentation · data conversion: • narrowing or widening • assignment • promotion...

Post on 21-Jun-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

ECE 122

Course Website: http://www.ecs.umass.edu/ece/ece122/

Piazza: https://piazza.com/umass/spring2017/ece122

TA Office Hours

Wed 4:30PM - 6:00PM

Thu 4:30PM - 6:00PM

Fri 3:00PM - 6:00PM

Location: Marston 34

ECE 122- LAB

Outline:

Compile-time vs Run-time error

Data conversion

Operator precedence

Scanner classes

Classes and Objects

Compile-time error

• Syntax errors

• Mismatch in datatypes

See example CompileError1.java & CompileError2.java

Run-time error

• Exceptions

See example RunError.java & InputOutput.java

Data Conversion:

• Narrowing or Widening• Assignment

• Promotion

• Casting

See example Dataconv.java & Temperature.java

Ref: MargretPosch, Java Basics - Promotion and Casting, https://www.youtube.com/watch?v=FNPEKwQUJwU

Ref: MargretPosch, Java Basics - Promotion and Casting, https://www.youtube.com/watch?v=FNPEKwQUJwU

Ref: MargretPosch, Java Basics - Promotion and Casting, https://www.youtube.com/watch?v=FNPEKwQUJwU

Data Conversion:

far = 40.0

celsius0 = 5 / 9 * (far - 32);

= 5/9 * (40.0 – 32)

= 5/9 * 8.0

= 0 * 8.0 = 0.0 * 8.0 = 0.0

celsius1 = (double) 5 / 9 * (far - 32);

= (double) 5/9 * (40.0 – 32)

= (double) 5/9 * 8.0

= 5.0 / 9 * 8.0

= 0.555 * 8.0 = 4.444

Operator precedence See example Arithmetic.java

Scanner classes:

int nextInt() Returns the next token as an int.

long nextLong() Returns the next token as a long

float nextFloat() Returns the next token as a float

double nextDouble() Returns the next token as a double

String nextLine() Returns the rest of the current line, excluding any line separator at the end.

Classes and Objects

• Creating objectsSee example BankAccount.java

• ReferencesSee example BankAccountref.java

Customer1

Customer2

1111

1050.75

Customer1

Customer2

2222

2020.29

Before

After

Practice

• Write a program that accepts 3 numbers (1 integer, 1 float,1 double ) through standard input and print the average of numbers.

• Create a Person class with class variables Date ,month, year

Create two objects pA,pB of Person class.

Assign values to these class variables and print the date of birth of 2 persons in mm/dd/yy format.

top related