Question

I need to create a code for this prompt: In this project we will build a...

I need to create a code for this prompt:

In this project we will build a generic UserInput class for getting keyboard input from the user.

Implementation:

The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard.

Write FOUR simple methods, one for each each type int, double, char and String.

For example, getInt() simply reads an int from the keyboard and returns it, getDouble reads a double and so on.

Here are the four method prototypes you must write:

public static int getInt()

public static char getChar()

public static double getDouble()

public static String getString()

The goal is simple, we will be writing 5-6 projects in this class. Some may have 4 - 5 .java files. Rather than have 20 different java files that import Scanner and do their OWN keyboard input, write ONE UserInput class and isolate ALL access to the Scanner class to ONE file. If we ever want to CHANGE how or where we get input, ONE file has to change, not 20.

The method getString() must allow spaces as part of the String.

Add four additional methods, one for each existing method that verifies the user input as valid.

For example, getInt() allows the user to input any integer, getInt(int min, int max) will only allow the user to input an integer between min and max. The new method should call the existing method then test the input and return valid input or loop and ask for re-input when the data is invalid.

Here are the four new methods you must write:

public static int getInt(int min, int max)

public static char getChar(char min, char max) // min char 'A', max char 'Z'

public static double getDouble(double min, double max)

public static String getString(int min, int max) // min and max length

These are called overloaded methods because they have the same name as the four original methods. The pattern for all methods is call the original method, check the input, return valid input or loop and ask for re-input when the data is invalid. The new method for a character should allow you to set a min ('A') and max ('Z') character and be case insensitive.

Please note we are using a command line interface for this project.

Write a main() method in this class that will test all eight input methods. The method main() will do for each method: 1.Print a message before calling the method

2.Call the method

3.Print a message with the user input returned by the method

The class UserInput will be a utility class that you will use for ALL console interface projects we do in this lab. Building 'generic' modules is a key concept in software reuse. It makes program development faster and more reliable. If done properly, this class will NOT change when used in future programs.

import java.util.Scanner; // Scanner is in java.util

public classTestScanner {

public static void main(String args[]) {

// Create a Scanner

Scanner input = new Scanner(System.in);

// Prompt the user to enter an integer

System.out.print("Enter an integer: ");

int intValue = input.nextInt();

System.out.println("You entered the integer " + intValue);

// Prompt the user to enter a double value

System.out.print("Enter a double value: ");

double doubleValue = input.nextDouble();

System.out.println("You entered the double value " + doubleValue);

// Prompt the user to enter a string

System.out.print("Enter a string without space: ");

String string = input.next();

System.out.println("You entered the string " + string); } }

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

If you have any doubts, please give me comment...

import java.util.Scanner; // Scanner is in java.util

public class TestScanner {

public static void main(String args[]) {

// Create a Scanner

Scanner input = new Scanner(System.in);

// Prompt the user to enter an integer

System.out.print("Enter an integer: ");

int intValue = UserInput.getInt();

System.out.println("You entered the integer " + intValue);

// Prompt the user to enter an character

System.out.print("Enter an character: ");

char charValue = UserInput.getChar();

System.out.println("You entered the character " + charValue);

// Prompt the user to enter a double value

System.out.print("Enter a double value: ");

double doubleValue = UserInput.getDouble();

System.out.println("You entered the double value " + doubleValue);

// Prompt the user to enter a string

System.out.print("Enter a string: ");

String string = UserInput.getString();

System.out.println("You entered the string " + string);

// by using overloaded methods with user input validation

// Prompt the user to enter an integer

System.out.print("Enter an integer: ");

intValue = UserInput.getInt(10, 50);

System.out.println("You entered the integer " + intValue);

// Prompt the user to enter an character

System.out.print("Enter an character: ");

charValue = UserInput.getChar('A', 'Z');

System.out.println("You entered the character " + charValue);

// Prompt the user to enter a double value

System.out.print("Enter a double value: ");

doubleValue = UserInput.getDouble(10.0, 100.0);

System.out.println("You entered the double value " + doubleValue);

// Prompt the user to enter a string

System.out.print("Enter a string: ");

string = UserInput.getString(8, 15);

System.out.println("You entered the string " + string);

}

}

class UserInput {

public static int getInt() {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

return num;

}

public static char getChar() { // min char 'A', max char 'Z'

Scanner input = new Scanner(System.in);

char ch = input.next().charAt(0);

return ch;

}

public static double getDouble() {

Scanner input = new Scanner(System.in);

double num = input.nextDouble();

return num;

}

public static String getString() { // min and max length

Scanner input = new Scanner(System.in);

String str = input.nextLine();

return str;

}

public static int getInt(int min, int max) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

while (num < min || num > max) {

System.out.print("Invalid input! Reenter: ");

num = input.nextInt();

}

return num;

}

public static char getChar(char min, char max) { // min char 'A', max char 'Z'

Scanner input = new Scanner(System.in);

char ch = input.next().charAt(0);

while (ch < min || ch > max) {

System.out.print("Invalid input! Reenter: ");

ch = input.next().charAt(0);

}

return ch;

}

public static double getDouble(double min, double max) {

Scanner input = new Scanner(System.in);

double num = input.nextDouble();

while (num < min || num > max) {

System.out.print("Invalid input! Reenter: ");

num = input.nextDouble();

}

return num;

}

public static String getString(int min, int max) { // min and max length

Scanner input = new Scanner(System.in);

String str = input.nextLine();

while (str.length() < min || str.length() > max) {

System.out.print("Invalid input! Reenter: ");

str = input.nextLine();

}

return str;

}

}

Add a comment
Know the answer?
Add Answer to:
I need to create a code for this prompt: In this project we will build a...
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
  • 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 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...

  • 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...

  • You will need to think about problem solving. There are several multi-step activities. Design, compile and...

    You will need to think about problem solving. There are several multi-step activities. Design, compile and run a single Java program, StringEx.java, to accomplish all of the following tasks. Add one part at a time and test before trying the next one. The program can just include a main method, or it is neater to split things into separate methods (all static void, with names like showLength, sentenceType, lastFirst1, lastFirst), and have main call all the ones you have written...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • How would I code this method in Java using a scanner? Begin by asking how many...

    How would I code this method in Java using a scanner? Begin by asking how many spaces the row should be with a prompt using System.out.print like the following, with 40 as the maximally allowed number of spaces: (user input shown bold) Please enter the number of spaces for the game (1-40): eight To do this, use the promptNumberReadLine method that you will write, described on the back page. If the user does not type a number in the correct...

  • 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...

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