Tutorial 5
Tutorial 5
Tutorial 5
Activity 1
Write a program that reads an integer and output its binary form. The program should include a
static method called toBinary(...) which is used to convert an integer to a binary string.
(*) Do not use the built-in Integer.toBinaryString() method.
Hints
An easy method of converting decimal number to binary string is to continually divide the
decimal by 2. Each division produces a result and a remainder of either a “1” or a “0”. Repeat
this process until the final result equals zero. Write down the remainders from right to left. For
example, to convert the decimal number 75 into its binary number equivalent:
Deliverable
DecToBin.java
Activity 2
Write a method named shiftArrayLeft that rotates the elements of an array by one position,
moving the initial element to the end of the array, like this:
Deliverable
ArrayShiftTest.java
Activity 3
Write a program that reads a set of floating-point values. Ask the user to enter the values
(prompting only a single time for the values), then print:
a) The average of the values.
b) The smallest of the values.
c) The largest of the values.
d) The range, that is the difference between the smallest and largest.
Hints
Create a method to get a set of numbers from user and return an array. This array can be used by
other methods. Then create several methods to calculate the average, smallest, largest… which
receive the above array as input.
Deliverable
FloatArrayInspect.java
Activity 4
Write a program that read a line of input as a string and print:
a) Only the uppercase letters in the string.
b) Every second letter of the string, ignoring other characters such as spaces and symbols.
For example, if the string is "abc, defg", then the program should print out aceg.
c) The string, with all vowels replaced by an underscore (_).
d) The number of vowels in the string.
e) The positions of all vowels in the string.
Hints
Deliverable
PlayWithString.java
Activity 5
A number is called a palindrome if the number is equal to the reverse of a number, e.g. 121 is a
palindrome because the reverse of 121 is 121 itself. On the other hand 321 is not a palindrome
because the reverse of 321 is 123, which is not equal to 321. Write a Java method to check if a
number is a palindrome in Java and a main method to test this method.
Hint
Deliverable
Palindrome.java
Activity 6
Write a program that reads an integer and displays, using asterisks, a filled and hollow square,
placed next to each other. For example, if the side length is 5, the program should display:
(grey texts are explanation, not part of the output)
***** ***** n stars, 1 space, n stars, line == 0
***** * * n stars, 1 space, 1 star, n-2 spaces, 1 star
***** * * n stars, 1 space, 1 star, n-2 spaces, 1 star
***** * * n stars, 1 space, 1 star, n-2 spaces, 1 star
***** ***** n stars, 1 space, n stars, line == n - 1
Hints
Analyze the shape carefully to discover the pattern. Focus on what is similar and what is
different between the lines of the output. You should create a separate method for generating the
shaped based on a given integer.
Deliverable
FilledAndHollowSquare.java
Submission
Submit a zip file containing all Java programs to this tutorial’s submission box in the course
website on FIT Portal.