Assignment #2

 

Problem #1

(20 points) Create a class called Faculty with fields for name (String), department (char), isTenured (boolean). 

This is a small university, so the only valid departments are History (‘H’), Music (‘M’), and Biology (‘B’).  Be sure that your mutator methods allow only valid values to be assigned to the department field.  For the mutator method assigning name and isTenured, you may assume that the user will always enter true or false.  That is, you do not have to have code to check for other values.  Also create a separate class with a main method in which you demonstrate some testing of your class..  

·         Part B): Create a separate class in which you test the Faculty class. Demonstrate use of toString (remember that you don’t explicitly invoke ‘toString’), equals, copy constructor and at least a couple of other methods.

·         The grader is going to be very strict in terms of making sure that you meet all of the requirements and with good form! Be sure that you follow carefully all of the guidelines discussed in class. You may, of course, refer to the Student class and test class examples on the class web page. For your convenience, here is a checklist of things that you do not want to forget:

1.       Choose appropriate datatypes for your fields

2.       Have a default constructor, copy constructor and at least one additional constructor. You may decide which fields should go in this second constructor

3.       Use appropriate visibility modifiers (all fields should be private, all methods, public)

4.       Have mutator and accessor methods for all fields – you don’t have to worry about validating values for the mutator methods unless the question requires it

5.       Include the toString method and equals method

 

Problem #2

(20 points) Simulating a dice roll:  Using the Math.random method we discussed in class (also see example below), create an array of 100 integers. Fill the array with random values between 1 and 6 using the Math.random code.  Then output how many times a 1 was rolled, how many times a 2 was rolled, etc  Also output which number was rolled most often.

The code to generate a random number between 1 and 6:

roll  =  (int) (Math.random()*6) + 1;

 

Using the Scanner class, have a do-while loop in which you rerun the simulation as long as the user enters a ‘Y’ or ‘y’ (or Yes/yes/YES/etc).  If they ente any permutation of ‘No (N, n, NO, no) end the game. Any other value and you should make them re-enter (Y/y or N/n).  Remember that the next() method only reads in a String. You need to extract the first character from that String (since the user may type ‘yes’ instead of ‘y’).

 

Output a graph that displays stars for the number of rolls.  For example:

1:     ****************

2:     ****************************

3:     ****

Etc

 

This last problem must be broken down into at least the following methods (you can additional methods if you wish). 

·         welcome             - In which you welcome the user to your game. This method should only run once when the program is run. It should not be run every time the user repeats the game.

·         rollDie                   - Fill an array with the results of the rolls

·         outputResults   - Accepts an array of integers (presumably completed in the previous step) and outputs the results and graph

·         playAgain            - Should ask the user if they wish to play again. The method should return ‘true’ if the user wants to repeat, and ‘false’ if they do not

·         goodbye              - When the user is done, thank them for playing and the program can end

 

 

To submit your assignment, zip all files and turn in to COL.