Assignment: Module 02 - Part 02
03. Create two classes named “Dog” and “Cat”.Define the objects for it and check if the objects are the instance of the classes or not.The class Dog and Cat should have the properties as : Name (string) ,Colour (string) , Breed (string) and Age (Integer). code and output. Assignment: Module 02 - Part 02
Code:
class Dog {
String name;
String colour;
String breed;
int age;
}
class Cat {
String name;
String colour;
String breed;
int age;
}
public class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.name = "Buddy";
dog1.colour = "Brown";
dog1.breed = "Golden Retriever";
dog1.age = 5;
Cat cat1 = new Cat();
cat1.name = "Whiskers";
cat1.colour = "Black";
cat1.breed = "Siamese";
cat1.age = 3;
System.out.println("dog1 is instance of Dog: " + (dog1 instanceof Dog));
System.out.println("cat1 is instance of Cat: " + (cat1 instanceof Cat));
}
}
Output:dog1 is instance of Dog: true
cat1 is instance of Cat: true
This code defines two classes named "Dog" and "Cat" with properties "name", "colour", "breed", and "age". Then, two objects dog1 and cat1 are created and initialized with values. In the main method, it uses the instanceof operator to check if the objects are an instance of the classes or not, and print the result.