IVE ALREADY CODED MOST OF THIS PROGRAM BUT I CANNOT DO THIS LAST QUESTION. I\'VE
ID: 3674291 • Letter: I
Question
IVE ALREADY CODED MOST OF THIS PROGRAM BUT I CANNOT DO THIS LAST QUESTION. I'VE PROVIDED THE CODE FOR ALL FOUR CLASSES AT THE BOTTOM:
Note: This must be written in java. You don't have to repaste all the java classes, you can just give me the method for Drill and I can put it in and what lines of code to put in what class. I just need someone to do this because I can't figure it out.
• Add a new class Drill. The class will implement all methods needed for studying (e.g., pickCard, getChallenge, getResponse, checkAnswer, hasCard). Under this method, the flashcards of Box 1 are copied into a separate pool. The application randomly picks one flashcard from the pool. If the user knows the answer, the card is removed from the pool. If the user does not know the answer the card remains in the pool. Drill ends when the pool is empty. When users end the drill session, the pool can be discarded. During a drill session the Leitner boxes remain unchanged.
------code-----
Lietner.java:
package mngrace_a5;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
/**
* A class implementing the Leitner system for flashcard boxes.
*
* @invariant boxes != null
*/
public class Leitner
{
/**
* Constructs a new Leitner object.
*
* @precondition theBoxes != null
*/
public Leitner(ArrayList theBoxes)
{
assert theBoxes != null;
boxes = theBoxes;
}
/**
* Internal method that computes the weight of a box.
*
* The weight of a Leitner box depends on the box id the number of cards
* it stores. The current implementation assigns a card in a lower box double
* the weight as a card in the next higher box.
*
* @param box the box for which this weight is computed.
* @param numBoxes total number of boxes. This is needed to compute the
* distance from box to the box with the highest Leitner id.
* @return the weight of this box.
*
* @precondition box != null && numBoxes >= box.id()
*/
private int weight(Box box, int numBoxes)
{
assert box != null && numBoxes >= box.id();
// Each card in box number N has a weight of 2 ^ (|boxes| - N).
// Note: 1 << X left-shifts the number 1 by X position ( = 2^X )
return (1 << (numBoxes - box.id())) * box.size();
}
/** Computes the total weight of all boxes */
private int totalWeight()
{
int num = 0;
for (Box box : boxes)
{
num += weight(box, boxes.size());
}
return num;
}
/**
* Picks a random card from all boxes.
*
* Cards in lower boxes receive higher weight (priority).
*
* @precondition there must be at least one box with one card.
*/
public void pickCard()
{
int maxRand = totalWeight();
assert maxRand > 0; // there must be at least one box with one card.
// rndVal identifies the box where the card is located
// | weight of box 1 | w. of box 2 | weight of Box 3 ... |
// ^=0 [min rndVal] ^first value for box 2 ^max(rndVal) [last value for highest box]
// ^ rndVal [falls in Box 2]
int rndVal = rand.nextInt(maxRand);
// Identify the box from where the card will be picked.
Iterator boxIter = boxes.iterator();
Box currBox = boxIter.next();
// Subtract the box's weight from rndVal until rndVal < weight(box)
// => we have identified the box.
while (rndVal >= weight(currBox, boxes.size()))
{
rndVal -= weight(currBox, boxes.size());
assert boxIter.hasNext();
currBox = boxIter.next();
}
// Set the box from where the next card will be picked.
box = currBox;
// All cards in a box are equally likely to be picked,
// thus we generate another pseudo random number.
card = box.get(rand.nextInt(box.size()));
// Choose side of card.
displayFront = (rand.nextInt(2) == 0);
}
/**
* Returns the question for the last picked card.
*
* @return the question
*
* @precondtion pickCard() has been called at least once
*/
public String getQuestion()
{
assert card != null;
return displayFront ? card.getChallenge() : card.getResponse();
}
/**
* Returns the answer for the last picked card.
*
* @return the answer
*
* @precondtion pickCard() has been called at least once
*/
public String getAnswer()
{
assert card != null;
return displayFront ? card.getResponse() : card.getChallenge();
}
/**
* Validates the response against the last picked card.
*
* @param s the response.
* @return true, iff s was correct. s is assumed to be correct, if it is empty
* or the string returned by getAnswer equals s.
*
* @precondition s != null
*/
public boolean checkAnswer(String s)
{
assert s != null;
int newBoxId = 1; // set target box id in case the response is incorrect
boolean isCorrect = "".equals(s) || getAnswer().equals(s);
if (isCorrect)
{
// response is correct -> update target box id
newBoxId = Math.min(box.id() + 1, boxes.size());
}
box.remove(card); // remove card from current box
boxes.get(newBoxId - 1).add(card); // store card in target box
return isCorrect;
}
private final Random rand = new Random(); /// random number generator.
private Box box = null; /// last picked box.
private FlashCard card = null; /// last picked card.
private boolean displayFront = true; /// indicates side to display.
private final ArrayList boxes; /// the list of boxes.
}
FlashCardApp.java
package mngrace_a5;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Class implementing a flashcard application
*
* @invariant boxes != null && boxes.size() > 0
*/
public class FlashCardApp
{
private final int MAX_BOXES = 5; /// Number of boxes
/** Constructs a new flashcard app object and sets the number of boxes */
public FlashCardApp()
{
for (int i = 1; i <= MAX_BOXES; ++i)
{
boxes.add(new Box(i));
}
}
/** Returns an object according to the Leitner study method. */
public Leitner leitner() { return new Leitner(boxes); }
/** Returns an iterator that lists all flash cards in the system. */
public Iterator listAll()
{
ArrayList allCards = new ArrayList();
for (Box box : boxes)
{
allCards.addAll(box.getCards());
}
return allCards.iterator();
}
/**
* Returns an iterator that lists all flash cards containing a given pattern.
*
* @param pattern search pattern for texts on flashcards.
* @return Iterator where all elements contain pattern in either
* front or back of the card.
*
* @precondition pattern != null
*/
public Iterator list(String pattern)
{
assert pattern != null;
ArrayList foundCards = new ArrayList();
for (Box box : boxes)
{
Iterator cardIter = box.iterator();
while (cardIter.hasNext())
{
FlashCard currCard = cardIter.next();
boolean inclCard = ( currCard.getChallenge().indexOf(pattern) >= 0
|| currCard.getResponse().indexOf(pattern) >= 0
);
if (inclCard) foundCards.add(currCard);
}
}
return foundCards.iterator();
}
/**
* Returns an iterator that lists all flash cards in a given box.
*
* @param boxid Leitner box id.
* @return Iterator where all elements contain pattern in either
* front or back of the card.
*
* @precondition 0 < boxid <= number of boxes in the app
*/
public Iterator list(int boxid)
{
assert boxid > 0 && boxid <= boxes.size();
return boxes.get(boxid - 1).iterator();
}
/**
* Creates a new flashcard and adds it to the first box.
*
* @precondition challenge != null && response != null
*/
public void create(String challenge, String response)
{
assert challenge != null && response != null;
boxes.get(0).add(new FlashCard(challenge, response));
}
private final ArrayList boxes = new ArrayList(); /// List of boxes
}
FlashCard.java
package mngrace_a5;
/**
* Represents a text based flashcard.
*
* This class is immutable.
* @invariant front != null && back != null
*/
public class FlashCard
{
/**
* Constructor setting up a flashcard object.
*
* @param challenge front of the card
* @param response back of the card
* @precondition challenge != null && response != null
*/
FlashCard(String challenge, String response)
{
assert challenge != null && response != null;
front = challenge;
back = response;
}
/** Returns the front side. */
public String getChallenge() { return front; }
/** Returns the back side. */
public String getResponse() { return back; }
/**
* Produces a textual representation of this flashcard.
*
* @return a string containing two lines, one for front and one for back.
*/
public String toString()
{
return front + ' ' + back + ' ';
}
private final String front;
private final String back;
}
Box.java
package mngrace_a5;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Implements a Leitner box holding flashcards.
*
* @invariant boxnum > 0 && cards != null
*/
public class Box
{
/**
* Constructs a new Box.
*
* @param num Leitner box number
* @precondition num > 0
*/
public Box(int num)
{
assert num > 0;
boxnum = num;
cards = new ArrayList();
}
/** returns current number of flashcards in the box. */
public int size() { return cards.size(); }
/**
* returns specific flashcard from the box.
*
* @param idx flashcard index
* @return the queried Flashcard
* @precondition 0 <= idx < size()
*/
public FlashCard get(int idx) { return cards.get(idx); }
/**
* Adds a new flashcard to this box.
*
* @param card the new flashcard
* @precondition card is not yet in the box
*/
public void add(FlashCard card) { cards.add(card); }
/**
* Removes a flashcard from the box.
*
* @param card the card to be removed
* @precondition card is in this box
*/
public void remove(FlashCard card) { cards.remove(card); }
/**
* Removes a flashcard from the box.
*
* @param idx of the card to be removed
* @precondition 0 <= idx < size()
*/
public void remove(int idx) { cards.remove(idx); }
/**
* Returns an iterator to go through all flashcards in this box.
*
* @return an Iterator
*/
public Iterator iterator() { return cards.iterator(); }
/** Returns the Leitner-ID of this box. */
public int id() { return boxnum; }
/**
* "Convenience function" that allows to add all of these cards to another box.
* @return a reference to the internal box storage.
* @todo This breaks encapsulation and exposes more class internals than needed.
*/
List getCards() { return cards; }
private final int boxnum; /// Leitner id
private final ArrayList cards; /// Flashcard storage
}
Explanation / Answer
This below code may helps you..
You have to provide your full Requirement,then it's easy for us to code..
Here you given about only one class..
package sophy;
import java.util.ArrayList;
public class Drill {
ArrayList<String> arlist=new ArrayList();
public void pickCard(){
if("add condition"!=null){
arlist.remove(i);
}
}
public void getChallenge(){
}
public void getResponse(){
}
public void checkAnswer(){
}
public void hasCard(){
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.