Question

In: Computer Science

Java program In the main(), create and load a 2-dimentional array to hold the following sales...

Java program

In the main(), create and load a 2-dimentional array to hold the following sales data

Number of Tacos Sold (hard code the data into the array when you create it)

Truck3 Truck4 .    Truck5

Monday 250 334 229

Wednesday   390 145 298

Friday .    434 285 . 156

• Write a method to calculate the grand total of all Tacos sold

Write a method to allow the user to enter a truck number and get the total sales for that truck

Write a method to allow the user to enter a Day and get the total sales for that day.

Invoke all of the methods from the main

Validate all entries the user makes

Comment your code

write the grand total to a .txt file

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Program

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Test {
   public static void main(String[] args) throws IOException {
       Scanner sc = new Scanner(System.in);
      
       //hardcoded 2d array
       int[][] tacosSold = { { 250, 334, 229 }, { 390, 145, 298 }, { 434, 285, 156 } };
      
       String str;
      
       //taking input and pasing it o the methods
       System.out.print("Enter Truck number: ");
       str = sc.nextLine();
       truckTotalSales(tacosSold, str);
      
       System.out.print("Enter Day: ");
       str = sc.nextLine();
       dayTotalSales(tacosSold, str);
      
       grandTotal(tacosSold);
       sc.close();
   }

   private static void grandTotal(int[][] tacosSold) throws IOException {
       int sum =0;
      
       for(int i=0; i<tacosSold.length; i++) {
           for(int j=0; j<tacosSold.length; j++) {
               sum+=tacosSold[i][j];//calculating grad total
           }
       }
       System.out.println("Grand total: $"+sum);
      
       //change the name of the file and path accordingly
       BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
       bw.write("Grand total: $"+sum);
       bw.close();
   }

   private static void truckTotalSales(int[][] tacosSold, String truckNumber) {
       int sum =0;
       int index = 0;
       //indetifying the index
       if(truckNumber.equalsIgnoreCase("truck3"))
           index = 0;
       if(truckNumber.equalsIgnoreCase("truck4"))
           index = 1;
       if(truckNumber.equalsIgnoreCase("truck5"))
           index = 2;
      
       for(int i=0; i<tacosSold.length; i++) {
          
           sum+=tacosSold[i][index];//calculating track number total
       }
       System.out.println("Total sales for "+truckNumber+": $"+sum);
   }
  
   private static void dayTotalSales(int[][] tacosSold, String day) {
       int sum =0;
       int index = 0;
      
       //indentifying index
       if(day.equalsIgnoreCase("monday"))
           index = 0;
       if(day.equalsIgnoreCase("wednesday"))
           index = 1;
       if(day.equalsIgnoreCase("friday"))
           index = 2;
      
       for(int i=0; i<tacosSold.length; i++) {
          
           sum+=tacosSold[index][i];//calculating day total
       }
       System.out.println("Total sales for "+day+": $"+sum);
   }
}

Output


Related Solutions

Write a program in c++ to do the following: 2. Create an array to hold up...
Write a program in c++ to do the following: 2. Create an array to hold up to 20 integers. 3. Create a data file or download attached text file (twenty_numbers.txt) that contains UP TO 20 integers. 4. Request the input and output file names from the user. Open the files being sure to check the file state. 5. Request from the user HOW MANY numbers to read from the data file, up to twenty. Request the number until the user...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
Java programming Create a application with a method void (after main() ) that creates an array...
Java programming Create a application with a method void (after main() ) that creates an array and asks for the user fill it with float numbers. This array have infinite elements until the user decide that it is enough and input a command to stop. Display this array's elements data in another method void. Thanks for your help!
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
Create a Java program with a method that searches an integer array for a specified integer...
Create a Java program with a method that searches an integer array for a specified integer value **(see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". starting header ** public...
Create an application named MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with...
Create an application named MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with random integers drawn from the range 1 to 100, and then output the index of the row with the highest sum along all the rows. To support your solution, create a class named RowSumThread.java from which you can instantiate Runnable objects, each of which will sum one row of the two-dimensional array and then place the sum of that row into the appropriate slot of a one-dimensional, 20-element array. To Summarize, your application will: 1. Generate the two-dimensional array of random integers 2. Start 20 concurrent threads, each of which places the sum of one row of the two-dimensional array into the corresponding slot of a one-dimensional array. 3. Onput the index of the row with the maximum value.
Create an application named MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with...
Create an application named MaxRowSum.java that instantiates a 20x20 two-dimentional array of integers, populates it with random integers drawn from the range 1 to 100, and then output the index of the row with the highest sum along all the rows. To support your solution, create a class named RowSumThread.java from which you can instantiate Runnable objects, each of which will sum one row of the two-dimensional array and then place the sum of that row into the appropriate slot...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT