EIE3320 Tut01 JavaSyntax

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

The Hong Kong Polytechnic University

Department of Electronic & Information Engineering


Object-Oriented Design and Programming (EIE3320)
Tutorial: Java Syntax and Data Types

1. Assignment. Use Java for loops to write a program that prompts the user to enter an integer from 1
to 15, and displays a pyramid as shown in the following sample run:
Enter the number of lines: 15

2. Write a Java program that prompts the user to enter time in seconds (e.g., 1000000) and displays the
equivalent number of hours and remaining minutes and seconds. Hints: you may need to use the
remainder operator % in Java.
Here is a sample run:
Enter the number of seconds: 1000000
1000000 seconds is 277 hours, 46 minutes, and 40 seconds.
3. Design a class named Rectangle to represent a rectangle. The class should contain the following
fields and methods:
z Two double data fields named width and height that specify the width and height of the
rectangle. The default values are 1 for both width and height.
z A no-arg constructor that creates a default rectangle.
z A constructor that creates a rectangle with the specified width and height.
z A method named getArea() that returns the area of this rectangle.
z A method named getPerimeter() that returns the perimeter.

Page 1

Implement the class Rectangle. Write a test program that creates two Rectangle objects
one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width,
height, area, and perimeter of each rectangle in this order. Here is a sample run.
The
The
The
The

area of a
perimeter
area of a
perimeter

rectangle with width


of this rectangle is
rectangle with width
of this rectangle is

4.0 and height 40.0 is 160.0


88.0
3.5 and height 35.9 is 125.64999999999999
78.8

4. A triangular number is defined as 1 + 2 + 3 + L + n for n = 1,2, K and so on. Accordingly, the first
few triangular numbers are 1, 3, 6, 10, . Write a method that returns a triangular number. The
signature of the method is as follows:
public static int getTriangularNumber(int n)
Write a test program that displays the first 100 triangular numbers with 10 numbers on each line.
Here is a sample run:
The first 100 triangular numbers are:
1 3 6 10 15 21 28 36 45 55
66 78 91 105 120 136 153 171 190 210
231 253 276 300 325 351 378 406 435 465
496 528 561 595 630 666 703 741 780 820
861 903 946 990 1035 1081 1128 1176 1225 1275
1326 1378 1431 1485 1540 1596 1653 1711 1770 1830
1891 1953 2016 2080 2145 2211 2278 2346 2415 2485
2556 2628 2701 2775 2850 2926 3003 3081 3160 3240
3321 3403 3486 3570 3655 3741 3828 3916 4005 4095
4186 4278 4371 4465 4560 4656 4753 4851 4950 5050

Hints: Use for-loop and the remainder operator %.


5. Write a method that returns the number of days in a given month. The methods signature is as
follows:

public static int daysInMonth(int month)


Let 1 represents Jan and 2 represents Feb and so on, and ignore leap years. Write a test program that
prompts the user to enter a month as an integer and displays the number of days in that month. Here
is a sample run:
Enter a month (January=1): 7
July has 31 days.

Hints: Use switch-case statements.

Page 2

6. You may not believe but the following is a question of an Admission Test of a primary school in
Hong Kong.

You are requested to write a Java program to help these poor kids to answer this question. To
generalize the solution, your program should be able to accept different values of n and m as input,
where n = 10 and m = 2 in this question. To avoid infinite number of answers, you may assume that
each candy has exactly one foil and it is not allowed to cut the foils.
Here are some example outputs of the program.

Hints:
You may need to use the while loop, integer division, and remainder operator (%) in Java.
What will happen when m = 1?
The formula for computing the total number of Candy T (n, m) is given by
T (n, m) = n + C(n, m)

n + n%m
n
C(n, m) = + C
, m
m
m
C(k, m) = 0

when k = 0 or k = 1

where represents integer division and % is the remainder operator. To use this formula,

you need to understand the concept of recursion. Search the internet (Google java
recursion) to find information on recursion.
Lawrence Cheung
August 2015

Page 3

You might also like