Question
Instructions Use separate methods for Part A and Part B. Part A Prompt the user for the number of cents 0-99. Assume that the user
Instructions
Use separate methods for Part A and Part B.
Part A
Prompt the user for the number of cents 0-99. Assume that the user will enter an integer. Use a method for validating the input. If the input value is not valiid: is less than 0 or greater than 99, then prompt the user to enter a valid number 0-99.
Use if/else-if/else for this part. Use logical operators && and || (AND and OR) as needed If the user enters invalid input 2 times, tell the user "They have exceeded the number of tries. Goodbye" and end the program.
When the user enters a valid input, calculate the change and display the results. Example calculation for Quarters:
cents=in.nextInt();
setQuarters(cents/25);
cents =cents % 25; // get the remaining cents for calculating dimes
Part B
Prompt the user for the number for a day 1-7. Assume that the user will enter an integer. Use SWITCH statements to evaluate the day of the week - DO NOT use if-else statements. Example:
switch(dayNumber){
case 1:
System.out.println("Monday");
break;
//type your code for case 2 to case 7
default:
System.out.println("Invalid day");
} //end switch
Part C
Use either the switch or the if-else statement to call the methods that execute both Part A and Part B.
package choices;
import java.util.Scanner;
public class Choices {
private int quarters; private int dimes; private int nickles; private int pennies; private int weekDay; public void Choices() { // initialize all attributes }
//Write a setter for each attribute //Write a getter for each attribute
// Complete the logic for the method public void convertMoney() { Scanner in = new Scanner(System.in); int cents = 0; System.out.println("Welcome to the Change Calculator"); System.out.print("Enter the number of cents (0-99): "); cents = /* capture the input */; if (/*test for less than 0 or greater than 99*/) { System.out.println(cents + " amount invalid."); System.out.println("Value must be between 0 and 99."); System.out.println(""); return; } setQuarters(cents / 25); cents = cents % 25; /*complete the code for dimes, nickles and pennies */ System.out.println(""); System.out.println("Quarters: " + getQuarters()); /* Complete the code for dimes, nickles and pennies*/ System.out.println(""); }
public void selectDay() { Scanner in = new Scanner(System.in); System.out.println("Welcome to the Day Selector"); System.out.print("Enter a number for the day (1-7): "); setWeekDay(in.nextInt()); switch (getWeekDay()) { case 1: System.out.println("Sunday: weekend"); break; case 2: System.out.println("Monday: weekday"); break; /* complete the code for Tuesday to Saturday */ default: System.out.println("Invalid day"); } } public static void main(String[] args) { String firstName; String lastName; int input; Scanner in = new Scanner(System.in); Choices Obj = new Choices(); System.out.print("Enter your first name: "); firstName = in.nextLine(); System.out.print("Enter your last name : "); lastName = in.nextLine(); System.out.println("Hello " /* complete the code to print the first and last name */); System.out.println(""); System.out.println("Press 1 - Calculate coins for change"); /* complete the code to display the info with Press 2 and Press 3 */ input = in.nextInt(); switch(input) { case 1: /* complete the code to convert the money */ break; case 2: /* complete the code to call the method that evaluates the days of the week */ break; case 3: /* complete the code to call both methods */ break; default: System.out.println(input + "is invalid"); } System.out.println(""); System.out.println("GoodBye"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started