Important java questions

Assignment Help Data Structure & Algorithms
Reference no: EM1380171

Question: Add a method addText to the Question class, and provide a different implementation of ChoiceQuestion that calls addText rather than storing an array list of selections.

Here is a sample program run:

Who was the inventor of Java?

In which country was the inventor of Java born?
1: Australia
2: Canada
3: Denmark
4: United States

Use the following class as your main class:

import java.util.Scanner;

public class QuestionDemo
{
public static void main(String[] args)
{
Question[] quiz = new Question[2];

quiz[0] = new Question("Who was the inventor of Java?");
quiz[0].setAnswer("James Gosling");

ChoiceQuestion question = new ChoiceQuestion(
"In which country was the inventor of Java born?");
question.addChoice("Australia", false);
question.addChoice("Canada", true);
question.addChoice("Denmark", false);
question.addChoice("United States", false);
quiz[1] = question;

Scanner in = new Scanner(System.in);
for (Question q : quiz)
{
q.display();
System.out.println("Your answer: ");
String response = in.nextLine();
System.out.println(q.checkAnswer(response));
}
}
}

Complete the following classes in your solution:

/**
A question with multiple choices.
*/
public class ChoiceQuestion extends Question
{
// Add any needed instance variables, but don't store the choices
// The choices should be added to the text of the superclass

/**
Constructs a choice question with a given text and no choices.
@param questionText the text of this question
*/
public ChoiceQuestion(String questionText)
{
...
}

/**
Adds an answer choice to this question.
@param choice the choice to add
@param correct true if this is the correct choice, false otherwise
*/
public void addChoice(String choice, boolean correct)
{
...
}
}

/**
A question with a text and an answer.
*/
public class Question
{
private String text;
private String answer;

/**
Constructs a question with a given text and an empty answer.
@param questionText the text of this question
*/
public Question(String questionText)
{
text = questionText;
answer = "";
}

/**
Sets the answer for this question.
@param correctResponse the answer
*/
public void setAnswer(String correctResponse)
{
answer = correctResponse;
}

/**
Checks a given response for correctness.
@param response the response to check
@return true if the response was correct, false otherwise
*/
public boolean checkAnswer(String response)
{
return response.equals(answer);
}

/**
Add a line of text to the question text.
*/
public void addLine(String line)
{
...
}

/**
Displays this question.
*/
public void display()
{
System.out.println(text);
}
}

 

Reference no: EM1380171

Questions Cloud

Preparing a java program : Prepare a program that asks the user how many automobiles are to be explained, and for each automobile it inputs the user's selection of make and color.
Creating villian : Announce a new Villian called sharpay who has a wit of 24, a stealth of sixteen, and who has currently claimed three victims: Chad, Troy, and Gabriella.
Creating a class for services : Make a class for services offered by a hair styling salon. Information fields with a String to hold the service description, a double to hold the price, and an integer to hold average number of minutes it takes to perform the service.
Design and implement a sorting algorithm : Your task is to design and implement a sorting algorithm and running time grows as a linear function of the array
Important java questions : Add a method addText to the Question class, and provide a different implementation of Choice Question that calls add Text rather than storing an array list of selections.
Benefits of dynamic over static arrays : Discuss the benefits of dynamic over-static arrays. Under what conditions will you choose dynamic arrays?
Creating class diagram : Think about a computer system used to manage loans for a library. Libraries loan books, CDs, videos and magazines to registered members.
Creating flowchart to compute and print the total sale : A coorporation's salesman are selling toothpaste and tooth powder. The corporation having fifty salesman gives 10% commission on the sale of toothpaste and 20 percent commission on tooth powder.
Creating a flowchart : Create a flowchart to illustrate the given problem. You are given input for the student name, number of credits, and cost per credit.

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Calculate worst-case run-time complexity of algorithm

Calculate the worst-case run-time complexity of your algorithm and prove optimality of the solution it gives. Suppose that the road is a straight line with a western end and an eastern end.

  Computing time complexity of procedure

What is the time complexity of the procedure? If A[l .. r] = [24, 30, 09, 46, 15, 19, 29, 86,78], what is the output?

  Data structures and algorithms

Provides learners with an understanding of how data structures are used in algorithms and enables them to design and implement data structures

  Difference between workbook and worksheet

Discuss the difference between a workbook and a worksheet and explain why would you want to use individual worksheets when using Excel?

  Linear-time algorithm to find odd-length cycle in graph

Give a linear-time algorithm to find an odd-length cycle in a directed graph. You may not suppose that graph is strongly connected.

  Write the algorithm which takes as input npda

Write the algorithm (described informally) which takes as input NPDA A and determines whether the language of A is nonempty.

  Create algorithm which takes as inputs matrices

Create the algorithm which takes as inputs, matrices C, D, and vertex indices i and j, and returns minimum-cost path from vertex i to vertex j.

  Developing gui application with jframe

Make a GUI application with JFrame that haves 5-labels explaining reasons that a customer might not buy a specific product.

  Insertion sort and merged using standard merging mechanism

Using "insertion sort" and then merged using standard merging mechanism, where k is value to be determined. How must be we select k in practice?

  Describe sorting algorithms and how they work

Describe sorting algorithms and how they work

  Design a representation of display screen

Create a form that lists possible potatoes and toppings in a manner that is easy for counter servers and kitchen crew to scan, and can also be used as input for the inventory reorder system.

  Algorithm to produce schedule for least completion time

What is the best order for sending people out, if one wants whole competition to be over as early as possible? More precisely, provide efficient algorithm which produces schedule whose completion time is as small as possible.

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