Assignment #3


This assignment may seem long, but much of the content on this page is simply me trying to give you detailed requirements on what I am looking for, along with some hints. So don’t panic or get psyched out!


HOWEVER!!!! Do NOT try to simply jump in to this assignment without making absolutely sure that you really GET the material that was covered in week #2. If you try to do this assignment without making absolutely sure that you’re up to speed with the previous material, it will not work! You will have a miserable time trying to complete this assignment properly, and your score will probably reflect that. In addition, you will not be prepared for the material coming up next week. So as I said (many times!) in lecture, be sure to review the material as much as it takes to get up to speed – this is especially true if you are behind.


Don’t forget the comments I discussed in class regarding the things to cover/watch for in your assignments. I let some things go with minimal or no penalty last week, but I am going to start being very strict about them now!  Things like clear identifiers, using whitespace properly, making sure your submitted code compiles without errors, no ‘main’ methods inside user-defined classes, etc.


1.      Create a class called Employee. Have fields for name (String), employeeId (int), annualSales  (int), baseSalary (int), bonus (int).  This class should have the following methods:

·         A method to print the state of the object (e.g. a printInfo type of method)

·         A constructor that initializes the name to “unset”, and all other fields to default values. Based on concepts we’ve discussed in class, use your judgement to decide on appropriate default values for the other fields.

·         Overload your constructor to accept the name and employeeId.  In other words, someone using the class you created should be able to instantiate an Employee object with:  Employee e5 = new Employee(“John Smith”, 12345);   This would create an object setting name to John Smith, the id number to 12345, and all other fields to appropriate default values.

·         Overload the constructor again to accept the name, employeeId and baseSalary. In other words, someone using the class you created should be able to instantiate an Employee object with:  Employee e33 = new Employee(“John Smith”, 12345, 92000);   This would create an object setting name to John Smith, the id number to 12345, baseSalary to 92000, and all other fields to appropriate default values.

·         Have a method called calculateBonus().  This method calculates a 5% bonus based on the employee’s annualSales. The method should look at the value for ‘annualSales’ , calculate 5% of that value, and then assign ‘bonus’ the result of the calculation. So if the sales were $220,000 then bonus should be set to 5% of that which is 11000. 

                                                              i.      Important: Note that ‘bonus’ is an int – not a double! Use Math.round to take the result of your calculation and round it to the nearest int.  As an example:   Math.round(335.72);  would return 336.

                                                            ii.      Also note that for this method to work, annualSales must have a meaningful value. Where/how would you make this important detail known to someone who is making use of your Employee class?

·         Overload calculateBonus() to accept an integer representing the percentage bonus that should be applied. So calculateBonus(23) would add a bonus of 23% (instead of 5% in the original version).


2.      Create a method called flipSomeCoins .  This method should accept one argument of type int that represents how many times you want the coin to be flipped.  For example, flipSomeCoins(100) will simulate flipping a coin 100 times.  Inside your method, create an array of integers. The size of the array should match the argument (e.g. 100).  You should then output how many heads resulted, and how many tails resulted.

·         Use the Math.random method discussed last week to simulate a coin flip: flipResult = (int) (Math.random() * 2) + 1;  NOTE that this formula returns a random number between 1 and 2. If the result is 1, consider the coin to be a heads. If the result is 2, consider the coin to be a tails.

·         So: If the user asked for 100 coin flips, create an array of size 100. Each element of the array will hold either a 1 or a 2.  Go back and count the number of 1s present in your array – this is the number of heads that were flipped. Then count the number of 2s – this is the number of tails.

·         You can output the result using a simple System.out.println statement: 

·         Example:  If your method is invoked as   flipSomeCoins(20);   then you would create an array of 20 integers. Each of the 20 items in your array will hold either a 1 or a 2. If you ended up with twelve 1s and eight 2s, then you would output: Out of 20 rolls, there were 12 heads and 8 tails.


3.      Create a class called Assignment3. In this class, have two methods (in addition to ‘main’): 

·         One is called ‘testEmployeeClass()’. In the testEmployeeClass() method, instantiate at least 3 Employee objects. Use a different constructor for each object. Also demonstrate the calculateBonus() method for one of them. You do NOT have to prompt the user for this information. In other words, you don’t have to say:  “Please enter a name” and wait for them to enter the name and so on. You can simply hard-code values for name, id number, etc directly yourself.

·         The second method should be the method you created in problem #2.

·         In your main method, simply invoke the two methods: testEmployeeClass and flipSomeCoins.



To submit:

-          Submit a zip file containing all of the files used in your assignment. Submit your zip file to the dropbox. The due date is, as usual, Tuesday at 11:45 AM Central Time. You can submit as late as Thursday 11:45 with a 10% penalty for each day late. However, this is definitely not the time to be falling behind on material, so please try to avoid using this grace period!