Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class Dinosaur{ private int height; // The height of a dinosaur in feet private int weight; // The weight of a dinosaur in pounds private

class Dinosaur{

private int height; // The height of a dinosaur in feet

private int weight; // The weight of a dinosaur in pounds

private String color; // The color of the dinosaur

public Dinosaur(int height, int weight, String color){

this.height = height;

this.weight = weight;

this.color = color;

}

public Dinosaur(String color){

this.color = color;

this.height = 0;

this.weight = 0;

}

public void eat(int mass){

if(isNegative(mass)) return;

this.weight += mass;

}

public void grow(int amount){

if(isNegative(amount)) return;

this.height += amount;

}

private boolean isNegative(int value){

if(value < 0) return true;

return false;

}

public String getColor(){

return this.color;

}

public int getHeight(){

return this.height;

}

public int getWeight(){

return this.weight;

}

public void equals(Object object){

return this == object;

}

}

Using the code above

a.) Suppose you want to create a Dinosaur object, which is red, 100 feet tall, and weighs 300 pounds. You want to store this object in a variable called dino. Write the statement that would accomplish this:

b.) Suppose you wrote the following lines, written after your declaration in the previous question. For each

line, indicate whether there will be an error, or if there is no error, what the result of the line will be:

(a) dino.getWeight();

(b) dino.setWeight(50);

(c) Dinosaur.isNegative(-50);

(d) dino.height += 50;

(e) dino.eat(50);

(f) dino.eat(-50);

(g) dino.equals("Hello world");

c.) This question asks several questions about the .equals method in the Dinosaur class. The equals method,

in this class, is meant to return true if the other object is a Dinosaur object, isn't null, and if the height,

weight, and colors of both dinosaurs are identical. That is, if one Dinosaur is 5 feet tall, weighs 50

pounds, and has a color of "purple", it should only return true if the other Object is a Dinosaur that is

5 feet tall, weighs 50 pounds, and has a color of "purple".

(a) Does the .equals method in the Dinosaur class currently work correctly as it is supposed to?

(b) What happens if you call

dino.equals("Hello")

in the current implementation?

(c) How do you check if

other

is a Dinosaur object or another type of Object?

(a) Write a statement to check if

other

is a String object:

(b) Write a statement to check if

other

is a Dinosaur object:

(c) Now, write a new version of equals for the Dinosaur class that behaves correctly:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

More Books

Students also viewed these Databases questions

Question

Are my points each supported by at least two subpoints?

Answered: 1 week ago