Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Most of these slides are based on “Intro to.

Similar presentations


Presentation on theme: "1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Most of these slides are based on “Intro to."— Presentation transcript:

1 1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Most of these slides are based on “Intro to OOP with Java” text book by C. Thomas Wu

2 2 Java Translation The Java compiler translates Java source code into a special representation called bytecode in the.class file Java bytecode is not the machine language for any specific CPU Another software tool, called an interpreter (in our case the Java Virtual Machine), executes the bytecode Java is considered to be architecture-neutral The Java compiler is not tied to any particular machine

3 3 Program Structure public class MyProgram {}{} // comments about the class public static void main (String[] args) {}{} // comments about the method method header method body

4 4 Arithmetic Operators Intro to OOP with Java, C. Thomas Wu

5 5 Operator Precedence Rules

6 6 if ( testScore < 70 ) JOptionPane.showMessageDialog(null, "You did not pass" ); else JOptionPane.showMessageDialog(null, "You did pass " ); Syntax for the if Statement if ( ) else Then Block Else Block Boolean Expression Indentation is important! Can be visualized as a flowchart

7 7 Comparing Objects String str1 = new String("Java"); String str2 = new String("Java"); if (str1 == str2) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } Sol : They are not equal Discussion of some string methods With primitive data types, we have only one way to compare them, but with objects (reference data type), we have two ways to compare them. We can test whether two variables point to the same object (use ==), or We can test whether two distinct objects have the same contents.

8 8 Syntax for the switch Statement switch ( gradeLevel ) { case 1: System.out.print("Go to the Gymnasium"); break; case 2: System.out.print("Go to the Science Auditorium"); break; case 3: System.out.print("Go to Harris Hall Rm A3"); break; case 4: System.out.print("Go to Bolt Hall Rm 101"); break; } switch ( ) { : … : } Case Body Arithmetic Expression Case Label

9 9 while ( number <= 100 ) { sum = sum + number; number = number + 1; } Syntax for the while Statement while ( ) Statement (loop body) Boolean Expression

10 10 String inputStr; int age; inputStr = JOptionPane.showInputDialog(null, "Your Age (between 0 and 130):"); age = Integer.parseInt(inputStr); while (age 130) { JOptionPane.showMessageDialog(null, "An invalid age was entered. Please try again."); inputStr = JOptionPane.showInputDialog(null, "Your Age (between 0 and 130):"); age = Integer.parseInt(inputStr); } Example: Testing Input Data Priming Read

11 11 do { sum += number; number++; } while ( sum <= 1000000 ); Syntax for the do-while Statement do while ( ) ; Statement (loop body) Boolean Expression

12 12 for ( i = 0 ; i < 20 ; i++ ) { number = scanner.nextInt(); sum += number; } Syntax for the for Statement for ( ; ; ) 1) Initialization 2) Boolean Expression 4) Increment and back to 2) 3) (loop body)

13 13 Defining a Class class { } Import Statements Class Comment Class Name Data Members Methods (incl. Constructor) Methods (incl. Constructor) Let us take a look at Bicycle.java, and BicycleRegistration.java

14 14 Creating a Package The following steps illustrate the process of creating a package name company that includes the Employee class. 1. Include the statement package company; as the first statement of the source file for the Employee class. 2. The class declaration must include the visibility modifier public as public class Employee {... } 3. Create a folder named company, the same name as the package name. In Java, the package must have a one-to-one correspondence with the folder. 4. Place the modified Employee class into the company folder and compile it. 5. Modify the CLASSPATH environment variable to include the folder that contains the company folder. 6. Include the statement import company.* in the driver class : EmployeePayRaise

15 15 Arrays of Primitive Data Types What is an Array? Why do we need them? Array Declaration [ ] //variation 1 [ ]//variation 2 Array Creation = new [ ] Example double[ ] rainfall; rainfall = new double[12]; Variation 1 double rainfall [ ]; rainfall = new double[12]; Variation 2 An array is like an object!

16 16 Array Processing double[] rainfall = new double[12]; String[] monthName = new String[12]; monthName[0] = "January"; monthName[1] = "February"; … double annualAverage, sum = 0.0; for (int i = 0; i < rainfall.length; i++) { rainfall[i] = Double.parseDouble( JOptionPane.showinputDialog(null, "Rainfall for " + monthName[i] )); sum += rainfall[i]; } annualAverage = sum / rainfall.length; The same pattern for the remaining ten months. The actual month name instead of a number.

17 17 Javadoc and Java Style General information on javadoc is located at http://java.sun.com/j2se/javadoc Detailed reference on how to use javadoc on Windows is located at http://java.sun.com/j2se/1.5/docs/tooldocs/windows/javadoc.html Java Style Specifics http://geosoft.no/development/javastyle.html

18 18 Javadoc (cont’d)


Download ppt "1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Most of these slides are based on “Intro to."

Similar presentations


Ads by Google