Codingassignmenthelper | Home Codingassignmenthelper | Samples

ACS-1903

ACS

ACS-1903

		ACS-1903
Assignment 3
Due by Tuesday December 8 at 11:59 pm
- Submit your .java files via Nexus
- Include your name and student number in each file as a comment
Note: concepts that have not been covered in class cannot be used.

1. [25 marks]
Write a program ​TestScore​ that determines a student’s score on a multiple-choice test having
12 questions. The program has two ArrayLists: ​key​ and ​studentAnswers​. The ​key​ array list
holds the correct answers to the test.
Use the following for correct answers: ​a b c d a b c d a b c d
The student’s answers will be provided by the user of the program. The system then determines
the user’s score: the number of questions the student answered correctly.
Sample output:
System:
Enter the test answers:
User
a a a b b b c c c d d d
System:
You answered 4 out of 12 questions correctly.

2. [25 marks] ​RationalClient.java
A rational number is a number that can be expressed as a fraction whose numerator and
denominator are integers. Examples of rational numbers are 0.75 which is 3/4, and 1.125, which is
9/8. The value of π is not a rational number; it cannot be expressed as a ratio of 2 integers, but
that’s a story for another day.
Working with rational numbers is often a problem on a computer. Inaccuracies in floating point
precision can yield imprecise results. A possible solution to this problem is to deal with rational
numbers as fractions rather than floating point numbers.
Design and implement a class for rational numbers that will be expressed as the ratio of 2 integers
ie. 1/2. Name your class ​Rational.
Your class should provide for;

○ Two constructors. A default/no-argument constructor creates a new instance of the class
with a numerator of 0 and a denominator of 1, and another constructor that takes two
arguments. This other constructor creates an instance of the Rational class whose
numerator and denominator are specified by the client code.
○ Two fields/instance variables: the numerator and the denominator
- Note that the denominator should never be set to 0
○ getters and setters for both fields
○ Two additional setter methods that perform division and multiplication of 2 rational
numbers. These should be implemented as void methods and used as per the example
code found below
- These methods will take as parameters the 2 operand objects; the method
headings will look like this:
public void multiply(Rational operand1, Rational operand2);
public void divide(Rational operand1, Rational operand2);
○ Override the toString method so it prints the rational number in the form a/b with no
spaces between.
Your class must work with the attached driver/client code.
Sample output:
Enter the numerator and the denominator.
4 5
Enter another numerator and the denominator.
3 2
Now for the Math!
4/5 * 3/2 = 12/10
4/5 / 3/2 = 8/15
End of processing
For some extra challenge (but no extra marks) try to implement an add() method and a reduce()
method that will reduce 6/12 to 1/2
Some other methods that you need to implement for the extra challenge (but no extra marks)
(these will very probably be private methods)
○

findLCD: a method that will find a common denominator between 2 rational objects. You’ll
need this to perform addition. This can be a private method called by your add method.
○ Reduce: a method that will reduce a rational number for example 2/6 will reduce to 1/3.
For this you will need to find the GCD (Greatest Common Divisor) of the numerator and
denominator. There is an easy to implement algorithm for find the GCD called Euclid’s
algorithm. You should implement Euclid’s algorithm as a private method and call it in a
private method named reduce

3. [25 marks] ​QuizSample.java
Develop 2 classes, ​Question​ and ​Quiz​ organized as :

Write the classes with:
•

•

•
•

•

Fields:
- Question​ has string fields ​text​ and ​answer
- Quiz​ has a string field ​name
Constructors:
- No-arg constructors
- Question​ with 2 parameters and ​Quiz​ with 1
Getters and setters for all fields
toString method
- Question​ returns only the question text
- Quiz​ returns the quiz name and list of questions
required fields and methods for the association.

Test your classes in a driver class by adding sample questions to a Quiz and display the quiz.
Your code should work with the attached driver.
Sample output:
Sample 1903 Quiz:
Who is known as the father of Java?
Write a statement that assigns the length of a string s to int i
True or false: assigning an int to double is an example of a
widening conversion

4. [ 15 marks]
Develop a program named ​TriviaGame​ that uses the classes developed in question 3. The
game asks randomly selected questions from a file. The user is only allowed 3 incorrect answers:
upon the third mistake they lose the game, but once 10 questions are answered correctly, they
win.

The program reads a list of questions and corresponding answers from a file named ​A3Q4.txt​.
The program stores the information in an instance of the Quiz class.​ ​Give your quiz a name (e.g.
“Harry Potter Trivia”).
The quiz begins by randomly choosing a question from the list. The user then inputs their answer.
This continues until the test is over. If the user reaches the end of the quiz, congratulate them and
display their winning status. If they answer incorrectly 3 times, display the result (player loses).
Requirements:
• answers must be equal but are not case sensitive.
• ensure that individual questions are only asked once (i.e. the same question cannot be
repeated in the same quiz).
• assume that there are ​at least​ 13 questions/answers properly formatted in the file
- all questions and their corresponding answers are stored in a text file in alternating,
successive lines.
• your solution must scale: the marker may use any file that is formatted appropriately.

Sample output:
Let's play Harry Potter Trivia!
Who is George Weasley's twin?
Bart
Incorrect. The correct answer is Fred [Strike 1]
What is Professor Snape's first name?
Severus
Correct! [Score: 1]
What game, using flying broomsticks, is played by wizards?
Quidditch
Correct! [Score: 2]
Who was the Malfoy's house elf?
Dobby
Correct! [Score: 3]
What creature, from the Chamber of Secrets, petrifies students in
Harry's second year?
Basilisk
Correct! [Score: 4]

What is the name of Lord Voldemort's snake?
Neville
Incorrect. The correct answer is Nagini [Strike 2]
What platform does the train to Hogwarts depart from?
9 2/3
Incorrect. The correct answer is 9 3/4 [Strike 3]
You lose the game :(
Note: If the score reaches 10, the user wins the game.

5. [10 marks] ​Search.java
Develop 2 classes: ​PostalCode​ and ​Province​ organized as

•
•
•

The ​PostalCod​e class has fields ​firstSegment​, ​place​, ​latitude​ and ​longitude​.
The ​Province​ class has fields ​code​ and ​name​.
Each class must have:
○ necessary constructors, getters and setters for each field, and a toString method.
○ fields and methods required for the association.

Complete the attached program, ​Search.java,​ that reads postal code data from a file and
provides the user with a search function.
There are 3 things ​Search​ must do:
1. Create 13 Province instances and keep these in an array list (given in the starter code)
2. Create each postal code instance indicated in ​A2Q5.txt​. Each postal code has a firstSegment,
area, latitude, longitude, and a category it belongs to. As each postal code is created it ​must be
added to its corresponding province​. Use the province code (first value of each line eg. MB)
and the array list of provinces to determine which province instance to associate it with.

When reading A2Q5.txt we recommend that you read a line
String line = f.nextLine();
and then with another scanner, get each field e.g.
Scanner s = new Scanner(line).useDelimiter(",");
String code = s.next();
3. Prompt the user to choose an option to search by: postal code or coordinates, and display the
area and province name of each.
Sample output:
Make a selection:
[A] search by postal code
[B] search by geographic coordinates
[C] quit
A
Enter the first segment of the postal code
R3B
Winnipeg (Chinatown / Civic Centre / Exchange District)
Make a selection:
[A] search by postal code
[B] search by geographic coordinates
[C] quit
B
Enter the latitude and longitude
51.1762 -115.5698
Banff, Alberta
Make a selection:
[A] search by postal code
[B] search by geographic coordinates
[C] quit
C
End of program

Submit your files (​TestScore.java​,​ RationalNumbers.java, Question.java,
Quiz.java, TriviaGame.java, PostalCode.java, Province.java, Search.java​)
via Nexus​.


		
To Download Click Here > ACS-1903.pdf
Codingassignmenthelper | Home Codingassignmenthelper | Home