Array is
string productitems [] = {product A,….
double prices [] = {14.00,
How to use array to add product ?
and calculate the total price ?
and get total price in the array?
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
package com.company;
public class Main{
public static void main(String[] args) {
//lets define our arrays
String [] productitems = new String[5];
double [] prices = new double[5];
//ANSWER TO QUESTION a0
//How to use array to add product ?
//Now lets add 5 products and their prices
productitems[0]="product_A";
productitems[1]="product_B";
productitems[2]="product_C";
productitems[3]="product_D";
productitems[4]="product_E";
//Lets add their prices
prices[0]=14.00;
prices[1]=20.00;
prices[2]=30.00;
prices[3]=10.00;
prices[4]=25.00;
//lets calculate the total price and get it to print in the console
//declare a variable to store total price
double total_price = 0.00;
for(int i = 0; i< prices.length;i++)
{
total_price = total_price + prices[i];
}
//lets print the prodyctsitems array
System.out.println("\nThe products in the productitems array are: \n");
for(int i = 0; i< productitems.length; i++)
{
System.out.print(productitems[i]+" ");
}
System.out.println("\n");
//lets print the prices array
System.out.println("The prices in the prices array are: \n");
for(int i = 0; i< prices.length; i++)
{
System.out.print(prices[i]+" ");
}
System.out.println("\n");
//print the total price in the array
System.out.println("\nThe total price in the price array = "+total_price);
}
}
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment