Ask Question
7 August, 16:29

Suppose that class OrderList has a private attribute double cost[100] which hold the cost of all ordered items, and a private attributes int num_of_items which hold the number of items ordered. For example, if num_of_items is 5, then cost[0], cost[1], ..., cost[4] hold the cost of these 5 items. Implement the member function named total_cost which returns the total cost of this OrderList.

+3
Answers (1)
  1. 7 August, 19:13
    0
    OrderList. java

    public class OrderList { private double cost[]; private int num_of_items; public OrderList () { cost = new double[100]; } public double total_cost () { double total = 0; for (int i=0; i < num_of_items; i++) { total + = cost[i]; } return total; } }

    Main. java

    public class Main { public static void main (String[] args) { OrderList sample = new OrderList (); double totalCost = sample. total_cost (); } }

    Explanation:

    Firstly, define a class OrderList with two private attributes, cost and num_of_items (Line 1-3). In the constructor, initialize the cost attribute with a double type array with size 100. Next, create another method total_cost () to calculate the total cost of items (Line 9-15). To implement the total_cost member function, create an OrderList instance and use that instance to call the total_cost () and assign it to totalCost variable.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Suppose that class OrderList has a private attribute double cost[100] which hold the cost of all ordered items, and a private attributes ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers