Question

In: Computer Science

In Java design a class named Course to represent a course. The class contains: Private data...

In Java design a class named Course to represent a course. The class contains:

Private data fields:

• courseName of type String

• courseNumber of type String

• section of type int

• numberStudents of type int

• finalGrades array of 100 double numbers

A constructor that creates a course with the specified name, number, section , and number of students.

A method setGrades() that will prompt the user to the enter the grades into double [] finalGrades.

int numberStudents, will control how many cells will be used.

A method to find and print the maximum grade.

A method to find and print the average grade.

Implement the class.

Write a test program that

Creates the course course1/CS155 with the values (CS, 155, 01, 20)

Uses setGrades to read 20 grades for the course course1/CS155. Note numberStudents or

finalGrades.length can be used in your for loop holding the value of 20.

print the maximum grade for course1/CS155

print the average grade for course1/CS155

Creates the course course2/CS300 with the values (CS, 300, 03, 30).

Uses setGrades to read 30 grades for the course course2/CS300. Note numberStudents or finalGrades.length can be used in your for loop holding the value of 20.

print the maximum grade for course2/CS300

print the average grade for course2/CS300

Solutions

Expert Solution

Solution:

import java.util.*;

public class Course //class definition
{
private String courseName; //variable declaration
private String courseNumber;
private int section;
private int numberStudents;
double[] finalGrades=new double[100];


Course(String courseName,String courseNumber,int section,int numberStudents){ //constructor of class
this.courseName=courseName;
this.courseNumber=courseNumber; //store the data in respective variables
this.section=section;
this.numberStudents=numberStudents;
}

void setGrades(){ //method to read grades from user
System.out.println("Enter grades");
Scanner input=new Scanner(System.in); //Use Scanner class to read data
for(int i=0;i< this.numberStudents;i++){
finalGrades[i]=input.nextDouble(); //Read and store grades in array
}

}
void max(){ //method to find maximum grade
double max=finalGrades[0];
for(int i=1;i< this.numberStudents;i++){ //For every grade in array
if(max<finalGrades[i]){ //compare with max
max=finalGrades[i]; //If max is less than any element, update max with it
}
}
System.out.print("The maximum grade is ");
System.out.println(max); //print maximum grade
}
void average(){ //method to find average grade
double sum=0.0;
for(int i=0;i< this.numberStudents;i++){ //For every grade in array
sum+=finalGrades[i]; //add it to sum
}
sum=sum/numberStudents; //find average grades
System.out.print("The average grade is ");
System.out.println(sum); //print average
}
public static void main(String[] args) {
Course course1=new Course("CS","155",01,20); //create object
course1.setGrades(); //call method to read grades
course1.max(); //call method to find maximum grade
course1.average();  //call method to find average grade

Course course2=new Course("CS","300",03,30);   //create object
course2.setGrades();  //call method to read grades
course2.max();  //call method to find maximum grade
course2.average(); //call method to find average grade
}

}

Screenshots:

The screenshots are attached below for reference along with output. Please follow them.


Related Solutions

Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
Design a class named Account that contains: ■ A private int data field named id for...
Design a class named Account that contains: ■ A private int data field named id for the account (default 0). ■ A private double data field named balance for the account (default 0). ■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. ■ A private Date data field named dateCreated that stores the date when the account was created. ■ A no-arg constructor that creates...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT