Question

Need help debugging. first class seems fine. second class is shooting an error on s =...

Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:");

//--------------------------------------------------------------------------

import java.util.Scanner;

public class Console
{
    private Scanner sc;
    boolean isValid;
    int i;
    double d;
  
    public Console()
    {
        sc = new Scanner(System.in);
    }
    public String getString(String prompt)
    {
        System.out.print(prompt);
        return sc.nextLine();
    }
    public int getInt(String prompt)
    {
        while (!isValid)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid integer. Try again.");
            }
            sc.nextLine();
        }
        return i;
    }
    public double getDouble(String prompt)
    {
        while (!isValid)
        {
            System.out.print(prompt);
            if (sc.hasNextDouble())
            {
                d = sc.nextDouble();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid number. Try again.");
            }
            sc.nextLine();
          
        }
        return d;
    }
}

//--------------------------------------------------------------------------

public class MyConsole
{
    boolean isValid;
    String s;
    public MyConsole()
    {
        super();
    }
    public String getString(String prompt)
    {
        while (isValid == false)
        {
            s = super.getString(prompt);
            if (s.equals(""))
            {
                System.out.println("Error! This entry is required. Try again.");
            }
            else
            {
                isValid = true;
            }
        }
        return s;
    }
}

public class MyConsole
{
    boolean isValid;
    String s;
    public MyConsole()
    {
        super();
    }
    public String getString(String prompt)
    {
        while (isValid == false)
        {
            s = super.getString(prompt);
            if (s.equals(""))
            {
                System.out.println("Error! This entry is required. Try again.");
            }
            else
            {
                isValid = true;
            }
        }
        return s;
    }
}

//--------------------------------------------------------------------

public class OddEvenApp
{
    public static void main(String[] args)
    {
        System.out.println("Welcome to the Odd/Even Checker!\n");
        MyConsole console = new MyConsole();
        String choice = "y";
        String oddOrEven;
        while (choice.equalsIgnoreCase("y"))
        {
            int num = console.getInt("Enter an integer: ");
            oddOrEven = "odd";

            if (num % 2 == 0)
            {
                oddOrEven = "even";
                System.out.println("The number " + num + " is " + oddOrEven + ".\n");
                choice = console.getString("Continue? (y/n): ");
                System.out.println();
            }
        }
    }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Console

{

private Scanner sc;

boolean isValid;

int i;

double d;

  

public Console()

{

sc = new Scanner(System.in);

}

public String getString(String prompt)

{

System.out.print(prompt);

return sc.nextLine();

}

public int getInt(String prompt)

{

while (!isValid)

{

System.out.print(prompt);

if (sc.hasNextInt())

{

i = sc.nextInt();

isValid = true;

}

else

{

System.out.println("Error! Invalid integer. Try again.");

}

sc.nextLine();

}

isValid = false;

return i;

}

public double getDouble(String prompt)

{

while (!isValid)

{

System.out.print(prompt);

if (sc.hasNextDouble())

{

d = sc.nextDouble();

isValid = true;

}

else

{

System.out.println("Error! Invalid number. Try again.");

}

sc.nextLine();

  

}

return d;

}

}

//--------------------------------------------------------------------------

public class MyConsole extends Console

{

boolean isValid;

String s;

public MyConsole()

{

super();

}

public String getString(String prompt)

{

while (isValid == false)

{

s = super.getString(prompt);

if (s.equals(""))

{

System.out.println("Error! This entry is required. Try again.");

}

else

{

isValid = true;

}

}

isValid = false;

return s;

}

}

//--------------------------------------------------------------------

public class OddEvenApp

{

public static void main(String[] args)

{

System.out.println("Welcome to the Odd/Even Checker!\n");

MyConsole console = new MyConsole();

String choice = "y";

String oddOrEven;

while (choice.equalsIgnoreCase("y")) {

int num = console.getInt("Enter an integer: ");

oddOrEven = "odd";

if (num % 2 == 0)

{

oddOrEven = "even";

}

System.out.println("The number " + num + " is " + oddOrEven + ".\n");

choice = console.getString("Continue? (y/n): ");

System.out.println();

}

}

}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Need help debugging. first class seems fine. second class is shooting an error on s =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • 1. Write a function in Tree class which returns true if and only if the tree...

    1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT