Unit 5 Write Code for Toggle Code¶
This is the write code problems associated with the mixed up code problems.
Fix the following code so that the Dog
class has a constructor that takes in a String argument name
and assigns that value to the instance variable name
.
Create a constructor for the Dog
class that takes two parameters, name
(a String) and age
(an integer), and saves them in the correspondingly-named instance variables.
Fix the errors (commented as “TODO”) in the code so that it defines the Cat
class correctly. There should be a makeSound
method that prints "meow"
and returns nothing. There should also be a toString
method that returns "Name: name, Age: age"
(such that Cat("Lucky", 10)
’s toString
method would return "Name: Lucky, Age: 10"
).
Write code that completes the Square
class. It needs two variables: length
and numberOfSquares
. length
is an instance variable, while numberOfSquares
is a class variable that tracks how many squares have been made. getArea()
also needs to be completed, which will return the area of the square. Finally, there needs to be a completed toString()
method that returns "Length: length"
(such that Square(5)
’s toString method would return "Length: 5"
).
Write code that completes the Rectangle
class. It should have constructors that can take zero or two integer parameters. With zero arguments passed, the Rectangle
should be initialized with a length
of 10 and a width
of 10. With two integers passed, the Rectangle
should have a length
equal to the first argument and a width
equal to the second argument. There should also be a getArea
method that returns the area length
times width
.
Write code that completes the CelestialBody
class. Each CelestialBody
instance has an integer orbitLength
(in days) and a integer daysSinceDiscovered
attribute (which is initially 0). Using these, write the orbit(int numberOfTimes)
method that adds numberOfTimes * orbitLength
to daysSinceDiscovered
(e.g., if Planet X has done two orbits with an orbit length of 12 days, it was discovered 24 days ago. If it then orbits another three times, it was discovered 60 days ago). Also, fix the two errors in the class.
Write code to create a Person
class. Each Person
instance should have a String name
attribute and a integer age
attribute. There should also be getName
and setName
methods. Finally, there should to be a toString
method that returns “{name} is {age} years old” (e.g., Person("Carol", 12)
’s toString method would return "Carol is 12 years old"
).
Write code to create a Point
class. Each Point
instance should have integer x
and y
attributes (and the constructor should take those in that order). There should be getter methods for each: getX
and getY
. There should be a getDistance
method that takes in another Point
object as an argument and calculates the euclidean distance from this object to that one (which would be sqrt((this.x - other.x) ^ 2 + (this.y - other.y) ^ 2)). Finally, there should to be a toString
method that returns “(Point.x, Point.y)” (e.g., Point(3, 4)
’s toString method would return "(3, 4)"
).
Write code to create an Account
class. Each Account
instance should have integer balance
and String owner
attributes (and the constructor should take those in that order). To increase balance
, there should be a deposit
method that takes in an integer argument and adds that to balance
. To decrease balance
, there should be a withdraw
method that takes in an integer argument and subtracts that from balance
. However, if balance
would end as a negative number, it should just be set to zero. Finally, there should be a toString
method that returns "Account.owner: $Account.balance"
(so for Account(5, "Tom")
it should return "Tom: $5"
).
Write code that completes the Character
class. Each Character
instance has an integer healthPoints
(abbreviated HP) attribute and a String name
attribute. They also have the associated getHP
, setHP
, and getName
methods. Using these, write code that finishes the fight(Character other)
method that lets a character fight another. If the character’s healthPoints
are the same or more than other
’s, other
’s HP should be set to zero, the current character’s HP should be set to the difference, and the program should print “{the character’s name} wins”. That entire section is already completed. On the other hand, if other
’s HP is greater, the current character’s HP should be set to zero, other
’s HP should be set to the difference, and the program should print “{other’s name} wins”.