Upload
phungnga
View
231
Download
0
Embed Size (px)
Citation preview
CPSC 233 - Introduction to Computer Science for Computer
Science Majors II
Monir Zaman
(Md Moniruzzaman)
Applets
• Lightweight Java program
• Runs from a web browser; alternately, we can run applet from an applet viewer
• Can run from a location on the Internet; it does not have to be stored in the local machine
• Always use Windowing interface; doesn’t support console I/O
Try these applets
• http://english.op.org/~peter/ChessApp/#applet
• http://www.bodo.com/Applets/ping2/index.html
• In this course we will focus on applications not applets
– Application may or may not have a windowing interface
– Application supports console input and output
Exercise: Write these instructions in a Java program
int a=23; int b=3; int s=a+b*2; System.out.println(s); s=(a+b)*2; System.out.println(s);
Variable declarations
int number2, number1;
float leng= 3.4f;
double diam=54.3;
number2 = number1 = 3;
long num3=789l;
Casting
int b=23;
float c=1.2f;
b=(int) c; //information loss
c=b; Python type casting: temp=input(“enter a number”) N=int(temp)
printf Format Specifier
• The code double price = 19.8;
System.out.print("$");
System.out.printf("%6.2f", price);
System.out.println(" each");
will output the line $ 19.80 each
2-16 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
Right and Left Justification in printf
• The code double value = 12.123;
System.out.printf("Start%8.2fEnd", value);
System.out.println();
System.out.printf("Start%-8.2fEnd", value);
System.out.println();
will output the following
2-17 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
Right and Left Justification in printf
• The code double value = 12.123;
System.out.printf("Start%8.2fEnd", value);
System.out.println();
System.out.printf("Start%-8.2fEnd", value);
System.out.println();
will output the following
Start 12.12End
Start12.12 End
2-18 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
Multiple arguments with printf
• What is the output of the following code:
double price = 19.8;
String name = "magic apple";
System.out.printf("$%6.2f for each %s. %n" , price, name);
System.out.println("Wow");
2-19 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
Shorthand Assignment Statements
Example: Equivalent To:
count += 2; count = count + 2;
sum -= discount; sum = sum – discount;
bonus *= 2; bonus = bonus * 2;
time /=
rushFactor;
time =
time / rushFactor;
change %= 100; change = change % 100;
amount *=
count1 + count2;
amount = amount *
(count1 + count2);
1-20 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
Precedence and Associativity Rules
– Unary operators of equal precedence are grouped right-to-left +-+rate is evaluated as +(-(+rate))
– Binary operators of equal precedence are grouped left-to-right base + rate + hours is evaluated as
(base + rate) + hours
– Exception: A string of assignment operators is grouped right-to-left n1 = n2 = n3; is evaluated as n1 = (n2 = n3);
1-26 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.