Download presentation
Presentation is loading. Please wait.
Published byRalph Lester Modified over 8 years ago
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.