Store the name of the rock, the number of pounds of the rock (as a double), and the volume of
This problem has been solved!
Do you need an answer to a question different from the above? Ask your question!
Question:
- Store the name of the rock, the number of pounds of the rock (as a double), and the volume of the rock (as a double) as instance variables (or fields)
- Create the following:
- A constructor that takes only the name of the rock as a parameter
- Getters and setters with the following names (associated with the correct object variable): getName, setName, getNumPounds, setNumPounds, getVolume, setVolume.
- A method called calculateDensity that calculates the density of the rock (pounds divided by volume) and the return type is integer.
Example main method (Output below in red)
Rock r = new Rock("pet"); r.setVolume(2.0); r.setNumPounds(10.0); System.out.println(r.calculateDensity()); |
5
- A method called increasePounds that takes a double as a parameter and increases the number of pounds of the rock by the passed in argument. It does not return a value.
Example main method (Output below in red)
Rock r = new Rock("pet"); r.setNumPounds(10.0); r.increasePounds(5.0); System.out.println(r.getNumPounds()); |
15.0
- A method called decreasePounds that takes a double as a parameter and decreases the number of pounds of the rock by the passed in argument. It returns a double of the number of pounds of the rock after the passed in argument has been subtracted from the initial weight of the rock.
Example main method (Output below in red)
Rock r = new Rock("pet"); r.setNumPounds(10.0); System.out.println(r.decreasePounds(5.0)); |
5.0
- Write a toString method which takes no parameters and returns a string representing the rock in the format "Rock weighs pounds with a density of ". Pounds are specified to 3 digits of precision. A rock called "pet" that is 10 pounds and has a volume of 2 would return the string "Rock pet weighs 10.000 pounds with a density of 5.000".
Example main method (Output below in red)
Rock r = new Rock("pet"); r.setVolume(2.0); r.setNumPounds(10.0); System.out.println(r); |
Rock pet weighs 10.000 pounds with a density of 5
Related Book For
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch
View Solution
Create a free account to access the answer
Cannot find your solution?
Post a FREE question now and get an answer within minutes.
* Average response time.
Question Details
Chapter #
6
Section: PROGRAMMING PROJECTS
Problem: 6
Posted Date: September 14, 2023 04:20:12