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
  • 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...

  • 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...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • 5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....

    5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...

  • 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...

  • 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",...

  • Using Java write a program that performs the following: 1. Define a class that contains an...

    Using Java write a program that performs the following: 1. Define a class that contains an array of type int as a data member. 2. Define a constructor that creates an array of 1024 elements and assigns it to the class data member. The constructor shall populate the elements with random numbers ranging in value from 1 to 1024. 3. Define and implement a search() method that take a parameter of type int and returns the index location if the...

  • 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...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • 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 //...

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
ADVERTISEMENT