Question

(a) A class Scanner in Java can be used to get user input and its methods...

  1. (a) A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed:

         Scanner input = new Scanner(System.in);
    

    One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally the input should be returned. You can assume the aforementioned statement starting with Scanner has already been executed.

  2. (b) Write a method category(double height) which returns a suitable type in string form according to the following:

Condition Type height >= 200 Tall

100 <= height < 200 Medium height < 100 Short

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

(a) method : enterHeight():-

Explanation: The method enterHeight() must read height as a double value and return it. so its return type should be double.

here we are using do-while loop, in that using scanner object we read input height as double and if it is negative value then input reading is iterated, whenever we have supplied non negative value then only loop will be terminated and height value will be returned to the calling method.

double enterHeight()
{
Scanner input=new Scanner(System.in);
double height;
   do
   {
   System.out.print("Input hegiht:");
   height=input.nextDouble();
   }while(height<0);
  
   return height;
}

(b) method : category(height)

explanation: this method must return the words either "Tall" or "Medium" or "Short". So its return type should be string.

the calling method passes height value. based on the value of the parameter height. the category() method return string.

if height >= 200 then the method returns the string "Tall"

other wise if the height >=100 and height<200 then it returns "Medium"

otherwise the method returns "Short".

String category(double height)
{
if(height>=200)
return "Tall";
else if(height>=100 && height<200)
return "Medium";
else
return "Short";
}

java Program using above methods:

import java.util.*;
class Height_Example
{
double enterHeight()
{
Scanner input=new Scanner(System.in);
double height;
   do
   {
   System.out.print("Input hegiht:");
   height=input.nextDouble();
   }while(height<0);
  
   return height;
}

String category(double height)
{
if(height>=200)
return "Tall";
else if(height>=100 && height<200)
return "Medium";
else
return "Short";
}

public static void main(String arg[])
{
Height_Example p=new Height_Example();
double height=p.enterHeight();
System.out.println(p.category(height));
}
}

Add a comment
Know the answer?
Add Answer to:
(a) A class Scanner in Java can be used to get user input and its methods...
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
  • A class Scanner in Java can be used to get user input and its methods can...

    A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed: Scanner input = new Scanner(System.in); One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally the...

  • (TCO 1) The JVM executes a Java application by (Points : 6)        executing the public static...

    (TCO 1) The JVM executes a Java application by (Points : 6)        executing the public static main method of the class.        creating an object of the class and executing that object’s main method.        executing the class constructor.        compiling the Java code into byte code. (TCO 1) Which method call converts the value in variable stringVariable to a double? (Points : 6)        Double.parseDouble( stringVariable );        Convert.toDouble( stringVariable );        Convert.parseDouble( stringVariable );        Float.parseFloat( stringVariable ); (TCO 1) Which of the following can...

  • Write a JAVA program that prompts the user for student grades and displays the highest and...

    Write a JAVA program that prompts the user for student grades and displays the highest and lowest grades in the class. The user should enter a character to stop providing values. Starter code: import java.util.Scanner; public class MaxMinGrades{   public static void main(String[] args){     Scanner input = new Scanner(System.in);     System.out.println("Enter as many student grades as you like. Enter a character to stop.");     double grade = input.nextDouble();     double minGrade = Double.MAX_VALUE;     double maxGrade = Double.MIN_VALUE;     while (Character.isDigit(grade)) {       if (grade == 0)...

  • The Scanner class in Java has several methods for reading values entered from the keyboard. The...

    The Scanner class in Java has several methods for reading values entered from the keyboard. The methods include next, nextInt, and nextLine. Which of the following statements is INCORRECT about the methods? Pick the best applicable answer Assuming that the user has typed: Hello World! If you invoke the next method for the first time, it will return the string "Hello", and if you invoked next method for the second time, it will return the string "World!" If you want...

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

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • Show an example how to call a method with parameters with scanner input. For example class...

    Show an example how to call a method with parameters with scanner input. For example class distance.java With variables Then class distancetest.java with the method being called with the user entering input . Can be numbers with double type.

  • Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input...

    Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total. Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it...

  • Write a Java class that examines a single character input from the keyboard. Follow this dialog...

    Write a Java class that examines a single character input from the keyboard. Follow this dialog to guide your coding: Type a single character and press Enter: m Digit: No Letter: Yes Lowercase: Yes Toggle case: M Note: There is no "next" method of the Scanner class to obtain input from the keyboard of data type char. It simply doesn't exist so you need a workaround. Store the end-user input into a String variable; then extract the first character using...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

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