Question
help
6. (15 points) Define class GradeStat to describe the grades of all people in a group. (1) Declare data member grades as an a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Hey dear student finds the solution of your query if you have any doubt feel free to ask. Thanks!!

This java class has all methods those you mentioned in problem getters and setters,constructor,calculate average and difference methods and overriding toString() and equals() and all methods calls in Main class and gives output. If any issue comment me in comment box. thanks.

Java Code:

import java.util.*;

class GradeStat//class GradeStat
{
//private datamembers
int [] grades;
double avg;
int diff;
public GradeStat(int[] arr)//constructor that takes input paratmeter and initialize array
{
this.grades=arr;
}
public void get()//getter method
{
for(int i=0;i<5;i++)
{
System.out.println(grades[i]);
}
}
public void set(int[] arr)//setter method
{
for(int i=0;i<grades.length;i++)
{
if(arr[i]>0&&arr[i]<100)
{
grades[i] = arr[i];
}
}
}
public double calcAvg()//to calculate average
{
double sum =0;
for(int i=0;i<grades.length;i++)
{
sum += grades[i];
}
avg = sum/5;
return avg;
}
//method to calculate difference
public int difference()
{
//first find max and min
int max = grades[0];
int min = grades[0];

for(int i=1;i<grades.length;i++)
{
if(grades[i]>max)
{
max =grades[i];
}
else
if(grades[i]<min)
{
min = grades[i];
}
}
diff = max-min;//calculate difference
return diff;
}
//overridibg toString()
public String toString()
{
return String.format("Average of Grades:"+avg+"\nDifference between max and min: "+diff);
}
//overriding equals() method of object class
public boolean equals(Object ob)
{

//if same objects return true otherwise false
if(ob==this)
{
return true;
}
if(!(ob instanceof GradeStat))
{
return false;
}
GradeStat g = (GradeStat) ob;
return Double.compare(avg,g.avg)==0;
}
}
class Main//main class to test above class
{
public static void main(String[]args)
{
int arr[]={80,70,90,98,60}; //array
boolean ans;
GradeStat gd = new GradeStat(arr);//create object of class passing array as input parameter
GradeStat gd1 = new GradeStat(arr); //create another object to comparison
gd.set(arr); //call setter
gd.get(); //call getter
gd.calcAvg(); //call calcAvg()
gd.difference(); //call difference()
ans = gd.equals(gd1); //call equals()
System.out.println(gd); //print gd object
System.out.println(ans); //prin comprison result
}
}

Average of Grades:79.6 Difference: 38 false

Add a comment
Know the answer?
Add Answer to:
help 6. (15 points) Define class GradeStat to describe the grades of all people in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java Write a class encapsulating the concept of student grades (50 elements) on a test,...

    Using Java Write a class encapsulating the concept of student grades (50 elements) on a test, assuming student grades are composed of a list of integers between 0 and 100. Write the following methods: A constructor with just one parameter, the number of students; all grades can be randomly generated Accessor, mutator, toString, and equals methods A method returning the highest grade A method returning the average grade A method returning the lowest grade Write a client class to test...

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

  • Arrays in Functions 2. Arrays in Functions You can use both the array index variables and...

    Arrays in Functions 2. Arrays in Functions You can use both the array index variables and the entire array itself as arguments to functions. The following program updates the elements of the array grades, one-by-one. Thus, we have used a call-by-reference-ish mechanism to update the values (although no & was used). Note that there is a major difference between the grade in the function call and the one in the function definition. In the statement get_grade (grades[i]); "grades" is the...

  • Write the definition of the class Tests such that an object of this class can store...

    Write the definition of the class Tests such that an object of this class can store a student's first name, last name, five test scores, average test score, and grade. (Use an array to store the test scores.) Add constructors and methods to manipulate data stored in an object. Among other things, your class must contain methods to calculate test averages, return test averages, calculate grades, return grades, and modify individual test scores. The method toString must return test data...

  • Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students...

    Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students in a class. The class has the following member variables: int numStudents // holds the number of students in the class double* grades // to point to a dynamically allocated array of grades The class has the following public functions: +default constructors //set numStudents = 0; and grades = nullptr; +parameterized constructors //accepts numStudents and creates an array with size = numStudents; +AcceptGrades //...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • in java plz amaining Time: 1 hour, 49 minutes, 15 seconds. Jestion Completion Status: Write a...

    in java plz amaining Time: 1 hour, 49 minutes, 15 seconds. Jestion Completion Status: Write a program that asks the user for an input file name, open, read ar number of students is not known. For each student there is a name and and display a letter grade for each student and the average test score fc • openFile: This method open and test the file, if file is not found and exit. Otherwise, return the opened file to the...

  • a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a...

    a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a statement that declares an array namedstreetAddress that contains exactly eighty elements of typechar. 2. In a single statement: declare, create and initialize an arraynamed a of ten elements of type int with the values of the elements (starting with the first) set to 10, 20,..., 100 respectively. 3. Declare an array reference variable, week, and initialize it to an array containing the strings "mon",...

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT