Beruflich Dokumente
Kultur Dokumente
Chris Piech
CS106A, Stanford University
The program should ask the user for the subtotal, and then
calculate and print out the tax, tip and total.
3
. . . . . . . .
3
. . . . . . . . . . . . . . . .
2
1
. . . . . . . . 2
1
. . . . . . . .
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
. . . . . . . . . . . . . . . .
8
. . . . . . . .
8
7
. . . . . . . .
7
. . . . . . . . 6
. . . . . . . .
6
. . . . . . . . 5
. . . . . . . .
5
. . . . . . . . . . . . . . . .
4
. . . . . . . .
4
3
. . . . . . . .
3
. . . . . . . . . . . . . . . .
2
1
. . . . . . . . 2
1
. . . . . . . .
1 2 3 4 5 6Piech,
7 8CS106A, Stanford
1 2 University
3 4 5 6 7 8
Decision 2018
. . . . . . . .
8
7
. . . . . . . .
6
. . . . . . . .
5
. . . . . . . .
. . . . . . . .
4
3
. . . . . . . .
. . . . . . . .
2
1
. . . . . . . .
1 2 3 4 5 6 7 8
. . . . . . . .
8
7
. . . . . . . .
6
. . . . . . . .
5
. . . . . . . .
. . . . . . . .
4
3
. . . . . . . .
. . . . . . . .
2
1
. . . . . . . .
Piech, CS106A, Stanford
1 2 University
3 4 5 6 7 8
See You Later!
Enjoy Java!
Program
HelloConsole
hello, world
Add2Integers
Access
Variables
type name
Initial value
Piech, CS106A, Stanford University
Three Properties
name type
age
29 (contains an int)
value
Example:
int myBox = 5;
Piech, CS106A, Stanford University
Core Types
// integer values
int num = 5;
// real values
double fraction = 0.2;
// true or false
boolean isLove = true;
Piech,
* Why
CS106A,
is it called
Stanford
a double?
University
Core Types
// letters
char letter = ‘c’;
// “strings” of letters
String phrase = “Spring rocks!”;
// integer values
int num = 5;
// real values
double fraction = 0.2;
// true or false
boolean isLove = true;
Piech,
* Why
CS106A,
is it called
Stanford
a double?
University
Double: How Much Do I Weigh?
*Piech,
Answers couldStanford
CS106A, be real valued numbers
University
Int: How Many Children Do I Have?
* ItPiech,
is weird to sayStanford
CS106A, something like 1.7
University
Lots of Boxes
// integer values
int age = 30;
// real values
double weight = 180.2;
}
* Fun
Piech,
fact.CS106A,
Chris turns
Stanford
30 onUniversity
April 25th
Fancy Output
Add2Integers
type name
Initial value
Piech, CS106A, Stanford University
Making a New Variable
type name
Initial value
Piech, CS106A, Stanford University
Values from User Input
// Prompts user for a whole number. Stores
// result in a variable (aka a box)
int kids = readInt(“How many children?”);
Add2Integers
+ Addition * Multiplication
– Subtraction / Division
% Remainder
// Left to right!
int sum = 1 + 2 + (3 * 4); // 15
0.0
Piech, CS106A, Stanford University
Resulting Type of Binary Expression
int int
Expressive hierarchy:
boolean < char < int < double < String
Example:
int / doublePiech,
results in a double
CS106A, Stanford University
Even more examples…
double c = 100;
double f = 9 / 5 * c + 32;
9 / 5 * c + 32
Piech, CS106A, Stanford University
Pitfalls of Integer Division
You can fix this problem by converting the fraction to a double,
either by inserting decimal points or by using a type cast:
double c = 100;
double f = 9.0 / 5 * c + 32;
180.0
1.8
9.0 / 5 * c + 32
Piech, CS106A, Stanford University
Practice
•5+3/2-4 // 2
• 15 / 2.0 + 6 // 13.5
int x;
println(x); // Error: x has no value
int y = 3;
int y = 5; // Error: y already exists
Access
Variables
Access
Variables