I made a quiz about classes using a class. The facts are from chatgpt.
import java.util.ArrayList;
import java.util.Scanner;
public class JavaClass {
ArrayList<String> chatgptFacts = new ArrayList<String>();
ArrayList<Boolean> chatgptAnswers = new ArrayList<Boolean>();
public JavaClass() {
chatgptFacts.add("A class is a blueprint for creating objects.");
chatgptAnswers.add(true);
chatgptFacts.add("An object is an instance of a class.");
chatgptAnswers.add(true);
chatgptFacts.add("A constructor is a special method that is called when an object is created.");
chatgptAnswers.add(true);
chatgptFacts.add("A method is a block of code that performs a specific task.");
chatgptAnswers.add(true);
chatgptFacts.add("Inheritance is the process by which one class acquires the properties of another class.");
chatgptAnswers.add(true);
chatgptFacts.add("Java is a procedural programming language.");
chatgptAnswers.add(false);
chatgptFacts.add("Encapsulation in Java is a mechanism of wrapping the data and code together as a single unit.");
chatgptAnswers.add(true);
}
public void quiz() {
Scanner scanner = new Scanner(System.in);
int score = 0;
for (int i = 0; i < chatgptFacts.size(); i++) {
System.out.println(chatgptFacts.get(i));
System.out.print("(Type True or False)");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase(Boolean.toString(chatgptAnswers.get(i)))) {
score++;
}
}
System.out.println("Your score is " + score + " out of " + chatgptFacts.size());
}
}
JavaClass jc = new JavaClass();
jc.quiz();
A class is a blueprint for creating objects.
(Type True or False)An object is an instance of a class.
(Type True or False)A constructor is a special method that is called when an object is created.
(Type True or False)A method is a block of code that performs a specific task.
(Type True or False)Inheritance is the process by which one class acquires the properties of another class.
(Type True or False)Java is a procedural programming language.
(Type True or False)Encapsulation in Java is a mechanism of wrapping the data and code together as a single unit.
(Type True or False)Your score is 4 out of 7