0% found this document useful (0 votes)
2 views

Tutorial 5

The document outlines a series of programming activities for a tutorial on Java. Each activity includes specific tasks such as converting integers to binary, manipulating arrays, analyzing floating-point values, processing strings, checking for palindromes, and generating shapes with asterisks. Deliverables for each activity are specified as Java files to be submitted in a zip file.

Uploaded by

vthanhthanh69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Tutorial 5

The document outlines a series of programming activities for a tutorial on Java. Each activity includes specific tasks such as converting integers to binary, manipulating arrays, analyzing floating-point values, processing strings, checking for palindromes, and generating shapes with asterisks. Deliverables for each activity are specified as Java files to be submitted in a zip file.

Uploaded by

vthanhthanh69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming 1

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:

Division Result Remainder Binary 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

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

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.

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

Two approaches to this problem:


1. Turn number into string, reverse string, compare original with reversed.
2. Reverse the number with arithmetics, compare original number with reversed number.

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.

You might also like