Unit 9
Unit 9
public class Animal {
protected double mass; //protected so the attribute isn't modified outside by outside modifiers
protected double lifespan;
public Animal(double mass, double lifespan) { //constructor with parameters of mass and lifespan
this.mass = mass;
this.lifespan = lifespan;
}
public class Dog extends Animal { //extends superclass into dog subclass
protected String color;
public Dog(double mass, double lifespan, String color) { //another constructor
super(mass, lifespan);
this.color = color;
}
}
}
public class Animal {
protected double mass;
protected double lifespan;
public Animal(double mass, double lifespan) {
this.mass = mass;
this.lifespan = lifespan;
}
public void sound() {
System.out.println("sound");
}
public class Dog extends Animal {
protected String color;
public Dog(double mass, double lifespan, String color) {
super(mass, lifespan);
this.color = color;
}
@Override
public void sound() { //overrides the sound method, by printing woof instead of sound
System.out.println("woof");
}
}
}
public class Animal {
protected double mass;
protected double lifespan;
public Animal(double mass, double lifespan) {
this.mass = mass;
this.lifespan = lifespan;
}
public void sound() {
System.out.println("sound");
}
public void die(boolean a) {
System.out.println("the cat is dead: " + a); //method to be overloaded
}
public class Dog extends Animal {
protected String color;
public Dog(double mass, double lifespan, String color) {
super(mass, lifespan);
this.color = color;
}
@Override
public void sound() {
System.out.println("woof");
}
}
public class Cat extends Animal { //make another subclass for Cat that extends off of Animal
protected String breed;
public Cat(double mass, double lifespan, String breed) {
super(mass, lifespan);
this.breed = breed;
}
@Override
public void sound() { //overrides the sound
System.out.println("meow");
}
public void die(boolean a, boolean b) { //method overloading
System.out.println("the cat is dead: " + a + b);
}
}
}
To create a two dimensional array in Java, you have to specify the data type of items to be stored in the array, followed by two square brackets and the name of the array.
public String dayOfWeekToString() {
return ("{ \"month\": " + this.month + ", " + "\"day\": " + this.day + ", " + "\"year\": " + this.year + ", "
+ "\"dayOfWeek\": " + this.dayOfWeek + " }");
}
public String toString() {
return dayOfWeekToString();
}