|
FHTW Berlin
Fachbereich 4
Internationale Medieninformatik
PROG1: Programmierung I
Wintersemester 03/04
|
Laboratory 8: Exceptions and Arrays
This week's exercise is a recitation, we will be going through some of the
end-of-chapter exercises on exceptions.
- Start a new project in JBuilder. Define an Exception type called UnbelievableException. This is done by extending the class Exception. Look this up in the Java documentation! What will the file be called? Note that you will need two constructors, a paramterless one and one that takes a string.
- Using your UnbelievableException type, write an animate object that continually asks the user for the user's age, then throws an UnbelievableException if appropriate. You only need to know how to read integers to do this, not strings! Use Stein's AnimateObject class from the cs101 library. Note: the presence of an unbelievable age should not cause the program to terminate - yet.
- Now pick a value as a termination value (for example, -1). If you pick up this value as input, you should gracefully terminate your program and write a polite sign-off message to the DOS-Box.
- Now define a construct for keeping track of how often which number is input. Hint: this could be an array of Counter.... You will also want to count how many times you threw an exception.
- When the termination value is input, you now should calculate some interesting values: what was the maximum value input, the minimum, the average, and which value was input most often. Design some good tests for this!
- Only if you are bored, have a selection of questions to ask the user that you choose by random each time act() is executed. Make up some more Exceptions. Build a real nosey object that keeps asking personal questions! Keep track of anything interesting that you find out :-)
The
exercise for next week is an experiment, note that you will need to prepare some
finger exercises!
This course is an adaption for the
Fachhochschule für Technik und Wirtschaft
, Berlin
by
Prof. Dr. Debora
Weber-Wulff
of a part of
Prof. Dr. Lynn Andrea
Stein's
Rethinking CS101 project produced while she was at the MIT
AI Lab
belonging to the
Department
of Electrical Engineering and Computer
Science (EECS) at the
Massachusetts
Institute of Technology.
She is now with Franklin W. Olin College of Engineering.
The copyright for
all materials belongs to Lynn Andrea Stein, this adaptation is used
by permission. All rights reserved.
A textbook is in preparation by
Morgan Kaufmann Publishers
Questions or comments:
<weberwu@fhtw-berlin.de>