Question
1. Write a default constructor for Toy that will set its weight to 0 and its dimensions to a default Dimension object. In other words,
1. Write a default constructor for Toy that will set its weight to 0 and its dimensions to a default Dimension object. In other words, for that second part, you will use the default constructor for Dimension to create it.
2. Write a non-default constructor for Toy that will set its width, height, length, and weight to values passed in to the constructor. Notice that setting the width, height, and length will involve creating a Dimension object. Assume you have written a setWeight function for Toy already that ensures that the weight is not set to a negative value.
class Dimension { private double height; private double width; private double length; public Dimension() { height = 0; width = 0; length = 0; } public void setHeight(double height) { if(height < 0){ height = height * -1; } this.height = height; } @Override public String toString() { return "Dimension{" + "height=" + height + ", width=" + width + ", length=" + length + '}'; } } abstract class Toy { private Dimension dimensions; private double weight; public String getType() { return "Toy"; } abstract public void play(); }
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