Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task For this lab, you will write a class called Student. It should have the following instance variables: First name, a String. Last name, a

Task

For this lab, you will write a class called Student. It should have the following instance variables:

  • First name, a String.
  • Last name, a String.
  • ID number, an int.
  • GPA, a double.
  • Number of credits, an int.

It should also have one static class variable. Remember these are shared amongst all instances of a class:

  • The next available ID, an int. Youll have to initialize this to 1 where you declare it.

It should also have the following methods:

  • A static method called getNextId() which returns the next id currently available. It then increments this field. This way, each time the method is called, a new id is generated.
  • A constructor which takes the students name fields as parameters. The names should be set from the parameters. The credits and gpa should be set to 0. The id should be set to what getNextId returns.
  • A method called addCourse which takes the number of credits of the course (an int), and the grade the student got in the course (a String).

    The method to add a course should factor the grade into their GPA. To do that, weight the students current GPA by their old number of credits, and the grade from the new course by the number of credits. For example, imagine a students current GPA is 3.2, and they have 60 credits. If they get an A- in a 3 credit course, then their new GPA is calculated as:

    3.260+3.7360+3

  • The 3.7 comes from the number equivalent to an A-. You can get the list of these values from this page.

    It should then add the courses credits into the students total. If the student has 60 credits and adds a 3 credit course, then they now have 63.

  • A method called report() that prints a report on the student. This should take no parameters and return void. The method to print a report should print all of the students details to the screen in a readable way. The format is up to you.
  • A method called canGraduate() which determines if the student can graduate. To do this, it should check that the student has at least 120 credits and has a GPA of at least 2.0. If so, they can graduate; if not, not.

Testing

You can use the StudentMain.java file to test your program.

Here is an example run:

Alice, Johnson 1 0.00 0 Bob, Williams 2 0.00 0 Claire, Smith 3 0.00 0 Alice, Johnson 1 2.28 124 Bob, Williams 2 2.36 122 Claire, Smith 3 2.16 117 Alice can graduate. Bob can graduate. Claire CAN'T graduate. 

StudentMain.java

import java.util.Random; public class StudentMain { public static void main(String args[]) { // make three generic students Student alice = new Student("Alice", "Johnson"); Student bob = new Student("Bob", "Williams"); Student claire = new Student("Claire", "Smith"); // starting report alice.report(); bob.report(); claire.report(); // we randomly pull credit numbers and grades from these // arrays - this gives us reasonable testing data int[] credits = {1, 3, 3, 3, 3, 3, 3, 3, 4, 4}; String[] grades = {"A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "F"}; Random gen = new Random(); // give them each 40 random classes for (int i = 0; i < 40; i++) { int creditIdx = gen.nextInt(credits.length); int gradeIdx = gen.nextInt(grades.length); bob.addCourse(credits[creditIdx], grades[gradeIdx]); creditIdx = gen.nextInt(credits.length); gradeIdx = gen.nextInt(grades.length); alice.addCourse(credits[creditIdx], grades[gradeIdx]); creditIdx = gen.nextInt(credits.length); gradeIdx = gen.nextInt(grades.length); claire.addCourse(credits[creditIdx], grades[gradeIdx]); } // print reports now System.out.println(); alice.report(); bob.report(); claire.report(); // see if they can graduate System.out.printf("Alice %s graduate. ", alice.canGraduate() ? "can" : "CAN'T"); System.out.printf("Bob %s graduate. ", bob.canGraduate() ? "can" : "CAN'T"); System.out.printf("Claire %s graduate. ", claire.canGraduate() ? "can" : "CAN'T"); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions