Results 1 to 15 of 15

Thread: help

  1. #1
    Coma-Cozzy is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Question help

    Ok people i know my question is kind of basic for all you people but here is my code with question posted underneath it.


    import java.util.*;

    public class MonthlyPayChecks
    {
    static Scanner console = new Scanner(System.in);

    // declare constants
    static final double BONUS1 = 10.00;
    static final double BONUS2 = 20.00;

    public static void main(String[]args)
    {
    // declare variables
    String employeesName = "";
    double baseSalary = 0.0;
    int numYearsWorked = 0;
    double monthlySales = 0.0;
    double yearsWorkedBonus = 0.0;
    double salesBonus = 0.0;
    double totalBonus = 0.0;
    double employeesPayCheck = 0.0;
    int counter = 0;

    /* use a counter controlled loop because we have seven
    employees working for us */
    while (counter < 7)
    {
    System.out.println("Enter employees name, and press enter ");
    employeesName = console.next();
    System.out.println("Enter the employees base salary " +
    "and press enter");
    baseSalary = console.nextDouble();
    System.out.println("Enter the number of years they have " +
    "worked \nfor the company and press enter ");
    numYearsWorked = console.nextInt();
    System.out.println("Enter the employees total sales " +
    "for the month \nthen press enter");
    monthlySales = console.nextDouble();

    if (numYearsWorked > 5)
    yearsWorkedBonus = BONUS2;
    else
    yearsWorkedBonus = BONUS1;

    if (monthlySales >= 10000.00)
    salesBonus = monthlySales * .06;
    else if (monthlySales > 5000.00)
    salesBonus = monthlySales * .03;
    else
    salesBonus = 0.00;

    totalBonus = yearsWorkedBonus + salesBonus;
    employeesPayCheck = baseSalary + totalBonus;


    counter++;
    }


    System.out.printf("Employees Name : " + employeesName +
    " Employees Pay Check : %.2f%n", employeesPayCheck);
    System.out.println();

    }
    }




    Alrighty, how can I get the code to print out the first persons name with his total salary, then the second persons name with his total salary, and the third persons name with his total salary, all the way up to the seven people.

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Create two arrays ,one will be for the salaries of the person and the second will be the array of the persons' names,then with loop "for" print what you want by passing all over the two arrays

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you can, if you're allowed, I'd forgo the parallel array solution as suggested above since you run the risk of one array becoming out of sync with the other. Far better would be to create a class that links the names with the salary and then creating a single array of objects of this class.

  4. #4
    Coma-Cozzy is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Question

    :confused: Sorry, but what is an array? Can you show the code you would use?

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    What is the purpose of the assignment?
    If it is to read in and print out info for 7 people, you could do that by having a loop that reads and prints 7 times. No need for an array if you are not going to be keeping the info.

    It looks like your code has a while() loop that uses counter.
    What isn't that working for you?

  7. #7
    Coma-Cozzy is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    Its not working the way I was hoping it would. Yes I do have a loop, I was hoping to loop seven times, and then print out the seven names along with their pay at the end. Hopefully looking something like this


    Jane 5460
    Mike 6500
    Larry 4300
    Joan 6500
    Jim 3400
    AJ 3000
    Tiffany 5000

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Then it's arrays ...

    I see two ways to do it...

    • a) Store the formated information in a variable that gets incremented with each lopop. Something like:
      Output = output + formatedString
      Print the output variable after exiting the loop.
    • b)Use arrays. You could use arrays (a type of container) to store the information while you program loops and print the arrays contents (in another loop) when your program exits the first loop.


    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    I was hoping to loop
    You don't say what the code is doing? What happens when you execute the program? Can you copy and paste here the console from when you execute it?

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Coma-Cozzy View Post
    Its not working the way I was hoping it would. Yes I do have a loop, I was hoping to loop seven times, and then print out the seven names along with their pay at the end. Hopefully looking something like this


    Jane 5460
    Mike 6500
    Larry 4300
    Joan 6500
    Jim 3400
    AJ 3000
    Tiffany 5000
    You should study more about arrays. serjant gives you a nice link. Take few minutes from your time and read it first. It can be really helpful to find the solution for your question.

  11. #11
    Coma-Cozzy is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default here is the output

    Enter employees name, and press enter
    jim
    Enter the employees base salary and press enter
    23000
    Enter the number of years they have worked
    for the company and press enter
    12
    Enter the employees total sales for the month
    then press enter
    1200
    Enter employees name, and press enter
    john
    Enter the employees base salary and press enter
    34000
    Enter the number of years they have worked
    for the company and press enter
    5
    Enter the employees total sales for the month
    then press enter
    5400
    Enter employees name, and press enter
    betty
    Enter the employees base salary and press enter
    12000
    Enter the number of years they have worked
    for the company and press enter
    1
    Enter the employees total sales for the month
    then press enter
    3400
    Enter employees name, and press enter
    tiffany
    Enter the employees base salary and press enter
    43000
    Enter the number of years they have worked
    for the company and press enter
    15
    Enter the employees total sales for the month
    then press enter
    6000
    Enter employees name, and press enter
    paige
    Enter the employees base salary and press enter
    56000
    Enter the number of years they have worked
    for the company and press enter
    10
    Enter the employees total sales for the month
    then press enter
    9000
    Enter employees name, and press enter
    Anthony
    Enter the employees base salary and press enter
    34000
    Enter the number of years they have worked
    for the company and press enter
    21
    Enter the employees total sales for the month
    then press enter
    2300
    Enter employees name, and press enter
    ginger
    Enter the employees base salary and press enter
    45000
    Enter the number of years they have worked
    for the company and press enter
    14
    Enter the employees total sales for the month
    then press enter
    1500
    Employees Name : ginger Employees Pay Check : 45020.00


    As you can see I'm only getting out for the last person, when what I want is output for all seven. Example
    Employees Name: Jim Employees Pay Check 48000
    Employees Name: john Employees Pay Check 43000
    Employees Name: betty Employees Pay Check 63000
    and so on for all seven
    Last edited by Coma-Cozzy; 11-02-2008 at 03:48 AM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, learn to use arrays. Please click on Serjant's link above and ask any questions about this subject if you have any.

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    The only thing you did was put the print outs in the loop and yes that would be the result of that.
    Since you obviously are not going to go the array way (hey, that rimes!), then a quick & dirty way is to construct the output in a string variable.
    Some pseudo code:
    Java Code:
    loop {
    
    Get name;
    
    calc paycheck;
    
    var = var + name + paycheck + "\n";
    }
    Print var;
    I strongly suggest using arrays: it's more robust and they are very useful.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    Coma-Cozzy is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Smile Thank you

    Just wanted to post a thank you to everyone that replied. Oh BTW I did read about arrays and it seems to me that looping accomplishes the same tasks without typing everything over and over again, but then again I'm just a novice at this, that is why I have to post questions . Either way thank you all for your help.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Coma-Cozzy View Post
    Just wanted to post a thank you to everyone that replied. Oh BTW I did read about arrays and it seems to me that looping accomplishes the same tasks without typing everything over and over again, but then again I'm just a novice at this, that is why I have to post questions . Either way thank you all for your help.
    Yes, arrays can use to collect/hold data in runtim, just a collection.

    By the way you post a question here, because you are going wrong with something in your application. Are you worrying to do that?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •