Skip to content

lab #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

lab #71

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ Use the tests provided as examples to write your own tests for other supported t
### Part 3:

Modify your program from part 1 to use the Pet class and its subclasses. Keep a list of the pets your user lists and at the end of the program print out a list of their names and what they say when they speak.

14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
<groupId>io.zipcoder</groupId>
<artifactId>polymorphism-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>interfaces-1</name>
<url>http://maven.apache.org</url>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/pets/Bird.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.pets;

public class Bird extends Pets {


@Override
public String speak() {
return "Tweet";
}


}
10 changes: 10 additions & 0 deletions src/main/java/io/zipcoder/pets/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.zipcoder.pets;

public class Cat extends Pets{


@Override
public String speak() {
return "Meeeeeeeeow";
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/zipcoder/pets/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.zipcoder.pets;

public class Dog extends Pets {


@Override
public String speak() {
return "Bark";
}
}
37 changes: 37 additions & 0 deletions src/main/java/io/zipcoder/pets/Pets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.zipcoder.pets;

public class Pets {
String name;
String speak;

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

Integer age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSpeak() {
return speak;
}

public void setSpeak(String speak) {
this.speak = speak;
}

public String speak () {
return speak;
}

}
78 changes: 78 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Console.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.zipcoder.polymorphism;
import io.zipcoder.pets.Bird;
import io.zipcoder.pets.Cat;
import io.zipcoder.pets.Dog;
import io.zipcoder.pets.Pets;
import java.util.ArrayList;
import java.util.List;

public class Console {

List<Pets> listOfPets = new ArrayList<>();
private static Integer counter = 0;

public void run() {
System.out.println("Hello there! How many pets do you have?");
Integer amountOfPets = IO.numberOfPets();
while (counter < amountOfPets) {
for (int i = 0; i < amountOfPets; i++) {
counter++;
System.out.println("What kind of pet do you have?");
System.out.println("1. Cat" + "\n" + "2. Dog" + "\n" + "3. Bird");
switch (IO.chooseThePet()) {
case 1 :
listOfPets.add(i, new Cat());
break;
case 2 :
listOfPets.add(i, new Dog());
break;
case 3 :
listOfPets.add(i, new Bird());
break;
}
}
}
assigningNames();
assigningAges();
System.out.println("Wow! You have " + listOfPets.size() + " pets?! That's a lot!" +
"\n" + "Thank you for telling me about..." + "\n");
printingTheList();
}
public void assigningNames () {
System.out.println("What are their names?" + "\n");
for (Pets listOfPet : listOfPets) {
if (listOfPet instanceof Cat) {
System.out.println("The name of your cat?");
listOfPet.setName(IO.namesOfPets());
} else if (listOfPet instanceof Dog) {
System.out.println("The name of your dog?");
listOfPet.setName(IO.namesOfPets());
} else {
System.out.println("The name of your bird?");
listOfPet.setName(IO.namesOfPets());
}
}
}
public void printingTheList () {
for (Pets listOfPet : listOfPets) {
if (listOfPet instanceof Cat) {
System.out.println("Your cat, " + listOfPet.getName() + ", who is " + listOfPet.getAge() +
" years old, wants to tell you " + listOfPet.speak());
} else if (listOfPet instanceof Dog) {
System.out.println("Your dog, " + listOfPet.getName() + ", who is " + listOfPet.getAge() +
" years old, wants to tell you " + listOfPet.speak());
} else {
System.out.println("Your bird, " + listOfPet.getName() + ", who is " + listOfPet.getAge() +
" years old, wants to tell you " + listOfPet.speak());
}
}
}
public void assigningAges () {
System.out.println("What are their Ages?" + "\n");
for (Pets eachPet : listOfPets) {
System.out.println("How old is " + eachPet.getName() + "?");
eachPet.setAge(IO.thePetsAge());
}
}
}

29 changes: 29 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/IO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.zipcoder.polymorphism;

import java.util.Scanner;

public class IO {

public static Integer numberOfPets() {
Scanner scanner = new Scanner(System.in);
Integer userInitialInput = scanner.nextInt();
return userInitialInput;
}

public static String namesOfPets() {
Scanner scanner = new Scanner(System.in);
String nameOfPet = scanner.next();
return nameOfPet;
}

public static Integer chooseThePet() {
Scanner scanner = new Scanner(System.in);
Integer chooseYourPet = scanner.nextInt();
return chooseYourPet;
}
public static Integer thePetsAge() {
Scanner scanner = new Scanner(System.in);
Integer chooseYourPet = scanner.nextInt();
return chooseYourPet;
}
}
4 changes: 4 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package io.zipcoder.polymorphism;

public class MainApplication {
public static void main(String[] args) {
Console console = new Console();
console.run();
}
}