In Java: Design a CounterConsoleMenu class that provides a console-based user in
ID: 3857969 • Letter: I
Question
In Java:
Design a CounterConsoleMenu class that provides a console-based user interface for using Counter objects. This class should have, at minimum, the following behavior: • A mechanism to display the menu of choices (increment, decrement, reset, quit)
• A mechanism to get user input
• A mechanism to display the current count value
• A mechanism to increment, decrement, and reset the Counter
Develop a program called CounterTest to test the functionality of the CounterConsoleMenu and Counter classes working together.
Explanation / Answer
package chegg;
import java.util.Scanner;
public class CounterConsoleMenu {
Counter count;
public void run() {
System.out.println("Enter the input number");
Scanner in = new Scanner(System.in);
int number = in.nextInt();
count = new Counter(number);
System.out.println("Initial Count is " + number);
System.out.println("Enter your choice:");
System.out.println("1.Increment 2.Decrement 3.Reset 4.Quit");
int choice = in.nextInt();
while(choice!=4)
{
if(choice==1)
{
count.increment();
System.out.println("Current Count Value: " + count.getValue());
}
else if(choice == 2)
{
count.decrement();
System.out.println("Current Count Value: " + count.getValue());
}
else if (choice == 3)
{
count.reset();
System.out.println("Current Count Value: " + count.getValue());
}
System.out.println("1.Increment 2.Decrement 3.Reset 4.Quit");
choice = in.nextInt();
}
System.out.println("End of the operations");
}
}
package chegg;
public class Counter {
int number;
public Counter(int number)
{
this.number = number;
}
public void increment() {
// TODO Auto-generated method stub
number++;
}
public void reset() {
// TODO Auto-generated method stub
number = 0;
}
public void decrement() {
// TODO Auto-generated method stub
number--;
}
public int getValue() {
return number;
}
}
package chegg;
public class CounterTest {
public static void main(String[] args) {
CounterConsoleMenu ccm = new CounterConsoleMenu();
ccm.run();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.