HaQT Object-Oriented Programming
Homework 3. Exercises on Java Basics
Writing Good Programs
The only way to learn programming is program, program and program. Learning
programming is like learning cycling, swimming or any other sports. You can’t learn
by watching or reading books. Start to program immediately. On the other hands, to
improve your programming, you need to read many books and study how the masters
program.
It is easy to write programs that work. It is much harder to write programs that not
only work but also easy to maintain and understood by others – I call these good
programs. In the real world, writing program is not meaningful. You have to write
good programs, so that others can understand and maintain your programs.
Pay particular attention to:
1. Coding style:
• Read Java code convention: ”Google Java Style Guide”.
• Follow the Java Naming Conventions for variables, methods, and classes
STRICTLY. Use CamelCase for names. Variable and method names begin
with lowercase, while class names begin with uppercase. Use nouns for
variables (e.g., radius) and class names (e.g., Circle). Use verbs for methods
(e.g., getArea(), isEmpty()).
• Use Meaningful Names: Do not use names like a, b, c, d, x, x1, x2,
and x1688 - they are meaningless. Avoid single-alphabet names like i, j, k.
They are easy to type, but usually meaningless. Use single-alphabet names
only when their meaning is clear, e.g., x, y, z for co-ordinates and i for array
index. Use meaningful names like row and col (instead of x and y, i and j,
x1 and x2), numStudents (not n), maxGrade, size (not n), and upperbound
(not n again). Differentiate between singular and plural nouns (e.g., use
books for an array of books, and book for each item).
• Use consistent indentation and coding style. Many IDEs (such as Eclipse /
NetBeans) can re-format your source codes with a single click.
2. Program Documentation: Comment! Comment! and more Comment to
explain your code to other people and to yourself three days later.
3. The only way to learn programming is program, program and program on chal-
lenging problems. The problems in this tutorial are certainly NOT challenging.
There are tens of thousands of challenging problems available – used in training
for various programming contests (such as International Collegiate Programming
Contest (ICPC), International Olympiad in Informatics (IOI)).
1
HaQT Object-Oriented Programming
1 Exercises on Method
1.1 DateUtility
Complete the following methods in a class called DateUtility:
• boolean isLeapYear(int year): returns true if the given year is a leap year. A year is a
leap year if it is divisible by 4 but not by 100, or it is divisible by 400.
• boolean isValidDate(int year, int month, int day): returns true if the given year, month
and day constitute a given date. Assume that year is between 1 and 9999, month is
between 1 (Jan) to 12 (Dec) and day shall be between 1 and 28|29|30|31 depending on
the month and whether it is a leap year.
• int getDayOfWeek(int year, int month, int day): returns the day of the week, where 0
for SUN, 1 for MON, ..., 6 for SAT, for the given date. Assume that the date is valid.
• String toString(int year, int month, int day): prints the given date in the format
”xxxday d mmm yyyy”, e.g., ”Tuesday 14 Feb 2012”. Assume that the given date is
valid.
Hints
To find the day of the week (Reference: Wiki ”Determination of the day of the week”):
1. Based on the first two digit of the year, get the number from the following ”century”
table.
1700- 1800- 1900- 2000- 2100- 2200- 2300- 2400-
4 2 0 6 4 2 0 6
Take note that the entries 4, 2, 0, 6 repeat.
2. Add to the last two digit of the year.
3. Add to ”the last two digit of the year divide by 4, truncate the fractional part”.
4. Add to the number obtained from the following month table:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Non-Leap Year 0 3 3 6 1 4 6 2 5 0 3 5
Leap Year 6 2 3 6 1 4 6 2 5 0 3 5
5. Add to the day.
6. The sum modulus 7 gives the day of the week, where 0 for SUN, 1 for MON, ..., 6 for
SAT.
For example: 2012, Feb, 17
2
HaQT Object-Oriented Programming
1 ( 6 + 12 + 12/4 + 2 + 1 7 ) % 7 = 5 ( F r i )
The skeleton of the program is as follows:
1 /∗ ∗
∗ U t i l i t i e s f o r Date M a n i p u l a t i o n
3 ∗/
public class DateUtility {
5
// Month ’ s name − f o r p r i n t i n g
7 p u b l i c s t a t i c S t r i n g [ ] strMonths
= {” Jan ” , ”Feb” , ”Mar” , ”Apr” , ”May” , ”Jun” ,
9 ” J u l ” , ”Aug” , ” Sep ” , ”Oct” , ”Nov” , ”Dec” } ;
11 // Number o f days i n each month ( f o r non−l e a p y e a r s )
p u b l i c s t a t i c i n t [ ] daysInMonths
13 = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31};
15 // Returns t r u e i f t h e g i v e n y e a r i s a l e a p y e a r
p u b l i c s t a t i c boolean isLeapYear ( i n t year ) {
17 ......
}
19
// Return t r u e i f t h e g i v e n year , month , day i s a v a l i d d a t e
21 // y e a r : 1−9999
// month : 1 ( Jan ) −12(Dec )
23 // day : 1 − 2 8 | 2 9 | 3 0 | 3 1 . The l a s t day depends on y e a r and month
p u b l i c s t a t i c b o o l e a n i s V a l i d D a t e ( i n t year , i n t month , i n t day ) {
25 ......
}
27
// Return t h e day o f t h e week , 0 : Sun , 1 : Mon, . . . , 6 : Sat
29 p u b l i c s t a t i c i n t getDayOfWeek ( i n t year , i n t month , i n t day ) {
......
31 }
33 // Return S t r i n g ” xxxday d mmm yyyy ” ( e . g . , Wednesday 29 Feb 2 0 1 2 )
p u b l i c s t a t i c S t r i n g p r i n t D a t e ( i n t year , i n t month , i n t day ) {
35 ......
}
37
// Test D r i v e r
39 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
System . out . p r i n t l n ( i s L e a p Y e a r ( 1 9 0 0 ) ) ; // false
41 System . out . p r i n t l n ( i s L e a p Y e a r ( 2 0 0 0 ) ) ; // true
System . out . p r i n t l n ( i s L e a p Y e a r ( 2 0 1 1 ) ) ; // false
43 System . out . p r i n t l n ( i s L e a p Y e a r ( 2 0 1 2 ) ) ; // true
3
HaQT Object-Oriented Programming
45 System . out . println ( isValidDate (2012 , 2 , 29) ) ; // true
System . out . println ( isValidDate (2011 , 2 , 29) ) ; // false
47 System . out . println ( isValidDate (2099 , 12 , 31) ) ; // true
System . out . println ( isValidDate (2099 , 12 , 32) ) ; // false
49
System . out . p r i n t l n ( getDayOfWeek ( 1 9 8 2 , 4, 24) ) ; // 6 : Sat
51 System . out . p r i n t l n ( getDayOfWeek ( 2 0 0 0 , 1, 1) ) ; // 6 : Sat
System . out . p r i n t l n ( getDayOfWeek ( 2 0 5 4 , 6, 19) ) ; // 5: Fri
53 System . out . p r i n t l n ( getDayOfWeek ( 2 0 1 2 , 2, 17) ) ; // 5: Fri
55 System . out . p r i n t l n ( t o S t r i n g ( 2 0 1 2 , 2 , 1 4 ) ) ; // Tuesday 14 Feb 2012
}
57 }
Notes
You can compare the day obtained with the Java’s Calendar class as follows:
1 // C o n s t r u c t a Calendar i n s t a n c e with t h e g i v e n year , month and day
Calendar c a l = new G r e g o r i a n C a l e n d a r ( year , month − 1 , day ) ; // month i s
,→ 0−based
3 // Get t h e day o f t h e week number : 1 ( Sunday ) t o 7 ( Saturday )
i n t dayNumber = c a l . g e t ( Calendar .DAY OF WEEK) ;
5 S t r i n g [ ] c a l e n d a r D a y s = { ”Sunday ” , ”Monday” , ” Tuesday ” , ”Wednesday” ,
” Thursday ” , ” Friday ” , ” Saturday ” } ;
7 // P r i n t r e s u l t
System . out . p r i n t l n ( ” I t i s ” + c a l e n d a r D a y s [ dayNumber − 1 ] ) ;
The calendar we used today is known as Gregorian calendar, which came into effect in
October 15, 1582 in some countries and later in other countries. It replaces the Julian
calendar. 10 days were removed from the calendar, i.e., October 4, 1582 (Julian) was followed
by October 15, 1582 (Gregorian). The only difference between the Gregorian and the Julian
calendar is the ”leap-year rule”. In Julian calendar, every four years is a leap year. In
Gregorian calendar, a leap year is a year that is divisible by 4 but not divisible by 100, or it
is divisible by 400, i.e., the Gregorian calendar omits century years which are not divisible by
400. Furthermore, Julian calendar considers the first day of the year as march 25th, instead
of January 1st.
This above algorithm work for Gregorian dates only. It is difficult to modify the above
algorithm to handle pre-Gregorian dates. A better algorithm is to find the number of days
from a known date.
4
HaQT Object-Oriented Programming
1.2 GradesStatistics
Write a program called GradesStatistics, which reads in n grades (of int between 0 and 100,
inclusive) and displays the average, minimum, maximum, median and standard deviation.
Display the floating-point values upto 2 decimal places. Your output shall look like:
Command window
Enter t h e number o f s t u d e n t s : 4
2 Enter t h e g r a d e f o r s t u d e n t 1 : 50
Enter t h e g r a d e f o r s t u d e n t 2 : 51
4 Enter t h e g r a d e f o r s t u d e n t 3 : 56
Enter t h e g r a d e f o r s t u d e n t 4 : 53
6 The g r a d e s a r e : [ 5 0 , 5 1 , 5 6 , 5 3 ]
The a v e r a g e i s : 5 2 . 5 0
8 The median i s : 5 2 . 0 0
The minimum i s : 50
10 The maximum i s : 56
The s t a n d a r d d e v i a t i o n i s : 2 . 2 9
q P
1 n−1
The formula for calculating standard deviation is: σ = n i=0 x2i − µ2 , where µ is the
mean.
Hints
1 public class GradesStatistics {
p u b l i c s t a t i c i n t [ ] g r a d e s ; // D e c l a r e an i n t [ ] , t o be a l l o c a t e d l a t e r
3 // This a r r a y i s a c c e s s i b l e by a l l t h e methods .
5 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
readGrades ( ) ; // Read and s a v e t h e i n p u t s i n s t a t i c i n t [ ] g r a d e s
7 System . out . p r i n t l n ( ”The g r a d e s a r e : ” ) ;
print ( grades ) ;
9 System . out . p r i n t l n ( ”The a v e r a g e i s ” + a v e r a g e ( g r a d e s ) ) ;
System . out . p r i n t l n ( ”The median i s ” + median ( g r a d e s ) ) ;
11 System . out . p r i n t l n ( ”The minimum i s ” + min ( g r a d e s ) ) ;
System . out . p r i n t l n ( ”The maximum i s ” + max( g r a d e s ) ) ;
13 System . out . p r i n t l n ( ”The s t a n d a r d d e v i a t i o n i s ” + stdDev ( g r a d e s ) ) ;
}
15
// Prompt user for the number of students and allocate the static ”grades” array.
17 // Then, prompt user for grade, check for valid grade, and store in ”grades”.
p u b l i c s t a t i c v o i d readGrades ( ) {
19 ......
}
21
// P r i n t t h e g i v e n i n t a r r a y i n t h e form o f [ x1 , x2 , x3 , . . . , xn ] .
23 public s t a t i c void print ( i n t [ ] array ) {
......
5
HaQT Object-Oriented Programming
25 }
27 // Return t h e a v e r a g e v a l u e o f t h e g i v e n i n t [ ]
p u b l i c s t a t i c double average ( i n t [ ] array ) {
29 ......
}
31
// Return t h e median v a l u e o f t h e g i v e n i n t [ ]
33 // Median i s t h e c e n t e r e l e m e n t f o r odd−number array ,
// o r a v e r a g e o f t h e two c e n t e r e l e m e n t s f o r even−number a r r a y .
35 // Use Arrays . s o r t ( anArray ) t o s o r t anArray i n p l a c e .
p u b l i c s t a t i c d o u b l e median ( i n t [ ] a r r a y ) {
37 ......
}
39
// Return t h e maximum v a l u e o f t h e g i v e n i n t [ ]
41 p u b l i c s t a t i c i n t max( i n t [ ] a r r a y ) {
i n t max = a r r a y [ 0 ] ; // Assume t h a t max i s t h e f i r s t e l e m e n t
43 // From second element, if the element is more than max, set the max to this element.
......
45 }
47 // Return t h e minimum v a l u e o f t h e g i v e n i n t [ ]
p u b l i c s t a t i c i n t min ( i n t [ ] a r r a y ) {
49 ......
}
51
// Return t h e s t a n d a r d d e v i a t i o n o f t h e g i v e n i n t [ ]
53 p u b l i c s t a t i c double standardDeviation ( i n t [ ] array ) {
......
55 }
}
Take note that besides readGrade() that relies on class variable grades, all the methods are
self-contained general utilities that operate on any given array.
1.3 GradesHistogram
Write a program called GradesHistogram, which reads in n grades (as in the previous exer-
cise), and displays the horizontal and vertical histograms. For example:
6
HaQT Object-Oriented Programming
Command window
0 − 9: ∗∗∗
2 10 − 19: ∗∗∗
20 − 29:
4 30 − 39:
40 − 49: ∗
6 50 − 59: ∗∗∗∗∗
60 − 69:
8 70 − 79:
80 − 89: ∗
10 90 −100: ∗∗
12 ∗
∗
14 ∗ ∗ ∗
∗ ∗ ∗ ∗
16 ∗ ∗ ∗ ∗ ∗ ∗
0−9 10−19 20−29 30−39 40−49 50−59 60−69 70−79 80−89 90−100