Lab 2 Final Final
Lab 2 Final Final
Centennial College
This material provides the necessary information you need to complete the exercises.
You must name your Eclipse/IntelliJ project according to the following rule:
YourFullName_COMP228Labnumber
Example: JohSmith_COMP228Lab2
Each exercise should be placed in a separate package (if there are more than 1 exercise)
named exercise1, exercise2, etc.
Submit your assignment in a zip file that is named according to the following rule:
YourLastName_COMP228Labnumber.zip
Example: JohSmith_COMP228Lab2.zip
Apply the naming conventions for variables, methods, classes, and packages:
- variable names start with a lowercase character
Lab #2 Page 1 of 5
Java Programming COMP-228
Exercise 1:
Write a Java application that simulates a test. The test contains at least five questions about first
three lectures of this course. Each question should be a multiple-choice question with 4 options.
Design a Test class. Use programmer-defined methods to implement your solution. For example:
- create a method to simulate the questions – simulateQuestion
- create a method to check the answer – checkAnswer
- create a method to display a random message for the user – generateMessage
- create a method to interact with the user - inputAnswer
Display the questions using methods of JOptionPane class. Use a loop to show all the questions.
At the end of the test display the number of correct and incorrect answers, and the percentage of
the correct answers.
Your main class will simply create a Test object and start the test by calling inputAnswer
method.
Lab #2 Page 2 of 5
Java Programming COMP-228
Lab #2 Page 3 of 5
Java Programming COMP-228
(5 marks)
Exercise 2:
Design a Lotto class with one array instance variable to hold three random integer values (from 1
to 9). Include a constructor that randomly populates the array for a lotto object. Also, include a
method in the class to return the array.
Use this class to simulate a simple lotto game in which the user chooses a number between 3 and
27. The user runs the lotto up to 5 times and each time the sum of lotto numbers is calculated. If
the number chosen by the user matches the sum, the user wins and the game ends. If the number
does not match the sum within five rolls, the computer wins.
Use methods of JOptionPane class to interact with the user.
(3 marks)
Lab #2 Page 4 of 5
Java Programming COMP-228
Exercise 3:
Write a Java class that implements a set of three overloaded static methods. The methods should
have different set of parameters and perform similar functionalities. Call the methods within
main method and display the results.
(2 marks)
Changing Values:
Evaluation:
Functionality
Correct implementation of classes 40%
(instance variable declarations,
constructors, getter and setter methods,
etc.)
Correct implementation of driver 40%
classes (declaring and creating objects,
calling their methods, interacting with
user, displaying results)
Comments, correct naming of 5%
variables, methods, classes, etc.
Friendly input/output 15%
Total 100%
Lab #2 Page 5 of 5