Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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: http://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.

Java, Programming

  • Category:- Java
  • Reference No.:- M92183899

Have any Question?


Related Questions in Java

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As