Question

In Java 8 Write a HomeworkGrades class that stores the homework grades for 8 chapters into...

In Java 8

Write a HomeworkGrades class that stores the homework grades for 8 chapters into an array of doubles. • Write a constructor that takes an array as input and copies the contents of the array into the class’ array. • Write methods to calculate: • The average of the homework grades • The lowest grade • Write a main function that creates an array with this data: • double[ ] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 }; • Print the average for the homework and the lowest grade, by calling the methods written in the HomeworkGrades class. Average and lowest grade should be printed with 2 digits after the decimal.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

save the following code in file named Main.java

class HomeworkGrades
{
private double []grades;

public HomeworkGrades(double grades[])
{
this.grades=new double[grades.length];
for(int i=0;i<grades.length;i++)
this.grades[i]=grades[i];
}
public double getAverage()
{
double sum=0;
for(int i=0;i<grades.length;i++)
sum+=grades[i];
return sum/grades.length;
}
  
public double fildLowestGrade()
{
double min=grades[0];
for(int i=1;i<grades.length;i++)
if(min>grades[i])
min=grades[i];
return min;
}
  
}
public class Main
{
public static void main(String[] args)
{
double[ ] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90 };
HomeworkGrades homework=new HomeworkGrades(grades);
System.out.printf("Average grade is %.2f%n",homework.getAverage());
System.out.printf("Lowest grade is %.2f%n",homework.fildLowestGrade());
}
  
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Answer #2

Here is an implementation of the HomeworkGrades class that stores the homework grades for 8 chapters into an array of doubles, and provides methods to calculate the average and lowest grade:

public class HomeworkGrades {    private double[] grades;    public HomeworkGrades(double[] grades) {        this.grades = grades.clone();
    }    public double getAverage() {        double sum = 0;        for (double grade : grades) {
            sum += grade;
        }        return sum / grades.length;
    }    public double getLowestGrade() {        double min = Double.MAX_VALUE;        for (double grade : grades) {            if (grade < min) {
                min = grade;
            }
        }        return min;
    }
}

And here is a main function that creates an array with the given data and prints the average and lowest grade:

public static void main(String[] args) {    double[] grades = {98.7, 77.9, 90, 83, 67, 33, 81, 90};    HomeworkGrades hwGrades = new HomeworkGrades(grades);
    System.out.printf("Average: %.2f\n", hwGrades.getAverage());
    System.out.printf("Lowest grade: %.2f\n", hwGrades.getLowestGrade());
}

When you run the main function, it should output:

Average: 78.87Lowest grade: 33.00

Note that we use the printf method with the format string "%.2f" to print the average and lowest grade with two digits after the decimal.


answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
In Java 8 Write a HomeworkGrades class that stores the homework grades for 8 chapters into...
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...

  • In Java: Main function must be in a separate class called "IncomeDemo" Write an "Income" class...

    In Java: Main function must be in a separate class called "IncomeDemo" Write an "Income" class that stores the total monthly income for each of the 12 months into an array of doubles. The class should have methods that return the following: total income for the year average monthly income the month with the highest income the month with the lowest income Write a program that demonstrates this class. You'll need to pass a 12-element double array to the class...

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

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

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

  • using C# Write a program which calculates student grades for multiple assignments as well as the...

    using C# Write a program which calculates student grades for multiple assignments as well as the per-assignment average. The user should first input the total number of assignments. Then, the user should enter the name of a student as well as a grade for each assignment. After entering the student the user should be asked to enter "Y" if there are more students to enter or "N" if there is not ("N" by default). The user should be able to...

  • help 6. (15 points) Define class GradeStat to describe the grades of all people in a...

    help 6. (15 points) Define class GradeStat to describe the grades of all people in a group. (1) Declare data member grades as an array of integers (we can declare it as a double type, but use int type can be simpler). (2) Write a constructor taking an array of int as input parameter. Use it to initialize data member. Note that each element in the array should be a non- negative int not larger than 100, that is, an...

  • SOLVE IN PYTHON:Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes...

    SOLVE IN PYTHON:Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array....

  • Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person’s name and...

    Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person’s name and phone number. The class should have a constructor and appropriate accessor and mutator methods. Then write a program that creates at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList Copyright | Addison-Wesley | Starting Out with Java | kehindewilliams771@gmail.com | Printed from Chegg iOS App

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