-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexercises_d3.tex
306 lines (240 loc) · 10.3 KB
/
exercises_d3.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
\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 Understand the difference between simple and complex data types.
\item Be able to create your own classes, and to create new instances
of those clases using \verb+new+.
\item Understand what a field is, and how to access the fields in an
object (to read it or to alter ---write--- it).
\item Strengthen your understanding of loops.
\end{itemize}
Remember that star exercises are more difficult.
\textbf{Do not try star-exercises unless the other ones are clear to
you}.
\section{Equality of floating-point numbers}
\label{sec:eq}
Look at the following code. What does it do? What will it print on the screen?
\begin{verbatim}
double d1 = 1.255
double d2 = d1 + 7 - 4 - 3
if (d1 == d2) {
println("1.255 is equal to 1.255 plus 7 minus 7");
} else {
println("1.255 is NOT equal to 1.255 plus 7 minus 7");
}
\end{verbatim}
Execute the program. Does it print what you expected it to print? Why?
(Hint: print the value of \verb+d1+ and \verb+d2+; why do they have
these values?). If the program is not behaving correctly, fix it.
\section{Calculator}
\label{sec:calculator}
Write a program that reads two numbers from the user and then offers a
menu with the four basic operations: addition, subtraction,
multiplication, and division. Once the user has selected an operation
from the menu, the calculator performs the operation.
Hint: In the same way that there exists an Integer.parseInt() method
to parse integers, there is a Double.parseDouble() method to parse
real numbers.
\section{Command-line calculator (*)}
\label{sec:comm-line-calc}
Write a program that reads a text representing a mathematical
operation (one of the four basic ones) with two operands, and then
execute it. For example, if the user enters~``3/5'' the program
outputs~``0.6''; if the user enters~``23~*~4'' the program
outputs~``92''.
\section{Distance point--to--point}
\label{sec:distance-point-point}
Write a program that reads the X and Y coordinates of three points and
then outputs which of the three are closer. Use the following class
to store the data for the points:
\begin{verbatim}
class Point {
double x;
double y;
}
\end{verbatim}
Hint: The distance from $(x_1,y_1)$ to $(x_2,y_2)$ can be calculated
as
$$\sqrt{(x_1 - x_2)^2+(y_1 - y_2)^2}$$
\section{Rectangle}
\label{sec:rectangle}
Write a program that reads the X and Y coordinates of two points and
then outputs the area of a rectangle where both points are opposite
corners. Use the following class
to store the data for the points:
\begin{verbatim}
class Rectangle {
Point upLeft;
Point downRight;
}
\end{verbatim}
Your program should calculate (and write on the screen) the perimeter
and area of the rectangle.
\vspace{1em}
Note: For exercises~\ref{sec:rectangle},~\ref{sec:inside-or-outside},
and~\ref{sec:insideinside} you must access (i.e.~read or write) the
value of the coordinates of the points
through the rectangle, not directly through
the point, i.e. \verb+myRectangle.upLeft.x+, not \verb+point.x+ or
\verb+x+.
\section{Inside or outside}
\label{sec:inside-or-outside}
Write a program that reads the coordinates of the two points defining a
rectangle and then the coordinates of a third point. The program must
then determine whether the point falls inside or outside the
rectangle.
\section{Overlaps}
\label{sec:insideinside}
Write a program that reads the coordinates of four points, the
former two defining one rectangle and the latter two defining
another one. The program must read the coordinates of a fifth point and
say whether the point is inside both rectangles, inside one of them
only, or out of both.
\section{Line to column}
\label{sec:line-column}
Write a program that reads some text from the user and then writes the
same text on screen, but each letter on a different line.
%Use while loops and for loops. What version looks clearer?
Now modify your program to write each word (as defined by spaces)
rather than letter on a different line.
\section{Counting letters}
\label{sec:counting-letters}
Write a program that reads some text from the user and then says how
many letters 'e' are there in that text.
Then modify the program so that it reads the text from the user and
then asks for a letter. The program should then say how many times you
can find the letter in the text.
\section{Counting pairs of letters (*)}
\label{sec:count-pairs-lett}
Write a program that reads a short string and then some longer
text. The program must say how many times you can find the short string in the
text. You cannot use any method of String apart from \verb+charAt()+
and \verb+length()+.
\section{Counting letters redux (*)}
\label{sec:count-lett-redux}
Write a program that reads a text from the user and then enter a loop
of requesting letters and counting them. The program stops if the user
asks for the same letter twice. This is an example of the output of
such a program (with a rather short and boring text):
\begin{verbatim}
Please write a text: It was a dark and stormy night
Which letter would you like to count now? a
There are 6 in your text.
Which letter would you like to count now? s
There are 3 in your text.
Which letter would you like to count now? u
There are 0 in your text.
Which letter would you like to count now? a
Repeated character. Exiting the program...
Thank you for your cooperation. Good bye!
\end{verbatim}
\section{Your change, please}
\label{sec:your-change-please}
Write a program that reads the total cost of a purchase and an amount
of money that is paid to buy it. Your program should output the
correct change specifying the amount of notes (50, 20, 10, 5) and
coins (2, 1, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01) needed.
\section{Palindrome}
\label{sec:palindrome}
A palindrome is a word, phrase, number, or other sequence of units
that may be read the same way in either direction. Examples of strict
palindromes include ``ABBA'', ``madam'', ``radar'', ``kayak'', and
``step on no pets''. Write a program
that reads a text and detects whether the text is a strict palindrome.
\section{Palindrome creator}
\label{sec:palindrome-creator}
Write a program that reads a text and then writes on the screen a
palindrome by writing the same text followed by the same text in
reversed order. For example, if the user enters the text ``It was a
dark and stormy night'' the program must output ``It was a dark and
stormy nightthgin ymrots dna krad a saw tI''.
\section{Palindrome redux (*)}
\label{sec:palindrome-redux}
A strict palindrome is difficult to see in every day language. A
relaxed palindrome, a text that is a palindrome if you ignore
punctuation marks such as commas or spaces, is easier to see. Examples
include ``A man, a plan, a canal - Panama!'', ``Was it a car or a cat
I saw?'', and ``Rise to vote, sir''.
Write a program that reads a text from the user and then says whether
the text is a relaxed palindrome. Note that all strict palindromes are
relaxed palindromes by definition.
Hint: There are two methods that will make your life easier. The first
one is Character.isLetter(), that accepts a character (not a String,
even of length one) and returns true if the character is a letter
(e.g.~'a', 'R') and false otherwise (e.g.~'.', '5'). The second one is
Character.toLowerCase(), that accepts a character (not a String) and
returns the lower case version of the character.
\section{Text2number}
\label{sec:text2number}
Write a program that reads a number with commas and decimal dots (such
as ``23,419.34'') and then prints a number that is half of it. Do not
use \verb+Double.parseDouble()+.
If you were writing a simple spreadsheet, you could use this code to
parse the input in the cells.
\section{Mail server (*)}
\label{sec:mail-server}
Let's implements part of a mail server. A mail server is a program
that takes your emails and then sends them to the appropriate
recipient. In this exercise, you will implement a simplified version
of the SMTP protocol that is used to send emails over the Internet.
When your program starts, it should provide a command prompt to the
user. Then it must read the return address of the email using a
command of the form ``MAIL FROM: $<$email-address$>$''. The program
must check that the command is properly written and that the email
address is valid (i.e.~contains one and only one~``@''~sign
which is neither the
first nor the last character). There is no need for the email address
to actually exist, it only needs to be valid. If there is an error,
the program must
say so and wait for a correct return address.
Once the destination is correct, the program must say ``OK'' and wait
for the recipient. The recipient must be introduced by the user with a
command of the form ``RCPT FROM: $<$email-address$>$''. Once again, if
the user enters an invalid command or email address, the program must
wait for a correct one.
Once the return address and recipient are correct, and only then, the
user can enter the command ``DATA''. The program reads then the body
of the email, line after line, until the user enters a line that
consists of only a dot. At that point, the email is ready and the
program must write on the screen who is sending the email, to whom,
and what the email says.
If at any point the users types ``QUIT'' the program must
terminate. If the user enters any other command, or types one of these
commands at the wrong time (e.g.~RCPT before MAIL), the program must
say ``Invalid command'' on screen. See a simple example below:
\begin{verbatim}
Welcome to My Mail Server!
>>> DATA
Invalid command.
>>> MAIL FROM: [email protected]
OK
>>> RCPT TO: bro
Invalid email address
>>> RPCT TO: [email protected]
OK
>>> DATA
Hi bro,
Call Mum asap.
Take care,
.
Sending email...
from: [email protected]
Hi bro,
Call Mum asap.
Take care,
...done!
>>> QUIT
Bye!
\end{verbatim}
When you send an email from Thunderbird, Outlook, or your favourite
email program, it communicates with a mail server in a way very
similar to what you do in this exercise. Now you know why it seems
easy for spammers to fake a sender's email adress: it
really is that easy.
\end{document}