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

T-Mobile 12:01 PM 36% a ccbcmd-bb.blackboard.com C 1 of 1 Lab 8: Module 6- Array

ID: 3763086 • Letter: T

Question

T-Mobile 12:01 PM 36% a ccbcmd-bb.blackboard.com C 1 of 1 Lab 8: Module 6- Arrays 1. Write a program that declares three arrays named price, gty, and amt. Each array should be declared in main0 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 gty. (3 pts- create a method within the ShoppingCart class) Write another method to display the values in the three arrays. (3 pts- create a method within the ShoppingCart class) Allow users to change the price and qty for three items. Calculate and display the values in the three arrays after the three items have been entered. (5 pts- use the two methods created in order to calculate the amt values and displaying the arrays) 2. Write a Java program that calculates the sum of a variable sized matrix using a The user should provide the number of rows and columns (6 pts) o Test multiple user inputs (3 x2,4 x4, 6 x 2, etc) A sum should be created for each row, each column, and the total for the matrix (9 pts) Ex: How big would you like your matrix? Rows 7 3 Columns -2 Please enter your row 1? Column 1-3 Column 2-4 Please enter your row 2? Column 1-7 Column 2-40 Please enter your row 3? Column 1-20 Column 2-12 Your total for row 1 is-7 Your total for row 2 i$-47 Your total for row 3 is-32 Your total for column 1 is-30 Your total for column 2 is- 56 Your total for the 3 x 2 matrix is 86

Explanation / Answer

import java.util.Scanner;
class ShoppingCart {
public static void main(String args[] ) throws Exception {
double [] price = new double[3];
int [] qty = new int[3];
double [] amt = new double[3];
price[0]=2.5;
price[1]=3.7;
price[2] = 7.5;
qty[0]=2;
qty[1]=3;
qty[2]=6;
calculateAmt(price,qty,amt);
displayArrays(price,qty,amt);
Scanner in = new Scanner(System.in);
for(int i=0;i<3;i++)
{
System.out.println("enter price of "+(i+1)+" item");
price[i] = in.nextDouble();
System.out.println("enter quantity of "+(i+1)+" item");
qty[i] = in.nextInt();
}
calculateAmt(price,qty,amt);
displayArrays(price,qty,amt);
}
public static void calculateAmt(double []price, int [] qty, double[] amt)
{
for(int i=0;i<3;i++)
amt[i]=price[i]*qty[i];
}
public static void displayArrays(double[] price, int[] qty, double[] amt)
{
for(int i=0;i<3;i++)
System.out.println("price["+(i+1)+"] = "+price[i]);
for(int i=0;i<3;i++)
System.out.println("qty["+(i+1)+"] = "+qty[i]);
for(int i=0;i<3;i++)
System.out.println("amt["+(i+1)+"] = "+amt[i]);
}
}

input:

2.2

3

4.5

6

5.5

7

output:

import java.util.Scanner;
class ArraySum {
public static void main(String args[] ) throws Exception {
int row, col,i,sum;
Scanner in = new Scanner(System.in);
System.out.print("rows?");
row = in.nextInt();
System.out.print("columns?");
col = in.nextInt();
System.out.println(row+" "+col);
int[][] mat = new int[row][col];
for(i=0;i<row;i++)
{
System.out.println("plese enter your row"+(i+1));
for(int j=0;j<col;j++)
{
System.out.println("column"+(j+1)+"?");
mat[i][j]=in.nextInt();
}
}
for(i=0;i<row;i++)
System.out.println("the total for row"+(i+1)+"is "+rowSum(mat,i,col));
for(i=0;i<col;i++)
System.out.println("the total for column"+(i+1)+"is "+colSum(mat,row,i));
System.out.println("the total for "+row+"x"+col+" matrix is "+matSum(mat,row,col));
  
}

public static int rowSum(int [][] mat, int row, int col)
{
int sum=0;

for(int i=0;i<col;i++)
{
sum+=mat[row][i];
}
  
return sum;
}
public static int colSum(int [][] mat, int row, int col)
{
int sum=0;
for(int i=0;i<row;i++)
{
sum+=mat[i][col];
}
return sum;
}
public static int matSum(int [][] mat, int row, int col)
{
int sum=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
sum+=mat[i][j];
}
}
return sum;
}
  
  

}