Write a class called cat that has the following Three privat
Solution
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;
}
}

