Question

In: Computer Science

Part 1 Write a program that prompts the user to enter three sets of five double...

Part 1

  1. Write a program that prompts the user to enter three sets of five double numbers each. These numbers represent the five grades that each of three students has received. After prompting for the grades, the program:
  1. stores the data in a previously defined 3◊5 double array
  2. computes the average of each set of 5 grades after acquiring the grades for that one student
  3. determines the maximum and minimum values of each set of 5 grades acquiring the grades for that one student
  4. reports the results one student at a time after acquiring the grades for that one student

Your project will be named Lab01FL where F represents the first letter of your first name and L represents the first letter of your last name. For example, in my case the project would be named Lab01MG. The reason for this is that you will be zipping up the entire project folder and sending that to me by BlackBoard mail. In this way, each student will have a uniquely named file folder that I can run in the NetBeans Integrated Development Environment. It is assumed that you have installed Java and NetBeans from classes that serve as a prerequisite to this one. The only thing I insist upon is that you do not use a Java version greater than Java 8. I will only be testing your app with that version so if you use a feature from a higher version of Java, it will not run and I cannot grade your submission. The main class will be called Lab01. The main method of this class continuously re-prompts the user for new sets of 3◊5 data. Entering a Y or y will cause the program to loop for a new set of 3x5 data. Any other value stops the application. You must also write another class called GradeCalculator. GradeCalculator will contain two static methods

  • one to compute the average of an input one-dimensional array of doubles for a specific student
  • one to compute its maximum and minimum

The average function (I called mine avg) accepts a one-dimensional double array as the only input parameter and returns a double that is the average. The max/min function has a return type of class MaxMin. MaxMin is a trivial class that contains two private double data members – one to hold the maximum grade and one for the minimum one. Of course, the class will have getters and setters to retrieve and modify the data. The maxmin method accepts the same one-dimensional double array as its only input parameter and returns the maximum and minimum via the MaxMin class. The driver calls the proper functions with the correct parameters for average, maximum and minimum calculations and is responsible for displaying the results. Some key considerations are

  1. the use of static final to represent the two key parameters of the problem (namely the number of students and the number of grades per student) AND the use of final in argument lists to prevent methods from changing parameters that should be immutable
  2. efficient functions, that is, functions that require as little memory and operations as possible and are general enough for a wide variety of uses
  3. comments and good formatting for readability
  4. faithful reproduction of all formatting nuances shown in the sample output below
  5. be sure to test your application with different values

Part 2

This is an extension to an earlier lab involving a grade reporting system. Write a program that prompts the user to enter the number of students and the number of grades per student. Then Dynamically allocate the space for the two dimensional double array compute the average, maximum and minimum of each set of student grades compute the average, maximum and minimum of all the values report the results You will need a driver that continuously re-prompts the user for new sets of data. You must modify the functions you wrote in an earlier lab to compute the average of an input array of doubles and the one to compute the maximum and minimum. Recall that the original average function accepted the array as input parameter and returned a double that is the average. The max/min function had a return type of class MaxMin which contained the calculated maximum and minimum grade. It accepted the one dimensional array as input parameter. Since you are now tasked to compute the average, maximum and minimum of the entire class, you must write an additional average and max/min method that can accept as input a two dimensional array of doubles. Any other aspects of the new methods remain as is. The basic average, maximum and minimum finding algorithms are essentially unchanged, except that all work is done inside nested loops instead of a single loop. The driver calls the proper functions with the correct parameters for average, maximum and minimum calculations and is responsible for displaying results. I/O function calls should only appear in the driver. Keep in mind these other general rules of good program construction: Use static final constants to represent the key parameters of the problem. Use final to ensure that the compiler guards against changes being made to constants and/or method arguments that should remain immutable. All constants, variables and functions should have names that describe their intent. Write efficient functions, that is, functions that require as little memory and operations as possible and are general enough for a wide variety of uses. Add comments and good formatting for readability. Your output should faithfully reproduce all of formatting nuances shown in the sample output below; however, be sure to test your application with different values.

Just need help with PART 2 thanks


import java.util.Scanner;

public class lab01 {
public static final int start = 3;
public static final int end = 5;
  
public static void main(String[] args){
double[][] grades = new double [start][end];
MinMax stuff = null;
Scanner scanner = new Scanner(System.in);
String yorn = "Y";
StringBuilder answer = new StringBuilder( "" );
while( Character.toUpperCase(yorn.charAt(0))== 'Y'){
for( int s = 0; s < start; s++){
answer = answer.delete( 0, answer.length()).append("\n\nPlz enter the grades for student #").append(s+1 + ":");
System.out.println(answer);
for(int g = 0;g < end; g++){
grades[s][g]= scanner.nextDouble();
}
stuff = GradeCalculator.maxMin( grades[s]);
answer = answer.delete(0, answer.length()).append("The average grade for student #").append(s+1 + ":")
.append(" is ").append(String.format("%.1f", GradeCalculator.avg(grades[s])))
.append("\nThe maximum grade for student #").append(s+1 + ":")
.append(" is ").append((int) stuff.getMax())
.append("\nThe minimum grade for student $").append(s+1 + ":")
.append(" is ").append((int) stuff.getMin());
System.out.println(answer);
}
System.out.println("\n\nDo you wish to continue (Y/N) - ");
yorn = scanner.next();
}
  
}
}

class GradeCalculator {

public static double avg( final double[] array) {
double a;
int i;
  
for(a = array[0], i = 1; i < array.length; i++){
a += array[i];
}
return a / array.length;
}
public static MinMax maxMin( final double[] array){
double max, min;
int i;
MinMax scores = new MinMax();
  
for(max = min = array[0], i=1; i <array.length; i++){
if(array[i] < min){
min = array[i];
}
else if(array[i] > max){
max = array[i];
}
}
scores.setMax(max);
scores.setMin(min);
  
return scores;
}
}

class MinMax {

private double max;
private double min;
  
public double getMax(){
return max;
}
public double getMin(){
return min;
}
public void setMax(final double max){
this.max = max;
}
public void setMin(final double min){
this.min = min;
}
}

Solutions

Expert Solution

Screenshot

Program

import java.util.Scanner;
//Craete rade calculator class
class GradeCalculator {
   //Function to find averageof a single student
   public static double avg( final double[] array) {
       double a;
       int i;

       for(a = array[0], i = 1; i < array.length; i++){
           a += array[i];
           }
       return a / array.length;
       }
   //Function to calculate average of all class
   public static double avg( final double[][] array) {
       double a=0;
       int i;

       for( i = 0; i < array.length; i++){
           for(int j=0;j<array[0].length;j++) {
               a += array[i][j];
           }
       }
       return a / (array.length*array[0].length);
   }
   //Function to get min and max of a single student
   public static MinMax maxMin( final double[] array){
       double max, min;
       int i;
       MinMax scores = new MinMax();
       for(max = min = array[0], i=1; i <array.length; i++){
           if(array[i] < min){
               min = array[i];
           }
           else if(array[i] > max){
               max = array[i];
           }
       }
       scores.setMax(max);
       scores.setMin(min);

       return scores;
   }
   //Method to calculate min and max of whole class
   public static MinMax maxMin( final double[][] array){
       double max=array[0][0], min=array[0][0];
       int i;
       MinMax scores = new MinMax();
       for( i=0; i <array.length; i++){
           for(int j=0;j<array[i].length;j++) {
               if(array[i] [j]< min){
                   min = array[i][j];
               }
               else if(array[i][j] > max){
                   max = array[i][j];
               }
       }
   }
   scores.setMax(max);
   scores.setMin(min);

   return scores;
   }
}
//Create a class for MIn and Max
class MinMax {
   //Instance variables
   private double max;
   private double min;
   //Accessors
   public double getMax(){
       return max;
   }
   public double getMin(){
       return min;
   }
   public void setMax(final double max){
       this.max = max;
   }
   //Mutators
   public void setMin(final double min){
       this.min = min;
   }
}

//Main class
public class lab01 {
   public static void main(String[] args){
       //Array
       double[][] grades;
       //class count
       int cnt=1;
       MinMax stuff = null;
       Scanner scanner = new Scanner(System.in);
       String yorn = "Y";
       StringBuilder answer = new StringBuilder( "" );
       //Loop until user wishes
       while( Character.toUpperCase(yorn.charAt(0))== 'Y'){
           //Prompt for student count
           System.out.print("Enter number of students: ");
           int start=scanner.nextInt();
           //Prompt for grade count
           System.out.print("Enter grades per each student: ");
           int end=scanner.nextInt();
           //Dynamic array
           grades=new double[start][end];
           //Loop until full class
           for( int s = 0; s < start; s++){
               answer = answer.delete( 0, answer.length()).append("\n\nPlz enter the grades for student #").append(s+1 + ":");
               System.out.println(answer);
               //Loop until each student
               for(int g = 0;g < end; g++){
                   grades[s][g]= scanner.nextDouble();
               }
               //Calculated values/student
               stuff = GradeCalculator.maxMin( grades[s]);
               answer = answer.delete(0, answer.length()).append("The average grade for student #").append(s+1 + ":")
                       .append(" is ").append(String.format("%.1f", GradeCalculator.avg(grades[s])))
                       .append("\nThe maximum grade for student #").append(s+1 + ":")
                       .append(" is ").append((int) stuff.getMax())
                       .append("\nThe minimum grade for student $").append(s+1 + ":")
                       .append(" is ").append((int) stuff.getMin());
          
               System.out.println(answer);
           }
           //Calculated value of single class
           System.out.println("\n");
           stuff=GradeCalculator.maxMin(grades);
           answer = answer.delete(0, answer.length()).append("The average grade for class #").append(cnt+ ":")
                   .append(" is ").append(String.format("%.1f", GradeCalculator.avg(grades)))
                   .append("\nThe maximum grade for class #").append(cnt + ":")
                   .append(" is ").append((int) stuff.getMax())
                   .append("\nThe minimum grade for class #").append(cnt + ":")
                   .append(" is ").append((int) stuff.getMin());
           System.out.println(answer);
           cnt++;
           //Repetition question
           System.out.println("\n\nDo you wish to continue (Y/N) - ");
           yorn = scanner.next();
       }
   }
}

---------------------------------------------------------------------------

Output

Enter number of students: 2
Enter grades per each student: 2


Plz enter the grades for student #1:
78
89
The average grade for student #1: is 83.5
The maximum grade for student #1: is 89
The minimum grade for student $1: is 78


Plz enter the grades for student #2:
85
84
The average grade for student #2: is 84.5
The maximum grade for student #2: is 85
The minimum grade for student $2: is 84


The average grade for class #1: is 84.0
The maximum grade for class #1: is 89
The minimum grade for class #1: is 78


Do you wish to continue (Y/N) -
y
Enter number of students: 2
Enter grades per each student: 2


Plz enter the grades for student #1:
89
81
The average grade for student #1: is 85.0
The maximum grade for student #1: is 89
The minimum grade for student $1: is 81


Plz enter the grades for student #2:
68
75
The average grade for student #2: is 71.5
The maximum grade for student #2: is 75
The minimum grade for student $2: is 68


The average grade for class #2: is 78.3
The maximum grade for class #2: is 89
The minimum grade for class #2: is 68


Do you wish to continue (Y/N) -
n


---------------------------------------------------------

Note:-

I assume you are expecting this way


Related Solutions

1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
Write a program that prompts the user to enter a number within the range of 1...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they...
Write a program that prompts the user to enter a number within the range of 1...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they...
C Program 1. Write a program that prompts the user to enter 3 integers between 1...
C Program 1. Write a program that prompts the user to enter 3 integers between 1 and 100 from the keyboard in function main and then calls a function to find the average of the three numbers. The function should return the average as a floating point number. Print the average from main.The function header line will look something like this:float average(int n1, int n2, int n3) STOP! Get this part working before going to part 2. 2. Create a...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT