In: Computer Science
In Java create a program 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.
Please submit the following:
1. Your analysis: a. What information is required as input, information to be processed or output, and what corresponding variables and data types have you designed to represent them?
b. What selection structure or structures did you select and why?
2. Your flowchart
CODE IN JAVA:
RicePackage.java file:
import java.util.Scanner;
public class RicePackage {
public static void main(String[] args) {
// TODO Auto-generated method
stub
double
weight1,weight2,price1,price2;
Scanner sc = new
Scanner(System.in);
System.out.print("Emter the weight
for package1:");
weight1 = sc.nextDouble();
System.out.print("Enter price for
the package1:");
price1 = sc.nextDouble();
System.out.print("Emter the weight
for package2:");
weight2 = sc.nextDouble();
System.out.print("Enter price for
the package2:");
price2 = sc.nextDouble();
double unitPrice1,unitPrice2;
unitPrice1 = price1/weight1;
unitPrice2 = price2/weight2;
if(unitPrice1>unitPrice2)
{
System.out.println("Package2 has the better price....");
}
else if(unitPrice1<unitPrice2)
{
System.out.println("Package1 has the better price...");
}
else {
System.out.println("Two packages have the same price....");
}
}
}
OUTPUT: