11 Dec Give examples showing how ‘super’ and ‘this’ are useful with inher
1>
Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of using "super" and "this" both as constructors and as variables. (300 words)
2>
attached
CS 1102 Unit 5 – Programming Assignment
In this assignment, you will again modify your Quiz program from the previous assignment. You will create an abstract class called "Question", modify "MultipleChoiceQuestion" to inherit from it, and add a new subclass of "Question" called "TrueFalseQuestion".
This assignment will again involve cutting and pasting from existing classes. Because you are learning new features each week, you are retroactively applying those new features. In a typical programming project, you would start with the full suite of features, enabling you to re-use code in ways natural to Java and avoid cutting and pasting.
First create the new "Question" abstract class.
• Open your "CS1102" project in Eclipse. • Select "New" -> "Class" from the File menu. • Enter the Name "Question". • Check the box for the "abstract" modifier. • Click "Finish".
The new file "Question.java" should appear in the editor pane. Make sure it is also listed under "(default package)" in the "Package Explorer" pane, along with "Quiz.java" and "MultipleChoiceQuestion.java".
• If you forgot to check the "abstract" box, add the modifier "abstract" to the "Question" class. public abstract class Question {
Add variables and methods to the "Question" class that will be inherited by the subclasses. Copy the following from the "MultipleChoiceQuestion" class and paste them into the "Question" class.
• The class variables "nQuestions" and "nCorrect". • The instance variables "question" and "correctAnswer". • The instance method "check". • The class method "showResults".
Do not copy the constructor for "MultipleChoiceQuestion" or the instance method "ask".
The editor pane for "Question" should now show one error at the statement in "check" that calls the "ask" method.
• Add an abstract declaration for the "ask" method. This should be inside the "Question" class but outside all the method definitions. abstract String ask();
Note that this is a method declaration only, with no "{…}", because the method is abstract. It must be defined (implemented) in any concrete (non-abstract) subclasses of "Question".
The error warning in the editor pane should disappear. The "ask" call in "check" will use the methods defined in the subclasses of "Question".
Now modify the "MultipleChoiceQuestion" class to be a subclass of "Question".
• Delete from "MultipleChoiceQuestion" all the variables and methods that you pasted into "Question": "nQuestions", "nCorrect", "question", "correctAnswer", "check", and "showResults".
• Do not delete the constructor or the "ask" method.
The editor pane should show many errors, particularly in the constructor.
• Make "MultipleChoiceQuestion" a subclass of "Question" using the "extends" keyword. public class MultipleChoiceQuestion extends Question {
All the error warnings should disappear.
Convert "Quiz" to use "Question" variables with "MultipleChoiceQuestion" objects.
• Change the type of any "MultipleChoiceQuestion" variables in the main method of "Quiz" to "Question".
• But do not change the constructor calls used to initialize these variables. They should still be "MultipleChoiceQuestion".
Your program should still work using "Question" variables to reference "MultipleChoiceQuestion" objects. Test it to make sure.
Next add a new subclass of "Question" for true/false questions.
• Select "New" -> "Class" from the File menu. • Enter the Name "TrueFalseQuestion". • Enter the Superclass "Question". • Click "Finish".
The new file "TrueFalseQuestion.java" should appear in the editor pane.
• If you did not add the Superclass name, you will need to add "extends Question" to the class declaration. public class TrueFalseQuestion extends Question {
The IDE may have already added an empty "ask" method because of the abstract method it found in "Question". It may have also added the annotation "@Override", which is optional and instructs the compiler to make sure the method actually does override a method in a parent class.
Add an implementation of the "ask" method that is specific to true/false questions.
• Import the "JOptionPane" package. • Like in "ask" for "MultipleChoiceQuestion", have the new "ask" pose the "question" String
repeatedly until the user provides a valid answer.
• In this case, valid answers are: "f", "false", "False", "FALSE", "n", "no", "No", "NO", "t", "true", "T", "True", "TRUE", "y", "yes", "Y", "Yes", "YES".
• When users provide an invalid answer, display the message, "Invalid answer. Please enter TRUE or FALSE."
• Convert any valid answer representing true or yes to "TRUE" and any valid answer representing false or no to "FALSE".
• Hint: Convert all answers to upper case before checking their validity.
Add a "TrueFalseQuestion" constructor to initialize the "question" and "correctAnswer" Strings inherited from "Question".
• Give the constructor two String parameters, "question" and "answer". • Use "this" to set the instance variable "question" using the parameter "question". Add the text,
"TRUE or FALSE: " to the beginning of the question. this.question = "TRUE or FALSE: "+question; (You could also use "super.question", since the instance variable is in "Question". Either works because "TrueFalseQuestion" does not hide the instance variable in "Question" with its own instance variable named "question".)
• Set the instance variable "correctAnswer" to only "TRUE" or "FALSE" based on "answer". Allow the parameter "answer" to be any String that is considered valid by "ask".
Now add a true/false question to your quiz!
• Initialize a "Question" variable using a "TrueFalseQuestion" constructor. Question question = new TrueFalseQuestion(…);
Run your program and test that the following are true.
• It accepts only valid answers. • It responds appropriately to correct and incorrect answers. • It provides accurate counts of correct answers and total questions.
Finally, add at least four more true/false questions, for a total of at least five multiple-choice questions and five true/false questions. Test your program again.
,
import javax.swing.JOptionPane; public class Quiz { //static int variables that counts number of question and correct answers. static int nQuestions = 0; static int nCorrect = 0; public static void main(String[] args) { String question1 = "What is java?n"; question1 += "A. A shorthand for javascript.n"; question1 += "B. A programing language.n"; question1 += "C. A machine readable code.n"; question1 += "D. An organization for creating code syntax.n"; question1 += "E. None of the above."; check(question1, "C"); String question2 = "What is the popular search enginen"; question2+="A. Yahoon"; question2+="B. Googlen"; question2+="C. Bingn"; question2+="D. Duck Duck Gon"; check(question2,"B"); //question 3. String question3; question3 = "What is the color of the sky at night n"; question3+="A. Redn"; question3+="B. Bluen"; question3+="C. Black n"; question3+="D. Redn"; check(question3,"C"); //prints number of correct answers out of number of questions. JOptionPane.showMessageDialog(null, nCorrect + " correct out of " + nQuestions + " questions"); } //method ask. public static String ask(String question) { //keep asking if the answer entered is Invalid while(true) { String answer = JOptionPane.showInputDialog(question); answer = answer.toUpperCase(); if(!(answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D") || answer.equals("E"))){ JOptionPane.showMessageDialog(null,"Invalid Answer. Please enter A, B, C, D, or E."); continue; } return answer; //return answer entered by user. } } //method check static void check(String question, String correctAnswer) { nQuestions++;//increments the number of question. String answer = ask(question);//calls ask method to ask question. if(answer.equals(correctAnswer)) { JOptionPane.showMessageDialog(null,"Correct!"); nCorrect++;//increments if answer is correct. } else{ JOptionPane.showMessageDialog(null, "Incorrect. The correct answer is " + correctAnswer); } } }
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Wridemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.