Question 1

The objectives of this question are

  • to distinguish between a class and an instance by giving examples of each.
  • to write a basic Java class and a simple tester to test your Java class.
  • Follow the code convention,
  • identify a suitable class for the following items: Java, Cobol, Fortran, Pascal, Visual Basic, C#, Python
  • give an example of an instance for the class Sport
  • You are to implement a class to simulate a combination lock for a safe in the hotel room. A combination lock has a four-digit pin number; each digit has value '0' through '9'. There is a boolean variable to record whether the lock is opened (true if it is opened, false otherwise).

Write a java class called ComboLock with the following attributes and behaviours:

  • Suitable instance variables for the information described above.
  • The constructor(s) with the appropriate actions on the instance variables.
  • A set method that sets the pin.
  • The isOpen() method that returns true if the lock is currently unlocked, false otherwise.
  • The unlock(String) method that attempts to unlock the lock. This method accepts a parameter representing the pin entered and tries to unlock the lock. If the pin entered is more than 4 digits, it takes only the last 4 digits.
  • The lock() method that locks the combination lock.

Question 2

The objectives of this question are

  • to understand Java identifiers
  • to debug simple Java program
  • State the most appropriate data type for each variable in the program segments given below. Justify your answer. Explain what each of the program segments computes.

(i) x = 2.0; (ii) s = "2"; y = x + x; t = s + s;

John knows that the distance between two points (x1, y1) and (x2, y2) on a plane is given by the following formula

He wrote the following program to calculate the distance between two points given by the user during run-time. However, the program was not able to compile successfully in BlueJ.

1. public class ComputeDistance
2. {
3. public static void main(String[] args)
4. {
5. Scanner console = new Scanner(System.in);
6. int x1, y1, x2, y2;
7. double distance;
8. System.out.print("Enter the coordinate of the first point: );
9. x1 = console.nextInt();
10. y1 = console.nextInt();
11. System.out.print("Enter the coordinate of the second point: ");
12. x2 = console.nextInt();
13. y2 = console.nextInt();
14. d = Math.sqrt((x2-x1)(x2-x1) + (y2-y1)(y2-y1));
15. System.out.println("Point 1 : (" + x1 + ", " + y1 + ")");
16. System.out.println("Point 2 : (" + x2 + ", " + y2 + ")");
17. System.out.println("Distance : " + distance);
18. }

Identify the compilation errors in the program by completing the table below:

Explain clearly what happens when the user enters (1, 1) as the coordinate for the first point and (1.5, 2) as the second point (only the number without the brackets and commas).

Modify the program so that it is able to accept real numbers as the input data for the coordinates. Modify also the program so that the distance printed shows only 2 decimal places. Submit the program listing and a screenshot of output showing one run of the program after all correction and modification are done.

Question 3

The objectives of this question are

  • to write simple Java program
  • to practice the use of selection structure in simple Java program
  • to practice the use of repetition structure in a Java program
  • to practice the use of method in a Java program

A military format time is to indicate the time in a 24-hour clock from 0000 (12 mid-night) to 2359 (1 minute before 12 mid-night).

Write a program that reads two time in military format where the first time is earlier than the second time and prints the number of hours and minutes between the two times. E.g. first time: 0900 second time: 1730 duration: 8 hours 30 minutes

You are not allowed to make use of any Java pre-defined classes and methods. You are not required to perform data validation here as it is to be done in part (c) below. Your program output must show clearly the time entered and the duration computed (e.g. There is 8 hours 30 minutes from 0900 to 1730).

Submit your program listing together with screenshot showing one run of your program output. Your screenshot must show your name as part of the program output.

Modify your program in part (a) above so that it is able to calculate the time difference if the first time is bigger than second time.

E.g. first time: 1730 second time: 0900 duration: 15 hours 30 minutes

Submit your program listing with the changes clearly highlighted. Submit also screenshot of two runs of your program output. Your screenshot must show your name as part of the program output.

Modify your program in part (b) to perform input validation. Display an error message to indicate the type of error the input value has and repeat the input process until a valid input is entered. You should define a method to validate the input data.

Question 4

The objectives of this question are

  • to practice arrays in Java.
  • to practice methods in Java.

A pet shop gives a discount to its customers if they purchase one or more pets and at least five other items. The discount is equal to 20% of the cost of the other items except the pets. You are required to develop a simple application for the pet shop to manage the bill for its customers. The system should allow the user to perform the following tasks repeatedly until he quits the program:

  • Enter input
  • List items purchased
  • Print discount
  • Display bill
  • Check out

Your program should include the following:

The main() method to perform the following:

  • create two arrays, one to store the price for each item purchased and the other to store the boolean value to indicate whether the item is a pet for the corresponding item (the value is true if the item is a pet, false otherwise).
  • display a menu, allow the user to select the option according to the menu and call the desired method to perform task according to the user's selection (you are required to use
  • Note: you may assume that each bill can only contain at most 20 items.

The method displayMenu() to display the following menu:

MENU
A. Enter input
B. List items purchased
C. Print discount
D. Display bill
E. Check out
F. Exit

The method enterInput() that allows the user to enter the price and the indicator of whether it is a pet for each item in the purchases. This method should receive as the parameters the two arrays created in the main() method mentioned above and an integer number that keeps track of the number of items purchased. It prompts and reads the input data from the user and adds it to the arrays. Use a price of -1 as the sentinel. The method should return the number of items purchased.

The method listItems() to display the information of the purchases (prices for each item and whether it is a pet). The output should be nicely formatted for easy reading.

The method calculateDiscount() to calculate and return the discount that the customer is entitled to base on his purchases.

The method displayBill() to display the bill for the customer. The bill should include the information of the purchases (prices for each item and whether it is a pet), the total price before discount, the discount and the final bill after discount. The output should be nicely formatted for easy reading.

Note: the "Check out" option sets the no of item purchases to 0.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.