In: Computer Science
In Java create a Flowchart that does the following. Suppose you shop for rice in two different packages. You would like to write a program to compare the cost. The program prompts the user to enter the weight and price of each package and displays the one with the better price. Here are two sample runs: Enter the weight for package 1: 50 Enter the price for package 1: 24.59 Enter the weight for package 2: 25 Enter the price for package 2: 11.99 Package 2 has a better price. Enter the weight for package 1: 50 Enter the price for package 1: 25 Enter the weight for package 2: 25 Enter the price for package 2: 12.50 The two packages have the same price.
The flowchart for the given problem is provided below:
//The complete executable java code is provided below:
Screenshot of the code:
Sample Output:
//Sample Run 1:
//Sample Run 2:
Code To Copy:
//Include the header file.
import java.util.Scanner;
//Define the main class.
class Main {
//Define the main function.
public static void main(String[] args) {
//Create an object of scanner class.
Scanner sc = new Scanner(System.in);
//Prompt the user to enter the weight for package1.
System.out.print("Enter the weight for package 1: ");
double weight1 = sc.nextDouble();
//Prompt the user to enter the cost for package1.
System.out.print("Enter the cost for package 1: ");
double price1 = sc.nextDouble();
//Prompt the user to enter the weight for package2.
System.out.print("Enter the weight for package 2: ");
double weight2 = sc.nextDouble();
//Prompt the user to enter the cost for package2.
System.out.print("Enter the cost for package 2: ");
double price2 = sc.nextDouble();
//Compare the prices of each package.
if (price1 / weight1 < price2 / weight2)
{
System.out.println("Package 1 has a better price.");
}
else if (price1 / weight1 > price2 / weight2)
{
System.out.println("Package 2 has a better price.");
}
else
{
System.out.println("Two packages have the same price.");
}
}
}