For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings, and improve your skills with for loops. The methods you will write will be part of a short trivia game, feel free to try it out after you finish.
Determining Leap Years
For this method you will write code to determine whether or not a given year is a leap year.
The method is a public static method called isLeapYear that returns a boolean. It has one parameter of type int that represents a year. If the year is a leap year return true otherwise return false.
A year is a leap year if it is:
- divisible by 4 and not divisible by 100
- divisible by both 100 and by 400
Hint: in order to check if a number is divisible you use the modulo function (%). If you want to learn more about the modulo function click here
Testing isLeapYear
Uncomment the code provided for you in the main method with isLeapYear in them, and input a year where it says: “Enter program input (optional)”. Feel free to add your own tests or change the provided tests to test with new numbers.
Determining Prime Numbers Between 100-300
For this method you will write code to determine whether or not a given number is a prime number and between 100-300.
The method is a public static method called isPrime that returns a boolean. It has one parameter of type int that represents a number. If the number is a prime number and is between 100-300 return true otherwise return false.
A number is a prime number if it is only divisible by 1 and itself.
Hint: You will need to use a for loop to check if the number is Prime
Hint: in order to check if a number is divisible you use the modulo function (%). If you want to learn more about the modulo function click here
Testing isPrime
Uncomment the code provided for you in the main method with isPrime in them, and input a number where it says: “Enter program input (optional)”, keep previous inputs. Feel free to add your own tests or change the provided tests to test with new numbers.
Checking a Word
For this method you will write code to determine whether or not a given word has a length of 5 and has an ‘a’ in the middle.
The method is a public static method called aWord that returns a boolean. It has one parameter of type String that represents a word. If the word has a length of 5 and has an ‘a’ in the middle (third letter) return true otherwise return false.
Hint: You will need to use charAt() and length() String methods.
Testing aWord
Uncomment the code provided for you in the main method with aWord in them, and input a word where it says: “Enter program input (optional)”, keep previous inputs.
Checking if Palindrome
For this method you will reverse a String, and check whether or not it is a palindrome.
The method is a public static method called reverse that returns a boolean. It has one parameter of type String that represents a word. You will want to create a String variable in the method to hold the reversed String.
When reversing a String in a for loop you want your loop variable to start at one less then the length of the String and end at the beginning of the String. The reason you start at one less then the length of is because String index’s are zero based. You will then want to decrement (using –) your loop variable.
Once you have set up your for loop concatenate each character onto your String that contains the reversed String. Then you want to compare the orginal string and your new one to see if they match, if they do return true otherwise return false.(Hint: Comparing strings can’t be done with “==” you must use “.equals()”)
Testing reverse
Uncomment the code provided for you in the main method with reverse in it to test the method you just wrote, and input a String where it says: “Enter program input (optional)”, keep previous inputs. Feel free to add your own test cases to test the method.
Turning in
Once you have run the program and are satisfied it is working, you may submit for grading. Note that you only get five submission attempts, so make sure it is mostly working before you submit. We will bypass the main method in our tests, running our own tests on each method you wrote.