-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexercises_d5.tex
290 lines (240 loc) · 11 KB
/
exercises_d5.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\begin{document}
\section*{Learning goals}
\label{sec:learning-goals}
Before the next day, you should have achieved the following learning
goals:
\begin{itemize}
\item Become familiarised with the use of \verb+private+ and
\verb+public+. All your classes, fields, and methods should specify
explicitly wheter they are public or private according to the rules
of thumb in the notes. If you make a decision of visibility that
deviates from those rules, you should explain why in a comment.
\item Related to the former point, you should become used to use
constructors in all your classes. The constructor method or methods
should be used to initialise the fields of any new object of that
class.
\item Be able to create classes in their own \verb+.java+ file,
compile them using
\verb+javac+, and use those classes from
Groovy or Java Decaf programs.
\item Be able to cast simple types from one type to another.
\item Be able to create and use arrays in one or more dimensions.
\end{itemize}
You should be able to finish most of non-star exercises in the
lab. Remember that star exercises are more difficult. Do not try them
unless the normal ones are clear to you.
\section{Dividing integers}
\label{sec:casting}
Create a Java class called \verb+Calculator+. The class should
implement the following methods, each of them printing the result on
the screen.
\begin{itemize}
\item \verb+add(int, int)+
\item \verb+subtract(int, int)+
\item \verb+multiply(int, int)+
\item \verb+divide(int, int)+
\item \verb+modulus(int, int)+
\end{itemize}
Note that you will will need to cast the parameters into \verb+double+
to perform exact division.
Write a small Groovy or Java Decaf program that uses all the methods of
\verb+Calculator+.
\section{Checking arrays}
\label{sec:checking-arrays}
Create a new Java class called \verb+ArrayChecker+ with two methods:
\begin{description}
\item[isSymetrical(int[]): ] a method that returns true if the array of
\verb+int+ provided as argument is symmetrical and false otherwise.
\item[reverse(int[]): ] a method that takes an array of \verb+int+ and
returns another array of \verb+int+ of the same size and with the
same numbers, but in opposite order.
\end{description}
Write a Groovy script that
creates an object of class \verb+ArrayChecker+ and uses its methods
to check whether a few arrays are symmetrical and, if they are not,
reverses them.
\section{Copying arrays}
\label{sec:copying-arrays}
Create a new Java class called ArrayCopier with a method called
\verb+copy+ that takes two arrays of integers as parameters. The
method should copy the elements of the first array (you can call it
\verb+src+, from ``source'') to the second one (\verb+dst+, from
``destination'') as much as possible.
If the second array is smaller, then only those elements that fit will
be copied. If the second array is larger, it will be filled with
zeroes.
Write a Groovy script that
creates an object of class \verb+ArrayCopier+ and uses its \verb+copy+
method to copy some arrays in all three cases:
\begin{itemize}
\item Both arrays are of the same size.
\item The source array is longer.
\item The source array is shorter.
\end{itemize}
\section{Creating matrices}
\label{sec:creating-matrices}
Create a class Matrix that has a 2-D array of integers as a field. The
class should have methods for:
\begin{itemize}
\item a constructor method \verb+Matrix(int,int)+ setting the size of
the array as two
integers (not necessarily the same). All elements in the matrix
should be initialised to 1.
\item a method \verb+setElement(int,int,int)+ to modify one element
of the array, given its position
(the first two integers) and the new value to be put in that
position (the third integer).
The method must check that the indeces are valid
before modifying the array to avoid an
\verb+IndexOutOfBoundsException+. If the indeces are invalid, the
method will do nothing and the third argument will be ignored.
\item a method \verb+setRow(int,String)+ that modifies one whole row
of the array, given its position
as an integer and the list of numbers as a String like ``1,2,3''.
The method must check that the index is valid and the numbers are
correct (i.e.~if the array has three columns, the String contains
three numbers). If the index or the String is invalid, the
method will do nothing.
\item a method \verb+setColumn(int,String)+ that modifies one whole
column of the array, given its position
as an integer and the list of numbers as a String like ``1,2,3''.
The method must check that the index is valid and the numbers are
correct (i.e.~if the array has four rows, the String contains
four numbers). If the index or the String is invalid, the
method will do nothing.
\item a method \verb+toString()+ that returns the values in the array
as a String using square brackets, commas, and semicolons,
e.g. ``[1,2,3;4,5,6;3,2,1]''.
\item A method \verb+prettyPrint()+ that prints the values of the
matrix on screen in a legible format. Hint: you can use the
special character '\textbackslash t' (backslash-t)
to mark a tabulator so that all numbers are
placed in the same column regardless of their size. You can think of
a tabulator character as a move-to-the-next-column command when
printing on the screen.
\end{itemize}
Create a Groovy program that uses all those methods from the Matrix class:
creates matrices, modifies its elements (one-by-one, by rows, and by
columns), and prints the matrix on the screeen.
\section{One-liners for matrices (*)}
\label{sec:creat-matr-one}
Extend your \verb+Matrix+ class with a method \verb+setMatrix(String)+
that takes a String representing the numbers to be put in the elements
of the array separated by commas, separating rows by semicolons,
e.g.~1,2,3;4,5,6;7,8,9.
\section{Symmetry looks pretty}
\label{sec:symm-looks-pretty}
Make a class \verb+MatrixChecker+ with three methods:
\begin{itemize}
\item \verb+isSymmetrical(int[])+ takes an array of \verb+int+ and
returns true if the array is symmetrical and false otherwise. An
array is symetrical if the element at [0] is the same as the
element at [length-1], the element at [1] is the same as the
element at [length-2], etc.
\item \verb+isSymmetrical(int[][])+ takes an bidimensional array of
\verb+int+ and returns true if the matrix is symmetrical and false
otherwise. An matrix is symmetrical if \verb+m[i][j] == m[j][i]+ for
any value of \verb+i+ and \verb+j+.
\item \verb+isTriangular(int[][])+ takes an bidimensional array of
\verb+int+ and returns true if the matrix is triangular\footnote{A
matrix can be up-triangular or low--triangular, but just checking
one of the two is fine for this exercise.} and false
otherwise. An matrix is triangular if \verb+m[i][j] == 0+ for
any value of \verb+i+ that is greater than \verb+j+.
\end{itemize}
Add some methods to your \verb+Matrix+ class from the other exercise to
perform tests on the matrices you create using the methods from
MatrixChecker. (Hint: these methods will need to create objects of
type \verb+MatrixChecker+).
\section{Anti-aircraft aim (*)}
\label{sec:anti-aircraft-aim}
Create an enumerated type \verb+Result+ in its own file. The
\verb+enum+ must have 8 possible values: HIT, FAIL\_LEFT, FAIL\_RIGHT,
FAIL\_HIGH, FAIL\_LOW, FAIL\_SHORT, FAIL\_LONG, OUT\_OF\_RANGE. Hint:
the \verb+enum+ must be \verb+public+.
Then create a Java class \verb+Target+ with the following methods:
\begin{itemize}
\item A constructor method \verb+Target(int)+ that creates a 3-D array
of integers of the
proposed size in all three dimensions. All elements must be set to
zero.
\item A method called \verb+init()+
that sets all the elements in the matrix to 0 except one
---selected randomly--- that will be set to 1.
Hint: Remember that you can get a random
integer between 0 and N (not including N) by
using \verb+int numberToGuess = (int) Math.abs(N * Math.random())+.
\item \verb+fire(int,int,int)+ a method that checks whether the
element determined by the indexes is 1 and returns a type
\verb+Result+ according to the result: \verb+Result.HIT+ if the
element is 1, \verb+Result.FAIL_LEFT+ if the element of value one is
``to the left'' (you must decide what left and right are in your 3-D
array), etc. If any of the indeces is too big (or negative), the
method must return \verb+Result.OUT\_OF\_RANGE+. Left--right
information takes precedence over high--low, and this takes
precendence over short--long. If the 1 is to the left and behind,
the output should be \verb+Result.FAIL\_LEFT+.
\end{itemize}
Write a small program that tells the user they must hit a flying
target, and then let the user try to find it by introducing three
indeces. The program should use an object of class \verb+Target+ to
know whether the user hit or not, and provide feedback
accordingly. Here is a sample out of such a program in a space $10 x
10 x 10$.
\begin{verbatim}
Here they come! Try to bring the plane down!
Enter a coordinate X: 30
Enter a coordinate Y: 4
Enter a coordinate Z: 5
That shot is way out of range. Try harder!
Enter a coordinate X: 3
Enter a coordinate Y: 4
Enter a coordinate Z: 5
You missed! The target is to the right!
Enter a coordinate X: 5
Enter a coordinate Y: 4
Enter a coordinate Z: 1
You missed! The target is farther!
Enter a coordinate X: 5
Enter a coordinate Y: 4
Enter a coordinate Z: 5
You hit it! Well done!
Would you like to play again? y
Here they come! Try to bring the plane down!
Enter a coordinate X:
\end{verbatim}
% Leave for assignment
% \section{Sinking ships (classic)}
% \label{sec:sinking-ships}
% Create a class \verb+Board+ with the following methods:
% \begin{itemize}
% \item \verb+init(int)+ to create a 2-D array of integers of the
% proposed size in both dimensions. All elements must be set to
% zero.
% \end{itemize}
% Hundir la flota
% classical
% painting the board
% ships have one location, but different hit points and can move
% class ship with hit points and position
% more complicated: ships cannot share the same position
\section{Big enough (*)}
\label{sec:big-enough}
Write a small program that asks for the names and IDs of all employees
in a small company, and store them in an array of integers and an
array of Strings. (You will need to create a Java class to hold the
arrays, and to access them).
This is similar to the example from the notes, but you do not know the
number of employees in advance. Read the names and IDs of employees
until the user enters an empty name (i.e. length 0) or an ID equal to
0.
Once you have finished reading employee data, go through the employee
list and print the names and IDs of
those employees whose ID is even or their names start with ``S''.
(Hint: As you do not know how many employees you need in advance, you
will need a growing array. Create a small array, if it gets full
create an array twice as big, copy all data to the new array, and
discard the old array, etc).
\end{document}