Exercise 1:

Write a class ExamQuestion with field variables String question and int maximalMark. Write a suitable constructor, getters/setters, and a String toString() method.

Write two subclasses ExamQuestionSimpleChoice and ExamQuestionNumeric, in which the answer is supposed to be a choice from an ArrayList (you have also to represent which of the possible answers is the correct one), or a numerical answer of type int (again you have to represent the correct answer).

Write methods in the two subclasses that return full marks if the answer is correct and 0 otherwise.

E.g. assume a question:

ExamQuestionNumeric q1 = new ExamQuestionNumeric("2+3 = ?", 3, 5)

where 3 is the maximal number of marks and 5 the correct answer.

q1.answer(5) should return 3, whereas q1.answer(6) should return 0.

Likewise in the class ExamQuestionSimpleChoice with the possible answers represented in an ArrayList< String > with ArrayList< String > a = new ArrayList< String >(); and a.add("4"); a.add("5"); a.add("10"); a.add("20"); the right answer to the same question would be 2, (assumed we start counting the answers from 1). That is, with

ExamQuestionSimpleChoice q2 = new ExamQuestionSimpleChoice("2+3 = ?", 10, a, 2);

we should get from q2.answer(2) the 10 marks and with q2.answer(3) zero marks.

Exercise 2:

We have seen inWeek 5 on Friday how to manipulate pictures which are given in pnm format. On http://www.cs.bham.ac.uk/internal/courses/ java/msc/handouts/1-05/README.html you find the files as 10, 11, 12. The example picture starts with

P2
1155 690
255
255 255 255 ...

P2 indicates that it is a grey value picture (in contrast to P1 for black and white and P3 for colour). The next two values give the size of the picture, that is, 1155 × 690 pixels in the example. The next value gives the highest grey value (i.e., 255). The next three values (all 255 in the example) are the first three pixels of the picture. In the example in Week 5 we inverted the grey values so that dark goes to bright and vice versa.

In the current exercise you should read in a picture given in pnm format, but assume that it is of size 2n×2m and generate a new picture of size n×m so that each pixel in the newly generated image is the average of 4 pixels. Write out picture to a new pnm file. For an example how the four pixels are averaged see: See image.

Use exception handling to deal with possible problems such as the input file does not exist, or the dimensions in the pnm file do not match the grey values provided, or the dimensions are not even numbers.

Exercise 3:

Read in a file (assumed to consist mainly of text) and write out a corresponding file consisting only of lowercase letters [a-z], empty spaces, or newline characters, i.e., move all uppercase letters to lowercase and eliminate all characters which do not match [a-z], empty space, or a newline character. Note, no JUnit testing is required for this exercise. Test your program on suitable sample files, in particular the file provided in http://www.cs.bham.ac.uk/internal/courses/ java/msc/handouts/exercises/DonQuixote.txt.

Exercise 4:

Making use of the program you have written in Exercise 3, write a program that performs a frequency analysis for all the letters [a-z] with respect to a given file. That is, first you apply your program from Exercise 3 to move all uppercase letters to lowercase and eliminate all characters which do not match lowercase letters [a-z], empty space, or newline, then you compute the relative frequency of the letters from a through z. (The relative frequency of a letter is the number of times the letter occurs in the text divided by the number of all letters in the text.)

Exercise 5:

This exercise is related to Exercise 2. However this time you should read in a colour picture given in pnm and transform it into a grey scale image. Again read in the colour picture from a test-colour.pnm file and write a grey scale image to a corresponding file test-grey.pnm. Note that in pnm files of type P3 a single pixel is represented by three consecutive numbers for corresponding red, green, and blue values. There is a sample file at http://www.cs.bham.ac.uk/internal/courses/ java/msc/handouts/exercises/ComputerScience-colour.pnm.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.