Problems with understanding inheritance/method overiding

• June 25th, 2013, 10:18 PM
Jackbruns28
Problems with understanding inheritance/method overiding
Hey. Newby here. Was wondering if anyone has some pointers on solving my programming dilemna. Here is the assignment.

Quote:

Inheritance

Method Overriding

DESCRIPTION

Write a program that will compute and display the final grades for a list of students. A student’s final grade is determined from the scores obtained in the class exams. Each student in the class may be given a different number of exams as determined by the instructor. The final grade is determined by the average of the scores in all the exams given.

This program is an enhancement of a previous exercise that did the same. However, this exercise will additionally provide for assigning a CR or NCR (Credit or No Credit) grade.

For each student, the program will input student id, name, the number of exams given and scores in each of the exams.

Additionally, it will input the grade type: “Letter” or “Credit”. The grade type of “Letter” will indicate that student will receive a grade of “A”, “B”, “C”, “D”, or “F”. The grade type of “Credit” will indicate that the student will receive a grade of “CR” or “NCR”.

The scores in each of the exams will vary from 0 to 100. Each exam will carry the same weight. The final grade will be computed by taking the average of the scores in all the exams given (i.e. by adding the scores in all the exams and dividing it with the number of exams given). The final grade will be assigned depending upon the percentage of total points in the exams given as follows:

A – 90 to 100%

B – 80 to 89%

C – 70 to 79%

D – 60 to 69%

F – 0 to 59%

If the student qualifies for a C or better grade, the student will be assigned a CR (Credit) grade. Otherwise, the student will be assigned an NCR (No Credit) grade.

INSTRUCTIONS

Use a String variable for keeping final grade.

Use a String variable for keeping grade type.

Comparing Strings

For comparing two Strings for equality, use one of the following two String methods:

equals

equalsIgnoreCase

IMPLEMENTATION

For this assignment create the following classes.

Student class

Create a class Student that provides the following:

· Private instance variables for holding student id (int), name (String), exam scores (an int array).

· A public constructor with parameters for initializing student id (int), name (String) and exam scores. The exam scores are passed as an array of int.

· A public method for returning the final grade.

· Accessor (getter) methods for getting student id, name, exam scores .

Instead of rewriting this class, you may copy the contents of the same class from a previous exercise.

StudentExt class.

Create a class StudentExt that extends the class Student above. This class will extend the class Student in order to provide additional functionality for assigning a CR or NCR grade.

The class will provide the following:

· A private String instance variable gradeType for indicating the grade type (Letter or Credit). The variable gradeType will record “Letter” for a letter grade type and “Credit” for Credit/No Credit grade type.

· A public constructor for initializing student id (int), name (String), exam scores (an int array) and grade type (String). This constructor will call the parent class constructor for initializing student id, name and exam scores. It will initialize the grade type itself directly.

· A public method for returning the final grade. This method will over ride the parent class method used for computing the grade. This method will have the same name and the same parameter profile as the parent class method. This new method will call the parent class method (of the same name) to compute the student letter grade. After return from the parent class method, in the case of grade type “Credit”, it will change the letter grade returned by the parent class method to either CR or NCR.

TestStudentExt class

Write a class TestStudentExt containing the main method. The method main will do the following:

· Prompt the user to enter the total number of students (say n).

· Create an array of n references (for holding n StudentExt object references). (The StudentExt objects will be created in the next step).

· Create n StudentExt objects. Do this by setting up an n count loop. In each pass through the loop, do the following:

a. Ask the user to enter data for one student.

b. Create a StudentExt object and initialize it with data provided by the user. Store the object reference in the array of StudentExt references created above.

· Display the student results by grade type. First display students receiving grade A, followed by those receiving B, followed by those receiving C, followed by those receiving D, followed by those receiving F, followed by those receiving Cr, followed by those receiving NCR.

You may do this in a number of ways including the following:

Method I.

Create seven empty Strings, outA, outB, outC, outD, outF, outCr, outNcr. Use the String outA for storing output relating to A students; use String outB for storing output relating to B students; etc. Use the object accessor methods to access object values as needed. At the end, prepare a String out that concatenates all the above seven Strings. Then display the String out.

Method II.

Create a String array out of 7 Strings. Use out [0] for accumulating output relating to A students, out[1] for B students etc. Write a static method displayRestult to display output. Call the static method displayResult and pass it the array out.

displayResult

For method II above, write a static method displayResult. This method will receive a String array and display the contents of all elements of the String array one by one. Here is a proposed header for the method:

public static void displayResult (String [ ] s)

{

}

It is suggested that you use method II.

You may use the class TestStudent from a previous exercise and modify it to create this class TestStudentExt.

DISCUSSSION

Method Over-riding

When a child class defines a method having the same name and profile (parameter type list) as a method in the parent (ancestor) class. The child’s method is said to over ride the parent method.

When a user creates an object of the child class and calls it’s (over riding) method, it is the child class’s over riding method that gets called (and not the parent’s over ridden method).

However, the child’s over riding method may internally call the parent’s over ridden method (when it’s protected and public) using the word super and a dot in front of its name.

Sample Code

The method findGrade below over rides the parent method findGrade. Internally, it also calls the parent method findGrade for finding the student’s letter grade. On return from the parent method, in the case of a gradeType of “CR”, it reassigns the grade to be “CR” or “NCR” depending upon the student’s letter grade as described below:

For a gradeType of “Credit”, if the student’s letter grade is “A”, “B”, or “C”, it reassigns the grade to be “CR” and otherwise (for a letter grade of “D” or “F”) it reassigns it to be “NCR”.

{

{

else

}

}

When two methods have the same name but different profiles (parameter type list), the two methods are said to be overloaded.

The compiler treats overloaded methods as distinctly separate methods. From a method call to an overloaded method, the compiler can distinguish without ambiguity which of the overloaded method is being called by observing the types of the parameters being passed to the called method.

Converting a String to a boolean.

This is not needed to do the assignment but is presented here for discussion.

If a String is assigned a value “true” or “false”, it can be converted into a boolean true or false as shown in the code below:

String s = “true”;

boolean b = Boolean.valueOf (s.trim() ).booleanValue ( );

The following method does NOT work. Don’t use it.

String s = “true”;

boolean b = Boolean.getBoolean ( );

Converting a boolean to a String

This is not needed to do the assignment but is presented here for discussion.

//The code below converts a a boolean value true or false

//to a string value “true” of “false”

boolean b = true;

String s = “” + b;

TESTING

Input

Use the following test data for input.

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, 3, 70,77,72, Letter

9, Jackie Chan, 3, 85,89,84, Letter

10, Susan Wu, 3, 80,88,84, Letter

11, Bruce Lee, 4, 74, 79, 72, 75, Credit

12, Chuck Norris, 5, 63, 64, 62, 60, 68, Credit

13, Jet Li, 3, 85, 83, 89, Credit

14, Jessica Lauser, 3, 82, 84, 87, Letter

15, Mahnoosh Nik-Ahd, 2, 98, 99, Letter

In the above data, for the first entry, id is 1, name is John Adam, exam scores are 93, 91, 100 and grade type is letter grade.

Output

The output may appear as below:

5 Mary Russell (A)

15 Mahnoosh Nik-Ahd (A)

6 Andy Wong (B)

9 Jackie Chan (B)

10 Susan Wu (B)

14 Jessica Lauser (B)

7 Jay Russell (C)

8 Jimmie Wong (C)

2 Raymond Woo (D)

4 Ray Bartlett (D)

3 Rick Smith (F)

11 Bruce Lee (CR)

13 Jet Li (CR)

12 Chuck Norris (NCR)

SUBMIT

Submit a printed copy of the source code and final output only.

Here is my code below

Code :

```//TestfinalGradeExt.java   import java.util.StringTokenizer;   import javax.swing.JOptionPane;     public class TestfinalGradeExt {     public static void main(String[] args) {       String in,out; int id, examCount,stCount; double [] examScores; String name,grade; in = JOptionPane.showInputDialog("Enter student count"); stCount = Integer.parseInt(in); //Create an array of student references finalGradeExt [] ste = new finalGradeExt[stCount]; //populate the array references String delim = ","; String token; for(int i=0;i<ste.length;i++){   in = JOptionPane.showInputDialog("Enter one student data");   StringTokenizer stk= new StringTokenizer(in,delim); token = stk.nextToken(); token = token.trim(); token = stk.nextToken().trim();   id = Integer.parseInt(token); name = stk.nextToken().trim(); token = stk.nextToken().trim(); examCount = Integer.parseInt(token); //create an exam array   examScores = new double [examCount]; //populate the exam array for (int j=0;j<examScores.length;j++){ token = stk.nextToken().trim(); examScores[j]=Integer.parseInt(token);   id = Integer.parseInt(in); name = JOptionPane.showInputDialog("Enter name"); in = JOptionPane.showInputDialog("Enter exam count"); examCount = Integer.parseInt(in); examScores = new double[examCount]; //populate examScores array   in=JOptionPane.showInputDialog("Enter exam score");   examScores[j] = Double.parseDouble(in); } //Create one student object ste[i] = new finalGradeExt (int id, String n,double [] s, String gt);   } String gradeType = stk.nextToken().trim(); ste [i] = new finalGradeExt (id, name, examScores,gradeType);     } //call findGrade on each student String outA="",outB="",outC="",outD="",outF="",outAll="", outCR="", outNCR=""; for(int i=0;i<ste.length;i++){ grade = ste[i].findGrade();   } if (grade.equalsIgnoreCase("A")){ outA=outA + ste[i].getId()+" " + ste[i].getName() + "("+grade+")"+"\n"; } else if (grade.equalsIgnoreCase("B")){ outB=outB + ste[i].getId()+" " + ste[i].getName() + "("+grade+")"+"\n"; } else if (grade.equalsIgnoreCase("C")){ outC=outC + ste[i].getId()+" " + ste[i].getName() + "("+grade+")"+"\n"; } else if (grade.equalsIgnoreCase("D")){ outD=outD + ste[i].getId()+" " + ste[i].getName() + "("+grade+")"+"\n"; }   else outF=outF + ste[i].getId()+" " + ste[i].getName() + "("+grade+")" +"\n";   } {   outAll= outA+outB+outC+outD+outF+outCR+outNCR; JOptionPane.showMessageDialog(null, outAll); System.exit(0); } { } }```

followed by the blue print code below

Code :

```public class finalGradeExt extends finalGrade{   private String gradeType; public finalGradeExt (int id, String n,double [] s, String gt){ super (id,n,s); gradeType=gt;     } public String getGradeType(){ return gradeType; } public String findGrade(){ String grade=super.findGrade(); if (gradeType.equalsIgnoreCase("Credit")){ if (grade.equalsIgnoreCase("A") || grade.equalsIgnoreCase("B") || grade.equalsIgnoreCase("C")){ grade = "CR"; } else{ grade = "NCR"; } } return grade; }   }```

Any pointers or help on what I may be doing wrong. I know I'm over my head in this, as my community college never offered a beginning java programming class, so I'm heavy into objects and arrays without knowing fully the basics!!!
• June 26th, 2013, 01:26 AM
Junky
Re: Problems with understanding inheritance/method overiding
Thanks for posting your extremely long assignment. Come back when you have a specific question.
• June 26th, 2013, 07:43 PM
Jackbruns28
Re: Problems with understanding inheritance/method overiding
Thanks for the help. You obviously were privileged enough to have an instructor who took the time to teach you the basics and not feed you to the wolves.
• June 26th, 2013, 08:50 PM
Junky
Re: Problems with understanding inheritance/method overiding
My reply was a bit tongue in cheek but it was meant to get you to think about a better approach to get help.

You posted an extremely long assignment description which nobody will read.
You posted some code and ask for pointers. What sort of pointers are you after? We have no idea what your problem is. Hence why I suggested you ask a specific question. "Please help me" or "My code doesn't work" are not specific. "My for loop is only executing once" is a bit more specific.