I am tasked to Develop a ashcard application using the Leitner system and a simp
ID: 669616 • Letter: I
Question
I am tasked to Develop a ashcard application using the Leitner system and a simple text user interface (TUI). The TUI should be able to do the following:
-- Import a new set of cards. The TUI supports a command import followed by a lename.
The application opens the le and for each pair of challenge and response, a new ashcard is
created and stored in Box 1. When all entries have been imported, the application prints out
how many ashcards have been imported.
Suggested syntax:
import lename
-- Study mode according to Leitner system. To enter the study mode, the user enters study.
In study mode, the application randomly picks a ashcard out of all boxes. Note, a card in
Box 1 is picked twice as likely as from Box 2, four time as likely as from Box 3, etc. The
system randomly chooses to present the student one side of the card. As response, a student
enters a string.
– Empty string: Indicates that the student knows the answer. The card is moved to the
next higher box (if any). The application proceeds by randomly picking another card.
– Non-empty string: The string is compared with the other side of the ashcard. If the
strings are identical, the answer is correct and the ashcard moves to the next box
(if any). If the answer is incorrect, the ashcard is placed in Box 1. The application
proceeds by randomly picking another card.
– Special strings: (1) !exit indicates that the student ends the study session. The cur-
rently displayed ashcard remains in the same box. (2) % shows the other side of the
ashcard.
--List a box’s content. The TUI supports a command list followed by a number. The ap-
plication prints out each card that is currently in the specied box. A suggested format is
challenge, response, empty line.
--List all cards that contain a given string. The TUI supports a command list followed by a
blank and a string that ends with the line. Any ashcard that contains the string either in
challenge or response is listed. For example:
list ee
prints all ashcards containing “ee”
--To exit an application, the student enters !exit.
The TUI interface needs to be separated from the core application.
Any and ALL Help is very much appreciated!!
Explanation / Answer
CLASS PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.swing.filechooser.*;
import java.net.URL;
import java.awt.event.*;
import java.util.Random;
public class Program extends JFrame implements ActionListener
{
Flashcard[] cards;
JButton flipCard,settings,nextCard,prevCard;
JTextField termField,definitionField;
boolean randomOrder,termFirst;
int curCard;
File f;
public Program()
{
curCard = 0;
randomOrder = false;
termFirst = true;
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
f = new File(chooser.getSelectedFile().getPath());
}
cards = new Flashcard[0];
flipCard = new JButton("Flip Card");
settings = new JButton("Settings");
nextCard = new JButton("Next Card");
prevCard = new JButton("Previous Card");
termField = new JTextField("Terms");
definitionField = new JTextField("Definitions");
flipCard.addActionListener(this);
settings.addActionListener(this);
nextCard.addActionListener(this);
prevCard.addActionListener(this);
setSize(500,500);
setVisible(true);
setLayout(new GridLayout(3,2));
add(flipCard);
add(settings);
add(nextCard);
add(prevCard);
add(termField);
add(definitionField);
try{showFile();
}
catch(IOException exc){}
loadFirstCard();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == flipCard)
{
flipCard();
}
else if(e.getSource() == settings)
{
new Settings(this);
}
else if(e.getSource() == nextCard)
{
nextCard();
}
else if(e.getSource() == prevCard)
{
curCard--;
if(curCard < 0)
curCard = cards.length - 1;
termField.setText(cards[curCard].term);
definitionField.setText(cards[curCard].definition);
setProperSide();
}
}
public void addFlashCard(String t, String d)
{
Flashcard[] tempCards = cards;
cards = new Flashcard[tempCards.length+1];
for(int i = 0; i < tempCards.length; i++)
{
cards[i] = tempCards[i];
}
cards[tempCards.length] = new Flashcard(t,d);
}
public void showFile()
throws IOException
{
InputStream fstream = openFile();
BufferedReader in = new BufferedReader(new InputStreamReader(fstream));
System.out.println("File: ");
String line = in.readLine();
while(line != null)
{
String term = "";
String definition = "";
int colon = line.indexOf(":");
if(colon >= 0)
{
term = line.substring(0,colon);
definition = line.substring(colon+1,line.length());
boolean termEmpty = term.equals("");
boolean definitionEmpty= definition.equals("");
if(!termEmpty && !definitionEmpty)
addFlashCard(term,definition);
}
line = in.readLine();
}
}
public void checkedShowFile()
{
try
{
showFile();
}
catch(IOException exc)
{
System.out.println("There was a problem showing this file.");
System.out.println("The error encountered is:");
System.out.println(exc);
}
}
public InputStream openFile()
throws IOException
{
if(f.getName() == null)
throw new IOException("Cannot open file - filename was null.");
URL url = getClass().getClassLoader().getResource(f.getName());
if(url == null)
throw new IOException("File not found: " + f.getName());
return url.openStream();
}
public void loadFirstCard()
{
if(cards.length > 0)
{
termField.setText(cards[0].term);
definitionField.setText(cards[0].definition);
definitionField.setVisible(false);
}
}
public void flipCard()
{
if(termField.isVisible())
{
termField.setVisible(false);
definitionField.setVisible(true);
}
else
{
termField.setVisible(true);
definitionField.setVisible(false);
}
}
public void nextCard()
{
if(randomOrder)
{
Random r = new Random();
int x = Math.abs(r.nextInt()) % cards.length;
while(x == curCard)
x = Math.abs(r.nextInt()) % cards.length;
curCard = x;
}
else
{
curCard++;
if(curCard == cards.length)
curCard = 0;
}
termField.setText(cards[curCard].term);
definitionField.setText(cards[curCard].definition);
setProperSide();
}
public void setProperSide()
{
if(termFirst)
{
termField.setVisible(true);
definitionField.setVisible(false);
}
else
{
termField.setVisible(false);
definitionField.setVisible(true);
}
}
public static void main(String[] args)
{
new Program();
}
}
CLASS SETTINGS:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Settings extends JFrame implements ActionListener
{
JButton order,sideFirst;
JTextField orderText,sideFirstText;
Program prog;
public Settings(Program p)
{
prog = p;
setSize(500,500);
setLayout(new GridLayout(2,2));
setVisible(true);
if(!prog.randomOrder)
{
order = new JButton("Random Card Order");
orderText = new JTextField("Normal card order");
}
else
{
order = new JButton("Normal Card Order");
orderText = new JTextField("Random card order");
}
if(!prog.termFirst)
{
sideFirst = new JButton("Show Term First");
sideFirstText = new JTextField("Showing definition first");
}
else
{
sideFirst = new JButton("Show Definition First");
sideFirstText = new JTextField("Showing term first");
}
order.addActionListener(this);
sideFirst.addActionListener(this);
add(order);
add(sideFirst);
add(orderText);
add(sideFirstText);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == order)
{
if(prog.randomOrder)
{
prog.randomOrder = false;
orderText.setText("Normal Card Order");
}
else
{
prog.randomOrder = true;
orderText.setText("Random Card Order");
}
}
else if(e.getSource() == sideFirst)
{
if(prog.termFirst)
{
prog.termFirst = false;
sideFirstText.setText("Showing Definition First");
}
else
{
prog.termFirst = true;
sideFirstText.setText("Showing Term First");
}
}
}
}
CLASS FLASHCARD:
public class Flashcard
{
String term, definition;
public Flashcard(String t, String d)
{
term = t;
definition = d;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.