Coding Exercises 12-28
Coding Exercises 12-28
Coding Exercises 12-28
Write a method called printNumberInWord. The method has one parameter number which
is the whole number.
The method needs to print "ZERO", "ONE", "TWO", ... "NINE", "OTHER" if the int
parameter number is 0, 1, 2, .... 9 or other for any other number including
negative numbers.
You can use if-else statement or switch statement whatever is easier for you.
NOTE: Method printNumberInWord needs to be public static for now, we are only using
static methods.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 13: Number Of Days In Month
The parameter needs to be greater than or equal to 1 and less than or equal to
9999.
Otherwise, if it is in the valid range, calculate if the year is a leap year and
return true if it is, otherwise return false.
Examples of input/output:
* isLeapYear(-1600); → should return false since the parameter is not in the range
(1-9999)
NOTE: The solution to the Leap Year coding exercise earlier in the course created
the isLeapYear method. You can use that solution if you wish.
Write another method getDaysInMonth with two parameters month and year. Both of
type int.
This method needs to return the number of days in the month. Be careful about leap
years they have 29 days in month 2 (February).
You should check if the year is a leap year using the method isLeapYear described
above.
Examples of input/output:
NOTE: Methods isLeapYear and getDaysInMonth need to be public static like we have
been doing so far in the course.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 14: Sum Odd
Write a method called isOdd with an int parameter and call it number. The method
needs to return a boolean.
Write a second method called sumOdd that has 2 int parameters start and end, which
represent a range of numbers.
The method should use a for loop to sum all odd numbers in that range including
the end and return the sum.
The parameter end needs to be greater than or equal to start and both start and end
parameters have to be greater than 0.
If those conditions are not satisfied return -1 from the method to indicate invalid
input.
Example input/output:
* sumOdd(13, 13); → should return 13 (This set contains one number, 13, and it is
odd)
NOTE: Both methods needs to be defined as public static like we have been doing so
far in the course.
NOTE: Do not add a main method to solution code.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 15: Number Palindrome
Write a method called isPalindrome with one int parameter called number.
Example Input/Output
isPalindrome(11212); → should return false because reverse is 21211 and that is not
equal to 11212.
Find the the reverse of the given number. Store it in some variable say reverse.
Compare the number with reverse.
If both are the the same then the number is a palindrome otherwise it is not.
Declare and initialize another variable to store the reverse of a number, for
example reverse = 0.
Extract the last digit of the given number by performing the modulo division
(remainder).
Store the last digit to some variable say lastDigit = num % 10.
Increase the place value of reverse by one.
To increase place value multiply the reverse variable by 10 e.g. reverse = reverse
* 10.
Add lastDigit to reverse.
Since the last digit of the number is processed, remove the last digit of num. To
remove the last digit divide number by 10.
Repeat steps until number is not equal to (or greater than) zero.
Tip: Be careful with negative numbers. They can also be palindrome numbers.
Tip: Be careful with reversing a number, you will need a parameter for comparing a
reversed number with the starting number (parameter).
NOTE: The method isPalindrome needs to be defined as public static like we have
been doing
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 16: First And Last Digit Sum
Write a method named sumFirstAndLastDigit with one parameter of type int called
number.
The method needs to find the first and the last digit of the parameter number
passed to the method, using a loop and return the sum of the first and the last
digit of that number.
If the number is negative then the method needs to return -1 to indicate an invalid
value.
Example input/output
* sumFirstAndLastDigit(0); → should return 0, the first digit and the last digit is
0 since we only have 1 digit, which gives us 0+0 and the sum is 0.
* sumFirstAndLastDigit(5); → should return 10, the first digit and the last digit
is 5 since we only have 1 digit, which gives us 5+5 and the sum is 10.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 17: Even Digit Sum
Write a method named getEvenDigitSum with one parameter of type int called number.
The method should return the sum of the even digits within the number.
EXAMPLE INPUT/OUTPUT:
NOTE: The method getEvenDigitSum should be defined as public static like we have
been doing so far in the course.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 18: Shared Digit
The method should return true if there is a digit that appears in both numbers,
such as 2 in 12 and 23; otherwise, the method should return false.
EXAMPLE INPUT/OUTPUT:
* hasSharedDigit(12, 23); → should return true since the digit 2 appears in both
numbers
* hasSharedDigit(9, 99); → should return false since 9 is not within the range of
10-99
* hasSharedDigit(15, 55); → should return true since the digit 5 appears in both
numbers
NOTE: The method hasSharedDigit should be defined as public static like we have
been doing so far in the course.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 19: Last Digit Checker
Each number should be within the range of 10 (inclusive) - 1000 (inclusive). If one
of the numbers is not within the range, the method should return false.
The method should return true if at least two of the numbers share the same
rightmost digit; otherwise, it should return false.
EXAMPLE INPUT/OUTPUT:
* hasSameLastDigit (41, 22, 71); → should return true since 1 is the rightmost
digit in numbers 41 and 71
* hasSameLastDigit (23, 32, 42); → should return true since 2 is the rightmost
digit in numbers 32 and 42
* hasSameLastDigit (9, 99, 999); → should return false since 9 is not within the
range of 10-1000
Write another method named isValid with one parameter of type int.
EXAMPLE INPUT/OUTPUT
* isValid(468); → should return true since 468 is within the range of 10-1000
* isValid(1051); → should return false since 1051 is not within the range of 10-
1000
NOTE: All methods need to be defined as public static as we have been doing so far
in the course.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 20: Greatest Common Divisor
Write a method named getGreatestCommonDivisor with two parameters of type int named
first and second.
If one of the parameters is < 10, the method should return -1 to indicate an
invalid value.
The method should return the greatest common divisor of the two numbers (int).
The greatest common divisor is the largest positive integer that can fully divide
each of the integers (i.e. without leaving a remainder).
12 can be divided by 1, 2, 3, 4, 6, 12
The greatest common divisor is 6 since both 12 and 30 can be divided by 6, and
there is no resulting remainder.
EXAMPLE INPUT/OUTPUT:
HINT: Use a while or a for loop and check if both numbers can be divided without a
remainder.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 21: All Factors
Write a method named printFactors with one parameter of type int named number.
The method should print all factors of the number. A factor of a number is an
integer which divides that number wholly (i.e. without leaving a remainder).
EXAMPLE INPUT/OUTPUT:
* printFactors(6); → should print 1 2 3 6
NOTE: When printing numbers, each number can be in its own line. They don't have to
be separated by a space.
1
2
5
10
NOTE: The method printFactors should be defined as public static like we have been
doing so far in the course.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 22: Perfect Number
Write a method named isPerfectNumber with one parameter of type int named number.
The method must calculate if the number is perfect. If the number is perfect, the
method should return true; otherwise, it should return false.
EXAMPLE INPUT/OUTPUT:
* isPerfectNumber(6); should return true since its proper divisors are 1, 2, 3 and
the sum is 1 + 2 + 3 = 6
* isPerfectNumber(28); should return true since its proper divisors are 1, 2, 4, 7,
14 and the sum is 1 + 2 + 4 + 7 + 14 = 28
* isPerfectNumber(5); should return false since its only proper divisor is 1 and
the sum is 1 not 5
NOTE: The method isPerfectNumber should be defined as public static like we have
been doing so far in the course.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 23: Number To Words
Write a method called numberToWords with one int parameter named number.
The method should print out the passed number using words for the digits.
1. Extract the last digit of the given number using the remainder operator.
2. Convert the value of the digit found in Step 1 into a word. There are 10
possible values for that digit, those being 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Print the
corresponding word for each digit, e.g. print "Zero" if the digit is 0, "One" if
the digit is 1, and so on.
3. Remove the last digit from the number.
4. Repeat Steps 2 through 4 until the number is 0.
The logic above is correct, but in its current state, the words will be printed in
reverse order. For example, if the number is 234, the logic above will produce the
output "Four Three Two" instead of "Two Three Four". To overcome this problem,
write a second method called reverse.
The method reverse should have one int parameter and return the reversed number
(int). For example, if the number passed is 234, then the reversed number would be
432. The method reverse should also reverse negative numbers.
Use the method reverse within the method numberToWords in order to print the words
in the correct order.
Another thing to keep in mind is any reversed number with leading zeroes (e.g. the
reversed number for 100 is 001). The logic above for the method numberToWords will
print "One", but that is incorrect. It should print "One Zero Zero". To solve this
problem, write a third method called getDigitCount.
The method getDigitCount should have one int parameter called number and return the
count of the digits in that number. If the number is negative, return -1 to
indicate an invalid value.
For example, if the number has a value of 100, the method getDigitCount should
return 3 since the number 100 has 3 digits (1, 0, 0).
HINT: Use a for loop to print zeroes after reversing the number. As seen in a
previous example, 100 reversed becomes 1, but the method numberToWords should print
"One Zero Zero". To get the number of zeroes, check the difference between the
digit count from the original number and the reversed number.
NOTE: When printing words, each word can be in its own line. For example,
numberToWords(123); can be:
One
Two
Three
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 24: Flour Pack Problem
Write a method named canPack with three parameters of type int named bigCount,
smallCount, and goal.
The parameter bigCount represents the count of big flour bags (5 kilos each).
The parameter smallCount represents the count of small flour bags (1 kilo each).
The parameter goal represents the goal amount of kilos of flour needed to assemble
a package.
Therefore, the sum of the kilos of bigCount and smallCount must be at least equal
to the value of goal. The method should return true if it is possible to make a
package with goal kilos of flour.
If the sum is greater than goal, ensure that only full bags are used towards the
goal amount. For example, if goal = 9, bigCount = 2, and smallCount = 0, the method
should return false since each big bag is 5 kilos and cannot be divided. However,
if goal = 9, bigCount = 1, and smallCount = 5, the method should return true
because of 1 full bigCount bag and 4 full smallCount bags equal goal, and it's okay
if there are additional bags left over.
EXAMPLE INPUT/OUTPUT:
* canPack (1, 0, 4); should return false since bigCount is 1 (big bag of 5 kilos)
and goal is 4 kilos.
* canPack (1, 0, 5); should return true since bigCount is 1 (big bag of 5 kilos)
and goal is 5 kilos.
* canPack (0, 5, 4); should return true since smallCount is 5 (small bags of 1
kilo) and goal is 4 kilos, and we have 1 bag left which is ok as mentioned above.
* canPack (2, 2, 11); should return true since bigCount is 2 (big bags 5 kilos
each) and smallCount is 2 (small bags of 1 kilo), makes in total 12 kilos and goal
is 11 kilos.
NOTE: The method canPack should be defined as public static like we have been doing
so far in the course.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 25: Largest Prime
Write a method named getLargestPrime with one parameter of type int named number.
If the number is negative or does not have any prime numbers, the method should
return -1 to indicate an invalid value.
The method should calculate the largest prime factor of a given number and return
it.
EXAMPLE INPUT/OUTPUT:
* getLargestPrime (0); should return -1 since 0 does not have any prime numbers
HINT: Since the numbers 0 and 1 are not considered prime numbers, they cannot
contain prime numbers.
NOTE: The method getLargestPrime should be defined as public static like we have
been doing so far in the course.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 26: Diagonal Star
Write a method named printSquareStar with one parameter of type int named number.
EXAMPLE INPUT/OUTPUT:
EXAMPLE 1
→ NOTE: For text in Code Blocks below, use code icon {...} on Udemy
*****
** **
* * *
** **
*****
Explanation:
***** 5 stars
** ** 2 stars space 2 stars
* * * 1 star space 1 star space 1 star
** ** 2 stars space 2 stars
***** 5 stars
EXAMPLE 2
********
** **
* * * *
* ** *
* ** *
* * * *
** **
********
The patterns above consist of a number of rows and columns (where number is the
number of rows to print). For each row or column, stars are printed based on four
conditions (Read them carefully):
HINT: To print on the same line, use the print method instead of println, e.g.
System.out.print(" "); prints a space and does not "move" to another line.
HINT: To "move" to another line, you can use an empty println call, e.g.
System.out.println(); .
NOTE: The method printSquareStar should be defined as public static like we have
been doing so far in the course.
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 27: Input Calculator
When the user enters something that is not an int then it needs to print a message
in the format "SUM = XX AVG = YY".
EXAMPLES OF INPUT/OUTPUT:
EXAMPLE 1:
INPUT:
1
2
3
4
5
a
OUTPUT
SUM = 15 AVG = 3
EXAMPLE 2:
INPUT:
hello
OUTPUT:
SUM = 0 AVG = 0
TIP: Use casting when calling the round method since it needs double as a
parameter.
NOTE: Use the method Math.round to round the calculated average (double). The
method round returns long.
NOTE: Be mindful of users who may type an invalid input right away (see example
above).
===================================================================================
=========================
===================================================================================
=========================
Coding Exercise 28: Paint Job
Bob is a wall painter and he needs your help. You have to write a program that
helps Bob calculate how many buckets of paint he needs to buy before going to work.
Bob might also have some extra buckets at home. He also knows the area that he can
cover with one bucket of paint.
The second parameter should be named height of type double. This parameter
represents the height of the wall.
The third parameter should be named areaPerBucket. This parameter represents the
area that can be covered with one bucket of paint.
The fourth parameter should be named extraBuckets. This parameter represents the
bucket count that Bob has at home.
The method needs to return a value of type int that represents the number of
buckets that Bob needs to buy before going to work. To calculate the bucket count,
refer to the notes below.
If all parameters are valid, the method needs to calculate the number of buckets
and return it.
Examples of input/output:
*getBucketCount(-3.4, 2.1, 1.5, 2); → should return -1 since the width parameter is
invalid
*getBucketCount(3.4, 2.1, 1.5, 2); → should return 3 since the wall area is 7.14, a
single bucket can cover an area of 1.5 and Bob has 2 extra buckets home.
*getBucketCount(2.75, 3.25, 2.5, 1); → should return 3 since the wall area is
8.9375, a single bucket can cover an area of 2.5 and Bob has 1 extra bucket at
home.
2. Bob does not like to enter 0 for the extraBuckets parameter so he needs another
method.
This method needs to return a value of type int that represents the number of
buckets that Bob needs to buy before going to work. To calculate the bucket count,
refer to the notes below.
Examples of input/output:
*getBucketCount(3.4, 2.1, 1.5); → should return 5 since the wall area is 7.14, and
a single bucket can cover an area of 1.5.
*getBucketCount(7.25, 4.3, 2.35); → should return 14 since the wall area is 31.175,
and a single bucket can cover an area of 2.35.
3. In some cases, Bob does not know the width and height of the wall but he knows
the area of a wall. He needs you to write another method.
Write another overloaded method named getBucketCount with 2 parameters namely, area
and areaPerBucket (both of type double).
The method needs to return a value of type int that represents the number of
buckets that Bob needs to buy before going to work. To calculate the bucket count,
refer to the notes below.
If all parameters are valid, the method needs to calculate the number of buckets
and return it.
Examples of input/output:
*getBucketCount(3.4, 1.5); → should return 3 since the area is 3.4 and a single
bucket can cover an area of 1.5
*getBucketCount(6.26, 2.2); → should return 3 since the wall area is 6.26 and a
single bucket can cover an area of 2.2.
*getBucketCount(3.26, 0.75); → should return 5 since the wall area is 3.26, and a
single bucket can cover an area of 0.75 .
NOTE: Use the method Math.ceil to round the number of calculated buckets (double)
then convert it into an int before returning the value from the methods.
NOTE: All methods should be defined as public static like we have been doing so far
in the course.
===================================================================================
=========================
===================================================================================
=========================