Please enter the description: bananas You entered bananas Please enter the cost: 2.0711 You entered 2.07 Please enter the weight: 4.56789 You entered 4.56789 bananas: $2.07, 4.6 pounds ($0.45/pound)
Objectives:
- Use Javadocs to document a class and methods
- Design and write a Java class
- Use fields (instance variables)
- Write multiple constructors
- Write accessor methods
- Write mutator methods
- Use the this keyword
- Use string concatenation
- Round floating point numbers (using Math.round())
- Use a Scanner object
Description
ProduceItem class
For this project, you get to design and write a ProduceItem class that stores a description, cost and weight as fields. Include both a constructor without any parameters and one with parameters for the description (String), cost (double) and weight (double) (in that order). For the constructor without parameters, set the description to an empty string, the cost to 0.0 and the weight to 0.0. Include appropriate accessor and mutator methods (and label them with comments including the terms "accessor" or "mutator"). So that the test cases compile, name the accessor methods:
- getDescription()
- getCost()
- getWeight()
Additionally, name the mutator methods:
- setDescription()
- setCost()
- setWeight()
Include, appropriately, the this keyword in your code. Do not use it unless it's necessary. This may mean you need to create a situation where it is required. Additionally, include a method named toString() that returns a String formatted as in the examples below (including rounding cost and cost per pound to two decimal places and weight to one decimal place). Also, include a getCostPerWeight() method that returns the cost divided by the weight. Do not write a public setCostPerWeight() method. That would allow code outside of the class to make the calculation inconsistent with the cost and weight fields.
ProduceItemDriver class
Write a ProduceItemDriver class that requests input from the user and populates a ProduceItem object and calls that object's toString() method.
Examples (user input in bold face blue)
Example 1
Please enter the description: bananas You entered bananas Please enter the cost: 2.0711 You entered 2.07 Please enter the weight: 4.56789 You entered 4.56789 bananas: $2.07, 4.6 pounds ($0.45/pound)
Example 2
Please enter the description: carrots You entered carrots Please enter the cost: 0.89 You entered 0.89 Please enter the weight: 2.000001 You entered 2.000001 carrots: $0.89, 2.0 pounds ($0.44/pound)
Example 3
Please enter the description: fresh wasabi root You entered fresh wasabi root Please enter the cost: 10.95 You entered 10.95 Please enter the weight: 0.15 You entered 0.15 fresh wasabi root: $10.95, 0.2 pounds ($73.0/pound)
Step by step
Solved in 4 steps with 4 images