Created
September 13, 2017 03:37
-
-
Save dzt/2085b1ffd83c6c763ce80922344cbaff to your computer and use it in GitHub Desktop.
AP CS A Help for Bud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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