0 votes
1 view
in Java by (9.2k points)

Below is the code for Java worker class: 

public class Worker

{

  private int hours;

  private double rate;

public Worker ()

{

  hours = 999;

  rate = 999;

}

public Worker (int nHours, double nRate)

{

  hours = nHours;

  rate = nRate;

}

public int getHours ()

{

  return hours;

}

public void setHours (int nHours)

{

  hours = nHours;

}

public double getRate ()

{

  return rate;

}

public void setRate (double nRate)

{

  rate = nRate;

}

public double paycheck ()

{

  return rate * hours;

}

public void raiseRate (double raiseRate)

{

  rate = raiseRate + rate;

}

Below is the code for main class: 

public class Main

{

public static void main (String[]args)

{

  Worker bob = new Worker ();

  System.out.println (bob.getHours ());

  System.out.println (bob.getRate ());

  bob.setHours (9);

  bob.setRate (7.9);

  System.out.println (bob.getHours ());

  System.out.println (bob.getRate ());

  System.out.println (bob.payCheck ());

  System.out.println (bob.raiseRate ());

}

}

When I run this code on Eclipse, it works fine. When I try it with online compiler “onlinegdp”, it doesn’t let me use multiple files. I got the following error. 

Main.java:14: error: cannot find symbol

Worker bob = new Worker ();

^

symbol: class Worker

location: class Main


 

 Can anyone tell me what I’m doing wrong here? 

1 Answer

0 votes
by (19k points)

In general, online compilers save another class file in order to reference it. 

Check out the below code snippet on onlinegdp if you don’t want to create different file: 

public class Main

{

  public static void main (String[]args)

  {

    Worker bob = new Worker ();

      System.out.println (bob.getHours ());

  }

  static public class Worker

  {

    private int hours;

    private double rate;

    public Worker ()

    {

      hours = 999;

      rate = 999;

    }

    public Worker (int nHours, double nRate)

    {

      hours = nHours;

      rate = nRate;

    }

    public int getHours ()

    {

      return hours;

    }

    public void setHours (int nHours)

    {

      hours = nHours;

    }

    public double getRate ()

    {

      return rate;

    }

    public void setRate (double nRate)

    {

      rate = nRate;

    }

    public double paycheck ()

    {

      return rate * hours;

    }

    public void raise (double raise)

    {

      rate = raise + rate;

    }

  }

}

Interested in Java? Check out this Java tutorial by Intellipaat. 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...