Object Oriented Programming
Object Oriented Programming
Programming
statements
Close braces mark
the end
A First Program – What Does It Do?
System.out.println
("This is my first Java program.");
sum = 2 + 4 + 6;
an assignment statement
Assignment Statements
• Assignment statements take the form:
variable = expression
+ Addition
- Subtraction
* Multiplication
/ Division
Expressions – Some Examples
2+5
4 * value
x/y
Writing Our Second Program
• sum = 2 + 4 + 6;
System.out.println(?The average is ?
+ average);
The output method variable
name
Writing Our Second Program
public class Average3 {
public static void main(String[] args) {
int sum, average;
sum = 2 + 4 + 6;
average = sum / 3;
System.out.println
("The average is " +
average);
}
}
Tells the computer that sum and average are integers
Writing Our Second Program
public class Average3a {
public static void main(String[] args) {
int sum;
int average;
sum = 2 + 4 + 6;
average = sum / 3;
System.out.println
("The average is " +average);
}
}
We could also write this as two separate declarations.
Variables and Identifiers
• Variables have names – we call these names
identifiers.
1.1.1 System.out.println
("Enter the first value ?");
1.1.2 Read in the first value
1.2.1 System.out.println
("Enter the second value ?");
1.2.2 Read in the second value
1.3.1 System.out.println
("Enter the third value ?");
1.3.2 Read in the third value
variable= keyb.nextInt();
Writing the input statements in Average3b
System.out.println
("What is the first value\t?");
int value1 = keyb.nextInt();
System.out.println
("What is the second value\t?");
int value2 = keyb.nextInt();
System.out.println
("What is the third value\t?");
int value3 = keyb.nextInt();
sum = value1 + value2
+ value3;
3. Divide the sum by 3
4. Print the result Adding up the three values
Writing the output statement in Average3b
System.out.println
("What is the first value\t?");
int value1 = keyb.nextInt();
System.out.println
("What is the second value\t?");
int value2 = keyb.nextInt();
System.out.println
("What is the third value\t?");
int value3 = keyb.nextInt();
sum = value1 + value2 + value3;
average = sum / 3;
System.out.println
("How many hours did you work?");
double hours = keyb.nextDouble();
}
}
Character Data
• All of our programs so far have used
variables to store numbers, not words.
import java.util.Scanner;
public class Polite {
// A very polite program that greets you by name
public static void main(String[] args) {
String name = new String();
Scanner keyb = new Scanner(System.in);
// Ask the user his/her name
System.out.println
("What is your name?");
name = keyb.next();
// Greet the user
System.out.println
("Glad to meet you, " + name);
}
Using Stepwise Refinement to
Design a Program
pounds to kilograms
• Our program will convert a weight
expressed in pounds into kilograms.
– Our input is the weight in pounds.
– Our output is the weight in kilograms
– We also know that
Kilograms = Pounds / 2.2
System.out.println
("What is the weight in pounds?");
System.out.println
("The weight is " + kg+ " kilograms");
Pounds to Kilograms Program
(continued)
System.out.println
("What is the weight in pounds?");
double lbs = keyb.nextInt();
double kg = lbs / 2.2;