Programming Review. Java Class Structure All Java statements are part of a class public class...

18
Programming Review

Transcript of Programming Review. Java Class Structure All Java statements are part of a class public class...

Page 1: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Programming Review

Page 2: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Java Class Structure

All Java statements are part of a classpublic class ClassName{

}

Page 3: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

What’s inside a classprivate instance variables - attributesmethods – behaviors

public class Rectangle{

// declare instance variablesprivate int length;private int width;private int perimeter;

// define methodspublic void setLengthAndWidth(int len, int wid){

length = len;width = wid;

}……

Page 4: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

To run a program

Must have a class that has a main method

public static void main(String[] args){

program statements;}

Page 5: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Declaring variables

Variables store primitive data or addresses

primitives – built in data typesint, long, byte, double, char …

addresses – myScanner, myRectangle…Java is strongly typed – must declare variables

before usingint age; // declaration that will store an

integerdouble price // declaration that will

store adecimal value

Page 6: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Java Identifiers

Java namesletters, numbers, underscore, $can not start with numberClass names start with cap

MyClassVariable names start lowercase

calculatePerimeter

Page 7: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Assigning values

Assignment operator= var1 = 15; var2 = var1;

Compound operators+= -= *= /= %=

var1 = var1 + 5 same as var1+=5

Page 8: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Increment and Decrement operators

Increment (increase by 1)pre ++x; increment x then usepostx++; use x then increment

Decrement (decrease by 1)pre --x; decrement x then use

x++; use x then decrement

Page 9: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

© A+ Computer Sciencewww.apluscompsci.com

average = total / 5sum = one + two

Expressions usually consist of operators, variables, and/or values.

Page 10: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

String Concatenation

String literal “This is a String”

+ concatenates string and primitiveseg. String s1 = “This is my string.”

String s2 = “ I will print it “String s3 = “ times”int times = 2;

System.out.println(s1 + s2);This is my string I will print it.

System.out.println(s1 + s2 + times + s3);This is my string. I will print it 2 times

Page 11: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Writing CodeExample one: given existing code

given variables, use to solve problem

Given double priceONe, priceTwo, priceThree

double totalPrice, averagePriceWrite statements that assign values to each price,calculate and store total price, and calculate averge.Output to screen:

Price one: xx.xxPrice two: xx.xxPrice three: xx.xxTotal price: xx.xxAverage price: xx.xx

Page 12: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Don’t redeclare or create new variables here !

Use those givenpriceOne = 3.00;priceTwo = 14.50;priceThree = 2.99;

totalPrice = priceOne + priceTwo + priceThree;averagePrice = totalPrice / 3;

Page 13: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Output statements

System.out.println(“Price one:\t” + priceOne;System.out.println(“Price two:\t” + priceTwo;System.out.println(“Price three:\t” +

priceThree;System.out.println(“Total price:\t” +

totalPrice;System.out.println(“Average price:\t” +

averagePrice;

Page 14: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

Writing a program based on requirements

First read and understand program requirementsEg. Write a program that declares variables to store three test grades. Grades are recorded as integers. Calculate and store the average grade as a real number. Print each grade separated by a comma on one line and a line that looks like the following on the next:

The average grade is xx.xx (where xx.xx is calculated value)

Name your program MyAverage.

Page 15: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

What’s first?Since we are writing a complete program, we

know we need the basic java class and a main method. At least start those.public class MyAverage{

public static void main(String[] args){

// leave lots of space for statements

}}

Page 16: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

What’s next?We know that all statements should be in a

methodSo think about the storage you need and declare

variables of the right type.

We need variables that would hold each grade and the requirements state that the grades are integers. Declare those variable inside main.public class MyAverage{

public static void main(String[] args){

int grade1, grade2, grade3;// notice that it was not a requirement

to assign values.

}}

Page 17: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

What’s next?The requirements state that the average has

to be calculated and stored, so we need a variable of the right type.public class MyAverage{

public static void main(String[] args){ int grade1, grade2, grade3; double average;}

}

Page 18: Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }

What’s next?Now that all variables are declared, perform

calculations using java expressions.// Note: grades must be assigned a value or read in// at runtimeaverage = (grade1 + grade2 + grade3) / 3.0;

With all values stored in memory, we can now print.

System.out.println(grade1 + “,” + grade2 + “,”+grade3);

System.out.println(“The average grade is “ + average);