Helsingin yliopisto / Tietojenkäsittelytieteen
laitos / 581258-1 Johdatus ohjelmointiin
Copyright © 1998 Arto
Wikla. The use of this material is only allowed for studying purposes
of private persons. Any other use of this material, such as commercial
or other courses, is forbidden.
581258-1 Introduction to Programming: 2. exam 17.12.1998/AW
This is an English version of the examination, not a translation of
Finnish questions. Some of the questions are different.
Write on top of every answering paper the name of the course,
the date of the examination, your name, your date of birth and your signature.
You are allowed to use the tools of the class Read (in Finnish Lue) when
writing your programs.
- Explain the following concepts shortly and precisely:
primitive type
reference type
overloading
overriding
package
(5 points)
- In a game play each player has a name, the description of his/her role
in the game, his/her goodness as an integer -10...10, and his/her
strength as a decimal value 0.0...1.0. The weight of a
player in "defending the good" is calculeted with formula
goodness * strength
- a) Model players as a class Player.
- b) Program a method public static void sort that
gets an array of players as parameter and then sorts the array to
descending order according to player's weight
in "defending the good".
- c) Program a parameterless method public static Player readPlayer
that asks the data of a player from the user, and then returns
the corresponding Player object.
- d) Program an application, which by using these methods, reads the
data of the players, and then prints all the players
in the order given by the sorting method of point b.
Those methods are included in a class of their own, not to the class
of the application. Decide by yourself, how the program finds out the
number of players
(8 points)
- One of your friends, taking part to this course, has not totally
understood the idea of "classes" and "objects".
He/she hasn't even got the idea, why he/she should understand these
concepts. Help your friend and write him/her an explanation of the
matter. Remember that good examples are important in learning.
Your explanation will be evaluated from the point of view of your
friend. So do not write to your teacher, he/she already knows it!
(5 points)
- You are given a class called InpFile for reading input
files. The class has a constructor:
InpFile(String name) creates an InpFile object from where
you can read the lines of a file. The parameter is the name of the file.
If no file is found, the read-method will return null
values. So, empty and missing files will look similar to the user.
In this case you don't need to take care of any errors ("exceptions") in
file handling.
The class has a method for reading lines:
- String read() returns next line from the
input file. When the file ends (or doesn't exist at all!) the method will
return the value null.
By using this class, write a program that asks the user for the name of
the input file, a character to be replaced (oldChar), and a replacing string
(newString). Then the program prints the file on the screen, modified so
that all oldChar characters are replaced by the string newString.
(8 points)
Merry Christmas!