Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problems with understanding inheritance/method overiding

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Thanked 0 Times in 0 Posts

    Default 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.


    Method Overriding


    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%

    Additionally, for a grade type of Credit, the grade will be determined as follows:

    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.


    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:




    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.


    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.


    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”.

    public String findGrade ( )


    String grade = super.findGrade ( );

    if (gradeType.equalsIgnoreCase ("Credit"))


    if ( (grade.equalsIgnoreCase ("A")) ||

    (grade.equalsIgnoreCase ("B")) ||

    (grade.equalsIgnoreCase ("C")))

    grade = "CR";


    grade = "NCR";


    return grade;


    Method Overloading

    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;



    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.


    The output may appear as below:

    1 John Adam (A)

    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 a printed copy of the source code and final output only.
    Here is my code below

    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();
    						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";
    					outF=outF + ste[i].getId()+" "
    							+ ste[i].getName()
    							+ "("+grade+")" +"\n";
    			 outAll= outA+outB+outC+outD+outF+outCR+outNCR;
    				JOptionPane.showMessageDialog(null, outAll);

    followed by the blue print code below

    public class finalGradeExt extends finalGrade{
    	private String gradeType;
    	public finalGradeExt (int id, String n,double [] s, String gt){
    		super (id,n,s);
    		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";
    					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!!!

  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    My Mood
    Thanked 167 Times in 158 Posts

    Default Re: Problems with understanding inheritance/method overiding

    Thanks for posting your extremely long assignment. Come back when you have a specific question.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    My Mood
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

Similar Threads

  1. Help in understanding this recursive method
    By jameschristopher06 in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 20th, 2012, 01:23 PM
  2. Having problems understanding what is going on in this linked stack.
    By vysero in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2012, 12:08 AM
  3. Inheritance trouble understanding how to use it?
    By orbin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2012, 11:24 AM
  4. Inheritance problems
    By 93tomh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 15th, 2012, 06:01 AM
  5. Need help understanding method calls and such
    By hackman2007 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 14th, 2010, 08:18 AM