You are to write a Java program that prompts the user to enter a number for each
ID: 3886887 • Letter: Y
Question
You are to write a Java program that prompts the user to enter a number for each type of transportation. Use System.out.println method to output the message The prompt should read exactly Enter unicycles, bicycles, tandems Following this use the Scanner class to read in 3 whole numbers to represent each transportation type in order. When defining your variables, choose a data type that represents whole numbers When the program runs, you can enter the three numbers on one line or separate lines. Try it both ways. Example input. 0 8 3 Next your program will calculate the total cost. Rental prices are $2.50 for unicycles, $5.00 for bicycles, and $8.00 for tandem bicycles (a tandem bike has two seats). When defining the cost, choose a data type that represents a fractional (floating point) number. Note that it is not necessary to define a variable to store the cost, but it may make your program more easy to read Finally your program should output the cost calculated in step 3 preceded by the String "Total rental cost is s" Enter unicycles, bicycles, tandens 0 8 3 Total rental cost is $ 64.0 Don't worry that the program doesn't print $64.00. We will have an opportunity to learn to format output later in the semester 2.21 1 Lab2a public class Lob2aExplanation / Answer
So, according to the problem statement, we need to get the number of unicycles, bicycles and tandems from the user and calculate the total rental cost given the rent of each cycle type. Below is the code for the same in Java, followed with the explanation.
/*** CODE ***/
import java.util.Scanner;
class Lab2a
{
public static void main (String[] args)
{
// your code goes here
Scanner sc = new Scanner(System.in); // initialize a scanner
System.out.println("Enter unicycles, bicycles, tandems");
int unicycles = sc.nextInt(), bicycles = sc.nextInt(), tandems = sc.nextInt(); // define 3 variables
float totalCost = unicycles * 2.5f + bicycles * 5.0f + tandems * 8.0f; // calculated cost
System.out.println("Total rental cost is $" + totalCost); // Output the cost
}
}
/*** END ***/
Let's go through the above code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.