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

Hi, I need some assistance with a java problem. I have created a superclass call

ID: 3599753 • Letter: H

Question

Hi, I need some assistance with a java problem. I have created a superclass called Item with three subclasses (Equipable, Consumable, and Weapon). These are being added to an Array list called cargohold. I've also created a method called addItem. Under this method, I have code to add new items to the ArrayList. I use the java code syntax cargohold.add(new Item(input.nextLine())); to add new elements to Item. This code works fine, but I'm not sure how the syntax should be to add elements to the subclasses. For example, I tried  cargohold.add(new Item.Equipable (input.nextLine())); and  cargohold.add(new Equipable (input.nextLine())); Neither of these work. What syntax should I use?

I've added my Item.java code and my addItem method code below

////Item.java code below

import java.util.*;

public class Item {

private String Name;

private Double Weight;

private String Value;

private String Durability;

private String ID;

public Item(){

}

public String getName(){

return Name;

}

public void setName (String Name) {

this.Name = Name;

}

public Double getWeight(){

return Weight;

}

public void setWeight(Double Weight) {

this.Weight = Weight;

}

public String getValue(){

return Value;

}

public void setValue(String Value) {

this.Value = Value;

}

public String getDurability(){

return Durability;

}

public void setDurability (String Durability) {

this.Durability = Durability;

}

public String getID(){

return ID;

}

public void setID (String ID) {

this.ID= ID;

}

}

class Equipable extends Item

{

String Outfits;

String Materials;

int Size;

public Equipable(){

}

public String getOutfits(){

return Outfits;

}

public void setOutfits(String Outfits){

this.Outfits=Outfits;

}

public String getMaterials(){

return Materials;

}

public void setMaterials(String Materials){

this.Materials=Materials;

}

public int getSize(){

return Size;

}

public void setSize(int Size){

this.Size=Size;

}

}

class Consumable extends Item

{

String Beverage;

String Vegetable;

String Fruit;

public String getBeverage(){

return Beverage;

}

public void setBeverage(String Beverage){

this.Beverage=Beverage;

}

public String getVegetable(){

return Vegetable;

}

public void setVegetable(String Vegetable){

this.Vegetable=Vegetable;

}

public String getFruit(){

return Fruit;

}

public void setFruit(String Fruit){

this.Fruit=Fruit;

}

}

class Weapon extends Item

{

String Arrow;

String Ninjastar;

String Rock;

public String getArrow(){

return Arrow;

}

public void setArrow(String Arrow){

this.Arrow=Arrow;

}

public String getNinjastar(){

return Ninjastar;

}

public void setNinjastar(String Ninjastar){

this.Ninjastar=Ninjastar;

}

public String getRock(){

return Rock;

}

public void setRock(String Rock){

this.Rock=Rock;

}

}

  

  

////addItem method below

private void addItem(ArrayList<Item> cargohold) {
System.out.println("Enter name of item to add: ");
  
cargohold.add(new Item(input.nextLine()));
double getWeight =0;
System.out.println("What is the item's weight? ");
getWeight = input.nextDouble();
if(getWeight > 25)
{
System.out.println("Weight Limit Exceed");
return;
}
else
{
System.out.println("What is the item's value? ");
cargohold.add(new Item(input.nextLine()));
System.out.println("What is the item's durability? ");
cargohold.add(new Item(input.nextLine()));
System.out.println("What is the item's ID? ");
cargohold.add(new Item(input.nextLine()));
System.out.println("Item added successfully ");
System.out.println("What is the item's category (equipable, consumable, or weapon)? ");
if (input.nextLine()=="equipable")
{
System.out.println("What is the outfit?");
cargohold.add(new Item(input.nextLine()));
System.out.println("What is the material?");
cargohold.add(new Item.Equipable (input.nextLine()));
System.out.println("What is the item's size? ");
cargohold.add(new Equipable(input.nextInt()));
}else if((input.nextLine())=consumable)
{
System.out.println("What is the consumable (beverage, food, or vegetable?");
if ((input.nextLine())=beverage)
{
System.out.println ("What type of beverage is it?");
cargohold.add(new Consumable (input.nextLine()));
}else if((input.nextLine())=food){
System.out.println("What is the type of food?");
cargohold.add(new Consumable (input.nextLine()));
}else if((input.nextLine())=vegetable){
System.out.println("What is the type of vegetable? ");
cargohold.add(new Consumable(input.nextLine()));
}else if((input.nextLine())=weapon){
System.out.println("What is the type of weapon (arrow, ninjastar, rock? ");
cargohold.add(new Weapon (input.nextLine()));
}if((input.nextLine())=arrow){
System.out.println("What is the type of arrow?");
cargohold.add(new Weapon (input.nextLine()));
}else if((input.nextLine())=ninjastar){
System.out.println("What is the type of ninjastar? ");
cargohold.add(new Weapon(input.nextLine()));
}else if((input.nextLine())=rock){
System.out.println("What is the type of rock? ");
cargohold.add(new Weapon(input.nextLine()));
System.out.println("Item added successfully ");
}
}
}
}

Explanation / Answer

Correct Add Item Method

private void addItem(ArrayList<Item> cargohold) {
System.out.println("Enter name of item to add: ");
Equipable equipable=new Equipable();
Consumable consumable=new Consumable();
Weapon weapon=new Weapon();
Item item=new Item();
  
equipable.setID("ID");
equipable.setDurability("Durability");
equipable.setMaterials("Materials");
equipable.setName("Name");
equipable.setOutfits("Outfits");
equipable.setSize(22);
equipable.setValue("value");
equipable.setWeight(23.34);
  
consumable.setBeverage("Beverage");
consumable.setDurability("Durability");
consumable.setFruit("Fruit");
consumable.setID("ID");
consumable.setName("Name");
consumable.setValue("Value");
consumable.setVegetable("Vegetable");
consumable.setWeight(2.2);
  
weapon.setArrow("Arrow");
weapon.setDurability("Durability");
weapon.setID("ID");
weapon.setName("Name");
weapon.setNinjastar("Ninjastar");
weapon.setRock("Rock");
weapon.setValue("Value");
weapon.setWeight(2.3);
  
item.setDurability("Durability");
item.setID("ID");
item.setName("Name");
item.setValue("Value");
item.setWeight(2.1);
  
cargohold.add(item);
double getWeight =0;
System.out.println("What is the item's weight? ");
getWeight = input.nextDouble();
if(getWeight > 25)
{
System.out.println("Weight Limit Exceed");
return;
}
else
{
System.out.println("What is the item's value? ");
cargohold.add(item);
System.out.println("What is the item's durability? ");
cargohold.add(item);
System.out.println("What is the item's ID? ");
cargohold.add(item);
System.out.println("Item added successfully ");
System.out.println("What is the item's category (equipable, consumable, or weapon)? ");
if (input.nextLine()=="equipable")
{
System.out.println("What is the outfit?");
cargohold.add(item);
System.out.println("What is the material?");
cargohold.add(item.setEquipable(equipable));
System.out.println("What is the item's size? ");
cargohold.add(item.setEquipable(equipable));
}else if((input.nextLine())=consumable)
{
System.out.println("What is the consumable (beverage, food, or vegetable?");
if ((input.nextLine())=beverage)
{
System.out.println ("What type of beverage is it?");
cargohold.add(item.setConsumable(consumable));
}else if((input.nextLine())=food){
System.out.println("What is the type of food?");
cargohold.add(item.setConsumable(consumable));
}else if((input.nextLine())=vegetable){
System.out.println("What is the type of vegetable? ");
cargohold.add(item.setConsumable(consumable));
}else if((input.nextLine())=weapon){
System.out.println("What is the type of weapon (arrow, ninjastar, rock? ");
cargohold.add(item.setWeapon(weapon));
}if((input.nextLine())=arrow){
System.out.println("What is the type of arrow?");
cargohold.add(item.setWeapon(weapon));
}else if((input.nextLine())=ninjastar){
System.out.println("What is the type of ninjastar? ");
cargohold.add(item.setWeapon(weapon));
}else if((input.nextLine())=rock){
System.out.println("What is the type of rock? ");
cargohold.add(item.setWeapon(weapon));
System.out.println("Item added successfully ");
}
}
}

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