Question

In: Computer Science

In Java create a program that does the following. Suppose you shop for rice in two...

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

Solutions

Expert Solution

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:


Related Solutions

In Java create a Flowchart that does the following. Suppose you shop for rice in two...
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...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Create a JavaFX program in java that does the following items: Display a drawing area of...
Create a JavaFX program in java that does the following items: Display a drawing area of dimension 500 x 400, with a black background. Provides a radio button group to allow the user to select one of the following three colors: red, green, and blue. Upon startup, the red radio button should be selected. Each time the user clicks in the drawing area, a circle of size 10 of the color selected by the radio button group in item 2...
Using Java create a program that does the following: Modify the LinkedList1 class by adding sort()...
Using Java create a program that does the following: Modify the LinkedList1 class by adding sort() and reverse() methods. The reverse method reverses the order of the elements in the list, and the sort method rearranges the elements in the list so they are sorted in alphabetical order. Do not use recursion to implement either of these operations. Extend the graphical interface in the LinkedList1Demo class to support sort and reverse commands, and use it to test the new methods....
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a Java program that asks a user to enter two file names. The program will...
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist. first input is the name of the first file and it has 2 (length) 4 5 6 7 Second input is the name of the second file and it has 2 (length) 6 7 8 9 try catch method
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT