In: Computer Science
Part 1
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
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
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;
}
}
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