Presentation is loading. Please wait.

Presentation is loading. Please wait.

Day 23 5.1 Programming with Methods. The “do” loop The “do” loop is a type of while loop that only does one iteration (does one loop at least) It is hardly.

Similar presentations


Presentation on theme: "Day 23 5.1 Programming with Methods. The “do” loop The “do” loop is a type of while loop that only does one iteration (does one loop at least) It is hardly."— Presentation transcript:

1 Day 23 5.1 Programming with Methods

2 The “do” loop The “do” loop is a type of while loop that only does one iteration (does one loop at least) It is hardly ever used

3 Let’s write the Oracle class UML Class Diagram: Oracle - oldAnswer = “The answer is in your heart.”: String - newAnswer: String - question: String + dialog(): void + answerOne(): void + seekAdvice(): void

4 import java.util.*; public class Day_23_ChatterBox { … private instance variables

5 public void dialog() { String ans; Scanner s = new Scanner(System.in); do { answerOne(); // we’ll program this later System.out.println(“Do you wish to ask another question?”); ans = s.next(); } while (ans.equalsIgnoreCase(“yes)); System.out.println(“The oracle will now rest.”); }

6 private void answerOne() { System.out.println(“I am the oracle.”); System.out.println(“I will answer any one-line question.”); System.out.println(“What is your question?”); Scanner s = new Scanner(System.in); question = keyboard.nextLine(); seekAdvice(); System.out.println(“You asked the question:”); System.out.println(question); System.out.println(“Now, here is my answer:”); System.out.println(oldAnswer); update(); }

7 private void seekAdvice() { System.out.println(“Hmm, I need some help on that.”); System.out.println(“Please give me one line of advice.”); Scanner keyboard = new Scanner(System.in); newAnswer = keyboard.nextLine(); System.out.println(“Thank you. That helped a lot.”); }

8 private void update() { oldAnswer = newAnswer; }

9 public class OracleDemo { public static void main(String [] args) { Oracle delphi = new Oracle(); delphi.dialog(); } }

10 Do homework on wiki to prepare for test


Download ppt "Day 23 5.1 Programming with Methods. The “do” loop The “do” loop is a type of while loop that only does one iteration (does one loop at least) It is hardly."

Similar presentations


Ads by Google