Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Download and import the attached file Homework1Q1.zip to your Eclipse workspace

ID: 3728348 • Letter: D

Question

Download and import the attached file Homework1Q1.zip to your Eclipse workspace (Click File > Import > General > Existing Projects into workspace > Select archive file). You will see one runner class in the default package that has errors. Fix the errors by creating the corresponding missing classes without modifying Runner.java. Once done, the output of your program should look exactly like the following:

ID Name   Age    CPD    Type

-----------------------------------------------------------------------------------------

1   Calvin   5    960 Elephant

2    Fluffy    4    320 Tiger

Setting number of meals per day of Fluffy to 2

2    Fluffy    4    640 Tiger

Setting number of meals per day to 30! Error, animals cannot eat more than 3 meals per day!

2    Fluffy    4    640   Tiger

Setting Fluffy's age to 8

2    Fluffy    4    640   Tiger

Printing All Animals:

ID   Name   Age   CPD   Type

---------------------------------------------------------------------------------------

1   Calvin    5 960    Elephant

2    Fluffy    8    640   Tiger

Total number of animals is: 2

import qa.edu.qu.cmps251.homework1.animals.Animal;

/**

*

* DO NOT MODIFY THIS FILE

* This is a Runner program for Homework 1.

*

* Once done, all the compilation errors should be fixed

* and your program output must match that of the handout.

*

* @author CMPS 251 Instructors

*

*/

public class Runner {

public static void main(String[] args) {

//print top header

Animal.printHeader();

//Create an animal and print its info

//Parameters here are: Name, age, number of meals per day, and type (1 = tiger, 2 = elephant, 3 = turtle, 4 = puffin)

//Pay attention that the ID is auto generated

Animal a1 = new Animal("Calvin", 5, 3, 2);

//Print info will print the ID, name, Age, Calories Per Day (CPD), and Type

//Calories Per Day is the number of meals the animal eats * 320

a1.printAnimalInfo();

System.out.println();

//Create second animal and print its info

/*

* Default values are:

* Name = "Fluffy";

* Age = 4;

* Type = 1;

* Number of Meals = 1;

* Type = 1 (tiger);

*/

Animal a2 = new Animal();

a2.printAnimalInfo();

System.out.println();

//Set number of meals per day for Fluffy to 2

System.out.println("Setting number of meals per day of Fluffy to 2");

a2.setNumOfMeals(2);

a2.printAnimalInfo();

System.out.println();

//Set number of meals per day for Fluffy to 30!

System.out.println("Setting number of meals per day to 30!");

a2.setNumOfMeals(30);

a2.printAnimalInfo();

System.out.println();

//Set the age of Fluffy to 8

System.out.println("Setting Fluffy's age to 8");

a2.setAge(8);

a2.printAnimalInfo();

System.out.println();

//print all animals in system

System.out.println(" Printing All Animals: ");

Animal.printHeader();

a1.printAnimalInfo();

a2.printAnimalInfo();

System.out.println(" Total number of animals is: " + Animal.totalNumOfAnimals);

}

}

Explanation / Answer

package com;

import java.util.Scanner;

public class Runner {

public static int num;

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

  

Animal a1=new Animal();

  

System.out.println("enter animals name, age , mealsperday,type");

Animal.printHeader();

  

a1.setName(sc.nextLine());

a1.setAge(sc.nextInt());

a1.setNoofMeals(sc.nextInt());

sc.nextLine();

a1.setType(sc.nextLine());

a1.printAnimalInfo();

Animal a2=new Animal();

a2.printAnimalInfo();

System.out.println("Setting number of meals per day of Fluffy to 2");

a2.setNoofMeals(2);

a2.printAnimalInfo();

System.out.println("Setting number of meals per day to 30!");

a2.setNoofMeals(30);

a2.printAnimalInfo();

//Set the age of Fluffy to 8

System.out.println("Setting Fluffy's age to 8");

a2.setAge(8);

a2.printAnimalInfo();

//print all animals in system

System.out.println(" Printing All Animals: ");

Animal.printHeader();

a1.printAnimalInfo();

a2.printAnimalInfo();

System.out.println(" Total number of animals is: " + Animal.totalNumOfAnimals());

}

}

--------------------------------------------------------------------------------------------------------------------------ANimal class

package com;

public class Animal {

static int num;

int id;

String Name;

int age;

int NoofMeals;

String type;

public Animal()

{

this.Name="Flufy";

this.age=4;

this.NoofMeals=1;

this.type="Tiger";

this.id=++num;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return Name;

}

public void setName(String name) {

Name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public int getNoofMeals() {

return NoofMeals;

}

public void setNoofMeals(int noofMeals) {

if(noofMeals<=3&&noofMeals>0)

NoofMeals = noofMeals;

else

System.out.println("Error, animals cannot eat more than 3 meals per day!");

}

public String getType() {

return type;

}

public void setType(String i) {

if(i.equalsIgnoreCase("1"))

this.type ="TIGER";

else if(i.equalsIgnoreCase("2"))

this.type ="ElEPHANT";

else if(i.equalsIgnoreCase("3"))

this.type ="TURTLE";

else if(i.equalsIgnoreCase("4"))

this.type ="PUFFIN";

}

public void printAnimalInfo()

{

  

System.out.println("--------------------------------------------");

System.out.print(this.getId()+" ");

System.out.print(this.getName()+" ");

System.out.print(this.getAge()+" ");

System.out.print(this.getNoofMeals()*320+" ");

System.out.print(this.getType()+" ");

System.out.println();

}

public static void printHeader()

{

System.out.println("ID Name Age CPD Type");

}

public static int totalNumOfAnimals()

{

return num;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote