Agnieszka Jastrzebska, PhD
Programming 3
Laboratory no. 2
group 3, at 12:15 – 13:45
The task for today is to make a class named “Worm” with the following two fields:
1-dimensional integer array named “parts”,
2-dimensional character array named “body”
Each time when a new object of class “Worm” is created, constructor has to perform the
following actions:
constructor initializes “parts” array. Implement a constructor, which takes as an
argument a 1-dimensional integer array that will be used to initialize field
“parts”.
constructor initializes 2-dimensional array “body” with character ‘x’. The number
of characters ‘x’ to be used is taken from array “parts.” For example if “parts” is
{1,2,3,2,1} then “body” is {{‘x’},{‘x’,’x’},{‘x’,’x’,’x’},{‘x’,’x’},{‘x’}}. If “parts” is {1,1,2}
then “body” is {{‘x’},{‘x’},{‘x’,’x’}}.
Implement a toString method that will automatically print us out an object of class
“Worm” in a specific manner. An object of class “Worm” initialized using {1,2,3,2,1}
when printed out on the console will look as follows:
(x)-(xx)-(xxx)-(xx)-(x)=(oo)
In class “Worm” implement a method called “reshape” that takes as an argument a
positive integer, let us call this argument "units". “units” says how many body parts
should be attached to this worm. If “units” equals 0 or less, worm remains the same.
Length of attached elements has to be drawn randomly from [1,10] interval. For
example, if “units” is 3 and currently the worm looks as follows:
(x)-(xx)-(xxx)-(xx)-(x)=(oo) when the method is done this worm could look as follows:
(xxx)-(xxxx)-(x)-(x)-(xx)-(xxx)-(xx)-(x)=(oo)
Method executes the following actions:
extend “parts” array by “units” elements, particular values are drawn randomly
corrects array “body” by adding new elements
In the main method create a few worms and print them out on the console. Execute
method “reshape” for some worms and print them out on the console.
Grading:
class structure – 1 pts
constructors – 3 pts
toString – 2 pts
reshape – 2 pts
main – 2 pts
Your solution has to be sent via official university email account to my address by
the end of the labs. Solutions sent after the deadline will not be graded.
Your program must compile. Programs that cannot be successfully compiled will
get 0 pts.
The code must be commented.
© Copyright 2026 Paperzz