java review n.
Skip this Video
Download Presentation
Java Review

Loading in 2 Seconds...

play fullscreen
1 / 18

Java Review - PowerPoint PPT Presentation

  • Uploaded on

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. Java Translation.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Java Review' - chyna

Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
java review
Java Review


  • 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

java translation
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
program structure
Program Structure

// comments about the class

public class MyProgram



// comments about the method

public static void main (String[] args)



method header

method body

arithmetic operators
Arithmetic Operators

Intro to OOP with Java, C. Thomas Wu

syntax for the if statement

if( testScore < 70 )

JOptionPane.showMessageDialog(null,"You did not pass");


JOptionPane.showMessageDialog(null,"You did pass ");

Boolean Expression

Then Block

Else Block

Syntax for the if Statement

if( <boolean expression> )

<then block>


<else block>

Can be visualized as a flowchart

Indentation is important!

comparing objects

String str1 = new String("Java");

String str2 = new String("Java");

if(str1 == str2){

System.out.println("They are equal");


System.out.println("They are not equal");


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.

Sol : They are not equal

Discussion of some string methods

syntax for the switch statement

Arithmetic Expression

switch ( gradeLevel ) {

case 1: System.out.print("Go to the Gymnasium");


case 2: System.out.print("Go to the Science Auditorium");


case 3: System.out.print("Go to Harris Hall Rm A3");


case 4: System.out.print("Go to Bolt Hall Rm 101");



Case Label

Case Body

Syntax for the switch Statement

switch( <arithmetic expression> ) {

<case label 1> : <case body 1>

<case label n> : <case body n>


syntax for the while statement

while( number <= 100 ) {

sum = sum + number;

number = number + 1;


Boolean Expression

Statement(loop body)

Syntax for the while Statement

while( <boolean expression> )


example testing input data

Priming Read

Example: Testing Input Data

String inputStr;

int age;

inputStr = JOptionPane.showInputDialog(null,

"Your Age (between 0 and 130):");

age = Integer.parseInt(inputStr);

while(age < 0 || age > 130) {


"An invalid age was entered. Please try again.");

inputStr = JOptionPane.showInputDialog(null,

"Your Age (between 0 and 130):");

age = Integer.parseInt(inputStr);


syntax for the do while statement


sum += number;


}while( sum <= 1000000 );

Statement(loop body)

Boolean Expression

Syntax for the do-while Statement



while( <boolean expression> ) ;

syntax for the for statement

for ( i = 0 ; i < 20 ; i++ ) {

number = scanner.nextInt();

sum += number;


2) Boolean Expression

1) Initialization

4) Increment and back to 2)

3) (loop body)

Syntax for the for Statement

for( <initialization>; <boolean expression>; <increment> )


defining a class

Import Statements

Class Comment

Class Name



Data Members


(incl. Constructor)


Defining a Class

Let us take a look at, and

creating a package
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

publicclassEmployee {



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

arrays of primitive data types

double[ ] rainfall;


= new double[12];

double rainfall [];


= new double[12];

Variation 1

Variation 2

An array is like an object!

Arrays of Primitive Data Types
  • What is an Array? Why do we need them?
  • Array Declaration

<data type> [ ] <variable>//variation 1

<data type> <variable>[ ]//variation 2

  • Array Creation

<variable> = new <data type> [ <size> ]

  • Example
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.

Array Processing
javadoc and java style
Javadoc and Java Style
  • General information on javadoc is located at
  • Detailed reference on how to use javadoc on Windows is located at
  • Java Style Specifics