How can I fix these errors? - java

public class EmployeeTest {
public static void main(String args[]) {
Employee e = new Employee ("Jordan",70000.00);
Manager m = new Manager ("William Johnson",90000.00,"Computer Science");
Executive ex = new Executive ("GPC",120000.00,"School");
System.out.println(e);
System.out.println(m);
System.out.println(ex);
}
Here is my constructor:
public class Employee {
private String name;
private double salary;
public Employee(String name, double salary, int department) {
setName(name);
setSalary(salary);
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return this.salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void getDetails() {
System.out.println("Name:" + getName());
System.out.println("Salary:" +getSalary());
}
}
I am currently stumped with this assignment. I have all four of my classes implemented with Employee. Then Manager is inheriting from Employee while also having Executive inherit from Manager. When I go to compile it I get these 2 errors found:
What am I doing wrong?
File: C:\Users\Jordan\Downloads\EmployeeTest.java [line: 6]
Error: constructor Employee in class Employee cannot be applied to given types;
required: java.lang.String,double,int
found: java.lang.String,double
reason: actual and formal argument lists differ in length
File: C:\Users\Jordan\Downloads\Manager.java [line: 9]
Error: constructor Employee in class Employee cannot be applied to given types;
required: java.lang.String,double,int
found: java.lang.String,double
reason: actual and formal argument lists differ in length

You need to pass in one extra argument to your employee creation call like so:
Employee e = new Employee ("Jordan",70000.00,42);
You have defined the constructor as String, double, int so you have to supply all three.

Your output clearly says
Error: constructor Employee in class Employee cannot be applied to given types;
required: java.lang.String,double,int
found: java.lang.String,double
So instead of calling Employee e = new Employee ("Jordan",70000.00) constructor you need to pass three arguments to it which are String, Double and int. For example,
Employee e = new Employee ("Jordan",70000.00, 1); //I passed 1 as args for example it could be anything that you defined in your code.
Edit
There you go, you just posted your Employee class code which clearly shows you have created a constructor that takes three arguments String name, double salary, int department and it looks as if you completely forgot what you want to do with the third arguments department as I do not see it being used anywhere else in the code.

Related

How to add a user-defined data type into a set?

this is my mock test from my professor and I having trouble writing it in Java.
This is the question:
An ADT to manage a collection of students in a course is required. You
can assume that there are no more than 100 students in any course. A
student's record consists of ID (String), name (String), and GPA
(double). There is no duplication in student IDs, but it is possible
to have two or more students with the same name and/or GPA.
Create a new type StudentCollection (it is equivalent to a class in
Java). Except for the constructor, your StudentCollection type must
support the following 3 public operations (feel free to add additional
private operations as needed - but their scope have to be private)
void addStudent(Student std): add a new student std to your
collection. If there is a student having the same ID as std in your
collection already, do nothing.
Student searchByName(String name): search the student collection and
return any student whose name contains name completely (case
sensitive). Examples: "ABC" contains "ABC" completely; "ABC" contains
"A" completely; "ABC" contains "C" completely, "ABC DEF" contains "C
D" completely; "ABC" does NOT contain "CB" completely; "ABC" does NOT
contain "abc" completely. If there is more than one matching student,
your method can return any student. If there is no matching student,
return null. int rankStudent(String sID): return the rank of a student
whose ID is sID with regard to this collection. The ranking is done
using students' GPAs. A student with the highest GPA has a rank of 1.
In this example, let assume there are 4 GPA values [9.0, 8.5, 7.0,
8.5]. A student whose GPA = 9.0 has a rank of 1, a student whose GPA = 8.5 has a rank of 2 (there are 2 students who have the same rank of 2), and a student whose GPA = 7.0 has a rank of 4. If there is no
student found with the provided sID, return -1.
Create a StudentCollection object and use it in the main method
(client code). Your client code must call all the above 3 public
methods one or more times.
You are NOT allowed to use the Java Collection Framework classes for
this problem. Your code for this problem must be stored in a single
file StudentCollection.java.
The ADT I'm choosing here is Set. Since the instruction doesn't allow me to use the Java Collection Framework, I have to manually implement all of the functions.
But here is the problem:
for the first function, the question ask me to write void addStudent(Student std) which when implementing a Set ADT, I cannot pass in a user defined data type Student into the function, I have done some research and we have to pass in a Set parameter instead of a user defined data type. Here is the code for class Student:
static class Student {
private String ID;
private String name;
private double GPA;
Student(String ID, String name, double GPA) {
this.ID = ID;
this.name = name;
this.GPA = GPA;
}
}
let's say that we put in the Student class, then there have to be some getters and setters inside of the Student class. But the question limit the amount of public function to implement and all functions beside the three specify function above have to be private. How can a getter and setter be private? Is it possible?
The overall question is: How to add a user-defined data type into a set?
I'm sorry if there is any explanation of mine is not clear. Please reply to this question if you have any further question.
Here is the code that I have been working on:
import java.util.HashSet;
import java.util.Set;
public class StudentCollection {
static Set<Student> manage = new HashSet<>();
static class Student {
private String ID;
private String name;
private double GPA;
Student(String ID, String name, double GPA) {
this.ID = ID;
this.name = name;
this.GPA = GPA;
}
}
public static void addStudent(Student std) {
manage.add(std);
}
// public static Student searchByName(String name) {
//
// }
//
// public static int rankStudent(String sID) {
//
// }
public static void main(String[] args) {
Student std = new Student("s387", "nam", 3.7);
addStudent(std);
}
}
The Student class has to be public, with public getters. Otherwise, you couldn't create a Student instance to add a student.
I went ahead and coded the addStudent method. I'm leaving the rest of the code for you to finish.
You'll have to go over your class notes to verify, but this is how I would start coding the StudentCollection class. There are no static fields or methods, other than the main method.
public class StudentCollection {
public static void main(String[] args) {
StudentCollection sc = new StudentCollection();
sc.addStudent(sc.new Student("10001", "George", 9.0));
}
private int studentLength;
private Student[] students;
public StudentCollection() {
this.studentLength = 0;
this.students = new Student[100];
}
public void addStudent(Student student) {
for (int index = 0; index < studentLength; index++) {
if (student.getSID().equals(students[index].getSID())) {
return;
}
}
students[studentLength++] = student;
}
public Student searchByName(String name) {
}
public int rankStudent(String sID) {
}
public class Student {
private final double gpa;
private final String sID, name;
public Student(String sID, String name, double gpa) {
this.sID = sID;
this.name = name;
this.gpa = gpa;
}
public double getGpa() {
return gpa;
}
public String getSID() {
return sID;
}
public String getName() {
return name;
}
}
}

Constructor printing out NULL instead of object value

I created a new class "Lecturer" which extends another class "Person", i wanted to make 2 constructors for Lecturer and one would accept a name and a stipend (just a constant to say how much pay is), the other just accepts the name and uses the default stipend set in the code. i included appropriate getters and setters. I then wrote a writeOutput method to print an output similar to this
Name: (name) which gets the name and prints it
Stipend: (stipend) same process ^
heres what i have so far
Lecturer.java
public class Lecturer extends Person{
private static String name;
static double stipend;
public Lecturer(String name) {
super(name);
}
public Lecturer(String name, double stipend) {
super(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getStipend() {
return stipend;
}
public void setStipend(double stipend) {
this.stipend = stipend;
}
public static void writeOutput() {
System.out.println("Name: " + name);
System.out.println("Stipend: " + stipend);
}
}
Person.java
public class Person {
/** Every Person has a name */
private String name;
/** Person requires a name */
public Person(String n) {
this.name = n;
}
/** return this Person's name */
public String getName() {
return this.name;
}
/** Change this Person's name */
public void setName(String nn) {
this.name = nn;
}
Main file (Inheritance.java)
Lines 41-53
Lecturer l1 = new Lecturer("Zachary");
Lecturer l2 = new Lecturer("Wilhelmina", 11017.00);
l1.writeOutput();
l2.writeOutput();
pause();
l1.setName("Zack");
l1.setStipend(10800.00);
l1.writeOutput();
pause();
System.out.printf("%s's stipend is $%,4.2f.\n",
l1.getName(), l1.getStipend());
System.out.printf("%s's stipend is $%,4.2f.\n",
l2.getName(), l2.getStipend());
This is the output
Name: null
Stipend: 0.0
Name: null
Stipend: 0.0
press enter...
Name: Zack
Stipend: 10800.0
The 2nd part works as it should but the first one isnt and i tried to change the code but nothing is working properly.
In Lecturer you are declaring another name variable. This variable is separate from the name variable declared in Person. The call to the superclass constructor is setting the name variable in Person, not in Lecturer. But you don't need the second variable; remove it. You can access the name in Person via the getName method you've already declared. This means that you also don't need to re-declare getName and setName in Lecturer, so the Lecturer class can inherit them.
Also, in Lecturer, the two variables you've declared shouldn't be static. Per the above reasoning, name shouldn't even be there, but even if it should be there, it shouldn't be static. The variable stipend should be there, but it shouldn't be static. When you declare a member variable static, then there is only one variable for the entire class, no matter how many instances you create, which doesn't sound like what you want.
Your constructors should initialize stipend.
You have a static variable inside Lecturer which has the same name as the inherited one from Person and your getter is referring to that static one - are you sure you want these static variables? For completeness if you really want to keep the static one and the inherited one with the same name then change your getter to read return this.name; which will return the inherited name instance variable.... But that method can be inherited from Person class...
There are two name fields in your program , one is private static String name; in Lecturer.java and another is private String name; in person.java .
The thing is that you are just calling Lecturer javs's name field but not setting it.
Fixed the project based on rgettman answer.
Lecturer class should look like this:
public class Lecturer extends Person {
double stipend = 9144;
public Lecturer(String n) {
super(n);
}
public Lecturer(String n, double stipend) {
super(n);
this.stipend = stipend;
}
public double getStipend() {
return stipend;
}
public void setStipend(double stipend) {
this.stipend = stipend;
}
public void writeOutput() {
System.out.println("Name: " + this.getName());
System.out.println("Stipend: " + getStipend());
}
}

Class Declarations, Constructors and toString methods for all the classes

Question:
Implement a super class Person. Make two classes, Student and Instructor
that inherit from Person. A person has a name and a year of birth. A student
has major and the instructor has a salary. Write the class declarations, the
constructors, and the methods to String for all classes. Write a test program
that tests these classes and methods.
This may not be a complicated one but i'm a beginner in java. Please help me.
I'm getting the following error at the both the constructors 'student()' and 'instructor()'.
"constructor Person in class Person cannot be applied to given types;
required: String,int
found: no arguments
reason: actual and formal argument lists differ in length."
Here's my code:
package One;
class Person{
String name;
int yob;
Person(String s, int d){
name = s;
yob = d;
}
#Override
public String toString(){
return "Name: "+name+"\n Year of Birth: "+yob;
}
}
class Student extends Person{
String major;
Student(String s){
major = s;
}
#Override
public String toString(){
return "The student did his majors in "+major;
}
}
class Instructor extends Person{
int salary;
Instructor(int a){
salary = a;
}
#Override
public String toString(){
return String.format("The salary is ",salary);
}
}
public class Test {
public static void main(String[] args) {
Person p = new Person("Stephen", 1991);
System.out.println(p);
}
}
Thanks in advance.
In your code, the classes Student and Instructor derive from Person. This means that any constructor parameters needed by the base class are also needed by the derived class. When you create a Student, how will the compiler know what to put in the parameters of the Person constructor?
So the code should be like this:
// Student constructor
Student(String majorParam, String nameParam, int yobParam)
{
super(nameParam, yobParam);
this.major = majorParam;
}
...
// Similarly, Instructor constructor
Instructor(int salaryParam, String nameParam, int yobParam)
{
super(nameParam, yobParam);
salary = salaryParam;
}
Notice how we are transferring the constructor parameters required by the base class Person using the super constructor keyword. This always has to be the first line in the derived class constructor.
Then you can construct Student and Instructor as:
Student s = new Student("TheMajor", "TheName", 42);
Instructor i = new Instructor(1000, "TheName", 42);
There is no empty constructor in Person class, so you have to create that and it should work perfectly. Don't forget that super() is called by default unless explicitly called.

Constructor Chaning In Java

Java is not my strong suite, so please go easy! :)
I am trying to do construtor-chaining between below super and sub class
//SuperClass
class Furniture{
String name;
int cost;
boolean IsAvlbl;
void Furniture(String name,int cost,boolean IsAvlbl){
this.name = name;
this.cost = cost;
this.IsAvlbl = IsAvlbl;
}
}
//Sub-class
public class Table extends Furniture{
public Table(String name,int cost,boolean IsAvlbl)
{
super(name,cost,IsAvlbl);
}
public static void main(String args[])
{
Table t = new Table("dinning",2600,false);
t.runner();
}
void runner()
{
System.out.println("Name : "+this.name);
System.out.println("Cost : "+this.cost);
System.out.println("Is Avaiable : "+this.IsAvlbl);
}
}
Error popping up is :
Table.java:20: error: constructor Furniture in class Furniture cannot be applied to given types;
super(name,cost,IsAvlbl);
^ required: no arguments found: String,int,boolean
reason: actual and formal argument lists differ in length 1 error
I understand that constructor call has to be the first line and parameters have to be same....I tried doing it but error is persistent.
I would appreciate if some one can tell me why this error is popping up as i want to understand the cause of it..... try it this way kind of solution is not preferred!
Constructor will not have any return type
it should be like
Furniture(String name,int cost,boolean IsAvlbl){
this.name = name;
this.cost = cost;
this.IsAvlbl = IsAvlbl;
}
you have void keyword before constructor, which makes it a method. Since you had return type, java found only "no argument" (default) constructor in Furniture class, hence giving that compilation error.
In Java, constructor should not have return type. Remove void from Furniture constructor
Furniture(String name,int cost,boolean IsAvlbl){
this.name = name;
this.cost = cost;
this.IsAvlbl = IsAvlbl;
}
void Furniture(String name,int cost,boolean IsAvlbl){ // Method
is a method and not a constructor.
Replace it with :
Furniture(String name,int cost,boolean IsAvlbl){ // Constructor
Remember, constructors do not have return-types. Interestingly, you can define a method with the Class-Name (similar to constructors) but the only difference is that, methods will have return-types.
void Furniture() is not the constructor. It is treated as a method in your Furniture class.
public Furniture() is the proper syntax as constructors do not have any return type.
public Furniture(String name, int cost, boolean IsAvlbl) {
this.name = name;
this.cost = cost;
this.IsAvlbl = IsAvlbl;
}

Calling child methods while using polymorphism in an arraylist

I have a small project that I'm working with inheritance and polymorphism. I have an ArrayList of type Employee that contains both Hourly and Salary employee objects. I would like to be able to use a for loop to call a calcPay function in the Hourly class provided the object in the ArrayList of type Employee is an Hourly employee. The line
System.out.println("Wage: " e.calcPay());
Gives the error The method calcPay() is undefined for type employee. How do you downcast the object? I've looked in a lot of forums and I couldn't find an option that would allow me to do it inline or without writing an abstract method that I'd have to include in all of the child classes of Employee. Any insight would be much appreciated.
public class Driver {
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<Employee>();
Employee emp1 = new Hourly("Hourly Emp", "123 E Center", "555-555-5555", 00001, "123-45-6789", 12.75);
Employee emp2 = new Salary("Salary Emp", "123 E Center", "555-555-5555", 00001, "123-45-6789");
list.add(emp1);
list.add(emp2);
for(Employee e : list){
if(e instanceof Hourly)
{
System.out.println("Wage: " e.calcPay());
}
}
}
public abstract class Employee {
private String name, address, phone, ssn;
private int empNo;
Employee(String name, String address, String phone, int empNo, String ssn)
{
this.name = name;
this.address = address;
this.phone = phone;
this.empNo = empNo;
this.ssn = ssn;
}
}
public class Hourly extends Employee {
private double wage;
Hourly(String name, String address, String phone, int empNo, String ssn, double wage)
{
super(name, address, phone, empNo, ssn);
this.wage = wage;
}
public double calcPay(double hours)
{
return wage * hours;
}
}
Even though you are making sure e is of type Hourly, you still need to cast it and use Hourly type to call calcPay() because e is of type Employee and Employee is not aware of any calcPay() method because you have defined calcPay() as only Hourly class method.
if(e instanceof Hourly)
{
Hourly hourly = (Hourly)e;
System.out.println("Wage: " hourly.calcPay());
}
If you want calcPay() accessible for all Employee instances, you need to define calcPay() as abstract method in Employee class, then you can avoid casting.
Updated:
if(e instanceof Hourly)
{
System.out.println("Wage: " ((Hourly)e).calcPay());
}
If calcPay is supported for all Employees, then it should be an abstract method in Employee, which will let you call it without having to downcast at all.

Categories