lecture2
lecture2
The primitive data types used in this course define the set of
operations for numbers and Boolean(true or false) values.
2
Primitive types
The primitive types on the Advanced Placement Computer
Science A exam are:
3
Receipt example
What's bad about the following code?
public class Receipt {
public static void main(String[] args) {
// Calculate total owed, assuming 8% tax / 15% tip
System.out.println("Subtotal:");
System.out.println(38 + 40 + 30);
System.out.println("Tax:");
System.out.println((38 + 40 + 30) * .08);
System.out.println("Tip:");
System.out.println((38 + 40 + 30) * .15);
System.out.println("Total:");
System.out.println(38 + 40 + 30 +
(38 + 40 + 30) * .08 +
(38 + 40 + 30) * .15);
}
}
– The subtotal expression (38 + 40 + 30) is repeated
– So many println statements
– We will use variables to solve the above problems. 4
Variables
• variable: A piece of the computer's memory that is given a
name and type, and can store a value.
– Like preset stations on a car stereo, or cell phone speed dial:
• Syntax:
type name;
x
– int x;
6
Assignment
• assignment: Stores a value into a variable.
– The value can be an expression; the variable stores its result.
• Syntax:
name = expression;
x 3
– int x;
x = 3;
x = 4 + 7;
System.out.println("now x is " + x); // now x is 11
8
Declaration/initialization
• A variable can be declared/initialized in one statement.
• Syntax:
type name = value;
myGPA 3.95
– double myGPA = 3.95;
– int x = (12 - 3) * 2; x 18
9
Assignment and algebra
• Assignment uses = , but it is not an algebraic equation.
10
Multiple Variables
• Multiple variables of the same type can be declared and
initialized at the same time.
• Syntax:
11
Assignment and types
• A variable can only store a value of its own type.
– int x = 2.5; // ERROR: incompatible types
12
Compiler errors
• Order matters.
– int x;
7 = x; // ERROR: should be x = 7;
• A variable can't be used until it is assigned a value.
– int x;
System.out.println(x); // ERROR: x has no value
• You may not declare the same variable twice.
– int x;
int x; // ERROR: x already exists
– int x = 3;
int x = 5; // ERROR: x already exists
• Output:
Your grade was 83.2
There are 65 students in the course.
14
Receipt question
Improve the receipt program using variables.
public class Receipt {
public static void main(String[] args) {
// Calculate total owed, assuming 8% tax / 15% tip
System.out.println("Subtotal:");
System.out.println(38 + 40 + 30);
System.out.println("Tax:");
System.out.println((38 + 40 + 30) * .08);
System.out.println("Tip:");
System.out.println((38 + 40 + 30) * .15);
System.out.println("Total:");
System.out.println(38 + 40 + 30 +
(38 + 40 + 30) * .15 +
(38 + 40 + 30) * .08);
}
}
15
Receipt answer
public class Receipt {
public static void main(String[] args) {
// Calculate total owed, assuming 8% tax / 15% tip
int subtotal = 38 + 40 + 30;
double tax = subtotal * .08;
double tip = subtotal * .15;
double total = subtotal + tax + tip;
16
Type boolean
• boolean: A logical type whose values are true and false.
17
final
• The keyword final can be used in front of a variable
declaration to make it a constant that cannot be changed.
Constants are traditionally capitalized.
18
Naming variables
The name of the variable should describe the data it holds. A name
like score helps make your code easier to read.
19
Naming variables
The convention in Java and many programming languages is to always start a
variable name with a lower case letter and then uppercase the first letter of
each additional word.
Variable names can not include spaces so uppercasing the first letter of
each additional word makes it easier to read the name. Uppercasing the first
letter of each additional word is called camel case.
21
Input and System.in
(not on AP)
• interactive program: Reads input from the console.
– While the program runs, it asks the user to type input.
– The input typed by the user is stored in variables in the code.
22
Scanner syntax
(not on AP)
• The Scanner class is found in the java.util package.
import java.util.*; // so you can use Scanner
– Example:
Scanner console = new Scanner(System.in);
23
Scanner methods
(not on AP)
Method Description
nextInt() reads an int from the user and returns it
nextDouble() reads a double from the user
next() reads a one-word String from the user
nextLine() reads a one-line String from the user
Output:
What is your age? Timmy
java.util.InputMismatchException
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
...
26
Scanner example 2
(not on AP)
import java.util.*; // so that I can use Scanner
public class ScannerMultiply {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Please type two numbers: ");
int num1 = console.nextInt();
int num2 = console.nextInt();
int product = num1 * num2;
System.out.println("The product is " + product);
}
}
Modify your previous lab 1 to ask the user to enter the number
pens and pencils rather than hard coding the values. Use print
statements and String concatenation to have the following
output:
Output:
Enter the number of pens: 10
Enter the number of pencils: 18
Total is 28 pens and pencils.
29