Skip to content

finished lab finally xD #61

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
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ In this lab you will practice creating a simple Java program designed to make us

## Unit Test

Select a partner from your tribe; You will each write tests for the requirements below, but your partner must develop against your tests and vice versa. Be sure to use the `io.zipcoder.pets` package for your Pet classes to allow tests to execute properly.
Select a partner from your tribe; You will each write tests for the requirements below, but your partner must develop against your tests and vice versa. Be sure to use the `io.zipcoder.io.zipcoder.polymorphism.polymorphism.pets` package for your Pet classes to allow tests to execute properly.

**Hint:** *An easy way to achieve this is for each partner to set up a GitHub repository for this lab, and add the other partner as a collaborator with write access (in the repository settings).*

## Instructions

### Part 1:

Create a program that asks the user how many pets they have. Once you know how many pets they have, ask them what kind of pet each one is, along with each pet's name. For now your program should just hold onto the user input and print out the list at the end; we'll modify this in part 3.
Create a program that asks the user how many io.zipcoder.polymorphism.polymorphism.pets they have. Once you know how many io.zipcoder.polymorphism.polymorphism.pets they have, ask them what kind of pet each one is, along with each pet's name. For now your program should just hold onto the user input and print out the list at the end; we'll modify this in part 3.

### Part 2:

Create a Pet class, and a subclass for each type of pet that you want your program to support. Your classes should follow the following requirements:

- You must support at least three types of pets.
- You must support at least three types of io.zipcoder.polymorphism.polymorphism.pets.
- Dog must be one of the types you support.
- Cat must be one of the types you support.
- The Pet class must have a `speak` method that each subclass overrides.
- The Pet class must have a `name` field with setters and getters.

Use the tests provided as examples to write your own tests for other supported types of pets.
Use the tests provided as examples to write your own tests for other supported types of io.zipcoder.polymorphism.polymorphism.pets.

### 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.
Modify your program from part 1 to use the Pet class and its subclasses. Keep a list of the io.zipcoder.polymorphism.polymorphism.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.

10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
<modelVersion>4.0.0</modelVersion>

<groupId>io.zipcoder</groupId>
<artifactId>polymorphism</artifactId>
<artifactId>io.zipcoder.polymorphism.polymorphism</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
5 changes: 5 additions & 0 deletions src/main/java/io/zipcoder/pets/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.zipcoder.pets;

public interface Animal {
String speak();
}
16 changes: 16 additions & 0 deletions src/main/java/io/zipcoder/pets/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.zipcoder.pets;

public class Cat extends Pet {

public Cat(){
super("Furball");
}

public Cat(String name){
super(name);
}

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

public class Dog extends Pet {

public Dog(){
super("Clifford");
}
public Dog(String name) {
super(name);
}
public String speak() {
return "bow-wow";
}
}
19 changes: 19 additions & 0 deletions src/main/java/io/zipcoder/pets/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.zipcoder.pets;

public abstract class Pet implements Animal {
private String name;
protected Pet(){}

protected Pet(String name) {
this.name = name;
}

public String getName() {
return name;
}

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

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

public class Platypus extends Pet {

public Platypus(){
super("Perry");
}
public Platypus(String name) {
super(name);
}
public String speak() {
return "Perry the platypus noises";
}
}
89 changes: 89 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,96 @@
package io.zipcoder.polymorphism;
import io.zipcoder.pets.Cat;
import io.zipcoder.pets.Dog;
import io.zipcoder.pets.Pet;
import io.zipcoder.pets.Platypus;
import java.util.Scanner;

/**
* Created by leon on 11/6/17.
*/
public class MainApplication {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Integer amount = askNumberOfPet(scanner);
Pet[] pets = askTypesAndNamesToPet(scanner,amount);
System.out.println(petSpeaks(pets));
}

public static String printPetList(Integer numberOfPets, String[] typeOfPets, String[] petNames) {

String result = "";
for(Integer index = 0; index < numberOfPets; index++)
result += String.format("%s %s\n", typeOfPets[index], petNames[index]);

return result;
}


public static String petSpeaks(Pet[] pets) {
String result = "";

for(Pet pet: pets)
result += String.format("%s the %s says\n\t%s\n",
pet.getName(), typeOfPetInString(pet), pet.speak());


return result.substring(0,result.length()-1);
}

private static String typeOfPetInString(Pet pet){
if(pet instanceof Cat)
return "cat";
else if (pet instanceof Dog)
return "dog";
else if (pet instanceof Platypus)
return "platypus";
else
return null;
}

private static Integer askNumberOfPet(Scanner scanner){
System.out.println("How many io.zipcoder.polymorphism.polymorphism.pets do you have?");
return scanner.nextInt();
}

private static String askTypesAndNamesToString(Scanner scanner, Integer amount){
String[] typeOfPets = new String[amount];
String[] petNames = new String[amount];

for(Integer index = 0; index < amount; index++) {
System.out.println("What kind of pet is pet " + (index+1) + "?");
typeOfPets[index] = scanner.next();
System.out.println("What is the the name of pet " + (index+1) + "?");
petNames[index] = scanner.next();
}

return printPetList(amount, typeOfPets, petNames);
}

private static Pet[] askTypesAndNamesToPet(Scanner scanner, Integer amount){
String[] typeOfPets = new String[amount];
String[] petNames = new String[amount];
Pet[] pets = new Pet[amount];

for(Integer index = 0; index < amount; index++) {
System.out.println("What kind of pet is pet " + (index+1) + "?");
Pet pet = createPet(scanner.next());
System.out.println("What is the the name of pet " + (index+1) + "?");
pet.setName(scanner.next());
pets[index] = pet;
}
return pets;
}

private static Pet createPet(String petType){
if(petType.equals("dog"))
return new Dog();
else if(petType.equals("cat"))
return new Cat();
else if(petType.equals("platypus"))
return new Platypus();
else
return null;
}
}
36 changes: 36 additions & 0 deletions src/test/java/io/zipcoder/pets/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.zipcoder.pets;
import org.junit.Assert;
import org.junit.Test;

public class CatTest {

@Test
public void nullaryConstructorTest() {
String expectedName = "Furball";
Cat cat = new Cat();

String actualName = cat.getName();

Assert.assertEquals(expectedName, actualName);
}

@Test
public void constructorWithNameTest() {
String expectedName = "SirMeowsAlot";
Cat cat = new Cat(expectedName);

String actualName = cat.getName();

Assert.assertEquals(expectedName, actualName);
}

@Test
public void speakTest() {
Cat cat = new Cat();
String expected = "meow ~";

String actual = cat.speak();

Assert.assertEquals(expected, actual);
}
}
36 changes: 36 additions & 0 deletions src/test/java/io/zipcoder/pets/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.zipcoder.pets;
import org.junit.Assert;
import org.junit.Test;

public class DogTest {

@Test
public void nullaryConstructorTest() {
String expectedName = "Clifford";
Dog dog = new Dog();

String actualName = dog.getName();

Assert.assertEquals(expectedName, actualName);
}

@Test
public void constructorWithNameTest() {
String expectedName = "SirBarksAlots";
Dog dog = new Dog(expectedName);

String actualName = dog.getName();

Assert.assertEquals(expectedName, actualName);
}

@Test
public void speakTest() {
Dog dog = new Dog();
String expected = "bow-wow";

String actual = dog.speak();

Assert.assertEquals(expected, actual);
}
}
43 changes: 43 additions & 0 deletions src/test/java/io/zipcoder/pets/PetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.zipcoder.pets;
import org.junit.Test;
import org.junit.Assert;

public class PetTest {

@Test
public void testDogInheritance() {
Pet p = new Dog();
Assert.assertTrue(p instanceof Pet);
}

@Test
public void testCatInheritance() {
Pet p = new Cat();
Assert.assertTrue(p instanceof Pet);
}

@Test
public void testPlatypusInheritance() {
Pet p = new Platypus();
Assert.assertTrue(p instanceof Pet);
}

@Test
public void testDogImplementation() {
Pet p = new Dog();
Assert.assertTrue(p instanceof Animal);
}

@Test
public void testCatImplementation() {
Pet p = new Cat();
Assert.assertTrue(p instanceof Animal);
}

@Test
public void testPlatypusImplementation() {
Pet p = new Platypus();
Assert.assertTrue(p instanceof Animal);
}

}
36 changes: 36 additions & 0 deletions src/test/java/io/zipcoder/pets/PlatypusTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.zipcoder.pets;
import org.junit.Assert;
import org.junit.Test;

public class PlatypusTest {

@Test
public void nullaryConstructorTest() {
String expectedName = "Perry";
Platypus platypus = new Platypus();

String actualName = platypus.getName();

Assert.assertEquals(expectedName, actualName);
}

@Test
public void constructorWithNameTest() {
String expectedName = "SirBarksAlots";
Platypus platypus = new Platypus(expectedName);

String actualName = platypus.getName();

Assert.assertEquals(expectedName, actualName);
}

@Test
public void speakTest() {
Platypus platypus = new Platypus();
String expected = "bow-wow";

String actual = platypus.speak();

Assert.assertEquals(expected, actual);
}
}
Loading