Assignment 5 - Insurance / Poker
Option 1: Using Return Values and Parameters to Calculate Age and Life Expectancy for Car and Life Insurance
Option 2: Finish the 2-Card Poker game
Option 1: Using Parameters for Insurance Data
Setup Instructions
Create a new file called Assign5_Insurance.java
In run(), set up the following 2 variables:
Then code the following functionality
- Create a calculateAge method that returns the age of the user.
- public static int calculateAge()
- Inside the method: Ask the user for their birth year
- It then calculates the age of the person and returns the approximate age. This should take one to three lines of code
- In run(), print “Driving Insurance Application”
- In run(), call the calculateAge method and store the value it returns into the age variable.
- Create a testAge method that takes in the age as a parameter and returns nothing.
- public static void testAge(int age)
- This takes in the age of a person and then prints out one of the following messages. You can build your IF statement in any order.
- if the age is less than 16, print:
- Driving Eligibility: Learners License only
- Voting Eligibility: No
- if the age is 18 or greater, print:
- Driving Eligibility: Full License
- Voting Eligibility: Yes
- if neither of the above is true, print:
- Driving Eligibility: Graduated License
- Voting Eligibility: No
- Call the method from run(), passing in the age that was returned by the first method.
- In run(), print “Life Insurance Application”.
- Next, in run(), ask the user and scan for their sexAtBirth.
- Create a yearsLeft method that takes in the age and the sex as parameters. It returns nothing.
- public static void yearsLeft(int age, String atBirth)
- Before writing this method, do an Internet search to find out the life expectancy for both women and men in Canada.
- This method takes in the age of the person and their sex as parameters.
- It is a void method as it does not need to return anything to run().
- It should figure out how many years a person has left according to the life expectancy statistic. It should then print the following statement. “According to recent statistics, you will likely live for ____ years, until the year _____”
6. Call the yearsLeft method from run() (passing in the age and gender)
Option 2: Finish the Poker Game
Several days ago, we introduced parameters with a Casino example that included an isFlush and an isPair method. The code has been copied onto the next page if you need it.
NOTE: Our in-class example used Player1 and Player 2 rather than calling Player 2 the ‘casino’. You can leave the code as Player1. |
Your goal is to complete this example so it determines who wins the hand. The final result should say: “The Casino Wins!” or “The Player Wins!”. (I would recommend saying why but this is optional. “The Casino has a pair. The player has high card only. The Casino wins!”)
Copy the original example file into a new file called Assig5_Casino.
Here are the rules for determining the winner. This is NOT necessarily the order in which you should code your if statements:
- if one person has a flush and the other does not, the person with the flush wins (this working properly is worth 30%)
- if neither has a flush but if one person has a pair and the other does not, the person with the pair wins (this working properly is worth 30%)
- if neither has a pair or flush the highest cards are compared to see who wins (this working properly is worth 30%)
Final 10% - What if both have a flush or a pair? Well if both have a pair, the highest pair should win. If both have flush then the highest card should win.
- You must code for the situation where both have a pair or both have a flush.
- You do not need to code for the remaining situations (such as when they both have a flush with the same high card.) Instead, print “I don’t know who wins” in uncoded situations.
Ask your teacher for the Casino start code if you do not have it. A version is below but your in-class example may be different.