Programming Review. Java Class Structure All Java statements are part of a class public class...
-
Upload
moris-burke -
Category
Documents
-
view
217 -
download
0
Transcript of Programming Review. Java Class Structure All Java statements are part of a class public class...
Programming Review
Java Class Structure
All Java statements are part of a classpublic 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;
}……
To run a program
Must have a class that has a main method
public static void main(String[] args){
program statements;}
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
Java Identifiers
Java namesletters, numbers, underscore, $can not start with numberClass names start with cap
MyClassVariable names start lowercase
calculatePerimeter
Assigning values
Assignment operator= var1 = 15; var2 = var1;
Compound operators+= -= *= /= %=
var1 = var1 + 5 same as var1+=5
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
© A+ Computer Sciencewww.apluscompsci.com
average = total / 5sum = one + two
Expressions usually consist of operators, variables, and/or values.
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
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
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;
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;
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.
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
}}
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.
}}
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;}
}
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);