JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning...

10
JAVA BASICS II

Transcript of JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning...

Page 1: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

JAVA BASICS II

Page 2: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

EXPRESSIONS

Expression Value Type1/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

Page 3: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

MORE EXPRESSIONS

Expression Value Typeb = 7 7 int

(b == 7) || true true boolean

3 == 9 false boolean3 = 9 illegal illegal"Hi" instanceof String true boolean56 instanceof int illegal illegal

Page 4: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

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("| W A R 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("-----------------------------"); }}

Page 5: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

C:\heinz\examples\TestInit>java DisplayWarning-----------------------------| || W A R N I N G || Possible virus detected || Reboot and run virus |

| remover software || |-----------------------------

Page 6: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

/* 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("============================");

Page 7: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

// 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); }}

Page 8: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

C:\heinz\examples\TestInit>java InterestIncreaseSavBank 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 yearwill change by graz and cents: 3.3333333333333286

Page 9: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

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)); } }}

Page 10: JAVA BASICS II. EXPRESSIONS MORE EXPRESSIONS class DisplayWarning { /** * Displaying a warning program by J M Bishop Aug 1996 * ----------------------------

C:\heinz\examples\TestInit>java TemperatureTableTemperature Conversion Table

============================C F5 416 427 448 469 4810 5011 5112 5313 5514 5715 5916 6017 6218 6419 6620 68