Design an implement a class called Course that represents a course taken at TCNJ
ID: 3784357 • Letter: D
Question
Design an implement a class called Course that represents a course taken at TCNJ. A course object should keep track of up to seven students (use only an array to store the students), as represented by the Student class summarized below Course Class The constructor of the Course class (only accepts the name of the course The Course object should keep track of how many valid students have been added to the course by using an instance variable of the integer type called number Provide a method called addstudent that accepts one Student reference variable and adds the student to the course. When added to a course, the student object is added to one of the positions of the student array. This is the only acceptable way to add students to a course Provide a method called roll that prints (to the screen) a listing of all of students in the course Provide a method called average that computes and returns the average of all students' test score averages Student Class The Student class should contain a single constructor, which accepts two String objects as its parameters (first and last names of the student you are creating) Each Student object will store no more than five academic scores (floating point values between 0.0- 100.0 inclusive) Provide the getScore and setScore methods which will obtain or set the score for a particular quiz (quizzes are numbered 1-5 inclusive). Both methods will need to be passed (at least a quiz number to operate properly. Only permit valid quiz scores to be set by the setScore method. Invalid scores should he discarded with a warning mes SSExplanation / Answer
package com.chegg.resources;
import java.util.Scanner;
public class Course {
public String courseName;
public int number;
Student[] students;
Scanner scanner = new Scanner(System.in);
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudents() {
System.out
.println("how many no of students you want to add for this particular course");
int count = scanner.nextInt();
students = new Student[count];
for (int i = 0; i < count; i++) {
System.out.println("enter the first name");
String fname = scanner.next();
System.out.println("enter the last name");
String lname = scanner.next();
Student s = new Student(fname, lname);
s.store();
s.toStr();
s.calculatedAverage();
students[i] = s;
}
}
public void roll() {
int count = 0;
for (Student stud : students) {
count++;
}
System.out.println("the no of students in this " + courseName
+ " course is " + count);
}
}
-----------------------------------------------------------------------------------------------------------------------------------
package com.chegg.resources;
import java.util.Scanner;
public class Student {
public String firstName;
public String lastName;
public float[] scores;
Scanner scanner = new Scanner(System.in);
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void store() {
System.out.println("enter only five scores");
scores = new float[5];
int count = 0;
for (int i = 0; i < 5; i++) {
float score = 0;
++count;
if (count <= 5) {
System.out.println("enter the score " + count);
score = scanner.nextFloat();
if (score >= 0.0 && score <= 100.0)
scores[i] = score;
else {
System.out.println("you entered score is invalid");
scores[i] = 0;
}
} else
System.out.println(" max is five only");
}
}
public void toStr() {
System.out.println("firstname" + " " + firstName + " " + "lastname"
+ " " + lastName + " " + "the scores are");
int count = 0;
for (float score : scores) {
count++;
System.out.println("the score of " + count + " is " + score);
}
}
public void calculatedAverage() {
float sum = 0.0f;
for (float score : scores) {
sum = sum + score;
}
float avg = sum / 5;
System.out.println("average of a student is " + avg);
}
/*
* public static void main(String[] args) {
*
* Student s=new Student("koti","Bommisetty"); s.store(); s.toStr();
* s.calculatedAverage(); }
*/
}
--------------------------------------------------------------------------------------------------------------
package com.chegg.resources;
public class DriverClass {
public static void main(String[] args) {
Course course = new Course("csc");
course.addStudents();
course.roll();
}
}
------------------------------------------------------------------------------------------------------------------------------------
output
how many no of students you want to add for this particular course
2
enter the first name
david
enter the last name
s
enter only five scores
enter the score 1
25
enter the score 2
36
enter the score 3
14
enter the score 4
25
enter the score 5
36
firstname david lastname s
the scores are
the score of 1 is 25.0
the score of 2 is 36.0
the score of 3 is 14.0
the score of 4 is 25.0
the score of 5 is 36.0
average of a student is 27.2
enter the first name
raju
enter the last name
m
enter only five scores
enter the score 1
36
enter the score 2
58
enter the score 3
96
enter the score 4
101.2
you entered score is invalid
enter the score 5
36
firstname raju lastname m
the scores are
the score of 1 is 36.0
the score of 2 is 58.0
the score of 3 is 96.0
the score of 4 is 0.0
the score of 5 is 36.0
average of a student is 45.2
the no of students in this csc is 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.