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

A company manufactures 5 different devices and each device is built using a vary

ID: 3832757 • Letter: A

Question

A company manufactures 5 different devices and each device is built using a varying amount of 7 different components. Each device and its required amount of components is listed in the table below. Each component has an individual cost which is listed in the table below. Populating the Data: Your program will have three arrays: A 2D array to store the data of the first table A 1D array to store the names of each product A 1D array to store the costs of each component. Practice getting the data into your program in these ways: Console input with Scanner File I/O Using an Initializer List. Calculations: Compute and Display the cost of manufacturing each device. Compute and Display the Device name with the highest cost. Compute and Display the Device name with the lowest cost. Practice other calculations as you see fit, i.e. Average number of components per device. Device with the highest/ lowest number of components, etc. etc. Sample Results:

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Sam
*/
public class Manufacturer {
    private int[][] partsCount;
    private String[] names;
    private double[] partsCost;

    public Manufacturer() {
        partsCount = new int[5][7];
        names = new String[5];
        partsCost = new double[7];
    }
  
    void readFromScanner() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter names of 5 devices: ");
        for (int i=0; i<5; i++)
            names[i] = sc.next();
      
        System.out.println("Enter cost of 7 parts: ");
        for (int i=0; i<7; i++)
            partsCost[i] = sc.nextDouble();
      
        for (int i = 0; i < 5; i++) {
            System.out.println("Enter number of parts required to manufacture " + names[i]);
            for (int j = 0; j < 7; j++ )
            partsCount[i][j] = sc.nextInt();
        }
    }
  
    void readFromFile(File file) {
        try {
            Scanner sc = new Scanner(file);
            for (int i=0; i<5; i++)
                names[i] = sc.next();
          
            for (int i=0; i<7; i++)
                partsCost[i] = sc.nextDouble();
          
            for (int i = 0; i < 5; i++)
                for (int j = 0; j < 7; j++ )
                    partsCount[i][j] = sc.nextInt();
          
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Manufacturer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void readFromInitializerList(int[][] partsCount, String[] names, double[] partsCost) {
        this.partsCount = partsCount;
        this.names = names;
        this.partsCost = partsCost;
    }
  
    void calculateAndDisp() {
        double totalCost[] = new double[5]; //to hold cost of all device
        double sum = 0; //to count the sum of cost of all devices
        int maxIndex = 0; //index of costliest device
        int minIndex = 0; //index of cheaest device
        for (int i = 0; i<5; i++) { //loop to populate the cost of all device and find max/min at the same time
            totalCost[i] = 0;
            for (int j = 0; j <= 7; j++)
                totalCost[i]+= partsCount[i][j]*partsCost[j];
            if (totalCost[i] > totalCost[maxIndex]) //verify is the current device is the costliest
                maxIndex = i;
            if (totalCost[i] < totalCost[minIndex]) //verify is the current device is the cheapest
                minIndex = i;
            System.out.println(names[i] + " costs " + totalCost[i]); //print the cost of current device
            sum += totalCost[i]; // calcultate the sum
        }
        double avg = sum/5; // derive avg from sum **BONUS**
        System.out.println("Highest cost device: " + names[maxIndex]); //print remaining information
        System.out.println("Lowest cost device: " + names[minIndex]);
        System.out.println("Avg cost of a device: " + avg);
    }
}

I kept the code simple. NOTE that Java does not provide initializer list, so we can pass arrays as params. If you have any query on the code, please feel free to comment below. I shall be glad to help you.

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