Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }"— Presentation transcript:

1 Programming Review

2 Java Class Structure All Java statements are part of a class public class ClassName { }

3 What’s inside a class private instance variables - attributes methods – behaviors public class Rectangle { // declare instance variables private int length; private int width; private int perimeter; // define methods public void setLengthAndWidth(int len, int wid) { length = len; width = wid; } ……

4 To run a program Must have a class that has a main method public static void main(String[] args) { program statements; }

5 Declaring variables Variables store primitive data or addresses primitives – built in data types int, long, byte, double, char … addresses – myScanner, myRectangle… Java is strongly typed – must declare variables before using int age;// declaration that will store an integer double price// declaration that will store a decimal value

6 Java Identifiers Java names letters, numbers, underscore, $ can not start with number Class names start with cap MyClass Variable names start lowercase calculatePerimeter

7 Assigning values Assignment operator =var1 = 15; var2 = var1; Compound operators += -= *= /= %= var1 = var1 + 5 same as var1+=5

8 Increment and Decrement operators Increment(increase by 1) pre++x; increment x then use postx++; use x then increment Decrement(decrease by 1) pre--x; decrement x then use x++; use x then decrement

9 © A+ Computer Science www.apluscompsci.com average = total / 5 sum = one + two Expressions usually consist of operators, variables, and/or values.

10 String Concatenation String literal“This is a String” + concatenates string and primitives eg.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

11 Writing Code Example one:given existing code given variables, use to solve problem Givendouble priceONe, priceTwo, priceThree double totalPrice, averagePrice Write statements that assign values to each price, calculate and store total price, and calculate averge. Output to screen: Price one:xx.xx Price two:xx.xx Price three:xx.xx Total price:xx.xx Average price:xx.xx

12 Don’t redeclare or create new variables here ! Use those given priceOne = 3.00; priceTwo = 14.50; priceThree = 2.99; totalPrice = priceOne + priceTwo + priceThree; averagePrice = totalPrice / 3;

13 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;

14 Writing a program based on requirements First read and understand program requirements Eg.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.

15 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 }

16 What’s next? We know that all statements should be in a method So 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. }

17 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; }

18 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 runtime average = (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);


Download ppt "Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }"

Similar presentations


Ads by Google