Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,

Similar presentations


Presentation on theme: "CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,"— Presentation transcript:

1 CSCI S-1 Section 6

2 Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday, July 10, 10:00 EST, 1 Storey St., Room 306 Mid-Term Quiz – Monday, July 13, 15:15 EST, Science Center A

3 More Conditions We can set up conditions with a == b (equals), a != b (does not equal), a <= b (less than or equal to) or a >= b (greater than or equal to). We can also form more complex conditions using && (like AND in Scratch) and || (like OR in Scratch) if && Statement 1 else if || Statement 2 else Statement 3 Here, if BOTH condition 1 and condition 2 are true, then statement 1 will be executed. If, however, condition 1 or condition 2 are false, statement 1 will not be executed. If EITHER condition 3 or condition 4 is true, then statement 2 will be executed. If conditions 1-4 are false, statement 3 will be executed.

4 DeMorgan’s Law !(a && b) = !a II !b !(a II b) = !a && !b

5 Example 7 – Hungry // deduce hunger from the # of meals eaten so far and the time since the last meal. class Hungry { public static void main(String[] args) { System.out.print("Enter the number of meals you had today: "); System.out.print("Enter the number of hours elapsed since you ate: "); }

6 Example 7 – Hungry // deduce hunger from the # of meals eaten so far and the time since the last meal. import java.util.*; class Hungry { public static void main(String[] args) { System.out.print("Enter the number of meals you had today: "); Scanner scan = new Scanner(System.in); int meals = scan.nextInt(); System.out.print("Enter the number of hours elapsed since you ate: "); int hours = scan.nextInt(); if (meals 4) System.out.println("You are hungry!"); else if (meals > 3 || hours < 4) System.out.println("you are not hungry."); else System.out.println("You tell me!"); }

7 Example 8 – Error Checking // A man should weigh 106 pounds for the first 5 feet of height … // Write an error loop to prevent entry of height values less than 5. class ErrCheck { public static void main(String[] args) { }

8 Example 8 – Error Checking // A man should weigh 106 pounds for the first 5 feet of height … // Write an error loop to prevent entry of height values less than 5. import java.util.*; class ErrCheck { public static void main(String[] args) { Scanner keyboard= new Scanner (System.in); int feet= 0; while (feet <5) do { system.out.println(“please enter a value for height in feet, minimum 5: ”); feet= keyboard.nexInt(); }

9 Example 9 – Random Numbers // generate random numbers between 2-6. print out each hit. // then print out the hits as a % of TRIES numbers generated between 0-9 class Random1 { final int TRIES= 1000; public static void main(String[] args) { }

10 Example 9 – Random Numbers // generate random numbers between 2-6. print out each hit. // then print out the hits as a % of TRIES numbers generated between 0-9 class Random1 { final int TRIES= 1000; public static void main(String[] args) { double hits= 0; double d= 0; for (int i = 1; i <= TRIES; i++) { d= (10*Math.random()); if (d > 2 && d < 6) { hits++; System.out.println(d); } System.out.println( hits/TRIES + “ % of all attempts were between 2 and 6”); }


Download ppt "CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,"

Similar presentations


Ads by Google