Question

In: Computer Science

in java, write code that defines a class named Cat The class should have breed, name...

in java,
write code that defines a class named Cat The class should have breed, name and weight as attributes.
include setters and getters for the methods for each attribute.
include a toString method that prints out the object created from the class, and a welcome message.
And use a constructor that takes in all the attributes to create an object.

Solutions

Expert Solution

Kindly note that toString() method is defined in Object Class in Java and since any Class in Java is actually an Object of the Object Class , hence all classes already have a defined toString() method. If you simply print a Object (System.out.println(obj)). This invokes the toString() method and a output is returned. Originally , this output is the hashcode of the current object. Hence , we need to override the toString() method to print information as per our requirement.

Here is the complete implementation

class Cat{
    /* Attributes are private to provide more abstraction and therefore other classes need to use getter and setter functions to access these attributes */
    private String  breed;
    private String  name;
    private double weight;
    
    /* Class Constructor for initialisation of attributes */
    public Cat(String breed , String name , double weight){
        this.breed = breed;
        this.name = name;
        this.weight = weight;
    }
    
    /* Setter Functions */
    public void setBreed(String breed){
        this.breed = breed;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setWeight(double weight){
        this.weight = weight;
    }
    
    /* Getter Functions */
    public String getBreed(){
        return breed;
    }
    public String getName(){
        return name;
    }
    public double getWeight(){
        return weight;
    }
    
    /* Overriding toString function as it is already defined in the Object Class
       and if you call toString() without overriding it in your class , the output 
       will be the hashCode of the Object Eg:- Cat@453290 where last 6 digits are 
       hashcode of the object. Hence , we need to override the method*/ 
    @Override
    public String toString(){
         return ("Breed is : "+breed+",  Name is : "+name+
         ",  Weight is : "+weight+ "---WELCOME CAT---");
    }
    
} 
public class HelloWorld{

     public static void main(String []args){
         /* Creating two different objects */
        Cat C1=new Cat("Persian","Bella",6);  
        Cat C2=new Cat("Maine Coon","Lucy",9);
        /* Calling their toString method , which can be done by simple printing 
           the object in Java*/
        System.out.println(C1);
        System.out.println(C2); 
     }
}

Here is the screenshot of the output.


Related Solutions

Create a java class with name Cat. Instructions for Cat class: This class is modeled after...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after a Cat. You should have instance variables as follows: The Cat’s name The number of mice caught by the Cat. Whether or not the Cat is secretly plotting to kill you Note that you will need to choose both good types and meaningful identifiers for each of these instance variables. You may also assume that the Cat is not automatically always secretly plotting to...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a price of $50.99, prints the name and price of the product, reduces its price by 4.0, and then prints its price again. public class Product { private String name; private double price; public Product(String productName, double productPrice) { name = productName; price = productPrice; } public String getName() { return name; } public double getPrice() { return price; } public void reducePrice(double amount) {...
Design a class named Pet, which should have the following fields: Name – The name field...
Design a class named Pet, which should have the following fields: Name – The name field holds the name of a pet. Type – The type field holds the type of animal that is the pet. Example values are “Dog”, “Cat”, and “Bird”. Age – The age field holds the pet’s age. The Pet class should also have the following methods: setName – The setName method stores a value in the name field. setType – The setType method stores a...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT