Question

In: Computer Science

Java question, not sure how to do this... You need to create a base class that...

Java question, not sure how to do this...

You need to create a base class that should have the following functionality.

- Calculate avg. Students grade. Input parameter to this method is an array that shows grades for at least ten courses.

You need to create a child class that inherits the base class. The child class should have a method to calculate max grade from 10 courses.

You need to write a demo class. The demo class should have a main method that calls child class and able to provide max and avg. Grade.

Solutions

Expert Solution

Here is the completed code for this problem. Please save the code for each class in separate files namely – GradeStats.java, GradeStatsExtended.java and Demo.java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// GradeStats.java

//super class

public class GradeStats {

      // method to calculate the average of given grades

      public double calcAverage(double grades[]) {

            double sum = 0;

            // summing up all grades in the array

            for (double d : grades) {

                  sum += d;

            }

            // finding and returning the average

            double avg = (double) sum / grades.length;

            return avg;

      }

}

// GradeStatsExtended.java

//child class extending the GradeStats class

public class GradeStatsExtended extends GradeStats {

      // method to calculate the max grade from given grades

      public double calcMaxGrade(double grades[]) {

            double max = 0;

            for (int i = 0; i < grades.length; i++) {

                  // if this is first grade or this grade > current max, updating max

                  if (i == 0 || grades[i] > max) {

                        max = grades[i];

                  }

            }

            return max;

      }

}

// Demo.java , for testing

public class Demo {

      public static void main(String[] args) {

            // creating an array containing 10 grades

            double grades[] = { 90, 70, 85, 92, 78, 88, 83, 83, 90, 79 };

            // creating an object of GradeStatsExtended (child class)

            GradeStatsExtended stats = new GradeStatsExtended();

            // displaying the average using calcAverage. super class's calcAverage

            // method will be invoked.

            System.out.println("Average grade: " + stats.calcAverage(grades));

            // displaying max grade. GradeStatsExtended's own calcMaxGrade method

            // will be invoked

            System.out.println("Max grade: " + stats.calcMaxGrade(grades));

      }

}

//OUTPUT

Average grade: 83.8

Max grade: 92.0


Related Solutions

JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to do fraction arithmetic. To get you used to the idea of unit testing, this homework does not require a main method. You can create one if you find it useful, however, we will not be grading or even looking at that code. You should be comfortable enough with the accuracy of your test cases that you do not need to use print statements or...
You need to create a Java class library to support a program to simulate a Point-...
You need to create a Java class library to support a program to simulate a Point- of-Sale (POS) system. General Requirements: You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; You should create identifiers with sensible names; You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. Logical structures...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and height with setter and getter methods. A constructor that sets the values of base and height. A method named toString() that prints the values of base and height. A method named area() that calculates and prints the area of a triangle. Draw the UML diagram for the class. Implement the class. Q2: Write a Java program that creates a two-dimensional array of type integer...
Java question: I need to fix a point class (code below) Thank you! /** * A...
Java question: I need to fix a point class (code below) Thank you! /** * A point, implemented as a location without a shape. */ public class Point extends Location { // TODO your job // HINT: use a circle with radius 0 as the shape! public Point(final int x, final int y) { super(-1, -1, null); assert x >= 0; assert y >= 0; } }
You do not need to import the Math class into your Java program to use methods...
You do not need to import the Math class into your Java program to use methods or constants in it. Group of answer choices True False
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.
1. Java Abstract Class (2 pts) () • A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.• Following is an example of abstract class:abstract class Book{String title;abstract void setTitle(String s);String getTitle(){return title;}}If you try to create an instance of this class like the following line you will get an error:Book...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Create a Java to find the GCD of two numbers. You will need to create a...
Create a Java to find the GCD of two numbers. You will need to create a new variable to reflect the algorithm (numberOne, numberTwo, GCD = 0). Requirements: 1) read in numberOne 2) read in numberTwo. 3) run the loop, If numberTwo is 0 then print out numberOne, otherwise, temp =numberOne % numberTwo, numberOne = numberTwo, numberTwo = temp.
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT