advertisement

if Statement Worksheet #1 1. Assume that the int variable num contains a positive value. Write an if statement that displays "even" if num is even and "odd" if num is odd. 2. Write an if statement that displays "multiple of 5" if the variable num is a multiple of 5. 3. Write a class named Triplet that contains 3 integer instance fields named myNum1, myNum2, & myNum3. includes a default constructor that initializes the properties (values of the instance fields) to zero includes an "other" constructor that allows a client to initialize the 3 instance fields includes a method named computeMax that returns the largest of the 3 instance fields includes a method named computeMedian that returns the median value. For example, if the values of the three properties are 12, 4, & 7, then the median value is 7. (You may need to use the back of the page or another page) These worksheets were originally created by Mr. Minich at Wyomissing HS. if Statement Worksheet #2 You may use the division and/or modulus operators to examine the individual digits of an integer in the following exercises. Do not convert numbers to Strings or use loops. 1. Write an if statement that displays the message "ones digit is even" if the variable num is an even number. You can assume that num is greater than 1. 2. Write an if statement that displays the message "tens digit is odd" if the tens digit of the variable num is an odd number. You can assume that num is greater than 9. 3. Write an if statement that displays the message "hundreds digit is 5" if the hundreds digit of the variable num is 5. You can assume that num is greater than 99. 4. Write an if statement that displays the message "thousands digit is 3" if the thousands digit of the variable num is the number 3. You can assume that num is greater than 999. 5. Write a method named isNumericPalindrome that accepts an integer parameter named num. If num is a palindrome the method must return true. Otherwise the method must return false. You can assume as a precondition that num has exactly 5 digits (i.e. it is between 10000 and 99999.) For example, 12321 is a palindrome while 12231 isn't because it's the same number if reversed. public boolean isNumericPalindrome(int num) { These worksheets were originally created by Mr. Minich at Wyomissing HS. if Statement Worksheet #3 1. Write an if else if statement that uses a 0-59, 60-69, 70-79, 80-89, 90-100 grading scale to display "A", "B", "C", "D", or "F" depending on the value of the integer variable named testScore. You may assume that testScore is a positive integer less than 101. 2. Write an if statement that displays "divisible by 3" if the integer variable num is evenly divisible by 3. 3. Write an if statement that displays "multiple of 5 and 7" if the integer variable num is a multiple of 5 and 7. 4. Write a method named isPrime that accepts an integer parameter named num. The method returns true if num is prime. Otherwise it returns false. You can assume as a precondition that num is less than 100. ((Prime numbers have no prime factors other than themselves.)) public boolean isPrime(int num) { These worksheets were originally created by Mr. Minich at Wyomissing HS. if statement worksheet #4 Rewrite the following if statements so that any compile (i.e. syntax) and logic errors are fixed 1. if num > 0 System.out.println("hello"); 2. if (x > Math.pow(y, 2)); System.out.println("x is greater than y squared"); 3. if (num = 3) System.out.println("goodbye"); 4. if (3 < num < 10) System.out.println("over here"); 5. if (num > 5 || < 0) System.out.println("up there"); 6. if (grade >= 80) if (grade >= 90) System.out.println("A or B"); else System.out.println("C or less"); These worksheets were originally created by Mr. Minich at Wyomissing HS.