Java problem | Computer Science homework help




Question (1) asks that you demonstrate an understanding of simple graphics, inner classes, events and the ActionListener interface. Question (2) looks at the object-oriented programming paradigm: inheritance, interfaces, classes (including abstract classes) and instance methods.


IMPORTANT NOTE: Please use Java 8.




Electronic files you should have:


Question (1)


Question (2)

 getQuestions method only
 getQuestions method only




Please ensure that you hand in electronic versions of your .java files. Class files are not needed.


No zip or compressed files.


You are asked to give your classes certain names; please follow these instructions very carefully. Make sure there is no conflict between your file name and your class name. Remember that if you give your files names that are different from the names of your classes (e.g. file name versus RectanglesGUI class name) your program will not compile because of the conflict between the file name and the class name.




Compile and run the RectanglesGUI class. You should note that it uses an inner class to place a JPanel onto a JFrame. Some coloured rectangles are drawn on the JPanel:


Output of the RectanglesGUI class












































Please complete the following tasks:



1.            Shorten the paintComponent (Graphics) method by rewriting the statements to draw and fill the rectangles with colour. Put the statements into a loop of your choosing. The model answer uses


nested for loops, and has reduced the number of statements needed to draw the rectangles to 5.


2.            Using getWidth() and getHeight() for the RectangleDrawPanel, distribute the rectangles evenly across the drawPanel, so that each is fully visible and so that all of the available space is used (see the image below).


NOTE: your solution must be scalable – that is, the dimensions of the JFrame could be changed to be bigger or smaller, and the rectangles


would still all be visible and fill the available space, without any other changes being made.

3.           Add a JButton to the SOUTH region of the JFrame using BorderLayout (2 marks). Set the text on the button. The text shouldinvite the user to click the button.




Write an inner class called RandomColorListener to implement the ActionListener interface and to listen to the JButton in the SOUTH region.


NOTE: you can only get full credit for this part of the question by writing an inner class.


5.            When the user clicks on the JButton in the SOUTH region, the rectangles filled with color1 should all change to a random Color, while the rectangles filled with color2 should not change. A second click on the JButton should make the rectangles filled with color2 all change to arandom Color, while the rectangles filled with a random Color by the first click should stay the same Color. The user should be able to continue clicking on the button indefinitely, and with each click one set of rectangles will be filled with a random Color. In each case, the rectangles to change should be the ones that stayed the same on the last click (in other words, the Color change should alternate


between the two sets of rectangles). This means that with each click only one set of rectangles should change colour.


6.            Write a second inner class implementation of the ActionListener interface, called ResetListener. The ActionPerformed(ActionEvent) method of the class should have a means of resetting the colours of the


rectangles back to the colours that they were filled with at the start (orange and blue).


7.           Add a second JButton to the NORTH region, and add the ResetListener to it such that when the button is clicked the rectanglesare once again filled with orange and blue, as they were at the start of the program. Further clicks on the button will have no effect when the default orange and blue colours are displayed, but will reset the colours to the default setting once more if they have been changed by clicking the other button. Add an appropriate message to the


button telling the user what it does.


Output of the RectanglesGUI class, revised so that the rectangles are all the same size and divide up the available space between them.

Question (2)


In this part of the coursework assignment, you have been given the BaseQuiz and the


FreeQuiz classes, together with the Question, FreeQuizQuestion and MultipleChoiceQuestion classes. BaseQuiz is abstract, and FreeQuiz is one possibleconcrete class extension of it. You should compile and run the FreeQuiz class and see how it behaves. The FreeQuiz class uses FreeQuizQuestion objects, which are placed into an ArrayList of potential questions. The number of possible questions to be randomly chosen from this ArrayList and asked of the user is set in the getQuizSize() method. You might want to try changing the value (currently 5) a few times to see what happens.


The FreeQuizQuestion class is a child class of the Question class. The Question class specifies that the question itself will be a String, but it does not set the type of answer, which is left to the child classes. FreeQuizQuestion answers are Strings, while the MultipleChoiceQuestion class answers are more complicated. Multiple choice questionsneed a list of possible answers, and a record of the correct answer. In the MultipleChoiceQuestion class the possible answers are stored in an array, and the actualanswer is recorded as an int that holds the position of the correct answer in the list of possible answers; the list of possible answers are numbered starting at 1. Hence in the question:


MultipleChoiceQuestion q1 = new MultipleChoiceQuestion(“What is the primitive data type that starts with ‘i’?”, 1, “int”,


“INT”, “Int”, “Integer”);


the ‘1’ means that “int” is the correct answer.


You have been given the MultipleChoiceQuiz and TrueFalseQuiz classes; each contains only the populateList() method.


You have been given a PDF with some Generic classes. These classes,


GenericBaseQuiz, GenericQuestion, GenericFreeQuiz and GenericFreeQuizQuestion have been adapted from the BaseQuiz, Question, FreeQuiz and FreeQuizQuestion classes respectively.




Please complete the following tasks:



1.            Complete the MultipleChoiceQuiz class as a direct child class of BaseQuiz. Include a main method in your class with test statements tomake and run an instance of the class. Your completed class should behave in the same way as the FreeQuiz class, except that the questions that it is asking are multiple-choice ones. This means that the question is displayed with a numbered list of possible answers, and the user has to type in the number of the answer that they think is correct.


See the Appendix for an example of the output of this class.

2.            Write the TrueFalseQuestion class as a direct sub-class of the


Question class.


3.            Complete the TrueFalseQuiz class as a direct child class of BaseQuiz. Include a main method in your class with test statements to make and run an instance of the class. Your completed class should behave in the same way as the FreeQuiz class, except that the questions that it is asking are true or false. This means that the user is shown a statement and has to decide whether they think the statement is true or false,


typing in their answer. See the Appendix for an example of the output of this class.


4.            Can you explain how and why the Generic classes are different from the classes that they have been based on?


Consider the getRandomQuestions(List, int) method in the


GenericBaseQuiz class. The corresponding method was static in BaseQuiz, but is an instance method in GenericBaseQuiz. Can youexplain why?


You should write no more than two paragraphs in answer to this question


(note a paragraph is at most 8 sentences). You may give in your answer as a PDF, Word, OpenOffice or text file.





Reading for Question (2)


The following topics from Head First Java


        Object programming


        Object behavior


        The Java library






Reading for Question 4:





Deliverables for Question (2)


Please hand in an electronic copy of the following:


      Your completed


      Your completed


      A PDF, Word, OpenOffice or text file with your answer to Question (2) Part 4(b).



Sample output of the completed MultipleChoiceQuiz class (questions randomly chosen)


This quiz has 3 questions. Good luck.


QUESTION (6 possible answers): What is the minimum value of the byte data type?


1.  -63


2.  -64


3.  -127  4. -128  5. -255




Enter a number> 4



QUESTION (3 possible answers): What is the value of the expression “James”.charAt(“James”.length() – 1) ?


1.  no value, run-time error


2.  e


3.  s


Enter a number> 3



QUESTION (6 possible answers): What is the maximum value of the byte data type?


1.  63


2.  64


3.  127  4. 128  5. 255




Enter a number> 3



You scored 3/3. That’s 100%. Excellent!







Sample output of the completed TrueFalseQuiz class (questions randomly chosen)


This quiz has 5 questions. Good luck.


True or false? Static methods can operate on instance variables; true


True or false? Instance variables hold the same value for every instance of the class; true


True or false? Static methods can be run before an instance of the class is made; true

True or false? Java is case sensitive; true


True or false? The following expression type checks: int x = “elf”.compareTo(“7”)+11; false



You scored 2/5. That’s 40%. You’ll have better days.

Calculate your paper price
Pages (550 words)
Approximate price: -

Why Choose Us

Quality Papers

We value our clients. For this reason, we ensure that each paper is written carefully as per the instructions provided by the client. Our editing team also checks all the papers to ensure that they have been completed as per the expectations.

Professional Academic Writers

Over the years, our Acme Homework has managed to secure the most qualified, reliable and experienced team of writers. The company has also ensured continued training and development of the team members to ensure that it keep up with the rising Academic Trends.

Affordable Prices

Our prices are fairly priced in such a way that ensures affordability. Additionally, you can get a free price quotation by clicking on the "Place Order" button.

On-Time delivery

We pay strict attention on deadlines. For this reason, we ensure that all papers are submitted earlier, even before the deadline indicated by the customer. For this reason, the client can go through the work and review everything.

100% Originality

At Essay USA, all papers are plagiarism-free as they are written from scratch. We have taken strict measures to ensure that there is no similarity on all papers and that citations are included as per the standards set.

Customer Support 24/7

Our support team is readily available to provide any guidance/help on our platform at any time of the day/night. Feel free to contact us via the Chat window or support email:

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Essay USA has stood as the world’s leading custom essay writing services providers. Once you enter all the details in the order form under the place order button, the rest is up to us.


Essay Writing Services

At Essay USA, we prioritize on all aspects that bring about a good grade such as impeccable grammar, proper structure, zero-plagiarism and conformance to guidelines. Our experienced team of writers will help you completed your essays and other assignments.


Admission and Business Papers

Be assured that you’ll definitely get accepted to the Master’s level program at any university once you enter all the details in the order form. We won’t leave you here; we will also help you secure a good position in your aspired workplace by creating an outstanding resume or portfolio once you place an order.


Editing and Proofreading

Our skilled editing and writing team will help you restructure you paper, paraphrase, correct grammar and replace plagiarized sections on your paper just on time. The service is geared toward eliminating any mistakes and rather enhancing better quality.


Technical papers

We have writers in almost all fields including the most technical fields. You don’t have to worry about the complexity of your paper. Simply enter as much details as possible in the place order section.