1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

Written by: Dr. JJ Shepherd
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Sections 1-4.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Animated Version.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement a selection control.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Selection Statements.
CS180 Recitation 25th/26th October, 2007 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Lists.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 10 Arrays and Collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
Chapter Chapter 5 Selection Statements. Objectives Understand selection control statement –if statements –switch statements Write boolean expressions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Language and SW Dev’t
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Array Basics An array is a collection of data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
SE-1020 Dr. Mark L. Hornick 1 The switch statement int gradeLevel = kbd.nextInt(); // can be byte or short also, but not long switch( gradeLevel ) { //
Introduction to Java Java Translation Program Structure
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Most of these slides are based on “Intro to.
Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Chapter 5: Objectives After you have read and studied this chapter, you should be able to Implement a selection control using if statements Implement a.
By Mr. Muhammad Pervez Akhtar
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 5: Control Statements
Intro to OOP with Java, C. Thomas Wu Selection Statements
Java Review Most of these slides are based on
Basic Files + (Chapter 10 – Arrays) : Objectives
Chapter 10 Arrays ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1: Computer Systems
Arrays and Collections
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Review Most of these slides are based on
Chapter 6 Selection Statements
Presentation transcript:

1 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

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 Program Structure public class MyProgram {}{} // comments about the class public static void main (String[] args) {}{} // comments about the method method header method body

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

5 Operator Precedence Rules

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 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 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 while ( number <= 100 ) { sum = sum + number; number = number + 1; } Syntax for the while Statement while ( ) Statement (loop body) Boolean Expression

10 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 130) { System.out.println("An invalid age was entered. Please try again."); age = Integer.parseInt(keyboard.nextLine()); } Example: Testing Input Data

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

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 Defining a Class class { } Import Statements Class Comment Class Name Data Members Methods (incl. Constructor) Methods (incl. Constructor)

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 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 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(keyboard.nextLine()); sum += rainfall[i]; } annualAverage = sum / rainfall.length; The same pattern for the remaining ten months.

17 Javadoc and Java Style General information on javadoc is located at Java Style Specifics

18 Javadoc (cont’d)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Lists and Maps The java.util standard package contains different types of classes for maintaining a collection of objects. These classes are collectively referred to as the Java Collection Framework (JCF). JCF includes classes that maintain collections of objects as sets, lists, or maps. A Java interface defines only the behavior of objects –It includes only public methods with no method bodies. –It does not include any data members except public constants –No instances of a Java interface can be created

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter JCF Lists JCF includes the List interface that supports methods to maintain a collection of objects as a linear list L = (l 0, l 1, l 2,..., l N ) We can add to, remove from, and retrieve objects in a given list. A list does not have a set limit to the number of objects we can add to it.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter List Methods Here are five of the 25 list methods: boolean add ( Object o ) Adds an object o to the list void clear ( ) Clears this list, i.e., make the list empty Object get ( int idx ) Returns the element at position idx boolean remove ( int idx ) Removes the element at position idx int size ( ) Returns the number of elements in the list

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using Lists To use a list in a program, we must create an instance of a class that implements the List interface. Two classes that implement the List interface: –ArrayList –LinkedList The ArrayList class uses an array to manage data. The LinkedList class uses a technique called linked- node representation.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sample List Usage Here's an example of manipulating a list: ArrayList accounts = new ArrayList (); accounts.add(new BankAccount(1001)); accounts.add(new BankAccount(1015)); accounts.add(new BankAccount(1729)); accounts.add(1, new BankAccount(1008)); accounts.remove(0);

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( ) } 24

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

Files (cont’d) Make sure to close the file streams when done inputStream.close( ); outputStream.close( ); 26