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

Java question: create another class of your choice. Your programmer-defined clas

ID: 3577013 • Letter: J

Question

Java question: create another class of your choice. Your programmer-defined class should have at a minimum two instance variables and one method. The instance variables and method should be representative of the data and behavior that objects of this class will have. You will demonstrate your understanding of encapsulation by declaring the instance variables as private and by creating accessors and mutators for each instance variable. You should have at least one constructor created that initializes all instance variables. I believe the code below fulfills all the assignment requirements. Please correct if wrong. ALSO, document the code.

public class FoodItem { private String name, description; private double price; private boolean isTasty; public void printFoodInfo() { System.out.println("Name: "+name +" Price: $"+price +" Description: "+description ); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public boolean isTasty() { return isTasty; } public void setTasty(boolean isTasty) { this.isTasty = isTasty; } }

A separate java class was created:

public static void main(String[] args) { FoodItem fishItem = new FoodItem(); fishItem.setName("Fried Fish"); fishItem.setDescription("Tasty juicy tilapia"); fishItem.setPrice(8.50); fishItem.printFoodInfo(); }

Explanation / Answer

HI, friend , your code was almost correct. Only constructor was missing.

I have added two constructor.

Please let me know in case of any issue.

class FoodItem {

   private String name, description;

   private double price;

   private boolean isTasty;

  

   public FoodItem(){

       name = "";

       description = "";

       price = 0;

       isTasty = false;

   }

  

   public FoodItem(String name, String description, double price, boolean isTasty){

       this.name = name;

       this.description = description;

       this.price = price;

       this.isTasty = isTasty;

   }

   public void printFoodInfo() {

       System.out.println("Name: "+name +" Price: $"+price +" Description: "+description );

   }

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public String getDescription() {

       return description;

   }

   public void setDescription(String description) {

       this.description = description;

   }

   public double getPrice() {

       return price;

   } public void setPrice(double price) {

       this.price = price;

   }

   public boolean isTasty() {

       return isTasty;

   }

   public void setTasty(boolean isTasty) {

       this.isTasty = isTasty;

   }

}

public class TestFoodItem{

   public static void main(String[] args) {

       FoodItem fishItem = new FoodItem();

       fishItem.setName("Fried Fish");

       fishItem.setDescription("Tasty juicy tilapia");

       fishItem.setPrice(8.50);

       fishItem.printFoodInfo();

   }

}

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