In: Computer Science
Write a program that meets the following requirements:
Cat Class
- name
- breed
- number of legs
- year born
There should be NO main method in the Cat class.
CatTester Class
If you have any doubts, please give me comment...
public class Cat{
private String name;
private String breed;
private int noOfLegs;
private int year;
public Cat(){}
public Cat(String n, String b, int nLegs, int y){
name = n;
breed = b;
noOfLegs = nLegs;
year = y;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the breed
*/
public String getBreed() {
return breed;
}
/**
* @return the noOfLegs
*/
public int getNoOfLegs() {
return noOfLegs;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param breed the breed to set
*/
public void setBreed(String breed) {
this.breed = breed;
}
/**
* @param noOfLegs the noOfLegs to set
*/
public void setNoOfLegs(int noOfLegs) {
this.noOfLegs = noOfLegs;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
this.year = year;
}
public String toString(){
return "Name: "+name+"\tBreed: "+breed+"\tNo. of legs: "+noOfLegs+"\tYear of born: "+year;
}
}
public class CatTester{
public static void main(String[] args) {
Cat cats[] = new Cat[10];
cats[0] = new Cat("cat1", "breed 1", 4, 2005);
cats[1] = new Cat("cat2", "breed 2", 3, 2014);
cats[2] = new Cat("cat3", "breed 3", 5, 2005);
cats[3] = new Cat("cat4", "breed 4", 4, 2009);
cats[4] = new Cat("cat5", "breed 5", 4, 2004);
cats[5] = new Cat("cat6", "breed 6", 5, 2006);
cats[6] = new Cat("cat7", "breed 7", 3, 2010);
cats[7] = new Cat("cat8", "breed 8", 3, 2012);
cats[8] = new Cat("cat9", "breed 9", 5, 2008);
cats[9] = new Cat("cat10", "breed 10", 4, 1994);
//all cats:
System.out.println("All Cats: ");
for(Cat c: cats){
System.out.println(c);
}
System.out.println("Cats that are born after 2004 and have legs greater than 3");
for(int i=0; i<cats.length; i++){
if(cats[i].getYear()>2004 && cats[i].getNoOfLegs()>3){
System.out.println(cats[i]);
}
}
}
}