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

  Process of insertion into a heap-implemented priority queue

Explain the process of insertion into a heap-implemented priority queue, and informally explain its complexity and the process of removal from a heap-implemented priority queue, and informally explain its complexity.

  Factors-principles considering indecency regulation issues

What factors and principles should the federal government take into account when considering indecency regulation issues?

  Determine schedule that obtains maximum amount of profit

Assume you have one machine and a set of n jobs a1, a2, ..., an to process on that machine. Determine the schedule that obtains the maximum amount of profit. Compute the running time of your algorithm?

  Creating an object oriented data model

Create an object oriented data model, including all appropriate notations, to represent the given situation. In a particular region there are a number of gardens.

  Algorithm for finding smallest element in unsorted array

Consider the following algorithm for finding the smallest element in an unsorted array: RANDOMMIN(A[1 .. n]). What is the exact expected number of executions of line ( )?

  Dynamic-programming algorithm for rod-cutting problem

Consider a modification of the rod-cutting problem in which, in addition to a price pi for each rod, each cut incurs a fixed cost of c. Give a dynamic-programming algorithm to solve this modified problem.

  Create algorithm to prepare daily hotel charge report

Create the algorithm to prepare the daily hotel charge report. Input consists of series of records which contain a room number, customer name, cost of the room, and cost of meals charged to the room.

  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.

  Processor sharing to worse performance than fcfs

Create a second experiment answering the question "Is it possible for processor sharing to have worse performance than FCFS? "

  Explaining instruction format of operation code field

Operation code field, a mode field, to specify one of seven addressing modes, a register address field to specify one of 60 processor registers, and memory address. Specify instruction format and number of bits in each field if the instruction ..

  Explaining elementary operations used in algorithm

How many elementary operations are used in algorithm given below? The elementary operations are comparison operations (such as > and

  Efficient algorithm to achieve goal using few base stations

Certain points along the road, so that every house is within four miles of one of the base stations. Give an efficient algorithm that achieves this goal using as few base stations 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