Docsity

Basics question based on java, Lecture notes of Computer science

Toggle sidebar
bg1
Java Array & Class Practice Questions
These questions practise Java classes, private attributes, constructors, arrays, counters, methods,
searching and calculations.
Question 1 โ€” Shopping Basket
Write a class that will model a shopping basket. It should contain three corresponding arrays: an array
of the names of products, an array of the quantities of each product, and an array of the prices of each
product. Each index in the arrays corresponds to the same product. The class should also contain a
counter for the number of unique products. Assume that a maximum of 50 unique products can be
stored.
1.1 (4 marks) Declare private attributes for the three arrays and the counter.
1.2 (4 marks) Write a constructor that initialises all arrays and sets the counter to zero.
1.3 (5 marks) Write a method: addProduct(String name, int quantity, double price). If
the product already exists, increase its quantity. Otherwise, add it as a new product and increase the
counter.
1.4 (5 marks) Write a method getTotal() that returns the total cost of all products.
1.5 (5 marks) Write a method that prints all products, their quantities, prices, and the total cost.
Total: 23 marks
Question 2 โ€” Library Book System
Write a class called Library that stores information about books currently in a library. Use three
corresponding arrays: book titles, number of copies available, and price of each book. Each index
represents the same book. Include a counter for the number of different books. A maximum of 100
different books can be stored.
2.1 (4 marks) Declare the required private attributes.
2.2 (4 marks) Write a constructor to initialise the arrays and counter.
2.3 (5 marks) Write addBook(String title, int copies, double price). If the book already
exists, increase the number of copies. Otherwise, add a new book.
2.4 (5 marks) Write getLibraryValue() that calculates and returns the total value of all books.
Value = number of copies ร— price.
2.5 (5 marks) Write a method that prints all books and the total value of the library.
Total: 23 marks
Question 3 โ€” Student Marks
Write a class called StudentRecords that stores student information. Use three corresponding arrays:
student names, student marks, and student numbers. Each index corresponds to the same student.
Include a counter for the number of students. A maximum of 100 students can be stored.
3.1 (4 marks) Declare private attributes for the arrays and counter.
3.2 (4 marks) Write a constructor that initialises the arrays and counter.
pf3
pf4

Partial preview of the text

Download Basics question based on java and more Lecture notes Computer science in PDF only on Docsity!

Java Array & Class Practice Questions

These questions practise Java classes, private attributes, constructors, arrays, counters, methods, searching and calculations.

Question 1 โ€” Shopping Basket

Write a class that will model a shopping basket. It should contain three corresponding arrays: an array of the names of products, an array of the quantities of each product, and an array of the prices of each product. Each index in the arrays corresponds to the same product. The class should also contain a counter for the number of unique products. Assume that a maximum of 50 unique products can be stored.

1.1 (4 marks) Declare private attributes for the three arrays and the counter.

1.2 (4 marks) Write a constructor that initialises all arrays and sets the counter to zero.

1.3 (5 marks) Write a method: addProduct(String name, int quantity, double price). If the product already exists, increase its quantity. Otherwise, add it as a new product and increase the counter.

1.4 (5 marks) Write a method getTotal() that returns the total cost of all products.

1.5 (5 marks) Write a method that prints all products, their quantities, prices, and the total cost.

Total: 23 marks

Question 2 โ€” Library Book System

Write a class called Library that stores information about books currently in a library. Use three corresponding arrays: book titles, number of copies available, and price of each book. Each index represents the same book. Include a counter for the number of different books. A maximum of 100 different books can be stored.

2.1 (4 marks) Declare the required private attributes.

2.2 (4 marks) Write a constructor to initialise the arrays and counter.

2.3 (5 marks) Write addBook(String title, int copies, double price). If the book already exists, increase the number of copies. Otherwise, add a new book.

2.4 (5 marks) Write getLibraryValue() that calculates and returns the total value of all books. Value = number of copies ร— price.

2.5 (5 marks) Write a method that prints all books and the total value of the library.

Total: 23 marks

Question 3 โ€” Student Marks

Write a class called StudentRecords that stores student information. Use three corresponding arrays: student names, student marks, and student numbers. Each index corresponds to the same student. Include a counter for the number of students. A maximum of 100 students can be stored.

3.1 (4 marks) Declare private attributes for the arrays and counter.

3.2 (4 marks) Write a constructor that initialises the arrays and counter.

3.3 (5 marks) Write addStudent(String name, int studentNumber, double mark) that adds a student and increases the counter.

3.4 (5 marks) Write getAverageMark() that returns the average mark of all students.

3.5 (5 marks) Write a method that prints all students and their marks.

Total: 23 marks

Question 7 โ€” Cinema Tickets

Write a class called Cinema to keep track of movie ticket sales. Use an array of movie names, an array of tickets sold, an array of ticket prices, and a counter for the number of different movies. A maximum of 20 movies can be stored.

7.1 (4 marks) Declare the private attributes.

7.2 (4 marks) Write a constructor.

7.3 (5 marks) Write sellTickets(String movie, int numberOfTickets, double price). If the movie already exists, increase the number of tickets sold.

7.4 (5 marks) Write getTotalIncome() to calculate the total money received from ticket sales.

7.5 (5 marks) Write a method that displays all movies, tickets sold, ticket prices and total income.

Total: 23 marks

Question 8 โ€” Harder Version

Write a class called Trolley using three corresponding arrays: item names, quantities and prices. The trolley can contain a maximum of 100 unique items.

8.1 (5 marks) Write findItem(String name). Search for an item and return its index. If the item does not exist, return -1.

8.2 (5 marks) Write removeItem(String name). Remove an item and shift the remaining elements so there are no empty spaces between items.

8.3 (5 marks) Write getMostExpensiveItem(). Return the name of the item with the highest price.

8.4 (5 marks) Write getTotalQuantity(). Return the total number of individual items in the trolley.

Total: 20 marks

Suggested Practice Order

Attempt Questions 1โ€“3 first, then Questions 4โ€“7. Finish with Question 8 as the challenge question because it includes searching and removing array elements.