+1 (315) 557-6473 

Java Program to Create Flashcard Application Assignment Solution.


Instructions

Objective
Write a program to create flashcard application in java.

Requirements and Specifications

program to create flashcard application in java
      program to create flashcard application in java 1

Source Code

FLASH CARD

/**

* This class represents a FlashCard object.

*/

import java.util.Random;

public class FlashCard {

/**

* To contain the text of the challenge.

*/

private String challenge;

/**

* Contains the response.

*/

private String response;

/**

* Variables used to contain the challenge or response.

*/

private String question;

/**

* Variables used to contain the challenge or response.

*/

private String answer;

/**

* Random variable.

*/

private Random rnd;

/**

* Constructor given challenge and response.

* @param challenge String

* @param response String

*/

public FlashCard(String challenge, String response) {

this.challenge = challenge;

this.response = response;

this.question = "";

this.answer = "";

this.rnd = new Random();

}

/**

* Getter for challenge variable.

* @return String

*/

public String getChallenge() {

return challenge;

}

/**

* Getter for response variable.

* @return String

*/

public String getResponse() {

return response;

}

/**

* Show one face of the card.

*/

public void show() {

if (rnd.nextInt(2) == 0) {

this.question = this.challenge;

this.answer = this.response;

} else {

this.question = this.response;

this.answer = this.challenge;

}

}

/**

* Getter for question variable.

* @return String

*/

public String getQuestion() {

return this.question;

}

/**

* Getter for answer variable.

* @return String

*/

public String getAnswer() {

return this.answer;

}

}

DRIVER

/**

* This class represents the Driver of the program.

*/

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class Driver {

/**

* This function displays the menu to user and requests for an input.

* @param sc Scanner

* @return char

*/

public static char menu(Scanner sc) {

System.out.println("(a) Flip the card");

System.out.println("(b) Indicate answer");

System.out.println("(c) Indicate that you do not know the answer");

System.out.println("(d) Exit");

// Create Scanner

// Ask for input

char input;

while (true) {

input = sc.nextLine().toLowerCase().charAt(0);

if (input == 'a' || input == 'b' || input == 'c' ||input == 'd') {

return input;

} else {

System.out.println("Please enter a valid option.");

}

}

}

/**

* This is the main function.

* @param args Argumnents

* @throws Exception Exception

*/

public static void main(String[] args) throws Exception {

// Create Scanner

Scanner sc = new Scanner(System.in);

// Create a Random object

Random rnd = new Random();

// First, create the 5 boxes

Box box1 = new Box(0.5161);

Box box2 = new Box(0.2581);

Box box3 = new Box(0.1290);

Box box4 = new Box(0.0645);

Box box5 = new Box(0.0323);

// Add two card to each box

box1.addCard(new FlashCard("Dog", "Perro"));

box1.addCard(new FlashCard("Cat", "Gato"));

box2.addCard(new FlashCard("Keyboard", "Teclado"));

box2.addCard(new FlashCard("Chair", "Silla"));

box3.addCard(new FlashCard("I walk", "Yo camino"));

box3.addCard(new FlashCard("I am sleeping", "Yo estoy durmiendo"));

box4.addCard(new FlashCard("Monday in the morning", "Lunes por la mañana"));

box4.addCard(new FlashCard("Thursday afternoon", "Jueves por la tarde"));

box5.addCard(new FlashCard("I went to the school", "Yo fui a la escuela"));

box5.addCard(new FlashCard("I ate too much", "Comi demasiado"));

// Create an arraylist containing boxes

ArrayList boxes = new ArrayList();

boxes.add(box1);

boxes.add(box2);

boxes.add(box3);

boxes.add(box4);

boxes.add(box5);

// begin program

char input;

FlashCard picked = null;

int pickedFrom = -1;

String answer;

double prob;

while(true) {

// First, generate andom number to know from which box we will pick the card

while(picked == null) {

prob = rnd.nextDouble();

if(prob <= boxes.get(0).getProbability() && boxes.get(0).count() > 0) {

picked = boxes.get(0).drawCard();

pickedFrom = 0;

} else if(prob <= boxes.get(1).getProbability() && boxes.get(1).count() > 0) {

picked = boxes.get(1).drawCard();

pickedFrom = 1;

} else if(prob <= boxes.get(2).getProbability() && boxes.get(2).count() > 0) {

picked = boxes.get(2).drawCard();

pickedFrom = 2;

} else if(prob <= boxes.get(3).getProbability() && boxes.get(3).count() > 0) {

picked = boxes.get(3).drawCard();

pickedFrom = 3;

} else if(prob <= boxes.get(4).getProbability() && boxes.get(4).count() > 0) {

picked = boxes.get(4).drawCard();

pickedFrom = 4;

}

}

// Flip card

picked.show();

// DisplayEn esta fu

System.out.println("============================");

System.out.println("Picking a new card...");

System.out.println("The card shows: " + picked.getQuestion());

System.out.println("============================");

input = menu(sc);

if(input == 'a') {

// user decided to flip the card

// Display answer

System.out.println("You flip the card. The other side displays: " + picked.getAnswer());

// Add to box 1

boxes.get(0).addCard(picked);

} else if(input == 'b') {

System.out.print("Please enter your answer: ");

answer = sc.nextLine();

if(picked.getAnswer().toLowerCase().equals(answer.toLowerCase())) {

System.out.println("Correct!");

// Move card to next box (if there is any)

if(pickedFrom < 4) {

boxes.get(pickedFrom+1).addCard(picked);

}

} else {

System.out.println("Sorry, that's incorrect.");

// Move card to box 1

boxes.get(0).addCard(picked);

}

} else if(input == 'c') {

// user do not know the answer

// Send to box 1 without showing the answer

// Add to box 1

boxes.get(0).addCard(picked);

// Clear picked variable

picked = null;

} else if(input == 'd') {

System.out.println("Good bye!");

break;

}

picked = null;

System.out.println("");

}

}

}