Write a class called cat that has the following: Three private instance variable
ID: 3934989 • Letter: W
Question
Write a class called cat that has the following: Three private instance variables name - A String representation of the cat's name. fluffiness - The cat's fluffiness on an integer scale of I to 10. lives - The integer number of lives that the cat has left. Two constructors A default constructor that initializes the name of the cat to "Fluffy", and sets fluffiness to 10 and lives to 9. A parameterized constructor that takes a String, int, int as parameters and assigns them to the name, fluffiness, and lives Three accessors (getters) getName which returns a String corresponding to the cat s name getFluffiness which returns an int corresponding to the cat's fluffiness getLives which returns an int corresponding to the cat's remaining lives Three mutators (setters) setName which has one parameter, a String, which is used to assign the cat's name setFluffiness which has one parameter, an int, which is used to assign the cat's fluffiness setLives which has one parameter, an int, which is used to assign the cat's remaining livesExplanation / Answer
Following is code in java for your question:-
For compile use:- javac example.java
For run:- java example
I have pass some hard coded parameter you can take this parameters from user and just pass varable name in this constructor.
For taking parameter you can use scanner class.
//You can import io and utils if you want to take input from user
//import.java.io.*;
//import java.util.*;
public class example {
// Main method from here we will create constructor objects.
public static void main(String[] args) {
Cat c1 = new Cat("abc",5,6); //call to parameterized constructor
Cat c2 = new Cat(); // call to default constructor
}
}
// class cat
class Cat{
// this are instance variables
private String name;
private int fluffiness;
private int lives;
//parameterized constructor
public Cat(String name, int fluffiness, int lives){
this.setName(name);
this.fluffiness = fluffiness;
this.lives = lives;
System.out.println("Cat Info from parameterized constructor :- ");
System.out.println("Cat Name: "+name +" Fluffuness:"+fluffiness+" lives:"+lives);
}
//default constructor
public Cat(){
this.setName("Fluffy");
this.fluffiness = 10;
this.lives = 9;
System.out.println(" Cat Info from default constructor :- ");
System.out.println("Cat Name: "+name +" Fluffuness:"+fluffiness+" lives:"+lives);
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the number
*/
public int getFluffiness() {
return fluffiness;
}
/**
* @param fluffiness the age to set
*/
public void setFluffiness(int fluffiness) {
this.fluffiness = fluffiness;
}
/**
* @return the number
*/
public int getLives() {
return lives;
}
/**
* @param lives the age to set
*/
public void setLives(int lives) {
this.lives = lives;
}
public String toString(){
return this.name + " " + this.fluffiness + " " + this.lives;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.