Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project Outcomes: Develop a Java program that uses: branching constructs looping

ID: 3692223 • Letter: P

Question

Project Outcomes:

Develop a Java program that uses:

branching constructs

looping constructs

operations on an arrays (add, remove, display)

interactions between more than one non-runner classes

partially filled array operation

duplication constructor

Prep Readings:

Absolute Java textbook, Chapters 1 - 6.

Project Requirements:

The FAQKnowledgeBase class

This class will use a partially filled array to hold questions and implement related functionalities to manage the questions;

Refer to the UML class diagram below; No return value means void method;

Instance variables:

FAQs – an array of FAQ class typed object to hold questions;

maxCount – the maximum number of possible questions, used to initialize of the array;

questionCount – the current amount of questions in the knowledge base

Methods:

FAQKnowledgeBase() – default constructor that set the maxCount to the default value of 100 and use the maxCount as the size to initialize the FAQs array;

FAQKnowledgeBase(int maxCount) – parameterized constructor that set the maxCount to the value of the parameter of the same name and use the maxCount as the size to initialize the FAQs array;

isFull(): boolean – the method to check if the current array of questions is full; you may compare the questionCount with the maxCount value to decide this value;

isEmpty(): boolean – the method to check if the current array of questions if empty by checking the value of questionCount;

addQuestion(FAQ question) – add one question to the current question list (make sure you know how to add element to a partially filled array); You will have to check if the array is full before adding anything question; Print “The knowledge base if full! Cannot add more question!” and exit the method without adding anything to the array (tip: which statement exit a method immediately?);

removeQuestion(int questionIndex) – remove the question at the position of the index passed in; Check ifEmpty first, and print “No question in the Knowledge base! Nothing to remove” if empty; Check if the index is valid (tip: you should check questionCount rather than maxCount) and print “Index out of range!” if not valid; With the valid index, you can remove the ith element by moving all elements after this index one position forward to the smaller index direction.

For example, for an array with 6 elements, to remove the 4th (index = 3) element, you may move the original 5th and 6th position element to the 4th and 5th position first. Then decrease the length (corresponds to the questionCount variable in our case) by one.

In summary, to remove the ith element, you will iterate (using for or while loop with j as the pointer) from index i+1 to questionCount – 1 and do the assignment like a[j – 1] = a[j] to each of them;

displayQuestions() – display all questions’ content; Loop through all filled FAQ typed object in the FAQs array and print the question number as well as the object’s text (the toString() method of FAQ objects will be automatically called to provide the text);

Sample output: The blue fonts represent text from FAQ objects

Question 0:

Question: question text 1

Answer: answer test 1

Category: category text 1

Question 1:

Question: question text 1

Answer: answer test 1

Category: category text 1

displayQuestionsByCategary(String category) – display all questions’ content only when the category (use getter of FAQ class) of the question equals the category passed in this method as a parameter; For example displayQuestionsByCategary(“easy”) will display all questions whose category equals “easy”;

FAQKnowledgeBase

-FAQs:FAQ[]

-maxCount:int

-questionCount:int

+ FAQKnowledgeBase ()

+ FAQKnowledgeBase (int maxCount)

+ addQuestion(FAQ question)

+ removeQuestion(int questionIndex)

+ isFull():boolean

+ isEmpty():boolean

+ displayQuestions()

+ displayQuestionsByCategory(String category)

The FAQ class

The FAQ class manages one question per object;

Instance variables:

Question, answer and category – strings that store the question text, the answer text and the category text;

Methods

FAQ() – default constructor that initialize instance variables with some default empty values like “Empty” or “None”;

FAQ(String question, String answer, String category) – parameterized constructor that set all instance variables with the parameters passed to the method; Try using setter method to set the instance variables here;

FAQ(FAQ aFAQ) – duplication constructor that take one object of the same type and duplicate all instance variables to the current object; Try using getter and setter here rather than direct instance variable manipulation;

getQuestion(): String, getAnser():String, getCategory():String – the getter of all instance variables, return the content as string;

setQuestion(String question), setAnswer(String answer), setCategory(String category) – setter of all instance variables, set instance variables with the value passed in as parameter;

toString(): String – method to return a textual representation of the class as a string:

For example, to display the text like

Question: question text 1

Answer: answer text 1

Category: easy

You need to return a string like

“”

Where the means new line character

FAQ

- question:String

- answer:String

- category:String

+ FAQ()

+ FAQ(String question, String answer, String category)

+ FAQ(FAQ aFAQ)

+ getQuestion():String

+ setQuestion(String question)

+ getAnswer():String

+ setAnswer(String answer)

+ getCategory():String

+ setCategory(String category)

+ toString():String

The FAQRunner Class

The class to test run our FAQKnowledgeBase and FAQ classes;

Implement a main method in this class as the only class member, and no other instance variables or methods needed;

In the main method, make one FAQKnowledgeBase class object;

With the object, add three new questions with any question and answer field you like; Assign the category as “easy”, “medium” and “hard” respectively; Use “new” to make FAQ typed objects and use FAQKnowledgeBase object’s addQuestion method to add the FAQ objects one by one;

Display “There are totally X questions in the knowledge base.” first (replay X with the total number of questions); Then call the displayQuestions() method to display all questions’ contents;

Remove the question at position 0;

Repeat steps in 3.5 again; You should observe different output here since one question is removed;

Submission Requirements:

Your project must be submitted using the instructions below. Any submissions that do not follow the stated requirements will not be graded.

1.     You should have 3 files for this assignment:

FAQ.java The question class

FAQKnowledgeBase.java The knowledge base class

FAQRunner.java A driver program for your Golfer class

FAQKnowledgeBase

-FAQs:FAQ[]

-maxCount:int

-questionCount:int

+ FAQKnowledgeBase ()

+ FAQKnowledgeBase (int maxCount)

+ addQuestion(FAQ question)

+ removeQuestion(int questionIndex)

+ isFull():boolean

+ isEmpty():boolean

+ displayQuestions()

+ displayQuestionsByCategory(String category)

Explanation / Answer

class FAQRunner{ public static void main(String[] args) { FAQKnowledgeBase base = new FAQKnowledgeBase(); FAQ faq1 = new FAQ("Question 1?","answer 1","easy"); FAQ faq2 = new FAQ("Question 2?","answer 2","medium"); FAQ faq3 = new FAQ("Question 3?","answer 3","hard"); base.addQuestion(faq1); base.addQuestion(faq2); base.addQuestion(faq3); base.displayQuestions(); } } class FAQKnowledgeBase{ FAQ[] FAQs; int maxCount; int questionCount; public FAQKnowledgeBase() { } public FAQKnowledgeBase(int maxCount) { this.maxCount = maxCount; FAQs = new FAQ[maxCount]; } public boolean isFull(){ return maxCount==questionCount; } public boolean isEmpty(){ return questionCount==0; } public void addQuestion(FAQ question){ if(isFull()){ System.out.println("The knowledge base if full! Cannot add more question!"); }else { FAQs[++questionCount]=question; } } public void removeQuestion(int questionIndex){ if(isEmpty()){ System.out.println("No question in the Knowledge base! Nothing to remove"); return; } if(questionIndex>questionCount || questionIndex