View the step-by-step solution to:

Question

Use Scanner to get console input from the user:




Use next() and

next Line() to read Strings


Use next Int() to read int values


Use next Double() to read double values

Safely input numbers by checking before reading them:

o

has Next Int() before reading with next Int()

o


has Next Double() before reading with next Double()

Use the java if.. else control structure


Mini-lecture:


We use the Scanner class to get input from the user through the console. We have to import the


Scanner class and create instance of Scanner using the default input stream System.in.


import java.util.Scanner;


// imports go at the top of the file BEFORE the class!


Scanner in = new Scanner(System.in);


// it is very common to name the Scanner "in" or


console


// Scanner has a variety of methods that read data of a specific Java type:


int val = in.


next Int()


; // reads an int value throws an error if the input cannot be an int


double double Val = in.


next Double()


; // reads a double value error if the input isn't a double


String line = in.


next Line


(); // reads an entire line to a n newline character as a String


String word = in.


next


(); // reads part of the line up to a delimiter (space) or the n ...


The methods that return numeric types can throw an error if the input cannot be parsed into the


correct type. The following methods allow you to safely determine if you can successfully read


the numeric value:


in.


has NextInt ();


// returns true if next Int will succeed


in.


has Next Double();


// returns true if next Double will succeed


Since anything the user types can be read in as a legal java String, the next() and next Line()


methods cannot generate an error. We will need to use the java if and if .. else structures for this.


So let's take a look at them now.



Here is our key example for using the has Next X to bulletproof the code:


import java.util.Scanner; // before the class at top of the file


// this code in main:


Scanner in = new Scanner(System.in);


double wage = 0;


String trash = ""; // use for bad input which will read as a String


System.out.print("Enter your hourly wage: ");


if(in. has Next Double())


{


// OK safe to read in a double


wage = in .next Double ();


in.next Line(); // clears the newline from the buffer


}


else


{


// Not a double can't use next Double() read as String with next Line() instead


trash = in.next Line();


System .out. println ("n You said your wage was: " + trash);


System.out.println("Run the program again and enter a valid amount!");


System.exit(0); // terminate the program


}


So this block of code represents input for one variable. You will use this block multiple times


once for each variable that you need to read in. After you have all the inputs, complete the calc


and display the results. Of course the example is for a double var you have to modify it slightly


for an int var.


Lab Directions:


Complete each of the programs here. Create separate Net beans project for each program using


the name I specified. Create single java main class for each of the programs using the filename


I specified. You can help one another during the lab session but you should enter and submit the


code yourself. Make sure you understand any code you submit.




Task 1: (5 points)


Project name: CtoFConverter


Main file name: ctofConverter.java



A program that converts an inputted temperature in C and provides the equivalent


temperature in F. Hint: Google is your friend! Given C, solve for F. Again, check for a


valid input value and only respond with the F value if you got it, otherwise output an


appropriate error msg to the user. Testing: 3 conditions: Bad Input, then test for the


known freezing and boiling points.


EMBED SCREEN SHOT(S) OR COPY THE OUTPUT WINDOW OF NETBEANS


HERE SHOWING YOUR PROGRAM TEST RUN(S):


Task 2: (5 points)


Project name: FuelCosts


Main file name: fuelCost.java


Write program that asks the user to input



The number of gallons of gas in the tank



The fuel efficiency in miles per gallon



The price of gas per gallon


Then print the cost per 100 miles and how far the car can go with the gas in the tank.


Again, check for valid input and exit with an error msg if you do not have it. Testing: here


just use some reasonable values that you can inspect the calculations and determine they


are correct. Be sure to check of integer calculations truncating the results.


EMBED SCREEN SHOT(S) OR COPY THE OUTPUT WINDOW OF NETBEANS HERE


SHOWING YOUR PROGRAM TEST RUN(S):


Task 3: (5 points)


Project name: RectangleInfo


Main file name: rectInfo.java


Write program that asks the user for the lengths of the sides of a rectangle. Again, check


for valid input and exit with an error msg if you don't get it. Testing: use some known


values to confirm that the calculations are correct. E.g. 3 - 4 - 5 triangle >> 3 X 4 rectangle


Then print



The area and perimeter of the rectangle



The length of the diagonal (use the Pythagorean theorem)


EMBED SCREEN SHOT(S) OR COPY THE OUTPUT WINDOW OF NETBEANS


HERE SHOWING YOUR PROGRAM TEST RUN(S):


Task 4: (5 points)


Project name: MetricConverter


Main file name: metric.java



Write program that prompts the user for a measurement in meters and then converts it


to miles, feet, and inches. Again, check for valid input and exit with an error msg if you


don't get it. Testing: use some known values here and watch for integer truncation.


EMBED SCREEN SHOT(S) OR COPY THE OUTPUT WINDOW OF NETBEANS


HERE SHOWING YOUR PROGRAM TEST RUN(S):


Task 5 Graduate or Extra Credit: (3 points)


Project name: HighorLow


Main file name: highLow.java


Write program that has the computer generate a random int value between 1 to 10


inclusive. The program then asks the user to guess the number with a single guess. The


program displays the random number and then indicates if the users guess was high low or


on the money!

EMBED SCREEN SHOT(S) OR COPY THE OUTPUT WINDOW OF NETBEANS


HERE SHOWING YOUR PROGRAM TEST RUN(S)

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes