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

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

ID: 3571493 • Letter: I

Question

In 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.

Smart Phone

8

Digital Watch

Tablet

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 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:

Components 1 2 3 4 5 6 7 MP3 Player 9 13 4 7 1 14 10

Smart Phone

8

2 12 11 6 15 2

Digital Watch

9 6 7 10 15 8 3

Tablet

12 14 8 15 2 7 8 Portable Gaming System 12 10 3 11 8 3 5

Explanation / Answer

//include header file
import java.io.*;
import java.util.Scanner;

//class to calculate cost of componets
public class HelloWorld{
  
    //instance variables
    static float cost[]=new float[7];
    static int components[][]=new int[5][7];
    static String names[]=new String[5];
    static float totalComponentCost[]=new float[5];
  
    //function to initiliaze names
    public static void initializeNames()
    {
        names[0]="MP3 Player";
        names[1]="Smart Phone";
        names[2]="Digital Watch";
        names[3]="Tablet";
        names[4]="Portable Gaming System";
    }
  
    //function to intialize cost through scanner
    public static void initializeCost()
    {
        Scanner input=new Scanner(System.in);
        for(int i=0;i<7;i++)
        {
            System.out.println("Enter cost of component "+(i+1)+" : ");
            cost[i]=input.nextFloat();
        }
    }
  
    //function to intialize componnets through FILE IO
    public static void initializeComponents()
    {
        /*
        BufferedWriter bw = null;
       FileWriter fw = null;
       try {
           String content = "0,9,13,4,7,1,14,10 1,8,2,12,11,6,15,2 2,9,6,7,10,15,8,3 3,12,14,8,15,2,7,8 4,12,10,3,11,8,3,5 ";
           fw = new FileWriter("components.txt");
           bw = new BufferedWriter(fw);
           bw.write(content);
               bw.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
       */
      
       //read from file and store values into array
       BufferedReader br = null;
       FileReader fr = null;
       try {
           fr = new FileReader("components.txt");
           br = new BufferedReader(fr);
           String sCurrentLine;
           br = new BufferedReader(fr);
           while ((sCurrentLine = br.readLine()) != null) {
               String token[]=sCurrentLine.split(",");
               int com=Integer.parseInt(token[0]);
               for(int i=1;i<token.length;i++)
               {
                    components[com][i-1]=Integer.parseInt(token[i]);
               }
           }
           br.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
      
    }
  
    //function to calculate total cost
    public static void calTotal()
    {
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<7;j++)
            {
                totalComponentCost[i]+=components[i][j]*cost[j];
            }
        }
    }
  
    //fucniton to diaply cost of devices
    public static void displayCost()
    {
        System.out.println("*****Cost of devices*****");
        for(int i=0;i<5;i++)
        {
            System.out.println(names[i]+" : $"+totalComponentCost[i]);
        }
    }
  
    //fucntion to cal device with highets cost
    public static void getHighestCost()
    {
        int index=-1;
        float max=0;
        for(int i=0;i<5;i++)
        {
            if(totalComponentCost[i] > max)
            {
                max=totalComponentCost[i];
                index=i;
            }
        }
        System.out.println("Highest Cost Device is :"+names[index]);
    }
  
    //fucniton to cal device with low cost
     public static void getLowestCost()
    {
        int index=-1;
        float min=9999;
        for(int i=0;i<5;i++)
        {
            if(totalComponentCost[i] < min)
            {
                min=totalComponentCost[i];
                index=i;
            }
        }
        System.out.println("Lowest Cost Device is :"+names[index]);
    }
  
    //fucntion to cal avg,high,low no of components
    public static void getHighLowComponents()
    {
        int total[]=new int[5];
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<7;j++)
            {
                total[i]+=components[i][j];
            }
        }
      
        int totalCom=0;
        int index=-1;
        int max=0;
        for(int i=0;i<5;i++)
        {
            totalCom+=total[i];
            if(total[i]>max)
            {
                max=total[i];
                index=i;
            }
        }
        System.out.println("Highest no of components : "+names[index]);
      
        index=-1;
        max=9999;
        for(int i=0;i<5;i++)
        {
            if(total[i] < max)
            {
                max=total[i];
                index=i;
            }
        }
        System.out.println("Lowest no of components : "+names[index]);
      
        System.out.println("Average Num of components : "+ totalCom/5);

    }

     public static void main(String []args){
        initializeNames();
        initializeCost();
        initializeComponents();
        calTotal();
        displayCost();
        getHighestCost();
        getLowestCost();
        getHighLowComponents();
     }
}

Sample Output:

Enter cost of component 1 :                                                                                                                                     

10.75                                                                                                                                                           

Enter cost of component 2 :                                                                                                                                     

15.27                                                                                                                                                           

Enter cost of component 3 :                                                                                                                                     

5.98                                                                                                                                                            

Enter cost of component 4 :                                                                                                                                     

9.67                                                                                                                                                            

Enter cost of component 5 :                                                                                                                                     

4.32                                                                                                                                                            

Enter cost of component 6 :                                                                                                                                     

12.50                                                                                                                                                           

Enter cost of component 7 :                                                                                                                                     

1.42                                                                                                                                                            

*****Cost of devices*****                                                                                                                                       

MP3 Player : $580.3901                                                                                                                                          

Smart Phone : $510.93002                                                                                                                                        

Digital Watch : $495.99                                                                                                                                         

Tablet : $643.17                                                                                                                                                

Portable Gaming System : $485.17                                                                                                                                

Highest Cost Device is :Tablet                                                                                                                                  

Lowest Cost Device is :Portable Gaming System                                                                                                                   

Highest no of components : Tablet                                                                                                                               

Lowest no of components : Portable Gaming System                                                                                                                

Average Num of components : 58    

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