Question
This is my promt: PP 7.2 - Modify the Student class from the chapter 7 source files as follows. Each student object should also contain
This is my promt:
PP 7.2 - Modify the Student class from the chapter 7 source files as follows. Each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the constructor such that each test score is assumed to be initially zero. Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score. Also provide a method called getTestScore that accepts the test number and returns the appropriate score. Provide a method called average that computes and returns the average test score for this student. Modify the toString method such that the test scores and average are included in the description of the student. Modify the driver class main method of StudentBody to exercise the new Student methods.
PP 7.3 - Write a class called Course that represents a course taken at a school. Represent each student using the modified Student class from the previous programming project. Use an ArrayList in the Course to store the students taking that course. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter. Provide a method called average that computes and returns the average of all students' test score averages. Provide a method called roll that prints all students in the course. Create a driver class with a main method that creates a course, adds several students, prints a roll, and prints the overall course test average.
Course code:
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.getdetail(); s.tostring(); s.Averagecal(); students[i] = s; } } public void roll() { int count = 0; for (Student stud : students) { count++; } System.out.println("The number of students in this " + courseName + " course is " + count); } }
Student code: import java.util.Scanner; public class Student { public String firstName; public String lastName; public float[] marks; Scanner scanner = new Scanner(System.in); public Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public void getdetail() { System.out.println("enter only three marks"); marks = new float[3]; int count = 0; for (int i = 0; i < 3; i++) { float mark = 0; ++count; if (count <= 3) { System.out.println("enter score " + count); mark = scanner.nextFloat(); if (mark >= 0.0 && mark <= 100.0) marks[i] = mark; else { System.out.println("you entered mark is invalid"); marks[i] = 0; } } else System.out.println(" maximum is five only"); } } public void tostring() { System.out.println("firstname :" + "\t" + firstName + "\t" + "lastname :" + "\t" + lastName + " " + "the marks are"); int count = 0; for (float mark : marks) { count++; System.out.println("the mark of " + count + " is " + mark); } } public void Averagecal() { float sum = 0.0f; for (float mark : marks) { sum = sum + mark; } float avg = sum / 3; System.out.println("average of a student is " + avg); } }
School code(driver):
import java.util.Scanner; public class School { public static void main(String[] args) { Course course = new Course("java"); course.addStudents(); course.roll(); } }
What do I need to do to fix this problem? I have stuff missing as well, but do not know how to add it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started