Question

Read the entire document before starting to write code. Write and test a Java project that...

Read the entire document before starting to write code.
Write and test a Java project that satisfies the following requirements:

Create a measurement converter project that can convert between units of price. (pounds and ounces).

Use this table of conversion for your calculations.

The constant for conversion is 1 pound = 16 ounces or 16 ounces = 1 pound

Write a class definition file named UnitPriceConverter that has these fields and one constant.

priceOfItem weightInPounds pricePerPound pricePerOunce
constant: ouncesPerPound

Write the following methods: Two constructor methods:

The default constructor sets all the field values to zero except the constant. The constant is set using the conversion given above.

The overloaded constructor accepts a value for the price of the item and the weight of the item in pounds and then calculates the values for pricePerPound and pricePerOunce (use the constant for your conversion calculation).

Write two set( ) methods that allows the user to change the price of the item and the weight in pounds. This set( ) method must recalculate the price per pound and the price per ounce.

Write four get( ) methods. Once to get the price, one to get the weight in pounds, one to get the price per pound and one to get the price per ounce.

Write a driver class with the main( ) method. This main method should provide the following utility:

Create an instance of the UnitPriceConverter using the default values.

Write a loop that allows the user to reset the price and/or weight in pounds using the set( ) method and prints both the price per pound of the item and the price per ounce of the item in each iteration. Continue looping until the user indicates he/she is done. After the user decides to quit, print a goodbye message.

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

Following is the answer:

UnitPriceConverter.java
public class UnitPriceConverter {

    //constant value
    private static final int ouncesPerPound = 16;

    private double price;
    private double weight;

    //default constructor
    UnitPriceConverter(){
        this.price = 0.0;
        this.weight = 0.0;
    }

    //Paramaterize constructor
    UnitPriceConverter(double weight, double price){
        this.weight = weight;
        this.price = price;
    }

    //method that convert price per ounce
    public double getPricePerOunce(){
        return (weight * price) * 16;
    }

    //method that convert price per pound
    public double getPricePerPound(){
        return (weight * price)/ 16;
    }

    //getter and setter methods
    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    //toString method to print the values
    @Override
    public String toString() {
        return "Weight: " + weight + "
Price: " + price +  "
Price Per Ounce: " + getPricePerOunce() + "
Price Per Pound: " + getPricePerPound();
    }
}

main.java

import java.util.Scanner;

public class main {
    public static void main(String[] args) {

        //Create instance of UnitPriceConverter
        UnitPriceConverter u = new UnitPriceConverter();

        //Scanner for get the user inputs
        Scanner sc = new Scanner(System.in);

        char ch;
        //print the default values
        System.out.println(u.toString());

        //Ask user for continue or not
        System.out.print("Do Yo Want to Continue?Y/N: ");
        ch = sc.next().charAt(0);

        //loop iterates untill user inputs N
        while (ch == 'Y') {
            double weight, price;

            //get weight and price from user
            System.out.print("Enter new Weight: ");
            weight = sc.nextDouble();
            System.out.print("Enter new Price: ");
            price = sc.nextDouble();

            //set the price and weight
            u.setPrice(price);
            u.setWeight(weight);

            //print the values
            System.out.println(u.toString());

            //again ask the user to continue or not
            System.out.print("
Do Yo Want to Continue?Y/N: ");
            ch = sc.next().charAt(0);
        }

        //goodbye message
        System.out.println("goodbye.");
    }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Read the entire document before starting to write code. Write and test a Java project that...
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
  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from an input file. This program consists of two classes. The first class is the Weight class, which is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods and two private methods: 1. A public constructor that allows...

  • This assignment shpuld be code in java. Thanks Overview In this assignment you will write a...

    This assignment shpuld be code in java. Thanks Overview In this assignment you will write a program that will model a pet store. The program will have a Pet class to model individual pets and the Assignment5 class will contain the main and act as the pet store. Users will be able to view the pets, make them age a year at a time, add a new pet, and adopt any of the pets. Note: From this point on, your...

  • Java project Project Complex number class. Tuesday April 17 Write a Complex number class. It will...

    Java project Project Complex number class. Tuesday April 17 Write a Complex number class. It will have a default constructor, explicit constructor, and the following methods: read O public Complex add(Complex), public Complex subtract(Complex), public Complex multiply(Complex), public Complex divide(Complex), public boolean equals(complex) and a toString) method. Include get and set methods as well. On my website is an explanation of complex numbers and examples with expected answers along with a main demo program. Study the explanation and use your...

  • In C++ Write a program that contains a BankAccount class. The BankAccount class should store the...

    In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...

  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from a data file. This program consists of two parts. The first part is the Weight class and the second part is the Program Core. The Weight Class is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods...

  • Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes...

    Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes and Objects (15 Points) You will create 3 new classes for this project, two will be chosen (THE TWO CHOSEN ARE TENNIS SHOE AND TREE) from the list below and one will be an entirely new class you invent. Here is the list: Cellphone Clothes JuiceDrink Book MusicBand Bike GameConsole Tree Automobile Baseball MusicPlayer Laptop TennisShoe Cartoon EnergyDrink TabletComputer RealityShow HalloweenCostume Design First Create...

  • Write an ADT named circle in java containing the following. Include data: radius (double) Include methods:...

    Write an ADT named circle in java containing the following. Include data: radius (double) Include methods: A no-argument constructor A constructor that sets the data to passed values Getters and setters for each piece of data getArea (Math.PI * radius * radius … try using a Math method here) getDiameter (radius * 2) getCircumference (2 * Math.PI * radius) A toString( ) method to display the object data Include Javadoc comments for the class and every method, and generate the...

  • Question set 1 (Java) object oriented: 1.Write a class called Student. The class should be able...

    Question set 1 (Java) object oriented: 1.Write a class called Student. The class should be able to store information regarding the name, age, gpa, and phone number of a Student object. Please write all the setter and getter methods. Finally, write a Demo class to demonstrate the use of the class by creating two different objects. 2.Use the same Student class from the previous problem. This time,you will write a different Demo class, in which you will create three Student...

  • Tip Calculator App & Order Using NetBeans create a new JavaFX project with two classes TipCalculator...

    Tip Calculator App & Order Using NetBeans create a new JavaFX project with two classes TipCalculator (the JavaFX class) and Order Create the Tip Calculator as follows: Start with an Order class with two instance variables, item a String and price a double; include two constructors, a no-parameter constructor that passes default values empty String and zero (0) to the second constructor which then calls the set methods for item and price; the set method for price validates that price...

  • WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates...

    WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates a price change of a stock. When the program runs, it should do the following:  Create a Stock class which must include fields: name, symbol, currentPrice, nextPrice, priceChange, and priceChangePercentage.  The Stock class must have two constructor: a no-argument constructor and a constructor with four parameters that correspond to the four fields. o For the no-argument constructor, set the default value for...

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