Answer to Question #203550 in Software Engineering for Goku

Question #203550

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:47-0400
1. You have violated  Encapsulation  design principle without using
getter and setter methods.
2. You don't need to write a new method "run" in the class Cheetah. 
In such way you have violated Inheritance principle.

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