• No results found

Exercises

In document Java with BlueJ Part 2 (Page 41-47)

1. Write a program that displays the names of the months for the year 2005 when the snowfall in Denver exceeded 1 inch.

2. Write a program that displays the name of the month in the year 2005 when Denver received the greatest amount of snow.

3. Write a program that calculates and displays the total number of kilo- metres driven by each driver.

4. Write a program that calculates and displays the total number of kilo- metres driven (totalled over all drivers).

5. Write a program that displays each driver’s name and the total number of kilometres driven. As well as the two dimensional array trips, your program must include a one dimensional array containing driver names.

6. Modify the program in Example 5 so that it forms the product of A and B. If A has n rows and m columns and B has m rows and p columns, then the productA×B yields a third matrix of n rows and p columns. Each element of C is a sum of products involving the ith row of A and the jth column of B:

ci,j = Pnk=1 ai,k bk,j

7. Suppose A and B are two matrices as described in the previous ques- tion. However now let A be an m×m identity matrix. An identity matrix is one that has 1s on the diagonal and 0s everywhere else. That is,

Ai,j = 1 where i=j and

Ai,j = 0 where i6=j

For example, ifm= 4 we have A=

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

The program then multiplies matrix A by the matrix B . . . the result should be B.

8. Write a method initSequentialValues(. . .}that sets the values of the elements of a matrix to the values 1, 2, 3, .... For example suppose

A is a matrix of 4 rows and 5 columns. Then the result of calling initSequentialValues(A) we have A= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

9. Similar to the previous question, but place the sequential values in column order. Write a methodinitSequentialValues(. . .}that sets the values of the elements of a matrix to the values 1, 2, 3, .... For example suppose A is a matrix of 4 rows and 5 columns. Then the result of calling initSequentialValues(A)

we have A=

1 5 9 13 17

2 6 10 14 18

3 7 11 15 19

Validation and Text

Manipulation

In this chapter we look at issues related to verifying the correctness of your program, verifying the validity of argument values passed into a method, verifying the validity of field values, and the general concern of manipulating textual data.

3.1

Testing and the assert statement

When we develop a program there is the important task of verifying the pro- gram works as required. Often a programmer will insert multiple println statements to display program data, or to track the progression of some algorithm. In addition to debugging a program this way a programmer can also use the Java assertstatement. This statement has two forms:

• assertexpression1;

• assertexpression1 : expression2;

where expression1 is a boolean expression and expression2 is a value. If expression1 evaluates to false then your Java progam will terminate imme- diately with an Assertion error. If theexpression1 evaluates to true then nothing happens. In the second form of the assert statement expression2 provides a message to be displayed when the program is terminated.

Example 1. Calculate age

Suppose we need to calculate a person’s age in years. Given a person’s birth- date we can easily make this calculation. In Java 1.8 two classes introduced were LocalDate and Period. These classes model dates according to the ISO-8601 calendar system. UsingLocalDatewe can instantiate an instance representing a particular date such astoday, or, someone’s birthday. Period

can be used to create an interval that has a start date and an end date, such as the period between someone’s birthday and today. Period has several utility methods; for instancegetYears() returns the number of years in a period. The following method, given someone’s date of birth in the format

YYYY-MM-DDcalculates age in years.

1 /* * 2 * g e t A g e - d e t e r m i n e s age in y e a r s 3 * 4 * @ p a r a m y y y y m m d d b i r t h d a t e YYYY - MM - DD 5 * @ r e t u r n age in y e a r s 6 */ 7 p u b l i c s t a t i c int g e t A g e ( S t r i n g y y y y m m d d ) { 8 int y y y y = I n t e g e r . p a r s e I n t ( y y y y m m d d . s u b s t r i n g (0 ,4) ) ; 9 int mm = I n t e g e r . p a r s e I n t ( y y y y m m d d . s u b s t r i n g (5 ,7) ) ; 10 int dd = I n t e g e r . p a r s e I n t ( y y y y m m d d . s u b s t r i n g (8 ,10) ) ; 11 // age is d i f f e r e n c e b e t w e e n t o d a y and the

b i r t h d a y 12 L o c a l D a t e b i r t h d a y = L o c a l D a t e . of ( yyyy , mm , dd ) ; 13 L o c a l D a t e t o d a y = L o c a l D a t e . now () ; 14 P e r i o d p = P e r i o d . b e t w e e n ( b i r t h d a y , t o d a y ) ; 15 r e t u r n p . g e t Y e a r s () ; // age in y e a r s 16 }

Please study the code above to know how it works; it does perform the calculation properly. However there are cases where we can get a result we consider to be inappropriate. For example, suppose we use this method in a situation where we are calculating ages of employees. In this situation we expect someone’s age to be positive - a non-positive age indicates something

is wrong with our logic or with our data.

In the program CalculateAge below we include an assert statement to trap the case where the calculated age is not positive (we have used the birth date 2090-01-01). The result of executing this code is shown after the program listing. Note that the program has been terminated with an Assertion error.

Listing 3.1: Use assert to recognize a problem 1 i m p o r t j a v a . u t i l . S c a n n e r ; 2 i m p o r t j a v a . t i m e . L o c a l D a t e ; 3 i m p o r t j a v a . t i m e . P e r i o d ; 4 p u b l i c c l a s s C a l c u l a t e A g e 5 { 6 p u b l i c s t a t i c v o i d m a i n ( S t r i n g [] a r g s ) { 7 int age = g e t A g e (" 2090 -01 -01 ") ; 8 a s s e r t age > 0 : " age m u s t be p o s i t i v e "; 9 S y s t e m . out . p r i n t l n ( age ) ; 10 } 11 12 /* * 13 * g e t A g e - d e t e r m i n e s age in y e a r s 14 * 15 * @ p a r a m y y y y m m d d b i r t h d a t e YYYY - MM - DD 16 * @ r e t u r n age in y e a r s 17 */ 18 p u b l i c s t a t i c int g e t A g e ( S t r i n g y y y y m m d d ) { 19 int y y y y = I n t e g e r . p a r s e I n t ( y y y y m m d d . s u b s t r i n g (0 ,4) ) ; 20 int mm = I n t e g e r . p a r s e I n t ( y y y y m m d d . s u b s t r i n g (5 ,7) ) ; 21 int dd = I n t e g e r . p a r s e I n t ( y y y y m m d d . s u b s t r i n g (8 ,10) ) ; 22 // age is d i f f e r e n c e b e t w e e n t o d a y and the

b i r t h d a y 23 L o c a l D a t e b i r t h d a y = L o c a l D a t e . of ( yyyy , mm , dd ) ; 24 L o c a l D a t e t o d a y = L o c a l D a t e . now () ; 25 P e r i o d p = P e r i o d . b e t w e e n ( b i r t h d a y , t o d a y ) ; 26 r e t u r n p . g e t Y e a r s () ; // age in y e a r s

27 } 28 }

Figure 3.1: Assertion error on running CalculateAge.

Assert statements are used by programmers when they are developing and testing code. They should not be used inproductioncode. BlueJ, by default, enables assertions. This default behaviour is reasonable for an environment that is a learning environment, but assertions can be turned off. At the time of writing assertions can be disabled by changing a flag in the BlueJdefs file fromeatoda.

In document Java with BlueJ Part 2 (Page 41-47)

Related documents