Question

Hey, need some help in Java. I'm stuck on one step that prevents me from doing...

Hey, need some help in Java. I'm stuck on one step that prevents me from doing the rest of the steps and need help on it. I also have a problem when I print databaseCourse and programmingCourse. instead of saying 5 and 7 respectively is shows 51 and 71.

Step 8: in CourseGrades, create a method, add, that takes two parameters, studentNum and grade, and changes the grade of student studetNum to the given grade, grade. studentNum represents the student position in the array and the method should print an error if input studentNum is negative of greater than max number of students in course.

Step 9: in the Driver, write statements using the add method to enter grades for 3 students in the databaseCourse and 4 students in the programmingCourse. Then print the course info again to make sure the number of enrollments reflects the number of grades you added in your course.

Step 10: in CourseGrades, create a method called avgGrade that calculates and returns the average course grade. Call this method from the driver to print the average grade for both the databaseCourse and programmingCourse.

Step 11: in the same class, create a method called maxGrade that returns the max grade and call this method from the driver to print the max grade for databaseCourse and programmingCourse.

Step 12: in the same class, create a method called topScorer that returns the number of the student with the max grade. the method should return the position of the max grade in the array which corresponds to the student number and not the max grade. Call the method from the driver to print the top scorer for databaseCourse and programmingCourse.

my CourseGrades class:

public class CourseGrades
{
   private String title;
   private int maxNumStudents;
   private int [] grades;
  
   public CourseGrades(String courseTitle, int maxStudents)
   {
       this.title = courseTitle;
       this.maxNumStudents = maxStudents;
       this.grades = new int[maxStudents];
       for (int i = 0; i < grades.length; i++)
           grades[i] = -1;
      
   }
  
   public int getNumEnrollments()
   {
       int count = 0;
       if (grades.length > 0)
           count++;
       return count;
      
   }
  
   public String toString()
   {
       String output = this.title;
       output += maxNumStudents;
       output += getNumEnrollments();
       return output;
   }
  
   public add (int studentNum, double grade)
   {
       if (studentNum < 0)
           System.out.println("Error");
       else if (studentNum > maxNumStudents-1)
           System.out.println("Error");
       else
           for (int i = 0; i < studentNum; i++)
               studentNum =
              
   }

   public double avgGrade()
   {
      
   }
  
  
  
  
  
}

my Driver:

public class CourseDriver
{
   public static void main (String[]args)
   {
       CourseGrades databaseCourse = new CourseGrades ("Database ", 5);
       CourseGrades programmingCourse = new CourseGrades("Programming ", 7);
       System.out.println(databaseCourse);
       System.out.println(programmingCourse);
  
       databaseCourse.add(3, 20);
       programmingCourse.add(4, 20);
       System.out.println(databaseCourse);
       System.out.println(programmingCourse);
   }
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

Reason for issue and solution for the issue:

I also have a problem when I print databaseCourse and programmingCourse. instead of saying 5 and 7 respectively is shows 51 and 71.

Reason:

You are declaring an array size of 5 for the database. So your array size is 5. Below line grades.length will be true.

So, even none of the grades are entered the count is incrementing and eventually, you could see it as 51.

if (grades.length > 0)
       count++;

Also, there is a formatting issue in your toString. You are directly appending, grade and enrollment. I have added meaningful printout statements in the toString method.


I have developed the code for all the requirements.

Please use any editor for testing the code

Feel free to comment if you have any queries. I have provided adequate comments for your understanding.

I am pasting the code and output.

Code:

--------------------------------------------------------------


public class CourseGrades {
   private String title;
   private int maxNumStudents;
   private int[] grades;

   public CourseGrades(String courseTitle, int maxStudents) {
       this.title = courseTitle;
       this.maxNumStudents = maxStudents;
       this.grades = new int[maxStudents];
       for (int i = 0; i < grades.length; i++)
           grades[i] = -1;

   }

   public int getNumEnrollments() {
       int count = 0;
       /*
       * Reason for error you are intializing array with size in constructor. So
       * grades length is always positive. So you are seeing output as 51 and 41, even
       * if you didn't add any student to the course.
       */

       // if (grades.length > 0)
       // count++;
       // see if the grades of the students are greater than -1 for all the students.
       // increment the count when it is greater than -1
       for (int i = 0; i < grades.length; i++) {
           if (grades[i] > -1)
               count++;
       }

       return count;

   }

   // added maximum number of students: string for better understanding of the
   // output.
   public String toString() {
       String output = this.title;
       output += " maximum number of students: ";
       output += maxNumStudents;
       output += " ,enrolled students : ";

       output += getNumEnrollments();
       return output;
   }

   // When we speak of position, we start with 1, but in array index starts from
   // zero.
   // grades[studentNum-1]=grade, so I am assigning grade by decreasing the
   // position by 1

   public void add(int studentNum, int grade) {

       if (studentNum < 0)
           System.out.println("Error");
       else if (studentNum > maxNumStudents)
           System.out.println("Error");
       else {
           /*
           * for (int i = 0; i < studentNum; i++) studentNum =
           */

           grades[studentNum - 1] = grade;
       }

   }

   public double avgGrade() {
       double sum = 0;
       for (int i = 0; i < grades.length; i++)
           sum = sum + grades[i];

       return sum / (grades.length);

   }

   //traverse the array and find maximum value.
   public int maxGrade() {
       //assign first element to the max variable for initialization
       int max = grades[0];
       for (int i = 0; i < grades.length; i++) {
           if (grades[i] > max) {
               max = grades[i];
           }

       }
       return max;
   }

   //get the maximum grade from maxGrade function, then iterater over array to find the
   //position of the max score.
   public int topGrade() {
       //highest grade of the array
       int max = maxGrade();
       int position = 0;
       for (int i = 0; i < grades.length; i++) {
           if (grades[i] == max) {
               position = i + 1;
           }

       }
       return position;
   }

}

-------------------------------

public class CourseDriver {
   public static void main(String[] args) {
       //create course object
       CourseGrades databaseCourse = new CourseGrades("Database ", 3);
       CourseGrades programmingCourse = new CourseGrades("Programming ", 5);
       System.out.println(databaseCourse);
       System.out.println(programmingCourse);

       databaseCourse.add(3, 20);
       databaseCourse.add(1, 30);
       databaseCourse.add(2, 40);

       System.out.println("Database course average: " + databaseCourse.avgGrade());
       System.out.println("Database maximum grade: " + databaseCourse.maxGrade());

       System.out.println("Database maximum grade position: " + databaseCourse.topGrade());

       programmingCourse.add(1, 20);
       programmingCourse.add(2, 30);
       programmingCourse.add(3, 40);
       programmingCourse.add(4, 50);
       programmingCourse.add(5, 60);
       System.out.println("Programming course average: " + programmingCourse.avgGrade());
       System.out.println("Programming maximum grade: " + programmingCourse.maxGrade());

       System.out.println("Programming maximum grade position: " + programmingCourse.topGrade());

       System.out.println(databaseCourse);
       System.out.println(programmingCourse);
   }

}
-----------------------------

Output:

Add a comment
Know the answer?
Add Answer to:
Hey, need some help in Java. I'm stuck on one step that prevents me from doing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • Thank you in advance. Create a class called Main and write the main method. I need...

    Thank you in advance. Create a class called Main and write the main method. I need help with calling the printChart method Declare a variable to hold the grade as it is read in Declare 5 variables to hold counts to count the number of As, Bs, Cs, etc declare a variable to hold the class name and ask the user to enter the name of the class whose grades are going to be entered. Write a loop that reads...

  • In this exercise, we will be refactoring a working program. Code refactoring is a process to...

    In this exercise, we will be refactoring a working program. Code refactoring is a process to improve an existing code in term of its internal structure without changing its external behavior. In this particular example, we have three classes Course, Student, Instructor in addition to Main. All classes are well documented. Read through them to understand their structure. In brief, Course models a course that has a title, max capacity an instructor and students. Instructor models a course instructor who...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Input hello, I need help completing my assignment for java,Use the following test data for input...

    Input hello, I need help completing my assignment for java,Use the following test data for input and fill in missing code, and please screenshot output. 1, John Adam, 3, 93, 91, 100, Letter 2, Raymond Woo, 3, 65, 68, 63, Letter 3, Rick Smith, 3, 50, 58, 53, Letter 4, Ray Bartlett, 3, 62, 64, 69, Letter 5, Mary Russell, 3, 93, 90, 98, Letter 6, Andy Wong, 3, 89,88,84, Letter 7, Jay Russell, 3, 71,73,78, Letter 8, Jimmie Wong,...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • I ONLY need question 2 part C AND D answered...it is in bold. Please do not...

    I ONLY need question 2 part C AND D answered...it is in bold. Please do not use hash sets or tables. In java, please implement the classes below. Each one needs to be created. Each class must be in separate file. The expected output is also in bold. I have also attached the driver for part 4 to help. 1. The Student class should be in the assignment package. a. There should be class variable for first and last names....

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT