240 likes | 381 Views
1323Agenda3 HW i nt 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.
E N D
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
String objects String fname = “Jill”; How ‘long’ is fname? How many characters in fname? Sure, you just counted them – 4
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 jIGimn{ Gjio[ SGJi{SiSgj()S{ GJ9S gj[ adfg9pa GUUV(_ “; How long is someLets?
String objects String someLets = “asfgiopawrth8[w gae[gnklasdfga[r 9awe[rgklr 890S_yS nawoguS IS GiOPGIOS{G jIGimn{ 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? intlen = 0; len = someLets.length(); That looks something like num2Sqrt = Math.sqrt(num2); Yes, they are both ‘Method calls”.
Review To obtain user input from the terminal: • import java.util.Scanner; • Scanner console = new Scanner(System.in); • 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( ):
Sample Flowchart
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’
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
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”);
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”);
Subservient code.A standard practice is to indent subservient code consistently. At Dell, it is 2 spaces, Google, 4 spacesif (num1 > 5) { num1 = num1 * 7; num2 = 9; } else { num2 = 0; } Notes: { on line by itself else on line by itself
Nested if statements if (num1 > 5) { if (num2 == 4) { num1 = num1 * 7; num2 = 9; } else num1 = 5; } else { num2 = 0; }
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 “camden box” in a flowchart? (think chocolate chip cookies) Every method has its own flowchart. Every program and system have their own Block Diagram
lab 3 seu02 has one method - main seu03 will have several methods, main, calculator, stringAnalysis, rollDice and bigMoney main is a ‘controller’ method – s/he will just tell other people what to do. The calculator method will end up being your seu02 program logic So we will put several methods in one class?
lab 3 How will we do that? First things first – we already have a flowchart for calculator, so we just need a flowchart for seu03 main Here we go ----
lab 3 How will we do that? Did we get here on Tuesday? If so, roll on.
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?
Please Excuse My Dear Aunt Sally Order of operations (3*5 + 7) * 3/7 – 2 You can never have too many parentheses
Operators == Equality operator (for primitive data types) != > >= < <= || or && and
Logical expression – an expression that is true or false universally Truth Table 3 > 5 || today is Monday
Random numbers in java - book page 233 • import java.util.Random; • Random gener = new Random(); • 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); // ?
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