Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

Stumped.....

Thread view: 
Starshine Moonbeam - 20 Sep 2004 04:31 GMT
I'm not seeing what I'm doing wrong (or multiple things, probably). You
should be able to call a method for an object by object.method(); but
it's not giving me that. I'm getting method(double) cannot be applied to
(). I'm missing something fundamental. I have examples and they work so
the problem's me. I need to know the why of what's going wrong. The book
and the class lectures on the topic are a little....thin.

Here's what I have so far. I got the PayCheck class to compile but
TestPayCheck won't. I keep running into the method error message or this
really cute one, non-static method display() cannot be referenced from a
static context. Huh? I haven't declared anything static. Great, except I
haven't declared anything static.

Here's the code, please keep the heckling to a minimum.

/**
* PayCheck.java Chapter 4.
*
*@author    (Munged - I didn't write it. It's a class exercise)
*@since     January 28, 2004
*@re-created   September 17, 2004
*/
public class PayCheck {
   
   public double workHours;
   private double grossPay;
   public double netPay;
   public double withHoldingRate;
   public double hourlyRate;
   
   // instance variables

   /**
    * Constructor for the Pay object
    *
    *@param workHours      
    *@param hourlyRate              
    *@param withHoldingRate            
    *@since
    */
   public PayCheck(double workHours,
       double withHoldingRate,
       double hourlyRate) {
       
        //calls setWorkHours, setWithHoldingRate and setHourlyRate
           
       setWorkHours(workHours);
       
       //reserves the variable for whatever you use for the next class.
       
       setWithHoldingRate(withHoldingRate);
       setHourlyRate(hourlyRate);
       netPay = (workHours * hourlyRate) - (workHours * hourlyRate *
withHoldingRate);
       
   }
       
       // needed to put netPay up here in addition to putting it in the
       // constructor() obj
 

   /** call Constructor for the PayCheck object using no arguments */
   
   public PayCheck() {}
       
   /**
    * Sets the workHours attribute of the PayCheck object
    *
    *@param workHours  The new workHours value
    */
   public void setWorkHours(double workHours) {
       
       this.workHours = workHours;
       
   }
   
   /**
    * Sets the withHoldingRate attribute
    *
    *@param withHolding - the new withHoldingRate value
    */
   
    public void setWithHoldingRate(double withHoldingRate) {
        this.withHoldingRate = withHoldingRate;
    }
   
    /**
     * Sets the hourlyRate attribute
     *
     * @param hourlyRate - the new hourlyRate value
     */
   
    public void setHourlyRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

   /**
    * Sets the netPay variable
    *
    *@since
    */
   public void setNetPay(double workHours, double hourlyRate, double
withHoldingRate) {
     
       netPay = (workHours * hourlyRate) - (workHours * hourlyRate *
withHoldingRate);

   }
    public double getNetPay(double netPay) {
       
        return netPay;
    }

   /**
    * Description of the Method
    *
    *@since
    */
   public void display() {
       System.out.println("The net pay " + "for " + workHours + " hours
worked " +
       " at a rate of " + hourlyRate + " is " + netPay + ", assuming a
withholding rate of " +
       withHoldingRate);
       
   }
}

/**
* This class tests the Testpaycheck class
*
*@author    (Munged)
*@since     Sept 18, 2004
*@created  
*/
public class TestPayCheck {

   // Declare instance variables for the class TestPayCheck.
   
   private double check1;
   private double check2;

      // construction method
   public void processPayChecks() {
       
       
       
       //instantiate objects
       // Class (name) = new Class();
       
       
       PayCheck check1 = new PayCheck(40.0, 4.65, .15);
       //creates object by sending the three arguments.
       //workHours, hourlyRate, withHoldingRate
       
       PayCheck check2 = new PayCheck();
       //creates object by sending NO arguments.

       check2.setWorkHours(35);
       check2.setHourlyRate(10.50);
       check2.setWithHoldingRate(.17);
       
       check1.getNetPay();
       
       
   }
         

}

Signature

Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30

no spam - 20 Sep 2004 05:01 GMT
The problem is in PayCheck.java
the method getNetPay should not take an argument.

so change
public double getNetPay(double netPay) {
    //You are simply returning the value passed in
    // I assume you do not want to do this
    return netPay;
}

to

public double getNetPay() {
    return netPay;
}

that should solve your compiler problem..

> I'm not seeing what I'm doing wrong (or multiple things, probably). You
> should be able to call a method for an object by object.method(); but
[quoted text clipped - 165 lines]
>
>  }
Starshine Moonbeam - 20 Sep 2004 05:03 GMT
> The problem is in PayCheck.java
> the method getNetPay should not take an argument.
[quoted text clipped - 13 lines]
>
> that should solve your compiler problem..

Sure did. Thanks.
d'oh.

Signature

Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30

Andrew Thompson - 20 Sep 2004 05:34 GMT
> Here's the code, please keep the heckling to a minimum.

What about the guffaws?   ;-)

I have replaced oyu rcode with my variant
that has some changes and comments..
<sscce>
/**
* PayCheck.java Chapter 4.
*
*@author    (Munged - I didn't write it. It's a class exercise)
*@since     January 28, 2004
*@re-created   September 17, 2004
*/
public class PayCheck {

   public double workHours;
   private double grossPay;
   // can be calculated from other values
   //public double netPay;
   public double withHoldingRate;
   public double hourlyRate;

  // this constructor's documentation does not match the
  // parameter order!!
   /**
    * Constructor for the Pay object
    *
    *@param workHours
    *@param hourlyRate
    *@param withHoldingRate
    *@since
    */
   public PayCheck(double workHours,
       double withHoldingRate,
       double hourlyRate) {

        //calls setWorkHours, setWithHoldingRate and setHourlyRate

       setWorkHours(workHours);

       //reserves the variable for whatever you use for the next class.

       setWithHoldingRate(withHoldingRate);
       setHourlyRate(hourlyRate);

   }

// note how the order of the parameters chages
// between the constructor (hours, withhold, rate), and
// the netPay method (hours, rate, withhold).
// if you intend to use both (the second method is probably
// unnecessary). you sohuld standardise it, and also consider
// providing 'hourlyrate'
   /**
    * Sets the netPay variable
    *
    *@since
    */
   public void setNetPay(double workHours, double hourlyRate, double
withHoldingRate) {
     this.workHours = workHours;
     this.hourlyRate = hourlyRate;
     this.withHoldingRate = withHoldingRate;
   }

   /** call Constructor for the PayCheck object using no arguments */

   public PayCheck() {}

   /**
    * Sets the workHours attribute of the PayCheck object
    *
    *@param workHours  The new workHours value
    */
   public void setWorkHours(double workHours) {

       this.workHours = workHours;

   }

   /**
    * Sets the withHoldingRate attribute
    *
    *@param withHolding - the new withHoldingRate value
    */

    public void setWithHoldingRate(double withHoldingRate) {
        this.withHoldingRate = withHoldingRate;
    }

    /**
     * Sets the hourlyRate attribute
     *
     * @param hourlyRate - the new hourlyRate value
     */

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

    public double getNetPay(double netPay) {

        return netPay;
    }

    public double getNetPay() {

       return (workHours * hourlyRate) - (workHours * hourlyRate *
withHoldingRate);
}

   /**
    * Description of the Method
    *
    *@since
    */
   public void display() {
       System.out.println("The net pay " + "for " + workHours +
       " hours worked " + " at a rate of " + hourlyRate + " is " +
       getNetPay() + ", assuming a withholding rate of " +
       withHoldingRate);
   }

   public static void main(String[] args) {
       //creates object by sending the three arguments.
       //workHours, hourlyRate, withHoldingRate
       PayCheck check1 = new PayCheck(40.0, 0.15, 56);
       check1.display();
 
     // this is quite awkward to use..
       check1.setNetPay(40, 56, 0.18);
       check1.display();

       //creates object by sending NO arguments.
       PayCheck check2 = new PayCheck();

       check2.setWorkHours(35);
       check2.setHourlyRate(10.50);
       check2.setWithHoldingRate(.17);

       check2.display();
  }
}
<sscce>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Andrew Thompson - 20 Sep 2004 05:37 GMT
> // note how the order of the parameters chages
> // between the constructor (hours, withhold, rate), and
> // the netPay method (hours, rate, withhold).
> // if you intend to use both (the second method is probably
> // unnecessary). you sohuld standardise it, and also consider
> // providing 'hourlyrate' ..

// ..as an integer value in cents, which is usually
// recommended for monetary values.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Starshine Moonbeam - 20 Sep 2004 05:46 GMT
> > Here's the code, please keep the heckling to a minimum.
>
> What about the guffaws?   ;-)

That's cool. I can laugh. Now.

> I have replaced oyu rcode with my variant
> that has some changes and comments..
[quoted text clipped - 11 lines]
>     private double grossPay;
>     // can be calculated from other values

The assignment doesn't call for using it in any way. I don't know why
it's here. We have to take incomplete code and fix and make it do what
we wanted. I can't use your code either, so I'm not looking at it. I'll
take a look after I hand the lab in.

I'm getting addicted to this sh.t. It's tWisTed. I apparently don't mind
sitting in front of my computer for hours at a time with something
that'll twist my brain.

Signature

Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.