Page 1
1323Agenda3
HW
int number = 716;
System.out.println(“The number is “ + “number”);
Valid variable name? two_shoes
Flowchart symbols and clarity. “Single entry point, single exit” for I/O, computation/process
Lab2
Good work – Continue to bring the kitchen sink to lab.
I’m hiring 3 new people:
1 to convert code to flowchart (like last class quiz)
1 to convert flowcharts to code
1 to develop flowcharts
Page 2
String objects
String fname = “Jill”;
How ‘long’ is fname? How many characters in fname?
Sure, you just counted them – 4
Page 3
String objects
String fname = “Jill”;
How ‘long’ is fname? How many characters in fname?
Sure, you just counted them – 4
String someLets = “asfgiopawrth8[w gae[gnklasdfga[r 9awe[rgklr 890S_yS nawoguS IS GiOPGIOS{G jIG imn{ Gjio[ SGJi{SiSgj()S{ GJ9S gj[ adfg9pa GUUV(_ “;
How long is someLets?
Page 4
String objects
String someLets = “asfgiopawrth8[w gae[gnklasdfga[r 9awe[rgklr 890S_yS nawoguS IS GiOPGIOS{G jIG imn{ Gjio[ SGJi{SiSgj()S{ GJ9S gj[ adfg9pa GUUV(_ “;
How long is someLets?
Good news – java gives us a tool that tells us. A method called ‘length’.
How do we use it?
int len = 0;
len = someLets.length();
That looks something like num2Sqrt = Math.sqrt(num2);
Yes, they are both ‘Method calls”.
Page 5
Review
To obtain user input from the terminal:
1 import java.util.Scanner;
2 Scanner console = new Scanner(System.in);
3 gpa = console.nextDouble ( );
Example: (after having 1 and 2 in your program:)
double num1 = 0;System.out.println (“Please enter a number: “);num1 = console.nextDouble( ):
Page 7
Logic error, symbol error!
Page 8
if – else construct
if (newRate > oldRate) {
cost = price * newRate; System.out.println(“New cost is “ + cost);
}
else
{
cost = price * oldRate; System.out.println(“No change to cost. Cost is “ + cost);
}
Note the use of the concatenation operator +
What is the “TRUE Block” “FALSE Block”
Note the indention of ‘subservient code’
Page 9
Note that an if does not require an else.
if ( code > oldCode) {
System.out.println(“Code has changed. New code: “ + code);
}
System.out.println (“Hello Joe”);
Now I’ll put some examples on the board to try to trick you
Page 10
Pick a num1:
if (num1 > 5)
System.out.println(“Joe”);
else
{
System.out.println(“Sarah”);
System.out.println(“Alice”);
num1 = num1 + 1;
}
System.out.println(“Henry”);
// ------------------------------------------------------------------
if (num1 > 5)
System.out.println(“Joe”);
else
System.out.println(“Sarah”); System.out.println(“Alice”);
num1 = num1 + 1;
System.out.println(“Henry”);
Page 11
Pick a num1:
if (num1 > 5)
System.out.println(“Joe”);
System.out.println(“Alice”);
else
{
System.out.println(“Sarah”);
System.out.println(“Alice”);
num1 = num1 + 1;
}
System.out.println(“Henry”);
// ------------------------------------------------------------------
if (num1 > 5)
System.out.println(“Joe”);
else
System.out.println(“Sarah”);System.out.println(“Alice”);
num1 = num1 + 1;
System.out.println(“Henry”);
Page 12
Subservient code.A standard practice is to indent subservient code consistently. At Dell, it is 2 spaces, Google, 4 spaces
if (num1 > 5)
{
num1 = num1 * 7;
num2 = 9;
}
else
{
num2 = 0;
}
Notes: { on line by itself
else on line by itself
Page 13
Nested if statements
if (num1 > 5)
{
if (num2 == 4) {
num1 = num1 * 7;
num2 = 9;
} else
num1 = 5; }
else
{
num2 = 0;
}
Page 14
Let’s work on lab 3
Note: Answering questions in the lab:
Read the lab at least 5 times.
Complete flowcharts for the program that you are working on. (it doesn’t have to be perfect, but it does have to be complete)
What is a “workbench box” in a flowchart? (think chocolate chip cookies)
Every method has its own flowchart.
Every program and system have their own Block Diagram
Page 15
lab 3
seuCalculator has one method - main
seuController will have several methods, main, calculator, analyzeSentence, dieRoll and moneyBags
main is a ‘controller’ method – s/he will just tell other people what to do.
The calculator method will end up being your seuCalculator program logic
So we will put several methods in one class?
Page 16
lab 3
How will we do that?
First things first – we already have a flowchart for calculator, so we just need a flowchart for
seuController main method
Here we go ----
Page 17
lab 3
How will we do that?
Did we get here on Tuesday?
If so, roll on.
Page 18
int num1 = 3;
System.out.println(“Hello “ + num1 + “ cats”);
System.out.print(“well, “);System.out.println(“well.”);
System.out.println(“So, let’s go \n” + “party!”);
Pop a new line - - how?, automatic?
Page 19
Please Excuse My Dear Aunt Sally
Order of operations
(3*5 + 7) * 3/7 – 2
You can never have too many parentheses
Page 20
Operators
== Equality operator (for primitive data types)
!=
>
>=
<
<=
|| or
&& and
Page 21
Logical expression – an expression that is true or false universally
Truth Table
3 > 5 || today is Monday
A B A||B A&&B
!A A&&!B
T T T T F F
T F T F F T
F T T F T F
F F F F T F
Page 22
Random numbers in java - book page 188(we will just use nextInt(xx) there are others)
1. import java.util.Random;
2. Random gener = new Random();
3. int num1 = 0;
Ex: num1 = gener.nextInt(7);
// integer 0, 1, 2, 3, 4, 5, 6
Ex: num1 = gener.nextInt(20);
// integer 0, 1, 2, …, 19
Ex: num1 = gener.nextInt(2);
// ?
Page 23
Ex:
random integer between 4 and 13
we need rnum to be 4, 5, 6, 7, 8, 9, 10, 11, 12, 13so 10 numbers. Note: 13 – 4 is 9 we need 9 + 1 = 10
How do we get 10 random numbers? nextInt(10)yes, but 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
But we want one of the 10 numbers 4, 5, 6, …, 13
Well, just add 4 to each of these numbers and ….So: rnum = gener.nextInt(10) + 4; does the job
Ex: generate a random integer between -3 and 3