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

Write a Java Code:- 1) Part I: This is a continuation of the Item class we desig

ID: 3910602 • Letter: W

Question

Write a Java Code:-

1) Part I:

This is a continuation of the Item class we designed in the last assignment.

Add a toString method to your Item class. This method does not accept any parameters and returns a String reflecting the current state of the object. Once you have written the toString method, the following statement will display the contents of all member variables in an Item object called chair:

System.out.println(chair);

Part II:

Create an array of Item objects. Then write a loop that calculates the total weight of the Items in the array. Since the weight is a private data member of the Item class, the loop will need to call an accessor method to query the weight of each Item in the array.

Explanation / Answer

if you have any doubts, please give me comment...

Part:1

public class Item{

private String name;

private double weight;

private double price;

//constructors

//getters and setter methods

public String toString(){

return "Name: "+name+", Weight: "+weight+", Price: "+price;

}

}

Part II:

public class ItemsDemo{

public static void main(String args[]){

int n = 5;

Item items = new Item[n];

double tot_weight = 0.0;

for(int i=0; i<n; i++){

tot_weight += items[i].getWeight();

}

System.out.println("The total weight is: "+tot_weight);

}

}

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