JAVA SIMPLE CODE NO USE OF NULL . Did You Ever Have To Make Up Your Mind? Write
ID: 3878567 • Letter: J
Question
JAVA SIMPLE CODE NO USE OF NULL .
Did You Ever Have To Make Up Your Mind? Write a program using Scanner to help you decide what to do this weekend. Decision Trees Imagine you only ever do three things at the weekend: go shopping, watch a movie, or just stay in. What you do depends on three things: the weather (good or bad); how much money you have (rich or poor) and whether your parents are visiting. You say to your yourself: if my parents are visiting, we'll go to the cinema. If they're not visiting and the weather's good and I'm rich, then I'll go shopping. If they're not visiting, and the weather's good and I'm poor, then I will go to the cinema. If they're not visiting and the weather is bad and I'm rich, I'll go to the cinema. If they're not visiting and the weather is bad and I'm poor, I'll stay in. Create a program asking whether the parents are visiting, whether the weather is good, and whether you are rich or poor. Your program should print "go to the cinema" "go shopping" or "stay in" as appropriate. Hint: There are two possibilities for the "Parents visiting?" question, two for the "is weather good?" question, and two for the "are you rich?" question. That gives you 8 possible cases
Explanation / Answer
Design:
Get the input, parent visiting, weather and rich or poor.
If parents are visiting, print “go to the cinema”.
If parents are not visiting and the weather's good and I'm rich, print “ go shopping.”
If parents are not visiting, and the weather's good and I'm poor, print “ go to the cinema”.
If parents not visiting and the weather is bad and I'm rich, print “ go to the cinema. “
If they're not visiting and the weather is bad and I'm poor, print “ stay in”.
Coding:
import java.util.Scanner;
public class Jprogram {
public static void main(String args[]){
String parentVisit, rich, weather;
//prompt for user input weather parent visit
Scanner input = new Scanner(System.in);
System.out.print(" Is the parent's visiting to meet you ? (type 'yes'/'no'):");
parentVisit = input.next();
//if the parent visiting print goto cinema
if(parentVisit.equalsIgnoreCase("YES"))
System.out.print(" Go to Cinema. ");
// if parent not visiting check the condition
else if(parentVisit.equalsIgnoreCase("NO")){
//prompt the user for weather condition
System.out.print(" wheather is good or bad (type 'Good'/'bad') :");
weather = input.next();
//if the weather condition is good and rich print goto shopping else print goto cinema
if(weather.equalsIgnoreCase("GOOD")){
System.out.print(" Are you Rich(type 'yes'/'no') :");
rich = input.next();
if(rich.equalsIgnoreCase("YES")){
System.out.print(" Go Shopping.");
}
else if(rich.equalsIgnoreCase("NO")){
System.out.print(" Go to Cinema.");
}
}
//if the weather condition is Bad and rich print goto shopping else print stay in
else if(weather.equalsIgnoreCase("BAD")){
System.out.print(" Are you Rich(type 'yes'/'no') :");
rich = input.next();
if(rich.equalsIgnoreCase("YES")){
System.out.print(" Go Shopping.");
}
else if(rich.equalsIgnoreCase("NO")){
System.out.print(" Stay in.");
}
}
}
}
}
Output:
Is the parent's visiting to meet you ? (type 'yes'/'no'):yes
Go to Cinema.
Is the parent's visiting to meet you ? (type 'yes'/'no'):no
wheather is good or bad (type 'Good'/'bad') :good
Are you Rich(type 'yes'/'no') :yes
Go Shopping.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.