5 Replies - 1482 Views - Last Post: 09 March 2010 - 08:06 AM Rate Topic: -----

#1 biggmann  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 02-February 10

How to create random number ?

Posted 08 March 2010 - 10:57 PM

I am working on a school project so I am just learning and I am stuck on this small part. I am supposed to create an employee and a random employee number and cant figure out how to pick a random number. Here are the instructions givin and then the code I have so far.

Write a program that makes use of a class called Employee that will calculate an employee's weekly paycheque. Design your class based on the UML diagram to the right as well as the following notes:


* A static field, empCount, keeps track of the number of instantiated employees and can be retrieved using the static method getCount( )
* The constructor initializes the employee's name and employee number (a random number in the range of 1000 - 9999)
* The method setEmployeePay( ) is overloaded to accomodate different payment methods based on empType:

o Type 1 - Salaried employees have a yearly salary, payed on a weekly basis
o Type 2 - Hourly employees are paid an hourly rate for the number of hours worked (overtime is time and a half for hours over 40)
o Type 3 - Piece employees are paid a base amount plus $24.00 for every piece completed

* The method calculatePay( ) will determine what the employee's weekly pay is, based on their payment type
* The methods getName( ) and getNumber( ) return the employee's name and employee number respectively
* All input & output should be displayed in the main class only


The main program should test your class by instantiating four different employees:


* Test a salaried employee
* Test two hourly employees, one who has worked overtime and one who hasn't
* Test a piecework employee
* The following data can be hard-coded into your program (no prompts to enter data other than the names):

o Employee 1: (22.50, 35.0)
o Employee 2: (45350.00)
o Employee 3: (500.00, 25)
o Employee 4: (14.75, 48.0)



public class Employee {

    private String empName; //employee name
    private int empNumber;  //employee number
    private double empSalary;  //employee salary
    private double empRate;  //employee pay rate
    private double empHours;  //employee hours worked
    private double empBase;  //employee base pay rate
    private int empPieces;  //extra pieces made by employee
    private int empType;  //employee type
    private static int empCount = 0;  //number of objects in memory


    //initialize employee, add 1 to employee count
    public Employee( String first)
    {
        empName = first;
        empCount++; //increment static count of employees
            System.out.printf("Employee Name: %s %s; count = %d\n,",
                    empName, empCount);
    }//end employee constructor
   







}//end of employee class






Is This A Good Question/Topic? 0
  • +

Replies To: How to create random number ?

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: How to create random number ?

Posted 08 March 2010 - 11:01 PM

For a random number you could most easily just do this:
int number = (int)(Math.random()*upperLimit + lowerLimit);


Do take care that your lowerLimit will add to the upper.

For instance...
int number = (int)(Math.random()*10 + 1);


number will be from 1-10.
int number = (int)(Math.random()*10 + 2);


number will be from 2-11.

That said, make sure that adding numbers at the end won't make the upperLimit of the random number too much.

This post has been edited by zim1985: 08 March 2010 - 11:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

Re: How to create random number ?

Posted 08 March 2010 - 11:07 PM

@zim1985: It looks like you have an extra closing parenthesis in your two examples without a corresponding opening parenthesis. :)

@biggmann: You might also want to check out the Random class if you don't want to use Math.random().
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to create random number ?

Posted 08 March 2010 - 11:11 PM

Have a look at Random Numbers in Java

Here's the API page: Class Random

You want nextInt(int n)
Was This Post Helpful? 0
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: How to create random number ?

Posted 08 March 2010 - 11:17 PM

View Postmacosxnerd101, on 08 March 2010 - 09:07 PM, said:

@zim1985: It looks like you have an extra closing parenthesis in your two examples without a corresponding opening parenthesis. :)

I did rush a tad, thanks for pointing out my typo :D
Was This Post Helpful? 0
  • +
  • -

#6 biggmann  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 02-February 10

Re: How to create random number ?

Posted 09 March 2010 - 08:06 AM

ok thank you for the help, I will try and get it going with the information you guys gave me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1