Tugas Array dan Inheritance Kelompok 8


Soal (Nomor 8 hal 648)

Create a new class called Dog that is derived from the Pet class given in listing 6.1 of chapter 6. The new class has the additional attributes of breed (type String) and boosterShot (type boolean), which is true if the pet has had its booster shot and false if not. Give your classes a reasonablecomplement of constructors and accessor methods. Write a driver program to test all your methods, then write a program that reads in five pets of typeDog and displays the name and breed of all dogs that are over two years old and have not had their booster shots.

Penjelasan soal: Membuat class dog yang merupakan subclass dari class pet, yang mempunyai variable tambahan breed (string) dan boostershot (Boolean), jika pet memiliki boostershot maka bernilai true jika tidak akan bernilai false. Lalu ditampilkan jenis dog, umur dan berat.

Script code java:

Class pet

public class Pet {

/**

* @param args the command line arguments

*/

public String name;

private int age; //in years

private double weight;//in pounds

public Pet() {

name = "No name yet.";

age = 0;

weight = 0;

}

public Pet(String initialName, int initialAge, double initialWeight) {

name = initialName;

if ((initialAge < 0) || (initialWeight < 0)) {

System.out.println("Error: Negative age or weight.");

System.exit(0);

}else {

age = initialAge;

weight = initialWeight;

}

}

public void setPet(String newName, int newAge,double newWeight) {

name = newName;

if ((newAge < 0) || (newWeight < 0)) {

System.out.println("Error: Negative age or weight.");

System.exit(0);

} else {

age = newAge;

weight = newWeight;

}

}

public Pet(String initialName) {

name = initialName;

age = 0;

weight = 0;

}

public void setName(String newName) {

name = newName; //age and weight are unchanged.

}

public Pet(int initialAge) {

name = "No name yet.";

weight = 0;

if (initialAge < 0) {

System.out.println("Error: Negative age.");

System.exit(0);

} else

age = initialAge;

}

public void setAge(int newAge) {

if (newAge < 0) {

System.out.println("Error: Negative age.");

System.exit(0);

} else

age = newAge; //name and weight are unchanged.

}

public Pet(double initialWeight) {

name = "No name yet.";

age = 0;

if (initialWeight < 0) {

System.out.println("Error: Negative weight.");

System.exit(0);

} else

weight = initialWeight;

}

public void setWeight(double newWeight) {

if (newWeight < 0) {

System.out.println("Error: Negative weight.");

System.exit(0);

} else

weight = newWeight; //name and age are unchanged.

}

public String getName() {

return name;

}

public int getAge() {

return age;

}

public double getWeight() {

return weight;

}

public void writeOutput() {

System.out.println("Name: " + name);

System.out.println("Age: " + age + "years");

System.out.println("Weight: " + weight +"pounds");

}

}

Class dog

import java.util.Scanner;

public class dog extends Pet {

String breed;

boolean boosterShot;

public dog(){

super();

breed = "None";

boosterShot = false;

}

public dog(String newBreed, boolean boosterShot){

boolean booster = boosterShot;

breed = newBreed;

}

public dog(String name,int age, double weight){

String namePassed = null;

name = namePassed;

int agePassed = 0;

age = agePassed;

double weightPassed = 0;

weight = weightPassed;

}

public void writeOutput() {

System.out.println("berkembangbiak: " + breed);

System.out.println("boostershoot: "+ boosterShot);

}

public dog(String name,int age,int weight,String breed,boolean boosterShot){

String namePassed = null;

name = namePassed;

int agePassed = 0;

age = agePassed;

int weightPassed = 0;

weight = weightPassed;

String breedPassed = null;

breed = breedPassed;

boolean boosterShotPassed = false;

boosterShot = boosterShotPassed;

}

public String getBreed() {

return breed;

}

public boolean isBoosterShot() {

return boosterShot;

}

public void setBreed(String breed) {

this.breed = breed;

}

public void setBoosterShot(boolean boosterShot) {

this.boosterShot = boosterShot;

}

public static void main(String[] args) {

Pet p = new Pet();

dog d = new dog();

Scanner input = new Scanner(System.in);

for(int i=0; i<5; i++){

System.out.print("Masukan Nama Dog kamu : ");

String name = input.next();

System.out.print("Masukan Umur Dog kamu : ");

int umur = input.nextInt();

System.out.print("Masukan Berat Dog kamu : ");

int berat = input.nextInt();

System.out.print("Masukan Breed Dog kamu : ");

String breed = input.next();

System.out.print("Masukan Boostershoot Dog kamu : ");

boolean boost = input.nextBoolean();

p.setName(name);

p.getName();

p.setAge(umur);

p.getAge();

p.setWeight(berat);

p.getWeight();

d.setBreed(breed);

d.getBreed();

d.setBoosterShot(boost);

p.writeOutput();

d.writeOutput();

}

}

}


Kelompok 8:

– Arief Ryan Risbaya (1202160269)

– Azolla Dhigo (1202160171)

– Avisa Gavrilla (1202164302)

– Noviah Putri Haerunsyah (1202164162)

SI-40-02


Leave a Reply