Problem 1-Create a working main. Submit the whole src folder-call it src1. This
ID: 3812760 • Letter: P
Question
Problem 1-Create a working main. Submit the whole src folder-call it src1. This should include a file named CatStuff.java (with the main) and a folder named PetInfo (this is the package) that contains the Cat class.
Using the following Cat class, you will create a package called PetInfo and put the following Cat class inside. You will then create a working main in a class called CatStuff (sample run follows) that uses the Cat class.
Your main should instantiate a cat object, allow the user to enter information about the cat and finally allow the user to enter 3 favorite foods.
Cat class: (DO NOT MODIFY)
package PetInfo;
import java.util.ArrayList; import java.util.Scanner;
public class Cat {
private String name; private String breed; private String [] colors;
private ArrayList<String> favFoods=new ArrayList ();
public void giveAllInfo()
{
Scanner in=new Scanner(System.in);
System.out.println("Hello! You will be entering cat info.");
System.out.println("Enter cat name:"); name=in.nextLine(); System.out.println("Enter cat breed:"); breed=in.nextLine();
giveColors();
}
private void giveColors()
{
Scanner in=new Scanner(System.in); System.out.println("How many colors?"); int n=in.nextInt();
in.nextLine(); colors=new String[n];
for (int i=0;i<colors.length;i++)
{
System.out.println("Give color "+(i+1)+":"); colors[i]=in.nextLine();
}
}
public void printOutCatInfo()
{
System.out.println(" Name: "+name); System.out.println("Breed: "+breed);
for(int i=0;i<colors.length;i++)
{
System.out.println(" Color:"+colors[i]);
}
printOutFavFoods();
}
public void addFavFood()
{
System.out.println("Add a new favorite food:"); Scanner in=new Scanner(System.in);
String food=in.nextLine(); favFoods.add(food);
}
private void printOutFavFoods()
{
System.out.println("Favorite foods are:"); for (int i=0;i<favFoods.size();i++)
{
String fd=favFoods.get(i); System.out.println(fd);
}
}
}
}
Sample run:
Hello! You will be entering cat info. Enter cat name:
Tom
Enter cat breed: Persian cat
How many colors? 2
Give color 1: white
Give color 2: brown
Add a new favorite food: fish
Add a new favorite food: treats
Add a new favorite food: mice
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
public class CatStuff{
public static void main(String[] args) {
// creating Cat Object
Cat cat = new Cat();
// calling function to get all info
cat.giveAllInfo();
// adding first food
cat.addFavFood();
// adding second food
cat.addFavFood();
// adding third food
cat.addFavFood();
cat.printOutCatInfo();
}
}
/*
Sample run:
Hello! You will be entering cat info.
Enter cat name:
Tom
Enter cat breed:
Persian cat
How many colors?
2
Give color 1:
white
Give color 2:
brown
Add a new favorite food:
fish
Add a new favorite food:
treat
Add a new favorite food:
mice
Name: Tom
Breed: Persian cat
Color:white
Color:brown
Favorite foods are:
fish
treat
mice
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.