Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose that our application requires us to model students. A student has a name

ID: 3729659 • Letter: S

Question

Suppose that our application requires us to model students. A student has a name and an address. We are required to keep track of the courses taken by each student, together with the grades (between 0 and 100) for each of the courses. A student shall not take more than 30 courses for the entire program. We are required to print all course grades, and also the overall average grade.

We can design the Student class as shown in the class diagram. It contains the following members:

private instance variables name (String), address (String), numCourses (int), course (String[30]) and grades (int[30]). The numCourses keeps track of the number of courses taken by this student so far. The courses and grades are two parallel arrays, storing the courses taken (e.g., {"IM101", "IM102", "IM103"}) and their respective grades (e.g. {89, 56, 98}).

A constructor that constructs an instance with the given name and Address. It also constructs the courses and grades arrays and set the numCourses to 0.

Getters for name and address; setter for address. No setter is defined for name as it is not designed to be changed.

A toString(), which prints "name(address)".

A method addCourseGrade(course, grade), which appends the given course and grade into the courses and grades arrays, respectively; and increments numCourses.

A method printGrades(), which prints "name course1:grade1, course2:grade2,...".

A method getAverageGrade(), which returns the average grade of all the courses taken.

Explanation / Answer

public class Student {

   private String name;
   private String address;
   private int numCourses;
   private String[] course=new String[30];
   private int[] grades=new int[30];
  
   public Student(){
      
   }
   public Student(String name, String address,String[] course, int[] grades) {
       super();
       this.name = name;
       this.address = address;
       this.numCourses =0;
       this.course = course;
       this.grades = grades;
   }
   public String getAddress() {
       return address;
   }
   public void setAddress(String address) {
       this.address = address;
   }
   public String getName() {
       return name;
   }
   @Override
   public String toString() {
       return "Student [name=" + name + ", address=" + address + "]";
   }
  
  
   public void addCourseGrade(String Singlecourse, int Singlegrade){
      
       course[numCourses]=Singlecourse;
       grades[numCourses]=Singlegrade;
       numCourses++;
      
   }
  
   public void printGrades(){
       System.out.println("Name: "+name);
       for(int i=0;i<30;i++){
           if(course[i]!=null && grades[i]!=0){
               System.out.print("Course"+(i+1)+":"+course[i]+", ");
               System.out.print("Grade"+(i+1)+":"+grades[i]);
               System.out.println();
           }
       }
   }

   public double getAverageGrade(){
       double avg=0;
       int sum=0;
       int count=0;
       for(int i=0;i<30;i++){
          
           if(course[i]!=null && grades[i]!=0){
               sum+=grades[i];
               count++;
           }
       }
       avg=sum/count;
       return avg;
   }
  
   public static void main(String[] args) {
      
       Student st=new Student();
       st.addCourseGrade("java", 50);
       st.addCourseGrade("jee", 40);
       st.addCourseGrade("jdbc", 30);
       st.addCourseGrade("jsp", 60);
       st.name="tarun";
       st.setAddress("120,Laxmi chowk,pune");
       System.out.println(st);
       System.out.println(st.getAverageGrade());
       st.printGrades();
  
   }
}

/*output:-

Student [name=tarun, address=120,Laxmi chowk,pune]
45.0
Name: tarun
Course1:java   ,Grade1:50
Course2:jee   ,Grade2:40
Course3:jdbc   ,Grade3:30
Course4:jsp   ,Grade4:60


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote