Programming 1
Tutorial 5
Activity 1
Write a program that reads an integer and output its binary form. The program should make use
of a static method called toBinary(...).
Hints:
What’s the return value of this method?
This method receives one input parameter, what’s the type of this input?
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:
Divisio Resul Remainde Binary
n t r string
75 / 2 37 1 1
37 / 2 18 1 11
18 / 2 9 0 011
9/2 4 1 1011
4/2 2 0 01011
2/2 1 0 001011
1/2 0 1 1001011
Activity 2
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.
Activity 3
Write programs 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 a c e g.
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:
a) Create a method to check if a character is an uppercase letter.
b) Create a method to check if a character is a letter.
c) d) e) Create a method to check if a character is a vowel. Don’t forget that a vowel can be
uppercase or lowercase. To make this method simple, you can create a method to convert an
uppercase letter to a lowercase one.
Activity 4
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:
***** *****
***** * *
***** * *
***** * *
***** *****
Hints: Analyze the shape carefully to discover the pattern. Focus on what is similar and what is
different between the lines of the output.
Submission
Submit a zip file containing all Java programs to this tutorial’s submission box in the course
website on FIT Portal.