In Java: A company manufactures 5 different devices and each device is built usi
ID: 3834937 • 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 10Smart Phone
8
2 12 11 6 15 2Digital Watch
9 6 7 10 15 8 3Tablet
12 14 8 15 2 7 8 Portable Gaming System 12 10 3 11 8 3 5Explanation / Answer
code:
//*******************************************************************
// Dear CompileJava users,
//
// CompileJava has been operating since 2013 completely free. If you
// find this site useful, or would otherwise like to contribute, then
// please consider a donation (link in 'More Info' tab) to support
// development of the new CompileJava website (stay tuned!).
//
// Most sincerely, Z.
//*******************************************************************
import java.lang.Math; // headers MUST be above the first class
import java.util.Scanner; ;
// one class needs to have a main() method
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
double[][] comp = new double[5][7] ;
String[] devices = new String[5];
double[] cost = new double[7];
System.out.println("enter componenents info");
int i=0;
int j=7;
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
for(j=0;j<7;j++)
{
comp[i][j]=sc.nextDouble();
}
}
System.out.println("enter products info");
for(i=0;i<5;i++)
devices[i]=sc.next();
System.out.println("enter component cost info");
for(i=0;i<7;i++)
cost[i]=sc.nextDouble();
double[] man_cost = new double[5];
int high=0;
int low=0;
for(i=0;i<5;i++)
{
man_cost[i]=0;
for(j=0;j<7;j++)
{
man_cost[i]+=man_cost[i]+(comp[i][j]*cost[j]);
}
if(man_cost[i]>high)
high=i;
if(man_cost[i]<low)
low=i;
}
for(i=0;i<5;i++)
{
System.out.println("device name: "+devices[i]);
System.out.println("cost: "+man_cost[i]);
}
System.out.println("highest cost device: "+ devices[high]);
System.out.println("lowest cost device: "+ devices[low]);
}
}
// you can add other public classes to this editor in any order
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.