1. Write a Java application that asks the user to enter the scores in 3 differen
ID: 3571836 • Letter: 1
Question
1. Write a Java application that asks the user to enter the scores in 3 different tests (test1, test2, test3) for 5 students into a 2D array of doubles. The program should calculate the average score in the 3 tests for each student, as well as the average of all students for test1, test2 and test3.
2. Create a text file (you can name it sales.txt) that contains in each line the daily sales of a company for a whole month. Then write a Java application that: asks the user for the name of the file, reads the total amount of sales, calculates the average daily sales and displays the total and average sales. (Note: Use an ArrayList to store the data).
Please do the program separately and take your time
Explanation / Answer
Quesiton 1:
StudentTests.java
import java.util.Scanner;
public class StudentTests {
public static void main(String[] args) {
double tests[][] = new double[5][3];
Scanner scan = new Scanner(System.in);
for(int i=0;i<tests.length; i++){
System.out.print("Enter student "+(i+1)+" scores: ");
for(int j=0; j<tests[i].length; j++){
tests[i][j]=scan.nextDouble();
}
}
double totalSum = 0;
for(int i=0;i<tests.length; i++){
double eachSum = 0;
for(int j=0; j<tests[i].length; j++){
eachSum = eachSum+ tests[i][j];
}
totalSum= totalSum+eachSum;
System.out.println("Student "+(i+1)+" Average: "+(eachSum/3));
}
System.out.println("The average of all students: "+(totalSum/5));
}
}
Question 2:
SalesTest.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class SalesTest {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Double> list = new ArrayList<Double>();
Scanner scan = new Scanner(System.in);
System.out.print("Enter the file name: ");
String fileName = scan.next();
File file = new File(fileName);
if(file.exists()){
Scanner scan1= new Scanner(file);
while(scan1.hasNextDouble()){
list.add(scan1.nextDouble());
}
double totalSales = 0;
for(double sales: list){
totalSales = totalSales+sales;
}
double averageSales = totalSales/list.size();
System.out.println("Total Sales: "+totalSales);
System.out.println("Average sales: "+averageSales);
}
else{
System.out.println("File does not exist");
}
}
}
Output:
Enter the file name: D:\sales.txt
Total Sales: 450.0
Average sales: 50.0
sales.txt
10
20
30
40
50
60
70
80
90
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.