문제

I'm a fairly novice high school programmer and I'm finishing up my last projects on inheritance for school. I have a project called "Worker" which is really stumping me. I need to be able to print out everything but I can't seem to print out what I need to without either getting no print out or getting the "cannot access method within a static context error." Here is the code for the super class called worker:

package mod6;

public class Worker {

   public Worker(double wage, String name) {
       private double hours; // I GET AN ERROR HERE that says "illegal start of expression"
       private double wage;
       private int rate;
       private String name;


   public double computePay(int hours) {
       return hours * rate;
   }

   public String getName(String name) {  
       String temp[] = name.split("\\s");
       char initial = temp[0].charAt(0);
       String lname = temp[1];
       return name = (lname + ", " + initial + ".");
   }
}

EDIT: the only problem I now have is the error in line 7 of the worker class above. And here is the code for the WorkerTester class that I don't currently get a correct output from:

package mod6;

public class WorkerTester extends Worker {

  public WorkerTester(double wage, String name, int hours) {
      super(wage, name); 
  }

  public static void main(String[]args) {
  System.out.println("Type" + "\t" + "Name" + "\t" + "Wage" + "\t" + "Hours" + "\t" + "Number of Pieces" + "Income"); } //**Nothing prints out below this line**
  public void TestHourly() {
      HourlyWorker John = new HourlyWorker(9.48, "John Doe", 43);
      System.out.print("Hourly" + "\t" + getName("John Doe")  + "\t" + "9.48" + "\t" + "43" + "\t" + "n/a" + "\t" + John.Wage());
  }
  public void TestSalaried() {
      SalariedWorker Karen = new SalariedWorker(25.79, "Karen Jones", 52);
      System.out.println("Salaried" + "\t" + getName("Karen Jones")  + "\t" + "25.76" + "\t" + "52" + "\t" + "n/a" + "\t" + Karen.Wage());
  }
  public void TestPiece() {
      SalariedWorker Wesley = new SalariedWorker(5.65, "Wesley Smith", 36);
      System.out.println("Piece" + "\t" + getName("Wesley Smith")  + "\t" + "5.65" + "\t" + "36" + "\t" + "1025" + "\t" + Wesley.Wage());
  }
}

It's likely an easy fix, I just can't figure it out with my limited programming knowledge and I'm out of ideas for rearranging the code. Also, the coding could be very wrong (remember I'm new to this) so please keep that in mind. If someone could help me out in fixing this code to print out properly, that'd be great! Thanks.

Here is the SalariedWorker class for reference:

public class SalariedWorker extends Worker {

public SalariedWorker(double wage, String name, int hours) {

    super(wage, name);

}

public double Wage() {

    return computePay(40);

}

}

도움이 되었습니까?

해결책

You are declaring additional methods in your main method that you are not invoking. Remove the methods and just instantiate, initialize, and print all of your workers inside the main method.

Also, you are invoking getName without an instance of worker. You need something like this.

Worker worker = new Worker(//pass your parameter);
worker.getName();

Honestly, you are using the getter method incorrectly an appropriate getter should return the private variable or a small manipulation of it, not edit the parameter that you pass it.

This is a better getter:

public String getName() {  
   String temp[] = name.split("\\s");
   char initial = temp[0].charAt(0);
   String lname = temp[1];
   return (lname + ", " + initial + ".");
}

This way you refer to the instance variable name that is private and not the parameter "name" that you originally pass into your method

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top