lecture2.ppt

Download Report

Transcript lecture2.ppt

JAVA BASICS II
EXPRESSIONS
Expression
Value
Type
1/3
0
int
false && true
false
boolean
1/ 3.0
.33333
double
(char)65
‘A’
char
(int)99.9
99
int
65 > 89
false
boolean
MORE EXPRESSIONS
Expression
Value
Type
b=7
7
int
(b == 7) || true
true
boolean
3 == 9
3=9
"Hi" instanceof String
56 instanceof int
false
illegal
true
illegal
boolean
illegal
boolean
illegal
class DisplayWarning {
/**
* Displaying a warning program by J M Bishop Aug 1996
* ---------------------------- Java 1.1 October 1997
* Illustrates the form of a program and the use of println.
*/
public static void main(String[] args) {
System.out.println("-----------------------------");
System.out.println("|
|");
System.out.println("|
WAR N I N G
|");
System.out.println("| Possible virus detected |");
System.out.println("| Reboot and run virus |");
System.out.println("| remover software |");
System.out.println("|
|");
System.out.println("-----------------------------");
}
}
C:\heinz\examples\TestInit>java DisplayWarning
----------------------------|
|
|
WAR N I N G
|
| Possible virus detected
| Reboot and run virus
| remover software
|
|
|
|
|
-----------------------------
/* Interest Increase Program
by J M Bishop Aug 1996
* ------------------------Java 1.1 Oct 1997
* Calculates the difference in simple interest for two interest
* rates for the remainder of the year from a given month.
*
* Illustrates declarations, assignment, constants,
* expressions and printing.
*/
class InterestIncrease {
static final double p = 1000;
// in graz
static final int m = 4;
// for April
static final double oldRate = 12.5;
// %
static final double newRate = 13.00; // %
public static void main(String[] args) {
// Start with a title
System.out.println("SavBank Interest Calculation");
System.out.println("============================");
// perform the preliminary calculation
double ptOver100 = p * (12 - m) / 12 / 100;
// print out the results
System.out.println("Given a change of interest rate from "+
oldRate+"% to "+newRate+"% in month "+m+",");
System.out.println("on a principal of G"+p+
" the interest for the rest of the year");
System.out.print("will change by graz and cents: ");
System.out.println(ptOver100 * newRate - ptOver100 * oldRate);
}
}
C:\heinz\examples\TestInit>java InterestIncrease
SavBank Interest Calculation
============================
Given a change of interest rate from 12.5% to 13.0% in month 4,
on a principal of G1000.0 the interest for the rest of the year
will change by graz and cents: 3.3333333333333286
class TemperatureTable {
/* Displays a simple table converting Celsius
* to Farhenheit for a given range.
*
* Illustrates using the loop variable
* in expressions in the loop
*/
public static void main(String[] args) {
System.out.println("Temperature Conversion Table");
System.out.println("============================");
System.out.println();
System.out.println("C
F");
for (int c = 5; c <= 20; c++) {
System.out.print(c+"\t");
System.out.println(Math.round(c*9/5 + 32));
}
}
}
C:\heinz\examples\TestInit>java TemperatureTable
Temperature Conversion Table
============================
C
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
F
41
42
44
46
48
50
51
53
55
57
59
60
62
64
66
68