Introduction
The Flashcard App is a simple Java program designed to help users study by creating flashcards. The app will allow users to store questions and answers, then quiz themselves by displaying the questions and showing the answers upon request. The goal of this application is to provide an efficient and interactive way to test knowledge on various subjects.
Objective
The main objective of this project is to create a user-friendly and interactive flashcard application using Java. The app should allow users to:
- Create and store flashcards.
- View flashcards and their answers.
- Navigate between cards easily during study sessions.
- Enhance memory retention through a quiz format.
Code
Below is the Java code to create the Flashcard App:
import java.util.ArrayList;
import java.util.Scanner;
class Flashcard {
String question;
String answer;
Flashcard(String question, String answer) {
this.question = question;
this.answer = answer;
}
}
public class FlashcardApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList
boolean running = true;
System.out.println("Welcome to the Flashcard App!");
while (running) {
System.out.println("\n1. Add Flashcard");
System.out.println("2. Study Flashcards");
System.out.println("3. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("\nEnter question: ");
String question = scanner.nextLine();
System.out.print("Enter answer: ");
String answer = scanner.nextLine();
flashcards.add(new Flashcard(question, answer));
System.out.println("Flashcard added successfully!");
break;
case 2:
if (flashcards.isEmpty()) {
System.out.println("\nNo flashcards available. Please add some first.");
} else {
System.out.println("\nLet's start studying!");
for (Flashcard flashcard : flashcards) {
System.out.println("\nQuestion: " + flashcard.question);
System.out.print("Press Enter to show the answer...");
scanner.nextLine();
System.out.println("Answer: " + flashcard.answer);
}
}
break;
case 3:
running = false;
System.out.println("\nExiting the app. Happy studying!");
break;
default:
System.out.println("\nInvalid choice. Please try again.");
break;
}
}
scanner.close();
}
}
Explanation of the Program Structure
The program consists of two main components:
- Flashcard class: This class represents an individual flashcard. It has two properties: the question and the answer. A constructor is provided to initialize these properties.
- FlashcardApp class: This is the main class that contains the logic for interacting with the user. It has a loop that presents a menu with three options:
- Adding new flashcards.
- Studying the flashcards by reviewing the questions and answers.
- Exiting the application.
The program uses an ArrayList to store the flashcards and a Scanner object to read input from the user. The user can choose from the menu to either add flashcards, study flashcards, or exit the app. The program runs in a loop, making it interactive and easy to use.
How to Run the Program
To run the program, follow these steps:
- Ensure that you have Java installed on your system. You can download it from the official Java website.
- Save the code in a file named
FlashcardApp.java
. - Open a terminal or command prompt and navigate to the folder where the file is saved.
- Compile the Java file by running the command:
javac FlashcardApp.java
- Run the program using the following command:
java FlashcardApp
- Follow the on-screen prompts to interact with the flashcard app.