Item getName0 Returns the String name. getWeight0 Returns the integer weight. ex
ID: 3791256 • Letter: I
Question
Item getName0 Returns the String name. getWeight0 Returns the integer weight. examine0 Prints a description of the object, including name and neight Weapon getDamage0 Returns the integer damage getName0 Returns the String name. getWeight0 Returns the integer weight. examine0 Prints a description of the object, including name and neight Armor getDefense0 Returns the integer defense. getName0 Returns the String name. getWeight0 Returns the integer weight. examine0 Prints a description of the object, including name and neight. Food etNutrition0 Returns the integer nutrition O eturns the integer quantity get Quantity setOuatitvGnt new Ouantity) Stores nemOuantity in quantity getWeight0 Returns the integer weight, taking the quantity into account. getName0 Returns the String name examine0 Prints a description of the object, including name and weight.Explanation / Answer
ItemMain.java
public class ItemMain {
public static void main(String[] args) {
Weapon i = new Weapon("Gun", 20, 2);
i.examine();
System.out.println("Damage: "+i.getDamage());
Armor a = new Armor("AAA", 50, 5);
a.examine();
System.out.println("Defence: "+a.getDefence());
Food f = new Food("Apple", 100, 20, 5);
f.examine();
System.out.println("Nutrition: "+f.getNutrition());
System.out.println("Quantity: "+f.getQuantity());
System.out.println("Weight: "+f.getWeight());
f.setQuantity(10);
System.out.println("Quantity: "+f.getQuantity());
System.out.println("Weight: "+f.getWeight());
}
}
Item.java
public class Item {
private String name;
private int weight;
public Item(String name, int weight){
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public int getWeight() {
return weight;
}
public void examine(){
System.out.println("Name: "+getName()+" Weight: "+getWeight());
}
}
Weapon.java
public class Weapon extends Item{
private int damage;
public Weapon(String name, int weight, int damage){
super(name, weight);
this.damage = damage;
}
public int getDamage() {
return damage;
}
}
Armor.java
public class Armor extends Item{
private int defence;
public Armor(String name, int weight, int defence){
super(name, weight);
this.defence = defence;
}
public int getDefence() {
return defence;
}
}
Food.java
public class Food extends Item{
private int nutrition;
private int quantity;
public Food(String name, int weight, int nutrition, int quantity){
super(name, weight);
this.nutrition = nutrition;
this.quantity = quantity;
}
public int getNutrition() {
return nutrition;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int newQuantity) {
quantity = newQuantity;
}
public int getWeight(){
return super.getWeight() * getQuantity();
}
}
Output:
Name: Gun Weight: 20
Damage: 2
Name: AAA Weight: 50
Defence: 5
Name: Apple Weight: 500
Nutrition: 20
Quantity: 5
Weight: 500
Quantity: 10
Weight: 1000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.