Question

In: Computer Science

Write a program that meets the following requirements: Cat Class Create a class called Cat which...

Write a program that meets the following requirements:

Cat Class

  1. Create a class called Cat which has only the following instance variables:

- name

- breed

- number of legs

- year born

  1. Create the no-argument constructor
  2. Create the constructor which uses all fields as parameters
  3. Write the getter and setter methods for all instance variables
  4. Override the toString method using the example shown above

There should be NO main method in the Cat class.

CatTester Class

  1. Create a class called CatTester which constructs 10 different Cat objects
  2. Add each Cat object to an array
  3. Use an enhanced for loop to SOP each Cat reference
  4. Use a second for loop to SOP only those Cats that are born after 2004 and have legs greater than 3
  • You should use the class names of Cat and CatTester

Solutions

Expert Solution

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]);

}

}

}

}


Related Solutions

Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called glossary. Have the glossary include a list of five (5) programing words you have learned in chapters 4-6. Use these words as keys to your dictionary. Find the definition of these words and use them as the values to the keys. Using a loop, print each word and its meaning as neatly formatted output. Create three dictionaries called person. Have each of the person...
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and fill it with the names of various sandwiches. Make an empty list called finished_sandwiches. Loop through the list of sandwich orders and spring a message for each order such as "I am working on your tuna sandwich" As each sandwich is made, move it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing each sandwich that...
1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to Zion's Pizza Restaurant and ask the user how many people are in their dinner group. If the answer is more than eight (8), print a message saying they'll have to wait for a table. Otherwise, report that their table is ready and take their order. Assume the client orders one pizza, and ask what he/she would like on their pizza, include a loop that...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after a Cat. You should have instance variables as follows: The Cat’s name The number of mice caught by the Cat. Whether or not the Cat is secretly plotting to kill you Note that you will need to choose both good types and meaningful identifiers for each of these instance variables. You may also assume that the Cat is not automatically always secretly plotting to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT