Write a program with a mother class animal. Inside it define a name and an age variables, and
set_value() method.Then create two instance variables Zebra and Dolphin which write a message
telling the age, the name and giving some extra information (e.g. place of origin).
Animal.java
public class Animal {
private String name;
private int age;
void set_value(String name, int age) {
this.name = name;
this.age = age;
}
String get_name() {
return name;
}
int get_age() {
return age;
}
}
Main.java
public class Main {
private static Animal zebra = new Animal();
private static Animal dolphin = new Animal();
public static void main(String[] args) {
zebra.set_value("Zebra", 18);
System.out.println("Name: " + zebra.get_name());
System.out.println("Age: " + zebra.get_age());
System.out.println("Place of origin: Africa");
System.out.println("");
dolphin.set_value("Dolphin", 15);
System.out.println("Name: " + dolphin.get_name());
System.out.println("Age: " + dolphin.get_age());
System.out.println("Place of origin: Ocean");
}
}
Comments
Leave a comment