0% found this document useful (0 votes)
71 views7 pages

Activity #2 OOP Intruzo, Jaymarc P.

The document describes two programming activities. The first involves writing a DriverExam class to grade a multiple choice exam by checking student answers against correct answers, calculating scores, and identifying missed questions. The second activity involves creating a PhoneBookEntry class with name and number fields, and storing 5 objects in an ArrayList to display the phone book entries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views7 pages

Activity #2 OOP Intruzo, Jaymarc P.

The document describes two programming activities. The first involves writing a DriverExam class to grade a multiple choice exam by checking student answers against correct answers, calculating scores, and identifying missed questions. The second activity involves creating a PhoneBookEntry class with name and number fields, and storing 5 objects in an ArrayList to display the phone book entries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Activity #2

1. Driver’s License Exam


The local Driver’s License Office has asked you to write a program that grades the written
portion of the driver’s license exam. The exam has 20 multiple choice questions. Here are the
correct answers:

A student must correctly answer 15 of the 20 questions to pass the exam.


Write a class named DriverExam that holds the correct answers to the exam in an array field.
The class should also have an array field that holds the student’s answers. The class should have
the following methods:
 passed. Returns true if the student passed the exam, or false if the student failed
 totalCorrect. Returns the total number of correctly answered questions
 totalIncorrect. Returns the total number of incorrectly answered questions
 questionsMissed. An int array containing the question numbers of the questions that the
student missed
Demonstrate the class in a complete program that asks the user to enter a student’s answers, and
then displays the results returned from the DriverExam class’s methods.
Input Validation: Only accept the letters A, B, C, or D as answers.
2. Phone Book ArrayList
Write a class named PhoneBookEntry that has fields for a person’s name and phone number. The
class should have a constructor and appropriate accessor and mutator methods. Then write a
program that creates at least five PhoneBookEntry objects and stores them in an ArrayList. Use a
loop to display the contents of each object in the ArrayList.
1. Driver’s License Exam
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int exam_items = 20;
String[] students = new String[exam_items];
String remarks;
String validation1 = "A";
String validation2 = "B";
String validation3 = "C";
String validation4 = "D";
System.out.println("Answer in CAPITAL LETTERS (A, B, C, D) ONLY");
for(int i = 0; i < exam_items; i++){
System.out.print(i + 1 +"# ");
students[i] = input.next();
if(!students[i].equals(validation1) && !
students[i].equals(validation2)
&& !students[i].equals(validation3) && !
students[i].equals(validation4))
{
System.out.println("Answer in CAPITAL LETTERS (A, B, C, D)
ONLY");
i--;
}
}
DriverExam exam = new DriverExam (students);
if(exam.passed()){
remarks = "PASSED";
}
else {
remarks = "FAILED";
}
System.out.println("\t\t\t\t==========RESULTS==========");
System.out.println(remarks);
System.out.println("You've got " + exam.total_correct() +
" correct answers and " + exam.total_incorrect()
+ " incorrect answers." );
System.out.println("The questions that you missed are ");
for(int i = 0; i < exam.questions_missed().length;i++){
if(exam.questions_missed()[i] != 0){
System.out.print(exam.questions_missed()[i]+" ");
}
}
}
}
public class DriverExam {
private String[] correct_answer =
{"B","D","A","A","C","A","B","A","C","D",
"B","C","D","A","D","C","C","B","D","A"};
private int items = correct_answer.length;
private String[] student_answer;
public int total_correct(){
int correct = 0;
for(int i = 0; i < items; i++){
if(correct_answer[i].equalsIgnoreCase(student_answer[i])){
correct++;
}
}
return correct;
}
public int total_incorrect(){
int incorrect = 0;
for(int i = 0; i < items; i++){
if(!correct_answer[i].equalsIgnoreCase(student_answer[i])){
incorrect++;
}
}
return incorrect;
}
public boolean passed(){
int passing = 15;

if(total_correct() >= passing){


return true;
}
else{
return false;
}
}
public int [] questions_missed(){
int missed_index = 0;
int [] missed = new int[items];

for(int i = 0; i < items; i++){


if(!correct_answer[i].equalsIgnoreCase(student_answer[i])){
missed[missed_index] = i + 1;
missed_index++;
}
}
return missed;
}
public DriverExam(String[] student_input){
student_answer = student_input;
}
}
Perfect scores
If there is a wrong input and not following instructions

If have wrong answers


2. Phone Book ArrayList
import java.util.*;
public class Main {
public static void main(String[] args) {
int limit = 5;
ArrayList<String> input_names = new ArrayList<>();
ArrayList<String> input_phone_number = new ArrayList<>();

PhoneBookEntry person1 = new PhoneBookEntry("MarjunTzy",


"2011600034");
input_names.add(person1.getEntry_name());
input_phone_number.add(person1.getPhone_number());

PhoneBookEntry person2 = new PhoneBookEntry("PrinceTzy",


"2011600001");
input_names.add(person2.getEntry_name());
input_phone_number.add(person2.getPhone_number());

PhoneBookEntry person3 = new PhoneBookEntry("DarioTzy",


"1512140238");
input_names.add(person3.getEntry_name());
input_phone_number.add(person3.getPhone_number());

PhoneBookEntry person4 = new PhoneBookEntry("LovelyTzy",


"2011600032");
input_names.add(person4.getEntry_name());
input_phone_number.add(person4.getPhone_number());

PhoneBookEntry person5 = new PhoneBookEntry("JaymarcTzy",


"2011600014");
input_names.add(person5.getEntry_name());
input_phone_number.add(person5.getPhone_number());

System.out.println("\t\t=====PHONE BOOK=====");
System.out.println();
System.out.println("NAMES:" + "\t\t\t\t\t\tNUMBER#");
for(int i = 0; i < limit; i++){
System.out.println(input_names.get(i) + "\t\t\t\t\t" +
input_phone_number.get(i));
}
}
}
public class PhoneBookEntry {
private String entry_name;
private String phone_number;

public void setEntry_name(String input_name){


entry_name = input_name;
}
public void setPhone_number(String input_number){
phone_number = input_number;
}

public String getEntry_name(){


return entry_name;
}
public String getPhone_number(){
return phone_number;
}

public PhoneBookEntry(String entry, String number){


entry_name = entry;
phone_number = number;
}
}

You might also like