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

Lab #3: Inheritance The current class you have developed is an Inventory type. M

ID: 3720753 • Letter: L

Question

Lab #3: Inheritance The current class you have developed is an Inventory type. Most of you call it Inventory_Item (remember singular noun) A new class is created called Pets. It is less generic than the first one you developed. It includes general characteristics for ALL inventory items. We have added a new class called Food Use the concept of inheritance between Inventory_Item and Food, and between Inventory_ltem and Pets Each inheritance overloads the constructor of the Inventory_Item, depending on the parameters sent The main menu now asks before entering an inventory item if you want to enter a Food or Pets The print option also asks which one to print either Pets or Food.

Explanation / Answer

//1.

package inventory;

public class Inventory_Item {

private String food_item;

private String pet_name;

public Inventory_Item() {

super();

}

?

?

public String getFood_item() {

return food_item;

}

?

?

public void setFood_item(String food_item) {

this.food_item = food_item;

}

?

?

public String getPet_name() {

return pet_name;

}

?

?

public void setPet_name(String pet_name) {

this.pet_name = pet_name;

}

?

?

public Inventory_Item(String food_item, String pet_name) {

super();

this.food_item = food_item;

this.pet_name = pet_name;

}

public void print() {

System.out.println("pet: "+pet_name);

System.out.println("Food: "+food_item);

}

}

//2.

package inventory;

public class Pets extends Inventory_Item{

private String pet_name;

public Pets() {

super();

}

public Pets(String pet_name) {

super();

this.pet_name = pet_name;

}

public String getPet_name() {

return pet_name;

}

public void setPet_name(String pet_name) {

this.pet_name = pet_name;

}

@Override

public String toString() {

return "Pets [pet_name=" + pet_name + "]";

}

public void print() {

System.out.println(pet_name);

}

}

//3.

package inventory;

public class Food extends Inventory_Item {

private String food_item;

public Food() {

super();

}

public Food(String food_item) {

super();

this.food_item = food_item;

}

public String getFood_item() {

return food_item;

}

public void setFood_item(String food_item) {

this.food_item = food_item;

}

@Override

public String toString() {

return "Food [food_item=" + food_item + "]";

}

public void print() {

System.out.println(food_item);

}

}

//4.

package inventory;

import java.util.Scanner;

public class Main_Class {

public static void main(String[] a) {

Scanner sc=new Scanner(System.in);

System.out.println("Please type which you want to add: pets/food");

Inventory_Item p=null;

Inventory_Item p2=null;

String choice=sc.next();

if(choice.equals("pets")) {

System.out.println("Enter pet name:");

String name=sc.next();

p=new Pets(name);

}

else if(choice.equals("food"))

{

System.out.println("Enter food name:");

String food=sc.next();

p=new Food(food);

}

else {

System.out.println("Enter wrong choice:");

System.exit(0);

}

System.out.println("Please which you want to print: pets/food");

String choice2=sc.next();

if(choice2.equals("pets")) {

p.print();

}

else if(choice2.equals("food"))

{

p2.print();

}

else {

System.out.println("Wrong choice:");

}

}

}

/* output:-

Please type which you want to add: pets/food
pets
Enter pet name:
parrot
Please which you want to print: pets/food
pets
parrot
*/