1 Replies - 129 Views - Last Post: 28 August 2015 - 08:20 AM Rate Topic: -----

#1 Mike_23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-July 13

Need help with a toString method

Posted 28 August 2015 - 08:16 AM

ok it has been a long time sense i have coded and I have forgotten some basics, tried googling this with no success. I have written 90 percent of the program but I am messing up on the toString at the very bottom. I know its something simple but I cant see it.
public class PieceWorker extends Employee 
{
   /* declare instance variable wage */
   /* declare instance variable pieces */
   private double wage;
   private double pieces;

   // five-argument constructor
   public PieceWorker( String first, String last, String ssn, 
      double wagePerPiece, int piecesProduced )
   {
      /* write code to initialize a PieceWorker */
       super(first, last, ssn);
       wage = wagePerPiece;
       pieces = piecesProduced;
   } // end five-argument PieceWorker constructor

   // set wage
   /* write a set method that validates and sets the PieceWorker's wage */
   public void setpieceWorkerWage(double pay)
   {
       wage = pay;
   }

   // return wage
   /* write a get method that returns the PieceWorker's wage */
   public double getPieceWorkerWage()
   {
       return wage;
   }
   // set pieces produced
   /* write a set method that validates and sets the number of pieces produced */
   public void setPieces(double production)
   {
       pieces = production;
   }
   // return pieces produced
   /* write a get method that returns the number of pieces produced */
   public double getPieces()
   {
       return pieces;
   }

   // calculate earnings; override abstract method earnings in Employee
   public double earnings()
   {
      /* write code to return the earnings for a PieceWorker */
      return getPieces()*getPieceWorkerWage();
   } // end method earnings

   // return String representation of PieceWorker object
   public String toString()
   {
//error here when trying to call PieceWorker()
      /* write code to return a string representation of a PieceWorker */
      return String(PieceWorker() + earnings());
      
   } // end method toString
} // end class PieceWorker



any help would be greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a toString method

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1380
  • View blog
  • Posts: 3,021
  • Joined: 30-January 11

Re: Need help with a toString method

Posted 28 August 2015 - 08:20 AM

PieceWorker() tries to call the constructor which doesn't make sense in this case (and won't without the new keyword).

You don't want to create a new object in the toString method, you just want to return the properties of the current object - like you have done with calling earnings()

It will depend on what format you are wanting to be returned but getting rid of PieceWorker() + will at least compile.

Normally you would want to do something like
return "PieceWorker- pieces=" + getPieces() + ", wage=" + getPieceWorkerWage();

This post has been edited by Ryano121: 28 August 2015 - 08:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1