1 2 Previous Next 21 Replies Latest reply: Jul 11, 2007 6:23 PM by 807600 RSS

    Working with OOP, constructors, arrays? help....

    807600
      hi all,

      If you think this thread looks familiar, you have probably seen it before. Thousands of students come to this forum every year looking for help because, lets face it, its much easier to see code and ask questions than to learn from some old textbook.

      So here I am, I need some help with my payroll program and anyone that knows more than me is welcome to answer (which should be almost anyone on here) Here is my payroll code and below that I will list the new instructions and my progress. Thank you.
      import java.util.Scanner;
      
      public class Payroll 
           
           {
                    public static void main( String args[] )
                 {
      
      
                Scanner input = new Scanner ( System.in );
           float number1;
           float number2;
           float sum;
      System.out.println();
      System.out.print("Payroll Calculation Program\nPlease type STOP to end program");
      System.out.println();
      System.out.println();
      System.out.println();
      System.out.print("Enter Employee Name:");
      String empName = input.nextLine();
      
      while ( !empName.equalsIgnoreCase("stop") )
            {
           System.out.printf( "Enter Hourly Rate for %s: $",empName );
                     number1 = input.nextFloat();
                     while (number1 < 0.00) {
                     System.out.print("Please re-enter using positive numbers only: $");
                     number1 = input.nextFloat();
      }
                     System.out.print ( "Enter Hours Worked: ");
                     number2 = input.nextFloat();
                     while (number2 < 0.00) {
                     System.out.print ("Please re-enter using positive numbers only: ");
                     number2 = input.nextFloat();
      }
                     sum = number1 * number2;
                
                System.out.printf ("%s's weekly pay is:$%.2f\n",empName,sum);
                System.out.println();
                
      
                input.nextLine();
                  
                            System.out.print("Enter Employee Name:");               
                            empName = input.nextLine();
           
                
               }
      }
      
      }
      I need to modify the program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. I also need to use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay.

      So here is what I have done, aside from building the program above. I have begun to build what I think is a constructor within my payroll.java file, and it looks like this
      empName = new Employee (number1,number2, sum);
      and I have also begun work on my employee.java file which looks like this
      public class Employee {
      
           float number1;
           float number2;
           float sum;
      
           Employee (float number1, float number2, float sum) {
                number1 = ;
                
      Yah, I know none of this is probably correct, so please MTV, pimp my....no wait, fix my code!

      Thank you guys (sorry, got to through in a few jokes)
        • 1. Re: Working with OOP, constructors, arrays? help....
          807600
          If you think this thread looks familiar, you have probably seen it before.
          You're right. Which means that it still exists. Which means search for it.
          • 2. Re: Working with OOP, constructors, arrays? help....
            807600
            Can you post the entire *.java file here so we can take a look?

            I doubt this:
            public class Employee {
             
                 float number1;
                 float number2;
                 float sum;
             
                 Employee (float number1, float number2, float sum) {
                      number1 = ;
            is your entire code.

            < Yah, I know none of this is probably correct, so please MTV, pimp my....no wait, fix my code!

            for starters:

            1) close your brackets.
            2) number1 = ; ?
            3) Employee (float number1, float number2, float sum) , this is probably OK but when i make my constructers i make them public.


            edit

            probably i would make something like this:
            public class Employee {
             
                 float number1;
                 float number2;
                 float sum;
             
                 public Employee (float number1, float number2, float sum) {
                      number1 =  ; //not sure what you want to do with this...
            }
            }
            Message was edited by:
            deAppel

            Message was edited by:
            deAppel
            • 3. Re: Working with OOP, constructors, arrays? help....
              807600
              Ahhh, the old "Here's my code fix it for me because I'm a lazy doofus who is off to play xbox" trick.
              • 4. Re: Working with OOP, constructors, arrays? help....
                807600
                Ahhh, the old "Here's my code fix it for me because
                I'm a lazy doofus who is off to play xbox" trick.
                Aren't you being a little hard on him and presumptuous (It's probably a Wii)?
                • 5. Re: Working with OOP, constructors, arrays? help....
                  807600
                  Ahhh, the old "Here's my code fix it for me
                  because
                  I'm a lazy doofus who is off to play xbox" trick.
                  Aren't you being a little hard on him and
                  presumptuous (It's probably a Wii)?
                  I need a Wii and I always wash my hands afterwards.
                  • 6. Re: Working with OOP, constructors, arrays? help....
                    807600
                    to "Navycoder"-- thanks for nothing, your post is neither constructive, nor insightful. The forum rules state to search for previous answers, i did. The forum states to leave constructive and educated answers - you did not. Of course i have seen it posted before but their programs were built completely different than mine. I would not have posted this if I was able to learn from their post...... i don't make a habit of wasting my time, or others.

                    To deAppel, thank you, thank you, thank you..... It's nice to have someone who wants to help.

                    Anyway, unfortunently, this is my entire Employee.java file so far. I have built it based off of prior written threads (see note above :) )but I just don't quite understand where I am going with it. For example this is from a prior thread (written by someone else):

                    public class Employee {
                         
                         private String name;
                         private double hourlyRate;
                         private double hoursWorked;
                         
                         // Constructor to initialize the employee with the information provided.
                         public Employee(String name, double hourlyRate, double hoursWorked) {
                              setName(name);
                              setHourlyRate(hourlyRate);
                              setHoursWorked(hoursWorked);
                         }
                         
                         // Method to get the employee name.
                         public String getName() {
                              return this.name;
                         }
                         
                         // Method to set the employee name.
                         private void setName(String name) {
                              this.name = name;
                         }
                         
                         // Method to set the hourly rate.
                         private void setHourlyRate(double hourlyRate) {
                              this.hourlyRate = hourlyRate;
                         }
                         
                         // Method to set the hours worked.
                         private void setHoursWorked(double hoursWorked) {
                              this.hoursWorked = hoursWorked;
                         }
                         
                         // Method to compute the weekly pay.
                         public double getWeeklyPay() {
                              return this.hourlyRate *this.hoursWorked;
                         }
                         
                    }
                    I had my hourlyrate and hoursworked coded as number1 and number2. I also used float instead of double. So I started building my code my translating those. It's just getting to a point where I do not recognize the code (i don't know the terms such as: private and set) I am trying to find them in the text book but there are so many chapters and I am completely lost. Thank you to all those who help!
                    • 7. Re: Working with OOP, constructors, arrays? help....
                      807600
                      I havn't had time to play my wii, or xbox since I started this class. .... but in regards to your reply, isn't this forum for helping people like me?

                      I'm not asking you to write the code for me, just point me in the right direction.....

                      If i wanted just another school credit, I would have taken an easier class. I want to understand the subject of java and the textbook just isn't much help.

                      I've never been hashed this bad for trying to honestly learn something in my entire life, give me a break guys....
                      • 8. Re: Working with OOP, constructors, arrays? help....
                        807600
                        to "Navycoder"-- thanks for nothing, your post is
                        neither constructive, nor insightful.
                        That's right, throwing a temper tantrum is so gunna want us to help you.
                        Of course i have seen it
                        posted before but their programs were built
                        completely different than mine. I would not have
                        posted this if I was able to learn from their
                        post
                        Right! So when you were at school learning to count, your teacher taught you that 1 + 1 = 2. But when asked what 2 + 2 equals, you wail "but you never taught us that"
                        • 9. Re: Working with OOP, constructors, arrays? help....
                          807600
                          isn't this forum for helping people like me?
                          Yes this forum is for helping people. We do not do their work for them. And it is apparent from this statement in your first post "...no wait, fix my code!" that is exactly what you think help is, us doing your work for you.
                          • 10. Re: Working with OOP, constructors, arrays? help....
                            807600
                            Alright guys,

                            There are so many negative replies, I can only assume I have not presented my problem correctly. I never intended for any of you to build my code or do my work for me.

                            To flounder- after reviewing your post on other threads, I see you enjoy your superior intellect so much that you will not even share your knowlage with others. Why even take the time to post these negative statements? Everyone else take a look as well, his 5000+ replies have really done a lot of good to improving the information and communication of this entire forum.

                            ....and don't bother replying. I am off this forum for a while. Hopefully the next time I come looking for helpful advice, you'll be spewing your negativity and insults on myspace.

                            Thank you again to deappel for beginning to help me.
                            • 11. Re: Working with OOP, constructors, arrays? help....
                              807600
                              I will bother to reply. You haven't read many of my posts then have you? I have helped plenty of people. Those that get help are people who post their code and ask a specific question and display a genuine desire to work the problem out for themselves and most importantly, use their brain.

                              Those, like you, who simply plonk their code on the table (so to speak) and expect others to debug it for them without the slightest bit of information from the originator, get nothing but ridicule. Also those that continue to argue with the regulars and think they no better get nbo help at all.

                              So you make the choice, act like a twat-face pratt and get no help or acknowledge that your initial post was the wrong way to get help, amend your posting strategy and you will no doubt get plenty of help.

                              Or you can simply ran away and never return which I'm sure nobody here gives a flying fuck as to whether you do or don't.
                              • 12. Re: Working with OOP, constructors, arrays? help....
                                796254
                                Let's look at those parameters: float, float, float. Hmmmmm, not very useful since the requirement wants you to keep employee name and it's a String. Too bad.

                                Poor choice of variable names: number1, number2. What is this, VB? Why not hoursWorked?

                                Just what the forum needs - another "prizes for everybody" kid who can't bear criticism, knows no Java, and just wants everyone to do their homework for them. Great.

                                %
                                • 13. Re: Working with OOP, constructors, arrays? help....
                                  807600
                                  Let's look at those parameters: float, float, float.
                                  Hmmmmm, not very useful since the requirement wants
                                  you to keep employee name and it's a String. Too
                                  bad.

                                  Poor choice of variable names: number1, number2.
                                  What is this, VB? Why not hoursWorked?

                                  Just what the forum needs - another "prizes for
                                  everybody" kid who can't bear criticism, knows no
                                  Java, and just wants everyone to do their homework
                                  for them. Great.

                                  %
                                  Take a look at reply 6, he did a pretty good job there.
                                  • 14. Re: Working with OOP, constructors, arrays? help....
                                    796254
                                    Great, he's on his way.

                                    %
                                    1 2 Previous Next