Skip to content

Instantly share code, notes, and snippets.

@dzt
Created September 13, 2017 03:37
Show Gist options
  • Save dzt/2085b1ffd83c6c763ce80922344cbaff to your computer and use it in GitHub Desktop.
Save dzt/2085b1ffd83c6c763ce80922344cbaff to your computer and use it in GitHub Desktop.
AP CS A Help for Bud
public class Dog {
String name, breed;
double weight;
// constructor
public Dog(String name, String breed, double weight) {
this.name = name;
this.breed = breed;
this.weight = weight;
}
// Anything that gets data is called an "Accessor" method
// Anything that changes a value is called a "Mutator"
public String getName() {
return name;
}
public String getBreed() {
return breed;
}
public void changeName(String newName) {
name = newName;
}
public void changeBreed(String newBreed) {
breed = newBreed;
}
}
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner dogInput = new Scanner(System.in);
System.out.println("What do you want to name your dog?");
String dogName = dogInput.nextLine();
System.out.println("Breed?");
String dogBreed = dogInput.nextLine();
System.out.println("Weight?");
double dogWeight = dogInput.nextDouble();
// Create a New Object of the Class "Dog"
Dog newDog = new Dog(dogName, dogBreed, dogWeight);
System.out.println("Your dog's name is: " + newDog.getName());
System.out.println("Your dog's breed is: " + newDog.getBreed());
// close scanner
dogInput.close();
}
}
public class SuperDog extends Dog {
private String favFood;
public SuperDog(String name, String breed, double weight, String favFood) {
super(name, breed, weight);
this.favFood = favFood;
}
public String getFavFood() {
return favFood;
}
}
import java.util.Scanner;
public class SuperDogTest {
public static void main(String[] args) {
Scanner dogInput = new Scanner(System.in);
System.out.println("What do you want to name your dog?");
String dogName = dogInput.nextLine();
System.out.println("Breed?");
String dogBreed = dogInput.nextLine();
System.out.println("Fav Food?");
String favFood = dogInput.nextLine();
System.out.println("Weight?");
double dogWeight = dogInput.nextDouble();
// Create a New Object of the Class "Dog"
SuperDog newDog = new SuperDog(dogName, dogBreed, dogWeight, favFood);
System.out.println("Your dog's name is: " + newDog.getName());
System.out.println("Your dog's breed is: " + newDog.getBreed());
System.out.println("Your dog's fav food: " + newDog.getFavFood());
// close scanner
dogInput.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment