Answer to Question #203540 in Java | JSP | JSF for haris ali

Question #203540

Q2

Consider the following classes:

public abstract class Cat {

private int age;

private double weight;

private double top_speed;

public void run(double newSpeed) { ... }

private void eat(double portionSize) { ... }

// ... -> getXXX() and setXXX() methods here } 

}

public class Cheetah extends Cat {

private String type;

public Cheetah(int age, double wt, double sp, String type) {

this.type = type;

setAge(age);

setWeight(wt);

setTopSpeed(sp);

}

private void run(double newSpeed) { ... }

public void camouflage() { ... } // ... ->

//

getXXX()

//

and setXXX() methods here }

}

Which design principles are violated by these classes? Explain it and then correct it


1
Expert's answer
2021-06-05T07:11:44-0400
Inheritance  and Encapsulation  design principles were violated by these classes.
These classes don't  have getter and setter methods.
You have violated  inheritance design principle using the same method "run"  for classes Cat and Cheetah. You can call this method using the Cat class. 

Java code:

public abstract class Cat {


	private int age;
	private double weight;
	private double top_speed;


	public void run(double newSpeed) {
		this.top_speed = newSpeed;
	}


	private void eat(double portionSize) {
		this.weight += portionSize;
	}


	
	public int getAge() {
		return age;
	}


	
	public void setAge(int age) {
		this.age = age;
	}




	public double getWeight() {
		return weight;
	}




	public void setWeight(double weight) {
		this.weight = weight;
	}




	public double getTopSpeed() {
		return top_speed;
	}




	public void setTopSpeed(double top_speed) {
		this.top_speed = top_speed;
	}
}


public class Cheetah extends Cat {
	private String type;


	public Cheetah(int age, double wt, double sp, String type) {
		setAge(age);
		setWeight(wt);
		setTopSpeed(sp);
		this.type = type;
	}


	public void camouFlage() {
		
	}




	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS