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 / October 2006

Tip: Looking for answers? Try searching our database.

Can not figure out what I am doing wrong

Thread view: 
porky008 - 24 Oct 2006 23:38 GMT
I can't seem to find what is wrong with this. I know a lot is wrong
by the amounts of errors that are popping up but I can't seem to find
a way to fix them. Can some one please help with my stupid mistakes?
Thanks

import java.util.Scanner;
class Employee
{

  private double payrate;
  private double hoursworked;
  private String name = "blanknow";

public Employee ()
{
 payrate = 0.0;
 hours = 0.0;
}

public void setpayrate(double payrate)
{
 payrate = rate;
}

 public double getpayrate()
  {
     return payrate;
  }
  { System.out.printf ( "enter employees payrate");
    while (payrate<=0)
   System.out.printf("\nPayrate must be positive, try again:");
       get.payrate = payrate;
}
 public void sethoursworked(double hours)
  {
     hoursworked = hours;
  }
    public double gethoursworked()
  {
     return hoursworked;
  }
  {
    get.hours = hours;
System.out.printf( "Enter the number of hours worked in this pay
period: ");
       while ( hours <= 0 )
   System.out.printf( "\nSorry you need to enter a positive number for
the Hours:");
       get.hours = hours;
    }
     public void setname(String name)
{
 get.name = name;
System.out.printf( "Enter the employee's name, untill Stop entered");
    while (! get.name.equals("Stop"));
   }
public double calculateweeklypay()
  {
     return payrate * hoursworked;
  }
}
******************************************
public class Payroll3
{ // start Payroll3

 public static void main( String args[])
 {
 Scannerinput = new Scanner( System.in );

   Employee myEmployee = new Employee();

  System.out.printf("Employee Payroll calculator\n");

        System.out.print("Enter Employee Pay Rate: ");
        myEmployee.setpayrate(input.nextDouble());

        System.out.print( "Enter Hours Worked: ");
        myEmployee.sethoursworked(input.nextDouble());

        System.out.printf("Employee earned " +
myEmployee.calculateweeklypay() );

 }
} // end Payroll3
Paul Hamaker - 25 Oct 2006 08:41 GMT
First off, statements have to be inside methods.
Second, what are you trying to do with 'get.payrate' and similar
wording ?
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
porky008 - 25 Oct 2006 18:13 GMT
> First off, statements have to be inside methods.
> Second, what are you trying to do with 'get.payrate' and similar
> wording ?
> --------------------
> Paul Hamaker, SEMM, teaching ICT since 1987
> http://javalessons.com

i need it to repeat and make sure the hours and payrate are positive
numbers. i know it is a simple while loop but no matter where i try to
put it or word it i cant get it to go right. i have been rereading the
book for the last 2 weeks and it is just not clicking
Paul Hamaker - 25 Oct 2006 21:11 GMT
porky008 schreef:
> i need it to repeat and make sure the hours and payrate are positive
> numbers. i know it is a simple while loop but no matter where i try to

Move anything having to do  with input and validation to you 'main'
method.
Omit or comment out your get methods, for the time being.
Scannerinput : Scanner input.
To indicate  fields as opposed to a method parameters, you can use
'this' :
this.payrate , this.hours etc.

> put it or word it i cant get it to go right. i have been rereading the
> book for the last 2 weeks and it is just not clicking

What book is it ? Does it have examples ? Did you get the examples to
run ?
Do you realize that the first stage to a working (let alone correct)
program is getting the syntax right ?
Pay notice to the errors and warnings you're presented by the compiler,
solve them one by one.
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
porky008 - 25 Oct 2006 21:48 GMT
> porky008 schreef:
> > i need it to repeat and make sure the hours and payrate are positive
[quoted text clipped - 20 lines]
> Paul Hamaker, SEMM, teaching ICT since 1987
> http://javalessons.com

We are using Java: How to Program, Sixth Edition by H.M. Deitel and
P.J. Deitel. ? It has a lot of examples but I have never been able to
get one to work. I am going up to the library today to see if I can
find a better book. Any one have a good recommendation I try to notice
what it says is wrong and look to the web then the book to try to fix
it. Then I turn to here as a last resort. Thanks for all the help on
this I got it figured out thanks to all of you. Now to see if I can
figure out some simple arrays for the next assignment.
Patricia Shanahan - 25 Oct 2006 14:18 GMT
> I can't seem to find what is wrong with this. I know a lot is wrong
> by the amounts of errors that are popping up but I can't seem to find
> a way to fix them. Can some one please help with my stupid mistakes?

Your most basic mistake is writing far too much code without having got
the basics of Java syntax and programming logic worked out.

Go back to the "Hello World" level, and work forwards, towards the
program you want, making only small changes.

At each step, make sure the new program compiles and does exactly what
you intend it to do. If it doesn't, take the time to work out why.

Patricia
RedGrittyBrick - 25 Oct 2006 20:25 GMT
> I can't seem to find what is wrong with this. I know a lot is wrong
> by the amounts of errors that are popping up but I can't seem to find
> a way to fix them. Can some one please help with my stupid mistakes?
> Thanks

I had a look, all the mistakes are pretty obvious. I wrote up an answer
but after a moment's reflection I deleted it because I think you really
really need to learn how to do this for yourself.

Have you tried loading this code into an IDE like Eclipse? I think
you'll find it a *lot* easier to deal with the large number of errors
that way.

Here's a hint to get you started:
You have a field ...
   private double hoursworked;
but in the constructor you refer to what variable name?

Patricia's advice is good. Make a new .java file and just copy a few
bits from your existing .java file into it and try compiling it. Only
add another small chunk (e.g. a short method) when it compiles cleanly.

Start with this:

class Employee
{
   private double payrate;
   private double hoursworked;
   private String name = "blanknow";

 public Employee ()
 {
  payrate = 0.0;
  hours = 0.0;
 }

}
Lew - 26 Oct 2006 03:27 GMT
> Start with this:
>
[quoted text clipped - 11 lines]
>
> }

Or even this:

  public class Employee
  {
     private double payrate;
     private double hoursworked;
     private String name = "blanknow";
  }

which has exactly the same effect, but is accessible from "outside" thanks to
the "public" keyword.

As a second step, add "getPayrate()", "getHoursworked()" and "getName()"
methods.  These should return the values of the similarly-named "private"
variables.  They should be public methods.

As a third step, add a "main()" method that uses the "get...()" methods to
display the values of the "private" variables.  (BTW, those "private"
variables are called "instance variables".)  "getPayrate()" should return 0.0,
as should "getHoursworked()".  "getName()" should return what you expect.

As a fourth step, add "setPayrate()", "setHoursworked()" and "setName()"
public methods.  Pay attention to the advice others have given you.

Fifth, use those "set...()" methods in the "main()" method.

Don't proceed to step "n+1" until you are sure you got step "n" right.  At
each step, make sure the class compiles before you try to run it.  Make sure
it runs before you declare the step complete.

Make *extra* sure you spell the same thing the same way everywhere it's used.
  For example, don"t spell a variable "payrate" one place and "rate" another.

Watch out for missing blanks, as was previously mentioned.

Upper- and lower-case count!

Loose blocks of code simply contained in braces inside a class without a
surrounding method will not do what you seem to think, as was mentioned before.

This is all what others have told you, but I hope that providing just a wee
bit more detail might be useful.

- Lew


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.