Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Review Outline • • • • • Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides are based on “Intro to OOP with Java” text book by C. Thomas Wu 1 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 2 Program Structure // comments about the class public class MyProgram { // comments about the method public static void main (String[] args) { method body method header } } 3 Arithmetic Operators Intro to OOP with Java, C. Thomas Wu 4 Operator Precedence Rules 5 Syntax for the if Statement if ( <boolean expression> ) <then block> else <else block> Boolean Expression Can be visualized as a flowchart if ( testScore < 70 ) JOptionPane.showMessageDialog(null, "You did not pass" ); Then Block else Else Block JOptionPane.showMessageDialog(null, "You did pass " ); Indentation is important! 6 Comparing Objects 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. 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"); } Discussion of some string methods 7 Syntax for the switch Statement switch ( <arithmetic expression> ) { <case label 1> : <case body 1> … <case label n> : <case body n> } Arithmetic Expression switch ( gradeLevel ) { case 1: System.out.print("Go to the Gymnasium"); break; Case Label case 2: System.out.print("Go to the Science Auditorium"); break; case 3: System.out.print("Go to Harris Hall Rm A3"); break; Case Body case 4: System.out.print("Go to Bolt Hall Rm 101"); break; } 8 Syntax for the while Statement while ( <boolean expression> ) <statement> Boolean Expression while ( sum Statement (loop body) number <= 100 = ) { sum + number; number = number + 1; } 9 Example: Testing Input Data String inputStr; int age; // Create a Scanner object to read input. Scanner keyboard = new Scanner(System.in); keyboard.useDelimiter(System.getProperty("line.separator")); System.out.println(“Please enter your age (between 0 and 130):"); age = Integer.parseInt(keyboard.nextLine()); while (age < 0 || age > 130) { System.out.println("An invalid age was entered. Please try again."); age = Integer.parseInt(keyboard.nextLine()); } 10 Syntax for the do-while Statement do <statement> while ( <boolean expression> ) ; do { sum += number; Statement (loop body) number++; } while ( sum <= 1000000 ); Boolean Expression 11 Syntax for the for Statement for ( <initialization>; <boolean expression>; <increment> ) <statement> for ( 4) Increment and back to 2) 2) Boolean Expression 1) Initialization i = 0 ; i < 20 number = scanner.nextInt(); sum += number; ; i++ ) { 3) (loop body) } 12 Defining a Class Import Statements Class Comment class { Class Name Data Members Methods (incl. Constructor) } 13 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 14 Arrays of Primitive Data Types • What is an Array? Why do we need them? • Array Declaration <data type> [ ] <variable> <data type> <variable>[ ] //variation 1 //variation 2 • Array Creation <variable> = new <data type> [ <size> ] • Example Variation 1 Variation 2 double[ ] rainfall; double rainfall [ ]; rainfall rainfall = new double[12]; = new double[12]; An array is like an object! 15 Array Processing double[] rainfall = new double[12]; String[] monthName = new String[12]; monthName[0] = "January"; monthName[1] = "February"; … double The same pattern for the remaining ten months. annualAverage, sum = 0.0; for (int i = 0; i < rainfall.length; i++) { rainfall[i] = Double.parseDouble(keyboard.nextLine()); sum += rainfall[i]; } annualAverage = sum / rainfall.length; 16 Javadoc and Java Style • General information on javadoc is located at • http://java.sun.com/j2se/javadoc • Java Style Specifics • http://geosoft.no/development/javastyle.html 17 Javadoc (cont’d) 18 Files • Reading Files • • • • • • • • • • • Scanner inputStream = null; Scanner console = new Scanner(System.in); System.out.print("Input file: "); String inputFileName = console.next(); inputStream = new Scanner(new FileInputStream(inputFileName)); String line = null; while (inputStream.hasNextLine( )) { line = inputStream.nextLine( ) } 19 Files (cont’d) • Writing Files • PrintWriter outputStream = null; • System.out.print("Output file: "); • String outputFileName = console.next(); • outputStream = new PrintWriter( new FileOutputStream(outputFileName)); • String line = null; • while (inputStream.hasNextLine( )) • { • line = inputStream.nextLine( ); • outputStream.println(count + ":" + line); • } 20 Files (cont’d) • Make sure to close the file streams when done • inputStream.close( ); • outputStream.close( ); 21