Commit 0a4d5c9d authored by culve's avatar culve

This is the ysj zoo program.

The program: stores all animals in a single ArrayList, has appropriate
classes for all animals, displays all the details for any animal,
displays the details of a list of animals, generate a list of animals of
a certain type, displays the food required for a list of animals and
displays a list of all animals from a certain environment. Also, the
main method in Ysj_zoo.java has a while loop, so it doesn't need to run
multiple times to check it meets the success criteria.
parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>summative_assessment</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
/summative_assessment/
package summative_assessment;
//Create a public superclass called Animal
abstract public class Animal {
//Create an abstract public String method called getFood
abstract public String getFood();
//Create an abstract public String method called getHabitat
abstract public String getHabitat();
//Create an abstract public String method called getType
abstract public String getType();
//Create an abstract public String method called getName
abstract public String getName();
//Create an abstract public String method called getSpecies
abstract public String getSpecies();
}
package summative_assessment;
//Create a public subclass Beluga that inherits from Animal
public class Beluga extends Animal{
//Set private String variable food to equal 'Fish'
private String food = "Fish";
//Set private String variable habitat to equal 'Ocean'
private String habitat = "Ocean";
//Set private String variable type to equal 'Mammal'
private String type = "Mammal";
//Set private String variable name to equal 'Kana'
private String name = "Kana";
//Set private String variable species to equal 'Beluga Whale'
private String species ="Beluga Whale";
//Inherit public String method getFood
@Override
public String getFood() {
// TODO Auto-generated method stub
//Return the String variable food
return food;
}
//Inherit public String method getHabitat
@Override
public String getHabitat() {
// TODO Auto-generated method stub
//Return the String variable habitat
return habitat;
}
//Inherit public String method getType
@Override
public String getType() {
// TODO Auto-generated method stub
//Return the String variable type
return type;
}
//Inherit public String method getName
@Override
public String getName() {
// TODO Auto-generated method stub
//Return the String variable name
return name;
}
//Inherit public String method getSpecies
@Override
public String getSpecies() {
// TODO Auto-generated method stub
//Return the String variable species
return species;
}
}
package summative_assessment;
//Create a public subclass Frog that inherits from Animal
public class Frog extends Animal{
//Set private String variable food to equal 'Plants'
private String food = "Insects";
//Set private String variable habitat to equal 'Jungle'
private String habitat = "Jungle";
//Set private String variable type to equal 'Mammal'
private String type = "Amphibian";
//Set private String variable name to equal 'Miyubi'
private String name = "Frogger";
//Set private String variable species to equal 'Two-Toed Sloth'
private String species = "Poison Dart Frog";
//Inherit public String method getFood
@Override
public String getFood() {
// TODO Auto-generated method stub
//Return the String variable food
return food;
}
//Inherit public String method getHabitat
@Override
public String getHabitat() {
// TODO Auto-generated method stub
//Return the String variable habitat
return habitat;
}
//Inherit public String method getType
@Override
public String getType() {
// TODO Auto-generated method stub
//Return the String variable type
return type;
}
//Inherit public String method getName
@Override
public String getName() {
//Return the String variable name
return name;
}
//Inherit public String method getSpecies
@Override
public String getSpecies() {
// TODO Auto-generated method stub
//Return the String variable species
return species;
}
}
package summative_assessment;
//Create a public subclass Hyena that inherits from Animal
public class Hyena extends Animal{
//Set private String variable food to equal 'Meat'
private String food = "Meat";
//Set private String variable habitat to equal 'Savannah'
private String habitat = "Savannah";
//Set private String variable type to equal 'Mammal'
private String type = "Mammal";
//Set private String variable name to equal 'Yena'
private String name = "Yena";
//Set private String variable species to equal 'Spotted Hyena'
private String species = "Spotted Hyena";
//Inherit public String method getFood
@Override
public String getFood() {
// TODO Auto-generated method stub
//Return the String variable food
return food;
}
//Inherit public String method getHabitat
@Override
public String getHabitat() {
// TODO Auto-generated method stub
//Return the String variable habitat
return habitat;
}
//Inherit public String method getType
@Override
public String getType() {
// TODO Auto-generated method stub
//Return the String variable type
return type;
}
//Inherit public String method getName
@Override
public String getName() {
// TODO Auto-generated method stub
//Return the String variable name
return name;
}
//Inherit public String method getSpecies
@Override
public String getSpecies() {
// TODO Auto-generated method stub
//Return the String variable species
return species;
}
}
package summative_assessment;
//Import java.util.ArrayList library, to be able to use ArrayLists
import java.util.ArrayList;
//Create a public class called Inventory
public class Inventory {
//Create a public void method using an ArrayList (of Animal values) value called inventory
public void inventory(ArrayList<Animal> a) {
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//Output the returned values from all of a.get(i)'s methods
System.out.println("Animal: " + a.get(i).getSpecies() +
" |Name: " + a.get(i).getName() +
" |Food: " + a.get(i).getFood() +
" |Habitat: " + a.get(i).getHabitat() +
" |Type: " + a.get(i).getType() +
"\n");
}
}
}
package summative_assessment;
//Create a public subclass Koala that inherits from Animal
public class Koala extends Animal{
//Set private String variable food to equal 'Plants'
private String food = "Plants";
//Set private String variable habitat to equal 'Outback'
private String habitat = "Outback";
//Set private String variable type to equal 'Mammal'
private String type = "Mammal";
//Set private String variable name to equal 'Yukari'
private String name = "Yukari";
//Set private String variable species to equal 'Koala'
private String species = "Koala";
//Inherit public String method getFood
@Override
public String getFood() {
// TODO Auto-generated method stub
//Return the String variable food
return food;
}
//Inherit public String method getHabitat
@Override
public String getHabitat() {
// TODO Auto-generated method stub
//Return the String variable habitat
return habitat;
}
//Inherit public String method getType
@Override
public String getType() {
// TODO Auto-generated method stub
//Return the String variable type
return type;
}
//Inherit public String method getName
@Override
public String getName() {
// TODO Auto-generated method stub
//Return the String variable name
return name;
}
//Inherit public String method getSpecies
@Override
public String getSpecies() {
// TODO Auto-generated method stub
//Return the String variable species
return species;
}
}
package summative_assessment;
//Create a public subclass Lion that inherits from Animal
public class Lion extends Animal{
//Set private String variable food to equal 'Meat'
private String food = "Meat";
//Set private String variable habitat to equal 'Savannah'
private String habitat = "Savannah";
//Set private String variable type to equal 'Mammal'
private String type = "Mammal";
//Set private String variable name to equal 'King'
private String name = "King";
//Set private String variable species to equal 'Lion'
private String species = "Lion";
//Inherit public String method getFood
@Override
public String getFood() {
// TODO Auto-generated method stub
//Return the String variable food
return food;
}
//Inherit public String method getHabitat
@Override
public String getHabitat() {
// TODO Auto-generated method stub
//Return the String variable habitat
return habitat;
}
//Inherit public String method getType
@Override
public String getType() {
// TODO Auto-generated method stub
//Return the String variable type
return type;
}
//Inherit public String method getName
@Override
public String getName() {
// TODO Auto-generated method stub
//Return the String variable name
return name;
}
//Inherit public String method getSpecies
@Override
public String getSpecies() {
// TODO Auto-generated method stub
//Return the String variable species
return species;
}
}
package summative_assessment;
//Import java.util.ArrayList library, to be able to use ArrayLists
import java.util.ArrayList;
//Create a public class called ListTypes
public class ListClear {
//Create a public void method using an ArrayList (of Animal objects) called listclear
public void listclear(ArrayList<Animal> a) {
//Set i to equal the length of a - 1
int i = a.size() -1;
//Loop while a is not empty
while( a.isEmpty() == false) {
//Remove element in position i in a
a.remove(i);
//Set i to equal itself - 1
i--;
}
//Output that arraylist has been emptied
System.out.println("Arraylist emptied");
}
}
package summative_assessment;
//Import java.util.ArrayList library, to be able to use ArrayLists
import java.util.ArrayList;
//Import java.util.Scanner library, to be able to use user inputs
import java.util.Scanner;
//Create a public class called ListDetails
public class ListDetails {
//Create a public void method using an ArrayList (of Animal objects) called listdetails
public void listdetails(ArrayList<Animal> a) {
//Set animalChoice as an integer
int animalChoice;
//Set input to be user's input
Scanner input = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("We have " + a.size() + " animals in our zoo." + "\n" +
"Please enter a number between 1 and " + a.size() +
" to find more information about it.");
//Set animalChoice to equal input
animalChoice = input.nextInt();
//Set integer detail to equal animalChoice - 1
int detail = animalChoice - 1;
//Output all variables stored in chosen animal's class
System.out.println("Animal: " + a.get(detail).getSpecies() +
" |Name: " + a.get(detail).getName() +
" |Food: " + a.get(detail).getFood() +
" |Habitat: " + a.get(detail).getHabitat() +
" |Type: " + a.get(detail).getType() +
"\n");
}
}
package summative_assessment;
//Import java.util.ArrayList library, to be able to use ArrayLists
import java.util.ArrayList;
//Import java.util.Scanner library, to be able to use user inputs
import java.util.Scanner;
//Create a public class called ListTypes
public class ListFood {
//Create a public void method using an ArrayLists (of Animal objects) called listfood
public void listfood(ArrayList<Animal> a, ArrayList<Animal> b) {
int foodChoice;
//Set input to be user's input
Scanner input = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("> To list all who eat meat enter 1," + "\n" +
"> To list all who eat plants enter 2," + "\n" +
"> To list all who eat fish enter 3," + "\n" +
"> To list all who are insects enter 4." + "\n");
//Set foodChoice to equal input
foodChoice = input.nextInt();
//If foodChoice is equal to
switch(foodChoice) {
//1 then,
case 1:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getFood method called from the item in position i in ArrayList a is the same as 'Meat', then
if (a.get(i).getFood().equals("Meat")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it eats meat
System.out.println(b.get(j).getName() + " eats meat." + "\n");
}
//Prevent the program running the other cases
break;
//2 then,
case 2:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getFood method called from the item in position i in ArrayList a is the same as 'Plants',
if (a.get(i).getFood().equals("Plants")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it eats plants
System.out.println(b.get(j).getName() + " eats plants." + "\n");
}
//Prevent the program running the other cases
break;
//3 then,
case 3:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getFood method called from the item in position i in ArrayList a is the same as 'Fish',
if (a.get(i).getFood().equals("Fish")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it eats fish
System.out.println(b.get(j).getName() + " eats fish." + "\n");
}
//Prevent the program running the other cases
break;
//4 then,
case 4:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getFood method called from the item in position i in ArrayList a is the same as 'Insects',
if (a.get(i).getFood().equals("Insects")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it eats insects
System.out.println(b.get(j).getName() + " eats insects." + "\n");
}
//Prevent the program running the other cases
break;
//If foodChoice is not a listed case
default:
//Output that the choice of food is not available
System.out.print("choice of food not available");
}
//Set list_clear to refer to new ListClear object
ListClear list_clear = new ListClear();
//Call the listclear method, using ArrayList (of Animal objects) a, from list_clear
list_clear.listclear(b);
}
}
package summative_assessment;
//Create a public subclass Shark that inherits from Animal
public class Shark extends Animal{
//Set private String variable food to equal 'Fish'
private String food = "Meat";
//Set private String variable habitat to equal 'Ocean'
private String habitat = "Ocean";
//Set private String variable type to equal 'Mammal'
private String type = "Fish";
//Set private String variable name to equal 'Kana'
private String name = "Bruce";
//Set private String variable species to equal 'Beluga Whale'
private String species = "Great White Shark";
//Inherit public String method getFood
@Override
public String getFood() {
// TODO Auto-generated method stub
//Return the String variable food
return food;
}
//Inherit public String method getHabitat
@Override
public String getHabitat() {
// TODO Auto-generated method stub
//Return the String variable habitat
return habitat;
}
//Inherit public String method getType
@Override
public String getType() {
// TODO Auto-generated method stub
//Return the String variable type
return type;
}
//Inherit public String method getName
@Override
public String getName() {
// TODO Auto-generated method stub
//Return the String variable name
return name;
}
//Inherit public String method getSpecies
@Override
public String getSpecies() {
// TODO Auto-generated method stub
//Return the String variable species
return species;
}
}
package summative_assessment;
//Create a public subclass Sloth that inherits from Animal
public class Sloth extends Animal{
//Set private String variable food to equal 'Plants'
private String food = "Plants";
//Set private String variable habitat to equal 'Jungle'
private String habitat = "Jungle";
//Set private String variable type to equal 'Mammal'
private String type = "Mammal";
//Set private String variable name to equal 'Miyubi'
private String name = "Miyubi";
//Set private String variable species to equal 'Two-Toed Sloth'
private String species = "Two-Toed Sloth";
//Inherit public String method getFood
@Override
public String getFood() {
// TODO Auto-generated method stub
//Return the String variable food
return food;
}
//Inherit public String method getHabitat
@Override
public String getHabitat() {
// TODO Auto-generated method stub
//Return the String variable habitat
return habitat;
}
//Inherit public String method getType
@Override
public String getType() {
// TODO Auto-generated method stub
//Return the String variable type
return type;
}
//Inherit public String method getName
@Override
public String getName() {
//Return the String variable name
return name;
}
//Inherit public String method getSpecies
@Override
public String getSpecies() {
// TODO Auto-generated method stub
//Return the String variable species
return species;
}
}
package summative_assessment;
//Create a public subclass Wolf that inherits from Animal
public class Wolf extends Animal{
//Set food as a public String variable
private String food = "Meat";
//Set habitat as a public String variable
private String habitat = "Forest";
//Set type as a public String variable
private String type = "Mammal";
//Set name as public String variable
private String name = "Ranka";
//Set species as a public String variable
private String species = "Eastern Wolf";
//Inherit public String method getFood
@Override
public String getFood() {
// TODO Auto-generated method stub
//Return the String variable food
return food;
}
//Inherit public String method getHabitat
@Override
public String getHabitat() {
// TODO Auto-generated method stub
//Return the String variable habitat
return habitat;
}
//Inherit public String method getType
@Override
public String getType() {
// TODO Auto-generated method stub
//Return the String variable type
return type;
}
//Inherit public String method getName
@Override
public String getName() {
// TODO Auto-generated method stub
//Return the String variable name
return name;
}
//Inherit public String method getSpecies
@Override
public String getSpecies() {
// TODO Auto-generated method stub
//Return the String variable species
return species;
}
}
package summative_assessment;
//Import java.util.ArrayList library, to be able to use ArrayLists
import java.util.ArrayList;
//Import java.util.Scanner library, to be able to use user inputs
import java.util.Scanner;
//Create a public class called Ysj_zoo
public class Ysj_zoo {
//Set zoo_collection to equal a new static ArrayList which uses Animal objeccts
static ArrayList<Animal> zoo_collection = new ArrayList<Animal>();
//Set empty_array to equal a new static ArrayList which uses Animal objects
static ArrayList<Animal> empty_array = new ArrayList<Animal>();
//Main method
public static void main(String[] args) {
//Set wolf to refer to new Wolf object
Wolf wolf = new Wolf();
//Add wolf to ArrayList zoo_collection
zoo_collection.add(wolf);
//Set koala to refer to new Koala object
Koala koala = new Koala();
//Add koala to ArrayList zoo_collection
zoo_collection.add(koala);
//Set sloth to refer to new Sloth object
Sloth sloth = new Sloth();
//Add sloth to ArrayList zoo_collection
zoo_collection.add(sloth);
//Set hyena to refer to new Hyena object
Hyena hyena = new Hyena();
//Add hyena to ArrayList zoo_collection
zoo_collection.add(hyena);
//Set lion to refer to new Lion object
Lion lion = new Lion();
//Add lion to ArrayList zoo_collection
zoo_collection.add(lion);
//Set beluga to refer to new Beluga object
Beluga beluga = new Beluga();
//Add beluga to ArrayList zoo_collection
zoo_collection.add(beluga);
//Set shark to refer to new Shark object
Shark shark = new Shark();
//Add shark to ArrayList zoo_collection
zoo_collection.add(shark);
//Set Frog to refer to new Frog object
Frog frog = new Frog();
//Add frog to ArrayList zoo_collection
zoo_collection.add(frog);
// TODO Auto-generated method stub
//Set boolean variable finish to equal false
boolean finish = false;
//Loop while finish is the same as false
while (finish == false) {
//Set confirm as an integer
int confirm;
//Set question to be an integer value
int question;
//Set input to be user's input
Scanner input = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("> To find out what animals are at the zoo enter 1, " + "\n"
+ "> To find out which animals live in which habitats enter 2, "+ "\n"
+ "> To find out which animals are which type enter 3, " + "\n"
+ "> To find out which animals eat which food enter 4, " + "\n"
+ "> To find individual details about an animal enter 5");
//Set question to equal input
question = input.nextInt();
//If question is equal to
switch(question) {
//1 then,
case 1:
//Set option1 to refer to new Inventory object
Inventory option1 = new Inventory();
//Call inventory method in option1 using ArrayList (of Animal objects) zoo_collection
option1.inventory(zoo_collection);
//Set input1 to be user's input
Scanner input1 = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("If you are finished, enter 1, if not enter 0.");
//Set confirm to equal input
confirm = input1.nextInt();
//If confirm is the same as 1 then,
if(confirm == 1) {
//Output that the program will finish
System.out.println("Understood, finishing program.");
//Set finish to true
finish = true;
}
//Else then,
else{
//Output that the program will continue
System.out.println("Understood, continuing program.");
}
//Prevent the program running the other cases
break;
//2 then,
case 2:
//Set option2 to refer to new ListHabitats object
ListHabitats option2 = new ListHabitats();
//Call listshabitats method in option2 using ArrayLists (of Animal objects) zoo_collection and empty_array
option2.listhabitats(zoo_collection, empty_array);
//Set input1 to be user's input
Scanner input2 = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("If you are finished, enter 1, if not enter 0.");
//Set confirm to equal input
confirm = input2.nextInt();
//If confirm is the same as 1 then,
if(confirm == 1) {
//Output that the program will finish
System.out.println("Understood, finishing program.");
//Set finish to true
finish = true;
}
//Else then,
else{
//Output that the program will continue
System.out.println("Understood, continuing program.");
}
//Prevent the program running the other cases
break;
//3 then,
case 3:
//Set option3 to refer to new ListTypes object
ListTypes option3 = new ListTypes();
//Call listtypes method in option3 using ArrayLists (of Animal objects) zoo_collection and empty_array
option3.listtypes(zoo_collection, empty_array);
//Set input1 to be user's input
Scanner input3 = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("If you are finished, enter 1, if not enter 0.");
//Set confirm to equal input
confirm = input3.nextInt();
//If confirm is the same as 1 then,
if(confirm == 1) {
//Output that the program will finish
System.out.println("Understood, finishing program.");
//Set finish to true
finish = true;
}
//Else then,
else{
//Output that the program will continue
System.out.println("Understood, continuing program.");
}
//Prevent the program running the other cases
break;
//4 then,
case 4:
//Set option4 to refer to new ListFood object
ListFood option4 = new ListFood();
//Call listfood method in option4 using ArrayLists (of Animal objects) zoo_collection and empty_array
option4.listfood(zoo_collection, empty_array);
//Set input1 to be user's input
Scanner input4 = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("If you are finished, enter 1, if not enter 0.");
//Set confirm to equal input
confirm = input4.nextInt();
//If confirm is the same as 1 then,
if(confirm == 1) {
//Output that the program will finish
System.out.println("Understood, finishing program.");
//Set finish to true
finish = true;
}
//Else then,
else{
//Output that the program will continue
System.out.println("Understood, continuing program.");
}
//Prevent the program running the other cases
break;
//5 then,
case 5:
//Set option5 to refer to new ListDetails object
ListDetails option5 = new ListDetails();
//Call listdetails method in option5 using ArrayList (of Animal objects) zoo_collection
option5.listdetails(zoo_collection);
//Set input1 to be user's input
Scanner input5 = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("If you are finished, enter 1, if not enter 0.");
//Set confirm to equal input
confirm = input5.nextInt();
//If confirm is the same as 1 then,
if(confirm == 1) {
//Output that the program will finish
System.out.println("Understood, finishing program.");
//Set finish to true
finish = true;
}
//Else then,
else{
//Output that the program will continue
System.out.println("Understood, continuing program.");
}
//Prevent the program running the other cases
break;
//If question is not a listed case
default:
//Output that the question is not available
System.out.print("Question not available");
}
}
}
}
package summative_assessment;
//Import java.util.ArrayList library, to be able to use ArrayLists
import java.util.ArrayList;
//Import java.util.Scanner library, to be able to use user inputs
import java.util.Scanner;
//Create a public class called ListHabitats
public class ListHabitats {
//Create a public void method using an ArrayLists (of Animal objects) called listhabitats
public void listhabitats(ArrayList<Animal> a, ArrayList<Animal> b) {
//Set habitatChoice as an integer
int habitatChoice;
//Set input to be user's input
Scanner input = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("> To list all who live in forests enter 1," + "\n" +
"> To list all who live in the Outback enter 2," + "\n" +
"> To list all who live in jungles enter 3," + "\n" +
"> To list all who live in the Savannah enter 4," + "\n" +
"> To list all who live in oceans enter 5," + "\n");
//Set habitatChoice to equal input
habitatChoice = input.nextInt();
//If habitatChoice is equal to
switch(habitatChoice) {
//1 then,
case 1:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getHabitat method called from the item in position i in ArrayList a is the same as 'Forest', then
if (a.get(i).getHabitat().equals("Forest")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it lives in a forest
System.out.println(b.get(j).getName() + " lives in a forest." + "\n");
}
//Prevent the program running the other cases
break;
//2 then,
case 2:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getHabitat method called from the item in position i in ArrayList a is the same as 'Outback',
if (a.get(i).getHabitat().equals("Outback")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it lives in the Outback
System.out.println(b.get(j).getName() + " lives in the Outback." + "\n");
}
//Prevent the program running the other cases
break;
//3 then,
case 3:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getHabitat method called from the item in position i in ArrayList a is the same as 'Jungle',
if (a.get(i).getHabitat().equals("Jungle")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it lives in a jungle
System.out.println(b.get(j).getName() + " lives in a jungle." + "\n");
}
//Prevent the program running the other cases
break;
//4 then,
case 4:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getHabitat method called from the item in position i in ArrayList a is the same as 'Savannah',
if (a.get(i).getHabitat().equals("Savannah")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it lives in the Savannah
System.out.println(b.get(j).getName() + " lives in the Savannah." + "\n");
}
//Prevent the program running the other cases
break;
//5 then,
case 5:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getHabitat method called from the item in position i in ArrayList a is the same as 'Ocean',
if (a.get(i).getHabitat().equals("Ocean")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it lives in an ocean
System.out.println(b.get(j).getName() + " lives in an ocean." + "\n");
}
//Prevent the program running the other cases
break;
//If habitatChoice is not a listed case
default:
//Output that the choice of habitat is not available
System.out.print("choice of habitat not available");
}
//Set list_clear to refer to new ListClear object
ListClear list_clear = new ListClear();
//Call the listclear method, using ArrayList (of Animal objects) a, from list_clear
list_clear.listclear(b);
}
}
package summative_assessment;
//Import java.util.ArrayList library, to be able to use ArrayLists
import java.util.ArrayList;
//Import java.util.Scanner library, to be able to use user inputs
import java.util.Scanner;
//Create a public class called ListTypes
public class ListTypes {
//Create a public void method using an ArrayLists (of Animal objects) called listtypes
public void listtypes(ArrayList<Animal> a, ArrayList<Animal> b) {
//Set typeChoice as an integer
int typeChoice;
//Set input to be user's input
Scanner input = new Scanner(System.in);
//Ask user to select the question to run
System.out.println("> To list all who are mammals enter 1," + "\n" +
"> To list all who are reptiles enter 2," + "\n" +
"> To list all who are fish enter 3," + "\n" +
"> To list all who are amphibians enter 4." + "\n");
//Set typeChoice to equal input
typeChoice = input.nextInt();
//If typeChoice is equal to
switch(typeChoice) {
//1 then,
case 1:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getType method called from the item in position i in ArrayList a is the same as 'Mammal', then
if (a.get(i).getType().equals("Mammal")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it is a mammal
System.out.println(b.get(j).getName() + " is a mammal." + "\n");
}
//Prevent the program running the other cases
break;
//2 then,
case 2:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getType method called from the item in position i in ArrayList a is the same as 'Reptile',
if (a.get(i).getType().equals("Reptile")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it is a reptile
System.out.println(b.get(j).getName() + " is a reptile." + "\n");
}
//Prevent the program running the other cases
break;
//3 then,
case 3:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getType method called from the item in position i in ArrayList a is the same as 'Fish',
if (a.get(i).getType().equals("Fish")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it is a fish
System.out.println(b.get(j).getName() + " is a fish." + "\n");
}
//Prevent the program running the other cases
break;
//4 then,
case 4:
//Loop for i, which equals 0, is less than the size of ArrayList a and increase i by 1 each loop
for (int i = 0; i < a.size(); i++) {
//If the returned value of the getType method called from the item in position i in ArrayList a is the same as 'Amphibian',
if (a.get(i).getType().equals("Amphibian")) {
//Add the item in position i in ArrayList a to ArrayList b
b.add(a.get(i));
}
}
//Loop for j, which equals 0, is less than the size of ArrayList b and increase j by 1 each loop
for (int j = 0; j < b.size(); j++) {
//Output the returned value of the getName method called from the item in position i in ArrayList b and that it is an amphibian
System.out.println(b.get(j).getName() + " is an amphibian." + "\n");
}
//Prevent the program running the other cases
break;
//If typeChoice is not a listed case
default:
//Output that the choice of animal type is not available
System.out.print("choice of animal type not available");
}
//Set list_clear to refer to new ListClear object
ListClear list_clear = new ListClear();
//Call the listclear method, using ArrayList (of Animal objects) a, from list_clear
list_clear.listclear(b);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment