Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Whats wrong with this java code? HINT: related to inheritanc


What I have tried:

<pre>import java.util.*;
import java.lang.String;

class Person {
	protected String firstName;
	protected String lastName;
	protected int idNumber;
	
	// Constructor
	Person(String firstName, String lastName, int identification){
		this.firstName = firstName;
		this.lastName = lastName;
		this.idNumber = identification;
	}
	
	// Print person data
	public void printPerson(){
		 System.out.println(
				"Name: " + lastName + ", " + firstName 
			+ 	"\nID: " + idNumber); 
	}
	 
}

class Student extends Person{
	private int[] testScores;

    
	/*	
    *   Class Constructor
    

    *   
    *   @param firstName - A string denoting the Person's first name.
    *   @param lastName - A string denoting the Person's last name.
    *   @param id - An integer denoting the Person's ID number.
    *   @param scores - An array of integers denoting the Person's test scores.
    */
    // Write your constructor here
    
    Student(String firstName, String lastName, int id, int[] testScores)
    {
        this.firstName = firstName;
	this.lastName = lastName;
	this.idNumber = id;
        this.testScores = testScores;
    }
    /*	
    *   Method Name: calculate
    *   @return A character denoting the grade.
    */
    // Write your method here
    public char calculate()
    {
        int totalMarks = 0;
        char grade;
        for(int i = 0; i<testScores.length;i++)
        {
            totalMarks = totalMarks + testScores[i];
        }
        if(totalMarks >=90 ){
            grade = 'O';
    
        }else if(totalMarks >=80 && totalMarks < 90)
        {
            grade = 'E';
        }else if(totalMarks >=70 && totalMarks < 80)
        {
            grade = 'A';
        }else if(totalMarks >=55 && totalMarks < 70)
        {
            grade = 'p';
        }else if(totalMarks >=40 && totalMarks < 55)
        {
            grade = 'D';
        }else 
        {
            grade = 'T';
        }
        return grade;
    }
}

class Solution {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String firstName = scan.next();
		String lastName = scan.next();
		int id = scan.nextInt();
		int numScores = scan.nextInt();
		int[] testScores = new int[numScores];
		for(int i = 0; i < numScores; i++){
			testScores[i] = scan.nextInt();
		}
		scan.close();
		
		Student s = new Student(firstName, lastName, id, testScores);
		s.printPerson();
		System.out.println("Grade: " + s.calculate());
	}
}
Posted
Updated 28-Apr-20 0:17am
v2
Comments
phil.o 28-Apr-20 5:14am    
We do not do your homework.

If you know enough about this to give us a hint then you know enough to answer your own question.

But, what you have actually done is post your homework or course exercise.

We do not do your homework, although we will help if we can if you get completely stuck.

Homework is set to cement the knowledge in your brain and to help your tutor understand where you need more help.

We would not be doing you any favours if we gave a solution
 
Share this answer
 
Comments
Member 14815736 28-Apr-20 5:51am    
Brother this not my homework. I am learning java, and there is a problem, which is entitled to inheritance. I checked everything that they have said about that in their tutorial, but I still couldn't figure out with my code. Here, is the link to the problem you can check for yourself.
https://www.hackerrank.com/challenges/30-inheritance/problem
The constructor for Student contains one more parameter than the constructor for Person. You need to add a call to the parent constructor with the following code:
Java
Student(String firstName, String lastName, int id, int[] testScores)
{
    super(firstName, lastName, id);  // call parent constructor


[edit]
For a detailed explanation see Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)[^].
[/edit]
 
Share this answer
 
v2
Comments
Member 14815736 28-Apr-20 6:31am    
thank you so much, brother. It worked. I can't thank you more, I spent the whole day trying to fix it. MAy God bless you.
Richard MacCutchan 28-Apr-20 6:34am    
Happy to help, but I think God should bless the people who write such good documentation. When you are learning a new language it is always best to go to the official tutorials and work through them first. Most other tutorials on the internet only give you part of the picture.
CHill60 28-Apr-20 9:36am    
5'd
Richard MacCutchan 28-Apr-20 11:19am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900