Free University of Bolzano/Bozen
Faculty of Computer Science - Bachelor in Applied Computer Science
Bachelor in Production Engineering
Introduction to Programming - A.A. 2006/2007
Exercise 7
Loop statements and Arrays
Exercise 7A
Design and realize a class Experiment
that stores data obtained
from a temperature measurement.
The data values are delivered as a string of space-separated integers (at least
one) and are stored in an array of integers.
The class Experiment
should export the following functionalities:
- creation of an instance of an experiment, given a string with data
values;
- computation of the number of stored values;
- computation of the sum of all stored values;
- computation of the average of the stored values (use
a
double
!);
- computation of the smallest value;
- computation of the largest value.
Hint: use the StringTokenizer
class once more to implement the
constructor. Use its countTokens
methods to count the number of
integers in the input string.
Exercise 7B
Extend the class Experiment
with the following functionalities:
- verification that two experiments are equal - that is, they hold
the same values in the same order;
- verification that two experiments are similar - that is, they
hold the same values, regardless the of order and regardless of how often
each value appears;
- computing the merge two experiments - that is, a new experiment
that contains the values from both experiments;
- checking whether a given value is present in an experiment's data
set
- counting how often a given value is present in an experiment's data
set
- finding the first value in an experiment's data set that appears at least
twice;
- plotting an experiment's data set using text output: each text line
should contain a number of stars (
*
) proportional to each data
value up to a maximum of 80; in case of negative values just a single minus
sign (-
) should be printed;
Finally, implement a method toString
that returns a string
representation of the experiment's data values.
Solution for 7A and 7B
Exercise 7C
Write a client class for the class Experiment
that does the
following:
- create two experiments from data inserted by the user;
- print the number of values, the sum as well as the average and minimum
values of the two experiments;
- check whether the two experiments are equal and print a message
accordingly;
- if they're not equal, check whether they are similar and print a message
accordingly;
- create an experiment that contains the data of both experiments;
- have the user input an integer value and print out how often it is
present in the newly merged experiment;
- check if there are repeated values in the merged experiment;
- have the merged experiment plotted;
Hint: to have the user input a data set, implement a static method that:
- repeatedly calls
JOptionPane.showInputDialog()
(or
the nextLine()
or next()
method of
the Scanner
class) until the user presses Cancel
,
- builds a space-separated string from the so-inserted integer values,
- creates an
Experiment
object by passing the string to the
constructor of Experiment
, and
- returns such an object.
Solution