## Exercise 6 - Java Methods

Project Repository

Create a function type method called circleArea that will return the area of a circle given the radius. The radius should be a parameter of type double. The method should also return a double as the answer.

Create a procedure-type method called examGrade that will take an exam percentage as a parameter and print out the associated letter grade. For instance, exameGrade(81) should print out an A to the screen.

< 50 - F

50-59 - D

60-69 - C

70 - 79 - B

80+ - A

Create a procedure-type method called factors that will take an integer as a parameter and will print out every number that will divide the parameter evenly. That is, it will print out every number from 1 to the number passed into the parameter that can divide that number without a remainder. The % operator will be useful here to check for a remainder.

Example:

factors(10) would print out the numbers 1, 2, 5, 10

factors(30) whould print out the numbers 1, 2, 3, 5, 6, 10, 15, 30

Write a function-type method called compoundInterest that will return the new balance of an account after the compound interest is added. This method will have 3 parameters: a double represnting the initial principal being added to, a double representing the interest rate as a decimal number, and an integer representing the number of years. The formula used to calculate compound interest is B=P(1+r)^n where B is the balance, P is the principle (initial amount), r is the interest rate, and n is the number of years.

Use the predefined method Math.random() to produce a pattern of a random number of asterisks between 1 and 5 along the page. You will create a procedure-type method called chaotic. The total number of lines in the pattern will be an integer parameter. For example the pattern might look like this:

* *

*

* * *

* *

* * * * *

*

* * * *

and so on

where n is the upper limit to generate and a is the lower limit to generate.

So if we wanted to generate a number between 1 and 100 we would do this

Write a method named lastDigit that returns the last digit of an integer. For example,lastDigit(3572) should return 2. It should work for negative numbers as well. For example,lastDigit(-947) should return 7.

Write a method named firstDigit that returns the first digit of an integer. For example,firstDigit(3572) should return 3. It should work for negative numbers as well. For example,firstDigit(-947) should return 9.

Write a method named season that takes two integers as parameters representing a month and day and that returns a String indicating the season for that month and day. Assume that months are specified as an integer between 1 and 12 (1 for January, 2 for February, and so on) and that the day of the month is a number between 1 and 31.

If the date falls between 12/16 and 3/15, you should return "Winter". If the date falls between 3/16 and 6/15, you should return "Spring". If the date falls between 6/16 and 9/15, you should return"Summer". And if the date falls between 9/16 and 12/15, you should return "Fall".

Write a method named allDigitsOdd that returns whether every digit of a positive integer is odd. Your method should return true if the number consists entirely of odd digits and false if any of its digits are even. 0, 2, 4, 6, and 8 are even digits, and 1, 3, 5, 7, 9 are odd digits.

For example, allDigitsOdd(135319) returns true but allDigitsOdd(91

Write a method sameDashes that takes two strings as parameters and that returns whether or not they have dashes in the same places (returning true if they do and returning false otherwise). For example, below are four pairs of strings of equal length that have the same pattern of dashes. Notice that the last pair has no dashes at all.

string 1: "hi--there-you." "-15-389" "criminal-plan" "abc"

string 2: "12--(134)-7539" "-xy-zzy" "(206)555-1384" "9.8"

To be considered a match, the strings must have exactly the same number of dashes in exactly the same positions. The Strings might be of different length.

**Question 1**Create a function type method called circleArea that will return the area of a circle given the radius. The radius should be a parameter of type double. The method should also return a double as the answer.

**Question 2**Create a procedure-type method called examGrade that will take an exam percentage as a parameter and print out the associated letter grade. For instance, exameGrade(81) should print out an A to the screen.

**Note:**< 50 - F

50-59 - D

60-69 - C

70 - 79 - B

80+ - A

**Question 3**Create a procedure-type method called factors that will take an integer as a parameter and will print out every number that will divide the parameter evenly. That is, it will print out every number from 1 to the number passed into the parameter that can divide that number without a remainder. The % operator will be useful here to check for a remainder.

Example:

factors(10) would print out the numbers 1, 2, 5, 10

factors(30) whould print out the numbers 1, 2, 3, 5, 6, 10, 15, 30

**Question 4**Write a function-type method called compoundInterest that will return the new balance of an account after the compound interest is added. This method will have 3 parameters: a double represnting the initial principal being added to, a double representing the interest rate as a decimal number, and an integer representing the number of years. The formula used to calculate compound interest is B=P(1+r)^n where B is the balance, P is the principle (initial amount), r is the interest rate, and n is the number of years.

**NOTE:**^n means to the power of n. Not the coding equivalent (bitwise XOR) To do this in java, you can use the Math.pow(int b, int p) to take powers. b would be the number for the base, and p would be the power you are raising it to. For instance 4^10 would be done with Math.pow(4,10).**Question 5**Use the predefined method Math.random() to produce a pattern of a random number of asterisks between 1 and 5 along the page. You will create a procedure-type method called chaotic. The total number of lines in the pattern will be an integer parameter. For example the pattern might look like this:

* *

*

* * *

* *

* * * * *

*

* * * *

and so on

**Note:**Math.random() will return a value from 0 up to, but not including 1. To use this to create a random number we can use the following:*int randNum = (int)(Math.random()*(n - a + 1))+ a*where n is the upper limit to generate and a is the lower limit to generate.

So if we wanted to generate a number between 1 and 100 we would do this

*int randNum = (int)(Math.random()*(100 - 1 + 1))+ 1***Question 6**Write a method named lastDigit that returns the last digit of an integer. For example,lastDigit(3572) should return 2. It should work for negative numbers as well. For example,lastDigit(-947) should return 7.

*Hint:*You can pull apart a number into its digits using / 10 and % 10.**Question 7**Write a method named firstDigit that returns the first digit of an integer. For example,firstDigit(3572) should return 3. It should work for negative numbers as well. For example,firstDigit(-947) should return 9.

*Hint:*You can pull apart a number into its digits using / 10 and % 10.**Question 8**Write a method named season that takes two integers as parameters representing a month and day and that returns a String indicating the season for that month and day. Assume that months are specified as an integer between 1 and 12 (1 for January, 2 for February, and so on) and that the day of the month is a number between 1 and 31.

If the date falls between 12/16 and 3/15, you should return "Winter". If the date falls between 3/16 and 6/15, you should return "Spring". If the date falls between 6/16 and 9/15, you should return"Summer". And if the date falls between 9/16 and 12/15, you should return "Fall".

**Question 9**Write a method named allDigitsOdd that returns whether every digit of a positive integer is odd. Your method should return true if the number consists entirely of odd digits and false if any of its digits are even. 0, 2, 4, 6, and 8 are even digits, and 1, 3, 5, 7, 9 are odd digits.

For example, allDigitsOdd(135319) returns true but allDigitsOdd(91

**4**5**2**93) returns false.*Hint:*You can pull apart a number into its digits using / 10 and % 10.**Question 10**Write a method sameDashes that takes two strings as parameters and that returns whether or not they have dashes in the same places (returning true if they do and returning false otherwise). For example, below are four pairs of strings of equal length that have the same pattern of dashes. Notice that the last pair has no dashes at all.

string 1: "hi--there-you." "-15-389" "criminal-plan" "abc"

string 2: "12--(134)-7539" "-xy-zzy" "(206)555-1384" "9.8"

To be considered a match, the strings must have exactly the same number of dashes in exactly the same positions. The Strings might be of different length.