Docsity

This document is good for beginners, Study Guides, Projects, Research of Computer science

Toggle sidebar
bg1
Java Class Practice Questions โ€” No Arrays
These questions practise Java classes, private attributes, constructors, methods, calculations,
conditions, booleans and object state without using arrays.
Question 1 โ€” Bank Account
Write a class called BankAccount. The class should store information about one bank account:
account holder's name, account number and account balance. The attributes must not be accessible
outside the class.
1.1 (4 marks) Declare the private attributes.
1.2 (4 marks) Write a constructor that receives the account holder's name, account number and initial
balance.
1.3 (5 marks) Write deposit(double amount) that adds the amount to the account balance.
1.4 (5 marks) Write withdraw(double amount) that subtracts the amount from the balance. The
withdrawal should only happen if there is enough money.
1.5 (5 marks) Write a method that prints the account holder's name, account number and current
balance.
Total: 23 marks
Question 2 โ€” Employee Salary
Write a class called Employee that stores employee name, employee number, monthly salary and
number of years employed.
2.1 (4 marks) Declare the attributes.
2.2 (4 marks) Write a constructor to initialise all attributes.
2.3 (5 marks) Write increaseSalary(double percentage) that increases the employee's salary
by the specified percentage.
2.4 (5 marks) Write getAnnualSalary() that returns the employee's annual salary.
2.5 (5 marks) Write a method that displays all employee information.
Total: 23 marks
Question 3 โ€” Mobile Phone
Write a class called MobilePhone. The class should store phone model, phone number, battery
percentage, available storage in GB and phone price.
3.1 (4 marks) Declare all attributes as private.
3.2 (4 marks) Write a constructor to initialise the attributes.
3.3 (5 marks) Write chargeBattery(int percentage) that increases the battery percentage. The
battery must never exceed 100%.
3.4 (5 marks) Write useStorage(double amount) that reduces the available storage. The storage
must not become negative.
pf3
pf4
pf5

Partial preview of the text

Download This document is good for beginners and more Study Guides, Projects, Research Computer science in PDF only on Docsity!

Java Class Practice Questions โ€” No Arrays

These questions practise Java classes, private attributes, constructors, methods, calculations, conditions, booleans and object state without using arrays.

Question 1 โ€” Bank Account

Write a class called BankAccount. The class should store information about one bank account: account holder's name, account number and account balance. The attributes must not be accessible outside the class.

1.1 (4 marks) Declare the private attributes.

1.2 (4 marks) Write a constructor that receives the account holder's name, account number and initial balance.

1.3 (5 marks) Write deposit(double amount) that adds the amount to the account balance.

1.4 (5 marks) Write withdraw(double amount) that subtracts the amount from the balance. The withdrawal should only happen if there is enough money.

1.5 (5 marks) Write a method that prints the account holder's name, account number and current balance.

Total: 23 marks

Question 2 โ€” Employee Salary

Write a class called Employee that stores employee name, employee number, monthly salary and number of years employed.

2.1 (4 marks) Declare the attributes.

2.2 (4 marks) Write a constructor to initialise all attributes.

2.3 (5 marks) Write increaseSalary(double percentage) that increases the employee's salary by the specified percentage.

2.4 (5 marks) Write getAnnualSalary() that returns the employee's annual salary.

2.5 (5 marks) Write a method that displays all employee information.

Total: 23 marks

Question 3 โ€” Mobile Phone

Write a class called MobilePhone. The class should store phone model, phone number, battery percentage, available storage in GB and phone price.

3.1 (4 marks) Declare all attributes as private.

3.2 (4 marks) Write a constructor to initialise the attributes.

3.3 (5 marks) Write chargeBattery(int percentage) that increases the battery percentage. The battery must never exceed 100%.

3.4 (5 marks) Write useStorage(double amount) that reduces the available storage. The storage must not become negative.

3.5 (5 marks) Write a method that displays all phone information.

Total: 23 marks

Question 7 โ€” Temperature Monitor

Write a class called TemperatureMonitor. The class should store location, current temperature, minimum temperature recorded and maximum temperature recorded.

7.1 (4 marks) Declare the private attributes.

7.2 (4 marks) Write a constructor.

7.3 (5 marks) Write setTemperature(double temperature) that updates the current temperature and also updates the minimum and maximum temperatures when necessary.

7.4 (5 marks) Write getAverageRange() that returns (maximum temperature + minimum temperature) / 2.

7.5 (5 marks) Write a method that displays all temperature information.

Total: 23 marks

Question 8 โ€” Student Course

Write a class called Course. The class should store course name, course code, student's name, assignment mark, test mark and exam mark.

8.1 (4 marks) Declare all attributes as private.

8.2 (4 marks) Write a constructor.

8.3 (5 marks) Write calculateFinalMark(). The final mark is: Assignment 20%, Test 30%, Exam 50%.

8.4 (5 marks) Write hasPassed() that returns true if the final mark is at least 50, otherwise false.

8.5 (5 marks) Write a method that displays the student's information, final mark and whether the student passed.

Total: 23 marks

Question 9 โ€” Gym Membership

Write a class called GymMember. The class should store member name, membership number, monthly fee, number of months paid and number of gym visits.

9.1 (4 marks) Declare the private attributes.

9.2 (4 marks) Write a constructor.

9.3 (5 marks) Write makePayment(int months) that increases the number of months paid.

9.4 (5 marks) Write recordVisit() that increases the number of gym visits by one.

9.5 (5 marks) Write getTotalPaid() that returns the total amount paid.

Total: 23 marks

Question 10 โ€” Challenging Question

Write a class called Product. The class should store product name, product code, price, quantity in stock and discount percentage.

10.1 (4 marks) Declare all attributes as private.

10.2 (4 marks) Write a constructor.

10.3 (5 marks) Write sell(int quantity) that decreases the quantity in stock. The sale should only happen if enough stock is available.

10.4 (5 marks) Write getDiscountedPrice() that returns the price after applying the discount.

10.5 (5 marks) Write restock(int quantity) that increases the quantity in stock.

Total: 23 marks

Practice Focus

These questions use the same overall exam structure as the trolley question, but require different logic and do not use arrays. Students should focus on private attributes, constructors, updating object state, calculations, conditions, boolean values and methods.