Question

In java, create a method called getString that will read in Strings as in: Please enter...

In java, create a method called getString that will read in Strings as in:

Please enter the name of the traveler: david
Please enter the destination: rockey mountains
Please enter the car model: honda

  • The method is sent: "name of the traveler" or "name of the car" or "destination"
  • The method reads in the String
  • The method returns the String that was read in

In the main:
String name = getString("the name of the traveler");

Write the method called getString:



3) Create a method called getDouble that will read in doubles as in:

Please enter the MPG for this car: 14.6
Please enter the miles to destination: 1200

Please enter the cost per gallon: 3.62

  • The method is sent: "MPG for this car" OR "miles to destination" OR "cost per gallon"
  • The method reads in the double
  • If the number is less than or equal to zero, the method asks again and again until a positive number is entered
  • The method returns the double that was read in

In the main:
String name = getDouble("the miles per gallon");

Write the method called getDouble:



4) Write a main method to finish the class to include calling the methods that will create the following in the terminal window:

============================================================

Trip Calculator

This program will take a given mpg, destination, and miles,
will determine the gallons of gas needed for the trip.

============================================================
Please enter the name of the traveler: John

Please enter the destination: Walt Disney World
Please enter the miles to destination: 1200

Enter the information about the car...
Please enter the car model: Explorer
Please enter the MPG for this car: 14.6

Please enter the cost per gallon: 3.29


============================================================
Traveler: John
Trip Calculator to: Walt Disney World
Miles: 1200.0
Cost Per Gallon: 3.62

    Model Car MPG Cost for round trip
============================================================
        Explorer             14.6                   ?

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

2)

CODE

import java.util.Scanner;

public class Main {

public static String getString(String prompt) {

Scanner sc = new Scanner(System.in);

System.out.println("Please enter " + prompt + " : ");

return sc.nextLine();

}

public static void main(String[] args) {

String name = getString("the name of the traveler");

String destination = getString("the destination");

String carModel = getString("the car model");

System.out.println("Name entered: " + name);

System.out.println("Destination entered: " + destination);

System.out.println("Car model entered: " + carModel);

}

}

3)

CODE

import java.util.Scanner;

public class Main {

public static double getDouble(String prompt) {

Scanner sc = new Scanner(System.in);

System.out.println("Please enter " + prompt + " : ");

return sc.nextDouble();

}

public static void main(String[] args) {

double mpg = getDouble("the miles per gallon");

System.out.println("Miles per gallon entered: " + mpg);

}

}

NOTE: As per Chegg policy, I am allowed to answer only 2 coding questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
In java, create a method called getString that will read in Strings as in: Please enter...
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
  • // Enter your name as a comment for program identification // Program assignment TripCost.cpp // Enter...

    // Enter your name as a comment for program identification // Program assignment TripCost.cpp // Enter your class section, and time /* The program TripCost.cpp uses pointers to calculate    the cost of a trip depending on the cost of a gallon    of gas, the number of miles, and the number of miles    per gallon a car gets. */ /* The cost of gas, the number of miles, and number of    miles per gallon is entered. */...

  • C+ Create an application named CarDemo that declares at least two Car objects and demonstrates how...

    C+ Create an application named CarDemo that declares at least two Car objects and demonstrates how they can be incremented using an overloaded ++ operator. Create a Car class that contains the following properties: Model - The car model (as a string) Mpg The car's miles per gallon (as a double) Include two overloaded constructors. One accepts parameters for the model and miles per gallon; the other accepts a model and sets the miles per gallon to 20. Overload a...

  • In Java: Let's create a method that has parameters. Write a method, called returnHours that returns...

    In Java: Let's create a method that has parameters. Write a method, called returnHours that returns a string. Make the string from a string representing your name and a number representing the hours you spend sleeping, both are values that you pass in from the main. Create the variables to pass into the method call in main. Call the method and print the return value in main. Run and test it.

  • 1) (10 points) Create a new Java project, name it "Quiz02". Create a java class Traveler...

    1) (10 points) Create a new Java project, name it "Quiz02". Create a java class Traveler that has four data members, String name, Date bDate, int id and int noOfDependens. Generate the following: 1- Getters and Setters. 2- Four parametrize constructor to initialize the four data members above. 3- toString() method. Create a java class Flight that has four data members, String number, String destination, int capacity, and ArrayList<Traveler> travelers. • Notel - travelers will be used to track the...

  • A liter is 0.264179 gallons. Write a program that will read in the number of liters...

    A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user's car and the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon. After doing this... Modify your...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • JAVA PLEASE! Write a method called displayPets that takes an array of strings as input. The...

    JAVA PLEASE! Write a method called displayPets that takes an array of strings as input. The method should display the contents of the pets array on one line. Each name should be separated by a space. Use an enhanced for loop to iterate the array. Make sure to declare modifier, parameters and return values. Given an array String[] pets = {"Lucky", "Slinger", "Beast", "Trumpet", "Lulu", "Shadow", "Daisy"} A sample run when calling displayPets(pets) from main() would look like: Pets names:...

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

  • Drivers are concerned with the mileage their automobiles get. One driver has kept track of several...

    Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a C# app that will input the miles driven and gallons used (both as integers) for each tankful. The app should calculate and display the miles per gallon obtained for each tankful and display the combined miles per gallon obtained for all tankfuls up to this point. All averaging...

  • please help me add on this java code to run public class CarHwMain public static void...

    please help me add on this java code to run public class CarHwMain public static void main(String args 1/ Construct two new cars and one used car for the simulation Cari carl = new Car W"My New Mazda", 24.5, 16.0); Car car2 = new Cart My New Ford" 20.5, 15.0) Cari car) - new Cari ("My Used Caddie", 15.5, 16.5, 5.5, 1/ ADD CODE to completely fill the tanks in the new cars and off the used cars ton //...

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