Write an inner class called randomcolorlistener

Assignment Help JAVA Programming
Reference no: EM131374064

JAVA PROBLEM

Introduction

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)

• RectanglesGUI.java

Question (2)

• BaseQuiz.java
• FreeQuiz.java
• MultipleChoiceQuiz.java - getQuestions method only
• TrueFalseQuiz.java - getQuestions method only
• Question.java
• FreeQuizQuestion.java
• MultipleChoiceQuestion.java
• GenericQuizClasses.pdf

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

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. cwk1-partA.java 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 theJPanel:

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 theJFrame 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 usingBorderLayout (2 marks). Set the text on the button. The text shouldinvite the user to click the button.

4. Write an inner class called RandomColorListener to implement theActionListener 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 randomColor 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 ActionListenerinterface, 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 theResetListener 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.

Reading for Question (1)

• See: https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html for the constructors, for example Color(int r, int g, int b), that allow developers to make their own Colors.

• Chapter 12 of Head First Java, pages 353-81.

Deliverable for Question (1)

• An electronic copy of your revised program: RectanglesGUI.java

Question (2)

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

FreeQuiz classes, together with the Question, FreeQuizQuestion andMultipleChoiceQuestion classes. BaseQuiz is abstract, and FreeQuiz is one possibleconcrete class extension of it. You should compile and run the FreeQuizclass 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 Questionclass 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 ofBaseQuiz. 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 FreeQuizclass, 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 inBaseQuiz, 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
• Inheritance
• Abstraction

Reading for Question 4:

• https://docs.oracle.com/javase/tutorial/java/generics/

Deliverables for Question (2)

Please hand in an electronic copy of the following:

• Your completed MultipleChoiceQuiz.java

• TrueFalseQuestion.java

• Your completed TrueFalseQuiz.java

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

Appendix

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
6.-256
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
6.256

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.

Reference no: EM131374064

Questions Cloud

What are the symptoms of an oil pump with a leaky shutoff : What are the symptoms of an oil pump with a leaky shutoff?
Create application of your choice utilizing swing components : Create an application of your own choice utilizing the Swing Components. Example: You can make an application of your own choice showing the values of cars depending upon the models or years chosen.
What were the economic and social costs and benefits : Was it ethical of the Ohio Art Company to move production to China? What were the economic and social costs and benefits of this decision? What would have happened if production had not been moved?
What two methods are used to control the indoor fan : What unique problems can arise in oil furnaces because oil is a liquid fuel?
Write an inner class called randomcolorlistener : Write an inner class called RandomColorListener to implement theActionListener interface and to listen to the JButton in the SOUTH region.
What two manual resets do all oil burners have : What safety precaution should be followed when working on a furnace with a tripped primary control?
How did you decide how to respond to the assignment prompt : How did you decide how to respond to the assignment prompt? What steps did you take in approaching your response to the prompt? Why did you decide to take these particular steps
What are the potential costs of adopting a free trade regime : What are the potential costs of adopting a free trade regime? Do you think governments should do anything to reduce these costs? What?
What type of gas space heater has no flame : What is a "salamander"? What type of gas space heater has no flame?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Determine the length of a string

Determine the length of a string. The first version should use array subscripting, and the second version should use pointer arithmetic

  Print out the stock with the highest profit if sold

After the table is displayed, print out the stock with the highest profit if sold and its position in the array[row][column].

  Standard methods-processes for developing client solution

The company has an excellent IT department with experienced staff.As a consultant, you bring to the table standard methods and processes for developing the client solution.

  Explain the types of malware and the potential impact they

write a 200- to 300-word short-answer response to the following bull describe the types of malware and the potential

  Do you think that overall performance of app is use program

Do you think that overall performance of an app is one of the main reasons people wouldn't use the program? If you were designing an app what might be some of the things which could cause performance to increase?

  What value is assigned to the variable phrase

Create a class State with five properties to hold the information about a single state and a method that calculates the density (people per square mile) of the state.

  Write an array where the user can input 7 temperatures

Write an array where the user can input 7 temperatures; include a 'for' loop. Make sure the temperatures are larger than -100 and smaller than or equal to 120, if not, make sure the user enters a valid number. Make sure the temperature is between the..

  Define a car structure type

For this assignment you will write a program that determines which of a set of rental cars the user can rent for a user-specified rental rate. The rental car data is contained in a text file which will be fed into your program using stdin redirect..

  Write a class that has the subsequent fields

Write a class that has the subsequent fields: first name, last name, id number, department, payRate, hoursWork with constructors that accepts the subsequent values as arguments and assigns them to the appropriate fields.

  Write down a java program which reads a text file called

write a java program which reads a text file called mydata.txt containing the following numbers 23 18 78 64 28 39 92 93

  Program that lets the user enter four quarterly sales

The figures should be stored in a two-dimensional array. Once the figures arc entered, the program should display the following data for each quarter:

  Implement a probability calculator for the standard normal

implement a probability calculator for the standard normal distribution using GUI.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd