Question 1: Part 1: Draw a UML diagram for the class TwoDimOperations (10 points
ID: 3720569 • Letter: Q
Question
Question 1:
Part 1: Draw a UML diagram for the class TwoDimOperations (10 points).
Part 2: Write the Class TwoDimOperations. The class will include the following methods:
· getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array. (5 points)
· getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array. (5 points)
· getRowTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the total of the values in the specified row. (10 points)
· getColumnTotal. This method should accept a two-dimensional array as its argument and an integer as its second argument. The second argument should be the subscript of a column a in the array. The method should return the total of the values in the specified column. (10 points)
· getHighestInRow. This method should accept a two-dimensional array as its first argument as its argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the highest value in the specified row of the array and the subscript of the lowest value. (10 points)
· getLowestInRow. This method should accept a two-dimensional array as its first argument as its argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the lowest value in the specified row of the array and the subscript of the highest value. (10 points)
Part 3: Write the TestOperations.java program.
This program will create a two-dimensional array sales that holds annual sales by quarter and store branch. The column elements in sales array will hold thequarters (4 quarters in a year) and row elements will hold stores (number of store branches will be determined by the user).
1. Users will enter the sales data in the sales array (5 points).
2. Input validation: Number of store locations between 3 – 10 (5 points)
3. Input validation: Sale value between (100 - 200) (5 points)
4. The program will display a menu The menu includes the following options (5 points):
4a. Annual total sale by all stores
4b. Annual average sale by all stores
4c. Total annual sales for a given store
4d. Total annual sales for a given quarter
4e. Highest sale and quarter for a given store
4f. Lowest sale and quarter for a given store
5. Use a If and Else to perform the requested calculations (15 points).
5a. Note for option 4, user must specify the quarter before calling the methods.
5b. Note for options 3, 5, 6; a user must enter the store before calling the methods.
Add a Do-while loop that asks the user whether they want to perform another calculation. If yes, the menu will display. (5 points)
Explanation / Answer
TwoDimOperations.java
public class TwoDimOperations {
public TwoDimOperations() {
}
public int getTotal(int sales[][])
{
int tot=0;
for(int i=0;i<sales.length;i++)
{
for(int j=0;j<sales[0].length;j++)
{
tot+=sales[i][j];
}
}
return tot;
}
public double getAverage(int sales[][])
{
int tot=0;
for(int i=0;i<sales.length;i++)
{
for(int j=0;j<sales[0].length;j++)
{
tot+=sales[i][j];
}
}
return ((double)tot)/(sales.length*sales[0].length);
}
public int getRowTotal(int sales[][],int row)
{
int tot=0;
for(int j=0;j<sales.length;j++)
{
tot+=sales[row][j];
}
return tot;
}
public int getColumnTotal(int sales[][],int column)
{
int tot=0;
for(int i=0;i<sales.length;i++)
{
tot+=sales[i][column];
}
return tot;
}
public int getHighestInRow(int sales[][],int row)
{
int max=sales[row][0];
for(int j=0;j<sales.length;j++)
{
if(max<sales[row][j])
{
max=sales[row][j];
}
}
return max;
}
public int getLowestInRow(int sales[][],int row)
{
int min=sales[row][0];
for(int i=0;i<sales.length;i++)
{
if(min>sales[row][i])
{
min=sales[row][i];
}
}
return min;
}
}
___________________
TestOperations.java
import java.util.Scanner;
public class TestOperations {
public static void main(String[] args) {
// Declaring variables
int noOfStores, saleAmt, choice;
int quarter=0,storeNo = 0;
char ch ;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter no of Store Branches :");
noOfStores = sc.nextInt();
if (noOfStores < 3 || noOfStores > 10) {
System.out.println("** No of Stores must be between 3-10 **");
continue;
} else
break;
}
int sales[][] = new int[noOfStores][4];
for (int i = 0; i < noOfStores; i++) {
for (int j = 0; j < 4;) {
while (true) {
System.out.print("Enter store#" + (i + 1)
+ " sales in Quarter#" + (j + 1) + ":");
saleAmt = sc.nextInt();
if (saleAmt < 100 || saleAmt > 200) {
System.out
.println("** Sale Amount must be between 100-200 **");
continue;
} else {
sales[i][j] = saleAmt;
j++;
break;
}
}
}
}
TwoDimOperations td=new TwoDimOperations();
do {
System.out.println(" :: Menu ::");
System.out.println("1. Annual total sale by all stores");
System.out.println("2. Annual average sale by all stores");
System.out.println("3. Total annual sales for a given store");
System.out.println("4. Total annual sales for a given quarter");
System.out.println("5. Highest sale and quarter for a given store");
System.out.println("6. Lowest sale and quarter for a given store");
System.out.print(" Enter Choice :");
choice = sc.nextInt();
if(choice==4)
{
while(true)
{
System.out.print("Enter the Quarter No :");
quarter=sc.nextInt();
if(quarter<1 || quarter>4)
{
System.out.println("** Invalid.Must be between 1-4 **");
continue;
}
else
break;
}
}
else if(choice==3 || choice ==5 || choice==6)
{
while(true)
{
System.out.print("Enter the Store number :");
storeNo=sc.nextInt();
if(storeNo<0 || storeNo>noOfStores)
{
System.out.println("** Invalid.Must be between 1-"+noOfStores+" **");
continue;
}
else
break;
}
}
switch (choice)
{
case 1: {
int total=td.getTotal(sales);
System.out.println("Annual total Sale by all sales : "+total);
break;
}
case 2: {
double avg=td.getAverage(sales);
System.out.println("Annual average sale by all stores : "+avg);
break;
}
case 3: {
int totAnnualSalesOfStore=td.getRowTotal(sales,storeNo-1);
System.out.println("Total annual sales for a given store :"+totAnnualSalesOfStore);
break;
}
case 4: {
int totAnnualSalesOfQuarter=td.getColumnTotal(sales,quarter-1);
System.out.println("Total annual sales for a given quarter :"+totAnnualSalesOfQuarter);
break;
}
case 5: {
int max=td.getHighestInRow(sales,storeNo-1);
System.out.println("Highest Sales :"+max);
break;
}
case 6: {
int min=td.getLowestInRow(sales,storeNo-1);
System.out.println("Lowest Sales :"+min);
break;
}
}
//Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to continue(Y/N) ::");
ch = sc.next(".").charAt(0);
} while(ch=='y' || ch=='Y');
}
}
___________________
Output:
Enter no of Store Branches :3
Enter store#1 sales in Quarter#1:120
Enter store#1 sales in Quarter#2:145
Enter store#1 sales in Quarter#3:156
Enter store#1 sales in Quarter#4:165
Enter store#2 sales in Quarter#1:143
Enter store#2 sales in Quarter#2:132
Enter store#2 sales in Quarter#3:156
Enter store#2 sales in Quarter#4:189
Enter store#3 sales in Quarter#1:190
Enter store#3 sales in Quarter#2:121
Enter store#3 sales in Quarter#3:154
Enter store#3 sales in Quarter#4:165
:: Menu ::
1. Annual total sale by all stores
2. Annual average sale by all stores
3. Total annual sales for a given store
4. Total annual sales for a given quarter
5. Highest sale and quarter for a given store
6. Lowest sale and quarter for a given store
Enter Choice :1
Annual total Sale by all sales : 1836
Do you want to continue(Y/N) ::y
:: Menu ::
1. Annual total sale by all stores
2. Annual average sale by all stores
3. Total annual sales for a given store
4. Total annual sales for a given quarter
5. Highest sale and quarter for a given store
6. Lowest sale and quarter for a given store
Enter Choice :2
Annual average sale by all stores : 153.0
Do you want to continue(Y/N) ::y
:: Menu ::
1. Annual total sale by all stores
2. Annual average sale by all stores
3. Total annual sales for a given store
4. Total annual sales for a given quarter
5. Highest sale and quarter for a given store
6. Lowest sale and quarter for a given store
Enter Choice :3
Enter the Store number :4
** Invalid.Must be between 1-3 **
Enter the Store number :2
Total annual sales for a given store :431
Do you want to continue(Y/N) ::y
:: Menu ::
1. Annual total sale by all stores
2. Annual average sale by all stores
3. Total annual sales for a given store
4. Total annual sales for a given quarter
5. Highest sale and quarter for a given store
6. Lowest sale and quarter for a given store
Enter Choice :4
Enter the Quarter No :6
** Invalid.Must be between 1-4 **
Enter the Quarter No :2
Total annual sales for a given quarter :398
Do you want to continue(Y/N) ::y
:: Menu ::
1. Annual total sale by all stores
2. Annual average sale by all stores
3. Total annual sales for a given store
4. Total annual sales for a given quarter
5. Highest sale and quarter for a given store
6. Lowest sale and quarter for a given store
Enter Choice :5
Enter the Store number :4
** Invalid.Must be between 1-3 **
Enter the Store number :2
Highest Sales :156
Do you want to continue(Y/N) ::y
:: Menu ::
1. Annual total sale by all stores
2. Annual average sale by all stores
3. Total annual sales for a given store
4. Total annual sales for a given quarter
5. Highest sale and quarter for a given store
6. Lowest sale and quarter for a given store
Enter Choice :5
Enter the Store number :6
** Invalid.Must be between 1-3 **
Enter the Store number :2
Highest Sales :156
Do you want to continue(Y/N) ::y
:: Menu ::
1. Annual total sale by all stores
2. Annual average sale by all stores
3. Total annual sales for a given store
4. Total annual sales for a given quarter
5. Highest sale and quarter for a given store
6. Lowest sale and quarter for a given store
Enter Choice :6
Enter the Store number :1
Lowest Sales :120
Do you want to continue(Y/N) ::n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.