Answer to Question #203537 in Java | JSP | JSF for Muqeet Waraich

Question #203537


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:43-0400
Encapsulation and inheritance  design principles are violated by these classes.
Encapsulation design principle was violated by these classes, because they do not have getter and setter methods.
The inheritance design principle was violated by the class "Cheetah". 
It has  private void method "run", but according to the rules of inheritance 
you don't need to use the same method twice.


Correct code 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;
	}


	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}


	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}


	/**
	 * @return the weight
	 */
	public double getWeight() {
		return weight;
	}


	/**
	 * @param weight the weight to set
	 */
	public void setWeight(double weight) {
		this.weight = weight;
	}


	/**
	 * @return the top_speed
	 */
	public double getTopSpeed() {
		return top_speed;
	}


	/**
	 * @param top_speed the top_speed to set
	 */
	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() {
		
	}


	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}


	/**
	 * @param type the type to set
	 */
	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