Hi, below is the Question and MYCODE. I've attempted it but there still some problems that I'd appreciate if someone help me thrush out.
QUESTION
1. Create an interface called limb and modify your arm and leg classes so that they implement it.
2. Create a new abstract class called Mammal and move any properties and methods that all mammals have from your Human to the Mammal class.
3. Ensure that your mammal has a private property which is an collection of limbs and protected methods to add, remove and retrieve the limbs (alternative solution would be to have protected collection of limbs and protected constructor in which you can pass collection of limbs to add).
4.Modify your Human to extend Mammal.
5.Add methods to your Human that return collections of arms and legs. implement these methods by iterating through the collection of limbs and checking what type of limb they are. Note that you will need to use the instanceOf keyword but we haven't covered it so you will need to research it.
6. Create three new classes that represent pets. They should all be mammals but have some unique characteristics.
7.Create a Family class that creates two humans (yourself and your ideal partner) and three pets.
8. Extend the Human class to create two new classes Adult and Child.
9. (Done--) Make sure your human has an age property.
10.Modify your family so it includes a child.
11.Add an abstract canWatchMovie method to the Human which takes a parameter specifying the movie rating.
12. Create a MovieRating class that contains a private map with rating as the key and minimum age as the value, populate this map when the class is instantiated. Also include a method which takes two parameters, rating and age and checks the minimum age in the map and returns true or false to indicate whether or not a human with the specified age is allowed to watch a movie with the specified rating.
13. Implement the canWatchMovie method in the child and adult, in the adult always return true but in the child use the MovieRating class to determine whether the child can watch the movie.
14. Add a getMovieGoers method to the family that takes a rating as a parameter and returns a collection of all the family members that can watch the movie.
15. Demonstrate the use on the getMovieGoers method by calling it twice from within the main method of the family and prints out the names of the family members that can watch the movie.
MY CODE
[B] // Limb interface[/B]
public interface Limb
{
public void move(String s);
public void getArms();
public void getLegs();
boolean add();
boolean remove();
}
[B]//Leg class[/B]
public class Leg implements Limb
{
String length,legs,Leftleg,Rightleg;
String thickness;
String direction;
public Leg(String Left_Right)
{
this.Leftleg=Left_Right;
this.Rightleg=Left_Right;
}
public void move(String direction)
{
this.direction = direction;
}
public void getArms()
{
}
public void getLegs()
{
}
public boolean add()
{
return true;
}
public boolean remove()
{
return false;
}
}
[B]//Arms class[/B]
import java.util.*;
public class Arms implements Limb
{
String length,arms, Lefthand,Righthand;
String thickness,direction;
Limb limbs;
public Arms(String Left_Right)
{
this.Lefthand=Left_Right;
this.Righthand=Left_Right;
}
public void move(String direction)
{
this.direction = direction;
}
public void getArms()
{
}
public void getLegs()
{
}
public boolean add()
{
return true;
}
public boolean remove()
{
return false;
}
public static void main(String args[])
{
ArrayList Arms = new ArrayList();
Arms.add(new Arms("Left_Hand"));
Arms.add(new Arms("Right_Hand"));
System.out.println(Arms);
}
}
[B] //Mammal class[/B]
import java.util.*;
public abstract class Mammal
{
Head head;
char gender;
public String name;
protected int age = 0;
private List <Limb> limbs;
String myArm,myLeg;
protected Mammal()
{
this.limbs = new ArrayList<Limb>();
}
protected Mammal(List<Limb> limbs)
{
this.limbs = limbs;
}
// adding limbs:
protected void addLimbs(List<Limb>limbToAdd)
{
this.limbs.add((Limb) limbToAdd);
}
//remove limbs
protected void removeLimbs(List<Limb> limbToRemove)
{
Object temp = null;
Iterator iter = limbs.iterator();
while (iter.hasNext())
{
temp = iter.next();
if (temp instanceof Arms)
{
// myArm = (Arms)temp;
// iter.remove(myArm);
iter.remove();
if (temp instanceof Leg)
{
// myLeg = (Leg)temp;
// iter.remove(myLeg);
iter.remove();
}
}
}
}
protected List<Limb> getLimbs()
{
return this.limbs;
}
public Mammal(String name)
{
this.name = name;
}
public void getOlder(int years)
{
age += years;
}
public void makeSound()
{
System.out.print(name+" Oooops ");
}
public void eat()
{
System.out.print("Some food please...");
}
}
[B] //Human class[/B]
import java.util.*;
public abstract class Human extends Mammal
{
int MyAge,Age;
String mrating;
Arms[] arms = new Arms[2];
Leg[] legs = new Leg[2];
void walk()
{
legs[0].move("Right Leg");
legs[1].move("Left Leg");
}
void swim()
{
arms[0].move("Right hand");
arms[1].move("Left hand");
}
// default constructor
public Human(String name)
{
super(name);
gender = 'M'; // the default is M for Male
}
@Override
public void makeSound()
{
System.out.print(name+" Oooops ");
}
@Override
public void getOlder(int years)
{
age += years;
}
@Override
public void eat()
{
System.out.print("Some food please...");
}
// the getters and setters...
public int getAge()
{
return this.MyAge;
}
public void setAge(int Age)
{
this.MyAge=Age;
}
//methods for iterating through the limbs
public void getArms()
{
Limb limbs = new Arms("");
ArrayList myArms = new ArrayList();
// Populate the list using the .add() methods
myArms.add("Right Hand");
myArms.add("Left Hand");
Collection myforeLimbs =myArms;
if(limbs instanceof Arms)
{
System.out.println("\t"+myforeLimbs);
}
}
public void getLegs()
{
Limb limbs = new Leg("");
ArrayList myLegs = new ArrayList();
// Populate the list using the .add() methods
myLegs.add("Right Leg");
myLegs.add("Left Leg");
Collection myhindLimbs =myLegs;
if(limbs instanceof Leg)
{
System.out.println("\t"+myhindLimbs);
}
}
public abstract boolean canWatchMovie(String MovieRating);
public static void main(String args[])
{
}
}
[B] //Cats class[/B]
class Cats extends Mammal
{
protected String cat_color,miewSound,catClaw, MyCatType;
public Cats(String name)
{
super(name);
this.cat_color="";
}
@Override
public void makeSound()
{
System.out.print(name+" Oooops ");
}
@Override
public void eat()
{
System.out.print("Some food please...");
}
// Unique properties of a cat
protected void setCatColor(String color) // cats colour
{
cat_color = color;
}
protected void miew(String sound) // cats sound
{
miewSound=sound;
}
protected void clawSize(String size) // size of the claw
{
catClaw=size;
}
protected void catType(String catType) // the cat type/category
{
MyCatType=catType;
}
}
[B]//Dog class[/B]
class Dog extends Mammal
{
protected String dog_color,dogBark, MydogType;
public Dog(String name )
{
super(name);
this.dog_color="";
}
@Override
public void makeSound()
{
System.out.print(name+" Oooops ");
}
@Override
public void getOlder(int years)
{
age += years;
}
@Override
public void eat()
{
System.out.print("Some food please...");
}
protected void setDogColor(String color) // Dog Color e.g brown
{
dog_color = color;
}
protected void barkingSound(String bark) // Barking sound
{
dogBark=bark;
}
protected void dogType(String dogType) // Dog type e.g Bulldog
{
MydogType=dogType;
}
}
[B]//Rabbit class[/B]
class Rabbit extends Mammal
{
protected String rabbit_color,eye_color,earShape,MyRabbitType;
public Rabbit(String name )
{
super(name);
this.rabbit_color="";
}
@Override
public void makeSound()
{
System.out.print(name+" Oooops ");
}
@Override
public void eat()
{
System.out.print("Some food please...");
}
// some unique characteristics of the rabbit
protected void setRabbitColor(String color)
{
rabbit_color = color;
}
protected void earShape(String shape)
{
earShape=shape;
}
protected void seteyeColor(String color)
{
eye_color=color;
}
protected void rabbitType(String rabType)
{
MyRabbitType=rabType;
}
}
[B] //Family class[/B]
import java.io.*;
public class Family
{
private Dog ourDog;
private Cats ourCat;
private Rabbit ourRabbit;
private Adult adults;
// MovieRating
String me,myPatner;
private Child ourChild;
int Age;
public Family(String myName, String patner)
{
/*
* uses 'this' to refer to
* the current object being manipulated
*/
this.me =myName;
this.myPatner=patner;
adults = new Adult("",0);
ourChild=new Child("");
ourDog=new Dog("");
ourCat=new Cats("");
ourRabbit = new Rabbit("");
}
public void getMovieGoers()
{
String temp;
adults.getName("Robert");
adults.getPatnerName("Mrs Robbert");
System.out.println("What's the movie rating: ");
BufferedReader stdin1 = new BufferedReader(new InputStreamReader(System.in));
try
{
temp= stdin1.readLine();
//MovieRating class
MovieRating mvRate = new MovieRating();
String Rated =mvRate.getValue().toString();
String uRate=temp.trim();
if(uRate.equalsIgnoreCase(Rated))
{
System.out.println("Only " +adults.aName+" and " +adults.pName+
" can watch the movie since it's rated" +Rated+ "" );
}
else
{
System.out.println("The following can watch the movie: "
+adults.aName+" ," +adults.pName+ " and "+ourChild.cName+
" Since the movie is not rated" +Rated+ "");
}
}
catch(Exception e)
{
System.out.println("Movie not Rated?");
}
}
public static void main(String args[])throws IOException
{
String tempString;
int cAge;
/*
* new instance of the Family
*/
Family fam= new Family("Robert","Mrembo");
/*
* set string values to variables
*/
fam.ourChild.getName("Joe Kimani");
fam.ourChild.setAge(5);
fam.ourCat.catType("Angora");
fam.ourCat.setCatColor("Brownish white");
fam.ourDog.dogType("African Shepherd Dog");
fam.ourDog.setDogColor("Black");
fam.ourRabbit.setRabbitColor("White");
fam.ourRabbit.rabbitType("Akorino");
System.out.println("My name is Robert and my patner is Mrs Robert." +
"We own the following pets: " +
" " + fam.ourDog.MydogType+ " " + fam.ourDog.dog_color + " in color;" +
" a small " + fam.ourCat.cat_color + " " +fam.ourCat.MyCatType+ " cat;"+
" and a "+ fam.ourRabbit.rabbit_color +" "+ fam.ourRabbit.MyRabbitType +
" rabbit.");
System.out.println("How old is the Child: ");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
tempString= stdin.readLine(); //reads the age input
try
{
MovieRating mRate = new MovieRating();
/*getting minimum age allowed to watch movie rated PG
*from MovieRating class
*/
Integer mAge = mRate.getMinAge();
cAge =Integer.parseInt(tempString);
if(mAge != null)
{
if(cAge<mAge)
{
System.out.println("You cannot watch the movie "+
"since you are only " +tempString+ " Years old.");
}
else
{
System.out.println("Welcome to the movie, ");
fam.getMovieGoers();
}
}
else
{
System.out.println("Max age not found!");
}
}
catch(Exception e)
{
System.out.println("Provide your age.");
}
}
}
[B] //Adult class[/B]
class Adult extends Human
{
String movieRating,aName,pName;
public Adult(String name, int age)
{
super(name);
this.age=age;
}
protected void getName(String adultName)
{
aName = adultName;
}
protected void getPatnerName(String partnerName)
{
pName = partnerName;
}
public boolean isObjectEmpty(Object obj)
{
boolean result = obj==null;
//here you can add additional code to check is obj empty.
return result;
}
@Override
public boolean canWatchMovie(String MovieRating)
{
return true;
}
public static void main(String args[])
{
Adult me = new Adult("",0);
int myAge = 26;
// String rating ="PG";
String myname="Robert";
System.out.println("I'm " + myname + " and i'm "+ myAge + " years old" +
" that means I'm an Adult");
me.canWatchMovie("");
}
}
[B] //Child class[/B]
import java.io.*;
class Child extends Human
{
String cName ="Joe";
int childAge;
String movieRating;
public Child(String name)
{
super(name);
}
public boolean isObjectEmpty(Object obj)
{
boolean result = obj==null;
return result;
}
@Override
public void setAge(int cAge)
{
childAge = cAge;
}
@Override
public int getAge()
{
return this.age;
}
protected void getName(String childName)
{
cName = childName;
}
@Override
public boolean canWatchMovie(String MovieRating)
{
String temp;
System.out.println("How old are you:");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
try
{
temp=stdin.readLine();
int cAge = Integer.parseInt(temp);
//MovieRating class
MovieRating mRate = new MovieRating();
/*getting minimum age allowed to watch movie rated PG
*from MovieRating class
*/
Integer mAge = mRate.getMinAge();
if(mAge != null)
{
if (cAge < mAge)
{
//return false;
System.out.println("Under age, You cannot watch this movie");
}
else if (cAge >= mAge)
{
// return true;
System.out.println("You can watch the movie");
}
}
else
{
System.out.println("Max age not found");
}
}
catch(Exception e)
{
System.out.println("Age Required!");
}
return false;
}
public static void main(String args[])
{
Child kid = new Child("");
kid.canWatchMovie("");
}
}
[B]//MovieRating class[/B]
import java.util.*;
public class MovieRating
{
private Map ageMap = new HashMap();
public MovieRating()
{
ageMap.put("PG", 18); //parental Guidance
ageMap.put("G", 4); //general viewing
ageMap.put("A", 27); //Adult material
}
public boolean getStatus(String rating, int age)
{
int mAge;
mAge = Integer.getInteger((String) ageMap.get("PG"));
if (age < mAge )
{
return false;
}
else if (age >= mAge)
{
return true;
}
return false;
}
public int getMinAge()
{
// Retrieve the minimum age allowed to watch movie rated PG.
return (Integer)ageMap.get("PG");
}
public int getGeneral()
{
// Retrieve the age allowed to watch movie rated G.
return (Integer)ageMap.get("G");
}
public int getAdult()
{
// Retrieve the age allowed to watch movie rated Adult.
return (Integer)ageMap.get("A");
}
public Object getValue()
{
// return ageMap.values();
return ageMap.toString();
}
}
I've all the classes in the same folder. However, I don't get proper implementation, especially from question 11 to 15. Some help and suggestions will be of great help to me.