Write a program that declares three arrays named price, qty, and amt. Each array
ID: 3832592 • Letter: W
Question
Write a program that declares three arrays named price, qty, and amt. Each array should be declared in main() and capable of holding 3 values. Make up numbers for price and qty (quantity). (ShoppingCart.java) (4 pts) Write a method to fill the amt array with the product of the corresponding elements in price and qty. (3 pts – create a method within the ShoppingCart class) Allow users to change the price and qty for three items. Display the values in the amt array after the three items have been entered. (
Explanation / Answer
ShoppingCart.java
import java.util.Scanner;
public class ShoppingCart {
public static void main(String[] args) {
double price[] = new double[3];
int qty[] = new int[3];
double amt[] = new double[3];
getInputs(price, qty);
calcAmount(price, qty, amt);
printValues(price, qty, amt);
System.out.println("Again Enter the values: ");
getInputs(price, qty);
calcAmount(price, qty, amt);
printValues(price, qty, amt);
}
public static void getInputs(double price[], int qty[]) {
Scanner scan = new Scanner(System.in);
for(int i=0;i<qty.length;i++){
System.out.print("Enter the quantity: ");
qty[i]=scan.nextInt();
System.out.print("Enter the amount: ");
price[i]=scan.nextDouble();
}
}
public static void calcAmount(double price[], int qty[],double amt[]){
for(int i=0;i<qty.length;i++){
amt[i]=price[i]*qty[i];
}
}
public static void printValues(double price[], int qty[],double amt[]) {
for(int i=0;i<qty.length;i++){
System.out.println("Quantity: "+qty[i]+" Price: "+price[i]+" Amount: "+amt[i]);
}
}
}
Output:
Enter the quantity: 1
Enter the amount: 44
Enter the quantity: 2
Enter the amount: 55.5
Enter the quantity: 3
Enter the amount: 66.6
Quantity: 1 Price: 44.0 Amount: 44.0
Quantity: 2 Price: 55.5 Amount: 111.0
Quantity: 3 Price: 66.6 Amount: 199.79999999999998
Again Enter the values:
Enter the quantity: 1
Enter the amount: 11.1
Enter the quantity: 2
Enter the amount: 22.2
Enter the quantity: 3
Enter the amount: 33.3
Quantity: 1 Price: 11.1 Amount: 11.1
Quantity: 2 Price: 22.2 Amount: 44.4
Quantity: 3 Price: 33.3 Amount: 99.89999999999999
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.