Question
Write a java program: The goal of this assignment is to perform calculations using Weight objects (instances of Weight class). Weight Class Weight class should
Write a java program:
The goal of this assignment is to perform calculations using Weight objects (instances of Weight class).
Weight Class
Weight class should have three private variables, two private methods and four public methods.
Variables Hint: How should/can you access these private variables?
1. A private constant variable called OUNCES_IN_A_POUND that defines the number of ounces in a pound (16).
2. A private variable called pounds with a data type of integer.
3. A private variable called ounces with a data type of double.
Methods
1. A public parameterized constructor, which initializes the private variables. public Weight (int pounds, double ounces) {
// implementation
}
2. A private instance method called toOunces with a return type of double. This method has no parameters and should return the total number of ounces. For full credit, reuse this method across other methods when possible.
3. A private instance method called normalize with a return type of void. This method has no parameters and should ensure that the number of ounces is less than the number of ounces in a pound. For full credit, reuse this method across other methods when possible.
4. A public instance method called lessThan with a return type of boolean. This method should accept a Weight object as a parameter and determine if the object is greater or less than the initialized values.
public boolean lessThan (Weight weight) {
// implementation
}
5. A public instance method called addTo with a return type of void. This method should accept a Weight object as a parameter and add the objects weight values to the initialized values.
public void addTo (Weight weight) {
// implementation
}
6. A public instance method called to String with a return type of String. This method has no parameters and should have the following format:
x pounds and y ounces
where x is the number of pounds and y the number of ounces. Ounces should be displayed with two decimal places.
Deliverables
The submission requires uploading a single zip file, which will contain:
1. The source code of Weight.java (do not include the class file
3. A single document (DOC/DOCX or PDF), which includes the test case(s) in a tabular format and screenshot(s) of the test runs for the test case(s). Do not submit the screenshot(s) separately as they should be included in this document.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started