Question

In: Computer Science

I am to complete the following for Java but getting compiler errors...Implement a base class called...

I am to complete the following for Java but getting compiler errors...Implement a base class called Student that contains a name and major. Implement the subclass GraduateStudent, which adds a property called stipend.

Here is what I have for code with the compiler error following.

public class GraduateStudent extends Student {
  
   private double stipend;
  
   public GraduateStudent(String name, String major, double stipend) {
      
       super(name);
       super(major);
  
       this.stipend = stipend;
      
   }
  
   public void setStipend(double stipend) {
       this.stipend = stipend;
   }
  
   public double getStipend() {
       return stipend;
   }
}

public class Student {
  
protected String name;
protected String major;
  
public Student(String name, String major) {
this.name = name;
this.major = major;
}
  
public void setName(String name) {
this.name = name;
}
  
public String getName() {
return name;
}
  
public void setMajor(String major) {
this.major = major;
}
  
public String getMajor() {
return major;
}
}

GraduateStudent.java:7: error: constructor Student in class Student cannot be applied to given types;
                super(name);
                ^
  required: String,String
  found: String
  reason: actual and formal argument lists differ in length
GraduateStudent.java:8: error: call to super must be first statement in constructor
                super(major);
                     ^
2 errors
[ERROR] did not compile; check the compiler stack trace field for more info

PROGRAM EXECUTION STACK TRACE

[ERROR] did not compile; check the compiler stack trace field for more info

YOUR CODE'S OUTPUT

 
1 [ERROR] did not compile; check the compiler stack trace field for more info
2
3 false

Solutions

Expert Solution

  • Errors of the given program are solved in following solution.
  • Comments are made wherever changes are made.
  • Main class is used to create objects of class and to call methods.
  • Program snippet is attached below the typed code.
  • Sample output snippet is provided at the end.

Program:

class Student 
{
    protected String name;
    protected String major;

    public Student(String name, String major) 
    {
        this.name = name;
        this.major = major;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public String getName() 
    {
        return name;
    }

    public void setMajor(String major) 
    {
        this.major = major;
    }

    public String getMajor() 
    {
        return major;
    }
}

class GraduateStudent extends Student 
{

    private double stipend;

    public GraduateStudent(String name, String major, double stipend) 
    {
        /* This is the part where you made mistake */
        /* Super class constructor should be called like this */
        super(name, major);
        this.stipend = stipend;
    }

    public void setStipend(double stipend) 
    {
        this.stipend = stipend;
    }

    public double getStipend() 
    {
        return stipend;
    }
}

/* Main class for creating object of GraduateStudent class to test the code*/
public class Main 
{
    public static void main(String[] args) 
    {
        GraduateStudent g = new GraduateStudent("John", "Computer", 1500);

        // calling GraduateStudent methods
        System.out.println(g.getName());
        System.out.println(g.getMajor());
        System.out.println(g.getStipend());
    }
}

Program Snippet:

class Student 
{
    protected String name;
    protected String major;

    public Student(String name, String major) 
    {
        this.name = name;
        this.major = major;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public String getName() 
    {
        return name;
    }

    public void setMajor(String major) 
    {
        this.major = major;
    }

    public String getMajor() 
    {
        return major;
    }
}

class GraduateStudent extends Student 
{

    private double stipend;

    public GraduateStudent(String name, String major, double stipend) 
    {
        /* This is the part where you made mistake */
        /* Super class constructor should be called like this */
        super(name, major);
        this.stipend = stipend;
    }

    public void setStipend(double stipend) 
    {
        this.stipend = stipend;
    }

    public double getStipend() 
    {
        return stipend;
    }
}

/* Main class for creating object of GraduateStudent class to test the code*/
public class Main 
{
    public static void main(String[] args) 
    {
        GraduateStudent g = new GraduateStudent("John", "Computer", 1500);

        // calling GraduateStudent methods
        System.out.println(g.getName());
        System.out.println(g.getMajor());
        System.out.println(g.getStipend());
    }
}

Output:

I hope you got the provided solution.

Thank You.


Related Solutions

Hi I am getting error in implement some test case using Java. I am adding my...
Hi I am getting error in implement some test case using Java. I am adding my code and relevant files here, and the failed test. Please let me know where my problem is occuring and fix my code. Thanks! I will upvote. Implement a class to perform windowing of a Hounsfield value Implement the class described by this API. A partial implementation is provided for you in the eclipse project; however, unlike the previous class, very little work has been...
I am having trouble fixing my build errors. The compiler I am using is Visual Studio.Thanks....
I am having trouble fixing my build errors. The compiler I am using is Visual Studio.Thanks. #include <iostream> #include <string> #include <cstdlib> using namespace std; /* * structure to store employee details * employee details are stored as linked list */ struct Employee { private:    string full_name; //full name    double net_income; //income public:    struct Employee *next; //pointing to the next employee details in the list                        /*                   ...
Develop the following code for quiz (i am getting some errors)in python in such a manner...
Develop the following code for quiz (i am getting some errors)in python in such a manner that it shoulde be having extra 3 attempts(for wrong answrs) for all questions if the user entered wrong answer · for ex: If the user entered correct answer for first question #then 3 attempts will be carried to next questions. If the user entered 3 times wrong answer in 1st question itself means it sholud display as no more attempts and you got o...
I am having trouble with my assignment and getting compile errors on the following code. The...
I am having trouble with my assignment and getting compile errors on the following code. The instructions are in the initial comments. /* Chapter 5, Exercise 2 -Write a class "Plumbers" that handles emergency plumbing calls. -The company handles natural floods and burst pipes. -If the customer selects a flood, the program must prompt the user to determine the amount of damage for pricing. -Flood charging is based on the numbers of damaged rooms. 1 room costs $300.00, 2 rooms...
Using Java. The following is a constructor I need to implement but I am not sure...
Using Java. The following is a constructor I need to implement but I am not sure how. It also must pass the Junit test. Please give reasoning while answering. public class LZWDictionary { // FIELDS // map (for ease of checking existence of entries) // list (ease of recall of an entry) LinkedHashMap<String, Integer> map; List<String> list; /** * Initializes the LZWDictionary to have an initial set of entries taken from * the set of unique characters in a provided...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Implement Java source code for a class called Temperature that does the following: a. Prompts the...
Implement Java source code for a class called Temperature that does the following: a. Prompts the user to input a temperature b. Prints “Too Hot!” when the input temperature is above 75 c. Prints “Too Cold!” when the input temperature is below 40 d. Prints “Just Right!” when the input temperature is 40 to 75 e. Be precise, include comments, prologue, etc. if needed.
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting access to the vectors so that I can compare the guesses with the random numbers. I’ve been working on this for several days and don't feel any closer to a solution and could really use some help as I really don't know how to get this to work. Thank you! The assignment is: Design an ADT for a one-person guessing game that chooses 4...
This assignment is For a C++ class , I am having difficulty with getting the boolean...
This assignment is For a C++ class , I am having difficulty with getting the boolean statements the most; getting them set up properly inside the int main() area. You have been asked to define a tip-calculating function named calcTip that could be integrated into a larger piece of software designed to improve guest service at a restaurant. As a result, you must conform to the function specification used throughout the rest of the project's code: double calcTip(double checkAmount, bool...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand thanks! tree(root) node(value, leftchild,rightchild) method: insert(value)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT