Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble with MAIN please advise [duplicate]

Tags:

java

Sorry guys I am new to Java and I have an issue with my code. I have read through the threads and have seen many examples regarding this specific error (java.lang.NoSuchMethodError: main Exception in thread "main"). I just cant seem to wrap my head around where I would add (static void main(String[] args)) to the code. If you guys can point me in the right direction I would really appreciate it.

Here is what I have:

public class Employee {


String name;
String department;

double hourlyRate;

Employee(String name, String department, double hourlyRate) {
    this.name = name;
    this.department = department;
    this.hourlyRate = hourlyRate;
}

public void setDepartment(String department) {
    this.department = department;
}

public void setHourlyRate(double hourlyRate) {
    this.hourlyRate = hourlyRate;
}

public String getNameAndDepartment() {
    return name + " " + department;
}

double weeklyPay(int numOfHourWorked) {
    if (numOfHourWorked < 40) {
        return (numOfHourWorked * hourlyRate);
    } else
        return (40 * hourlyRate);

}
}

class UnionEmployee extends Employee {

double dues;

UnionEmployee(String name, String department, double hourlyRate, double dues) {
    super(name, department, hourlyRate);
    this.dues = dues;
}

public void setDues(double dues) {
    this.dues = dues;
}

double weeklyPay(int numOfHourWorked) {
    if (numOfHourWorked <= 40) {
        return (super.weeklyPay(numOfHourWorked));
    } else
return ((super.weeklyPay(40) + ((numOfHourWorked - 40) * hourlyRate * 1.5)) - dues);
}
}

class CommissionEmployee extends Employee {

double commisionRate;
double salesAmount;

CommissionEmployee(String name, String department, double hourlyRate) {
    super(name, department, hourlyRate);
}

public void setCommisionRate(double commisionRate) {
    this.commisionRate = commisionRate;
}

public void setSalesAmount(double salesAmount) {
    this.salesAmount = salesAmount;
}

double weeklyPay(int numOfHourWorked) {
    return (super.weeklyPay(numOfHourWorked) + (commisionRate * salesAmount));
}

}

class TestEmployee {
UnionEmployee uEmp = new UnionEmployee(null, null, 0, 0);
CommissionEmployee cEmp = new CommissionEmployee(null, null, 0);
Employee emp = new Employee(null, null, 0);

void display(Employee emp, int numOfHourWorked) {
    System.out.println("Name and department :" + emp.getNameAndDepartment    ());
    System.out.println("Weekly pay of employee :"
            + emp.weeklyPay(numOfHourWorked));
}

void display(UnionEmployee uEmp, CommissionEmployee cEmp,
        int numOfHourWorked) {
    System.out.println("Weekly Pay for UnionEmployee"
            + uEmp.weeklyPay(numOfHourWorked));
    System.out.println("Weekly Pay for UnionEmployee"
            + cEmp.weeklyPay(numOfHourWorked));
}

}

OK so I started by separating each class into a different file. In looking through the Java tutorials it said to add static void main(String[] args) the way the tutorial had it setup was like this:

public class Misc {
static void main(String[] args) {

//body
   }
}

So I did this:

class TestEmployee {

static void main(String[] args) {

UnionEmployee uEmp = new UnionEmployee(null, null, 0, 0);
CommissionEmployee cEmp = new CommissionEmployee(null, null, 0);
Employee emp = new Employee(null, null, 0);

void display(Employee emp, int numOfHourWorked) {
    System.out.println("Name and department :" + emp.getNameAndDepartment    ());
    System.out.println("Weekly pay of employee :"
            + emp.weeklyPay(numOfHourWorked));
}

void display(UnionEmployee uEmp, CommissionEmployee cEmp,
        int numOfHourWorked) {
    System.out.println("Weekly Pay for UnionEmployee"
            + uEmp.weeklyPay(numOfHourWorked));
    System.out.println("Weekly Pay for UnionEmployee"
            + cEmp.weeklyPay(numOfHourWorked));
}

}

}

Still get the same error : (java.lang.NoSuchMethodError: main Exception in thread "main").

OK I add public but now I get this:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

void is an invalid type for the variable display
Syntax error on token "(", ; expected
Duplicate local variable emp
Syntax error on token ",", ; expected
Syntax error on token ")", ; expected
void is an invalid type for the variable display
Syntax error on token "(", ; expected
Duplicate local variable uEmp
Syntax error on token ",", ; expected
Duplicate local variable cEmp
Syntax error on token ",", ; expected
Duplicate local variable numOfHourWorked
Syntax error on token ")", ; expected

at TestEmployee.main(TestEmployee.java:9)
like image 963
Bertswoot Avatar asked Nov 10 '13 04:11

Bertswoot


People also ask

Is it correct to say please advise or advice?

Advice is a noun, meaning “suggestions for what to do,” and advise is a verb meaning “to give advice.” Mix up your s and your c and you’ll need advice on your spelling, as well as on the question you’re actually asking. In the end, there’s nothing grammatically wrong with “please advise.” It’s just a question of usage and style.

What does “please advise should you need further assistance mean?

Still related to offering further assistance, “please advise should you need further assistance” is clearly a straightforward expression to use. This is to confirm that your account has been retrieved and reactivated successfully. Please advise should you need further assistance.

How do you interpret “Please Advise”?

The way readers interpret “please advise” depends not only on what the message is about but also on the tone of the rest of the message. In most cases, this confusion can take place when “please advise” appears as the end of an email.

Is please a substitute for Please Advise?

You might as well substitute please with an exasperated sounding for the love of God and have the same effect. I’m not saying that Please advise necessarily communicates exasperation, but I am saying that, by itself, including please doesn’t make something polite.


1 Answers

Your problem with main is that it doesn't exist, and you need to put one in your program for it to run. Put it in the main class, whichever one that is, but while it needs to be inside of the class, inside of the curly braces that define the class, you must also make sure that you don't put it inside of another method.

Above, I'd put it in TestEmployee.

I'd also take care to make sure every class above is declared public and is in its own file. So your code above which contains 4 classes, should be comprised of 4 files.


Edit
Also, be sure to declare your main method as a public method as @Aniket noted in comment below.


Edit 2

  • You're still not declaring main as a public method.
  • You have methods embedded within the main method. Remember that in Java you can't do this since all methods need to be class level. Get them out of the main method.
  • Your code indentation is horrible to say the least, and this will make it very difficult for you or us to see your coding problems. You will want to invest time and effort towards indenting your code properly. If you did this, you would see in an instant that you had methods inside of methods.
like image 56
Hovercraft Full Of Eels Avatar answered Oct 31 '22 15:10

Hovercraft Full Of Eels