Introduction to Java Methods
Below are several example programs discussed in class. Complete these according to the lecture. You must
create additional methods other than main
to compete these.
Solving Greatest Common Divisor
Write a program called GreatestCommonDivisor
that prompts the user to enter two positive integers and finds their greatest common divisor. A simple albeit an inefficient algorithm to solve this problem is as follows:
Let the two input integers be n1 and n2. You know number 1 is a common divisor, but it may not be the greatest common divisor. So you can check whether k (for k = 2, 3, 4, and so on) is a common divisor for n1 and n2, until k is greater than n1 or n2
You must design the implement the following methods and use them in your solution:
public static int getSmallest(int n1, int n2)
that finds the smallest number between n1 and n2 and returns the number.
public static int isDivisor(int d, int n1, int n2)
that checks if d
is a divisor of both n1
and n2
. If it is, returns true
; otherwise false
.
public static int computeGcd(int n1, int n2)
that returns the great common divisor of n1
and n2
.
The following are several test runs of the program:
$ java GreatestCommonDivisor
Enter two positive integers: 10 8
The greatest common divisor of 10 and 8 is 2
$ java GreatestCommonDivisor
Enter two positive integers: 24 16
The greatest common divisor of 24 and 16 is 8
$ java GreatestCommonDivisor
Enter two positive integers: 7 3
The greatest common divisor of 7 and 3 is 1
$ java GreatestCommonDivisor
Enter two positive integers: 24 12
The greatest common divisor of 24 and 12 is 12
$ java GreatestCommonDivisor
Enter two positive integers: 24 24
The greatest common divisor of 24 and 24 is 24
Printing First 50 Prime Numbers
Write a program called PrintPrimes
that displays the first 50 prime numbers in five lines, each of which contains 10 numbers. An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not.
Noting the first prime number is 2, we can break the problem into the following big steps:
- For number = 2, 3, 4, 5, 6, …,
- test whether the number is prime.
- count the prime numbers.
- print 10 prime numbers per line.
The following is a test run of the program:
$ java PrintPrimes
2 3 5 7 11 13 17 19 23 29
31 37 41 43 ...
...
Not to give all the 50 prime numbers, only 1st line and part of the 2nd line are shown in the above. Also note that to have a well-aligned output, we set the field width for each number as 4, followed by a space, including the last field on a line/row.
To satisfy the requirement of this exercise, you will have to implement the following methods:
public static boolean isPrime(int num)
that checks if num
is a prime number. The method returns true
if num
is a prime; or false
if not. Note that
the method only needs to work for positive numbers.
public static boolean isLineFull(int numOfPrimesPrinted)
that checks if a line is full. Note that you can only print 10 primes per line. If 10 primes is already printed on
the line, it should return true
; otherwise false
. You should use this method to determine whether you need to
print out a line break.
Checking whether a String is a Palindrome
The problem is to write a program that prompts the user to enter a string and reports whether the string is a palindrome. A string is a palindrome if it reads the same forward and backward. The words “mom”, “dad”, “noon”, for instance, are all palindromes.
The problem is to write a program called CheckingPalindrome
that prompts the user to enter a string and reports whether the string is a palindrome. You may follow the algorithm below:
- Check whether the first character in the string is the same as the last character. If so, check whether the second character is the same as the second-to-last character.
- This process continues until a mismatch is found or all the characters in the string are checked, except for the middle character if the string has an odd number of characters.
For this exercise, you must implement the following method and use it in your program:
public static boolean isStringPalindrome(String str)
that returns true
if the value of str
is a palindrome; false
otherwise.
The following are several test runs of the program:
$ java CheckingPalindrome
Enter a string (one string per line):
Is String <> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line):
Is String < > a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line):
Is String < > a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): a
Is String <a> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): dad
Is String <dad> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): moon
Is String <moon> a palindrone? false
$ java CheckingPalindrome
Enter a string (one string per line): cat
Is String <cat> a palindrone? false
$ java CheckingPalindrome
Enter a string (one string per line): da
Is String <da> a palindrone? false
$ java CheckingPalindrome
Enter a string (one string per line): moom
Is String <moom> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): lilil
Is String <lilil> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): liljl
Is String <liljl> a palindrone? false
Converting Hexadecimal Number to Decimal Number
Write a program called HexadecimalToDecimal
that converts a hexadecimal numeral to its corresponding decimal numeral.
The program should work as follows,
- prompt the user to enter a valid hexadecimal numeral on the standard input
- read the hexadecimal numeral
- convert the hexadecimal numeral to its corresponding decimal numeral
- print out the conversion result on the standard output
You must implement the following method and use it in your program:
public static long convertToDecimal(String hexText)
that converts the hexadecimal number in hexText
to a decimal number. It is assumed that hexText
contains
a valid hexadecimal number whose corresponding decimal number is within the range of the long
data type.
Below are several runs of the program that exhibits additional requirements, such as, how you should format the output of the program:
$ java HexadecimalToDecimal
Enter a hexadecimal number (no more than 8 digits): 1
Hexadecimal number 1 is decimal number 1
$ java HexadecimalToDecimal
Enter a hexadecimal number (no more than 8 digits): 9
Hexadecimal number 9 is decimal number 9
$ java HexadecimalToDecimal
Enter a hexadecimal number (no more than 8 digits): F
Hexadecimal number F is decimal number 15
$ java HexadecimalToDecimal
Enter a hexadecimal number (no more than 8 digits): FF
Hexadecimal number FF is decimal number 255
$ java HexadecimalToDecimal
Enter a hexadecimal number (no more than 8 digits): ABCD
Hexadecimal number ABCD is decimal number 43981
$ java HexadecimalToDecimal
Enter a hexadecimal number (no more than 8 digits): FFFFFFFF
Hexadecimal number FFFFFFFF is decimal number 4294967295
Converting Decimal Number to Hexadecimal Number
Write a program called DecimalToHexadecimal
that converts a decimal numeral to its corresponding hexadecimal numeral.
The program should work as follows,
- prompt the user to enter a valid decimal numeral on the standard input
- read the decimal numeral
- convert the decimal numeral to its corresponding hexadecimal numeral
- print out the conversion result on the standard output
You must implement the following method and use it in your program:
public static String convertToHexadecimal(long decNumber)
The method convert the decimal number in decNumber
to a hexadecimal numerical. It is assumed that the user’s
input is a valid positive decimal number and within the range of the long
data type.
Below are several runs of the program that exhibits additional requirements, such as, how you should format the output of the program:
$ java DecimalToHexadecimal
Enter a decimal number: 1
Decimal number 1 is hexadecimal number 1
$ java DecimalToHexadecimal
Enter a decimal number: 15
Decimal number 15 is hexadecimal number F
$ java DecimalToHexadecimal
Enter a decimal number: 25
Decimal number 25 is hexadecimal number 19
$ java DecimalToHexadecimal
Enter a decimal number: 255
Decimal number 255 is hexadecimal number FF
$ java DecimalToHexadecimal
Enter a decimal number: 4294967295
Decimal number 4294967295 is hexadecimal number FFFFFFFF