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

Using Java: A company manufactures 5 different devices and each device is built

ID: 3835811 • Letter: U

Question

Using Java: 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 Using the information in the tables, your program will calculate some data about the products. Populating the Data: Your program w have three arrays A2D 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 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.

Explanation / Answer

Hi,

Please see below the class.

Please comment for any queries/feedbacks.

Thanks.

DeviceManufacturer.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;


public class DeviceManufacturer {
   static int [][] data = new int[5][7];
   static String [] productNames = {"MP3 Player","Smart Phone","Digital Watch","Tablet","Portable Gaming System"};
   static double [] productCosts = new double[5];
  
   static double [] costArray = {10.75, 15.27,   5.98, 9.67,   4.32, 12.50, 1.42};
   static int compCounter = 0;
   static int rowCounter=0 ;

   public static void main(String[] args) {
      
       double costForProduct =0;
       double highestCost =0;
       double lowestCost =0;
       String highestCostProduct = "";
       String lowestCostProduct = "";
      
       //Reading the component measures from file
       readFromFile("components.txt");
      
       //Compute and Display the cost of manufacturing each device.
       for(int i=0;i<productNames.length;i++){
           costForProduct = 0.0;
           for(int j=0;j<costArray.length;j++){
               costForProduct = costForProduct + data[i][j] * costArray[j];
           }
           System.out.println(productNames[i]+" Cost $: "+ String.format("%.2f", costForProduct));
           productCosts[i] = costForProduct;
       }
      
       //Compute and Display the Device name with the highest cost.
       highestCost = productCosts[0];
       for(int i=0;i<productCosts.length;i++){
           if(productCosts[i]> highestCost){
               highestCost = productCosts[i];
               highestCostProduct = productNames[i];
           }
       }
      
       //Compute and Display the Device name with the lowest cost.
       lowestCost = productCosts[0];
       for(int i=0;i<productCosts.length;i++){
           if(productCosts[i] < lowestCost){
               lowestCost = productCosts[i];
               lowestCostProduct = productNames[i];
           }
       }
      
       System.out.println(" ");
       System.out.println("Highest Cost Device: "+highestCostProduct);
       System.out.println("Lowest Cost Device: "+lowestCostProduct);
   }

   /**
   * Reading the component measures from file
   * @param filename
   */
   public static void readFromFile(String filename){
       BufferedReader br = null;
       FileReader fr = null;

       try {

           fr = new FileReader(filename);
           br = new BufferedReader(fr);

           String sCurrentLine;


           while ((sCurrentLine = br.readLine()) != null) {
               StringTokenizer tkens = new StringTokenizer(sCurrentLine);
               compCounter =0;
               while(tkens.hasMoreTokens()){
                   data[rowCounter][compCounter] = Integer.valueOf(tkens.nextToken());
                   compCounter++;
               }
              
               rowCounter++;
           }

       } catch (IOException e) {

           e.printStackTrace();

       }
   }
}

components.txt

9   13   4   7   1   14   10
8    2   12   11   6   15   2
9   6   7   10   15   8   3
12   14   8   15   2   7   8
12   10   3   11   8   3   5

Sample output :

MP3 Player Cost $: 580.39
Smart Phone Cost $: 510.93
Digital Watch Cost $: 495.99
Tablet Cost $: 643.17
Portable Gaming System Cost $: 485.17


Highest Cost Device: Tablet
Lowest Cost Device: Portable Gaming System

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