trying to write code underneath the appropriate comment. Needs to ask user to en
ID: 3754114 • Letter: T
Question
trying to write code underneath the appropriate comment. Needs to ask user to enter in a positive integer 5 times. Need to print out each integer after its entered. Using a call to method inputInteger. Here is the code so far
package lab2Package;
import java.util.Scanner;
public class Lab2Class
{
public static void main(String[] args)
{
//declare variables
String name;
int userNum;
//initialize variables
name = inputString("Enter your name ");
//enter your name at the keyboard
System.out.println("My name is "+ name);
//enter 5 integers at the keyboard and print it out
System.out.println("Integers from Keyboard");
//enter 5 decimal numbers at the keyboard and print each one out
System.out.println("Decimal values from keyboard");
//generate 20 random numbers and print each one out
System.out.println("Random numbers between one and two inclusively");
}//end main
//
// inputString
//
// the purpose of this method is to input a string from the keyboard
//
// Input: prompt the prompt message for the dialog box
//
// Return: inputStr the string that was entered at the keyboard
//
public static String inputString(String prompt)
{
Scanner input = new Scanner(System.in);
String inputStr;
//prompt user to enter in string
System.out.print(prompt);
//read in value as string from keyboard
inputStr = input.nextLine();
//return input string
return(inputStr);
}//end input string
//
// inputInteger
//
// the purpose of this method is to input an integer value form the keyboard
//
// Input: prompt the prompt message for the user
//
// Return: num the integer entered at the keyboard
//
public static int inputInteger(String prompt)
{
Scanner input = new Scanner(System.in);
String cleanUpStr;
int num;
num = 0;
cleanUpStr = "no string yet";
System.out.println(prompt);
num = input.nextInt();
cleanUpStr = input.nextLine();
return(num);
}//end inputInteger
}//end lab2
Explanation / Answer
package lab2Package; import java.util.Random; import java.util.Scanner; public class Lab2Class { public static void main(String[] args) { //declare variables String name; int userNum; double decimalValue; //initialize variables //enter your name at the keyboard name = inputString("Enter your name "); //enter 5 integers at the keyboard and print it out for(int i = 0; i < 5; ++i) { userNum = inputInteger("Enter an integer: "); System.out.println("You entered " + userNum); } //enter 5 decimal numbers at the keyboard and print each one out System.out.println("Decimal values from keyboard"); Scanner input = new Scanner(System.in); for(int i = 0; i < 5; ++i) { System.out.print("Enter a decimal value: "); decimalValue = input.nextDouble(); System.out.println("You entered " + decimalValue); } //generate 20 random numbers and print each one out System.out.println("Random numbers between one and two inclusively"); Random random = new Random(); for(int i = 0; i < 20; ++i) { System.out.println(random.nextDouble() + 1); } }//end main // // inputString // // the purpose of this method is to input a string from the keyboard // // Input: prompt the prompt message for the dialog box // // Return: inputStr the string that was entered at the keyboard // public static String inputString(String prompt) { Scanner input = new Scanner(System.in); String inputStr; //prompt user to enter in string System.out.print(prompt); //read in value as string from keyboard inputStr = input.nextLine(); //return input string return(inputStr); }//end input string // // inputInteger // // the purpose of this method is to input an integer value form the keyboard // // Input: prompt the prompt message for the user // // Return: num the integer entered at the keyboard // public static int inputInteger(String prompt) { Scanner input = new Scanner(System.in); String cleanUpStr; int num; num = 0; cleanUpStr = "no string yet"; System.out.println(prompt); num = input.nextInt(); cleanUpStr = input.nextLine(); return(num); }//end inputInteger }//end lab2Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.