Java Programming Assignments
Java Programming Assignments
John Maina
1
Java Programming Lecturer
John Maina
main function should then specify which numbers were even, which ones were odd
and their respective totals. It should also specify how many numbers were odd and
how many were even. For example, if the user enters 25 34 56 17 14 20, the output
should be: -
25 is an odd number
34 is an even number
56 is an even number
17 is an odd number
14 is an even number
20 is an even number
8) Making use of object orientation write a program that stores and evaluates the total
cost for items bought from a supermarket. The cashier should enter the following: -
Product code, Price and Quantity. The total price should be evaluated as follows: -
Total cost = Price * Quantity
If the total cost per item is more than 20,000 there is a discount of 14% on that item
and a discount of 10% on an item whose total cost is between 10,000 and 20,000. No
discount is given on items whose total cost is less than 10,000
NB: The cashier should decide how many Items he/she wants to work with. If he/she
chooses 3, for example, the output should take the format shown below.
Item Code Price Quantity Total Cost Discount Net
109 6000 4 24000 3360 20640
201 900 8 7200 0 7200
127 600 20 12000 1200 10800
The total amount payable is 38640
2
Java Programming Lecturer
John Maina
NB: Your class should have constructors (including default constructor) and a
destructor. It should also have setters and getters to set and return price, quantity and
total cost. (15 marks)
9) Rewrite the program above making use of arrays only i.e. don’t use object
orientation your program. No constructors, destructors, setters and getters.
(10 marks)
10) Write a program that accepts a set of numbers (The user decides how many) and
stores them in array. The program should the sort all the numbers in the array in
ascending order. It should output the numbers in the array before sorting and then
again after sorting. (10 marks)
11) Write a program that accepts two numbers and swaps them. Both input and output
should be done in the main method and the swapping (actual swapping) should be
done in a method called swapNumbers. Output the two numbers in the main method
before and after swapping. (5 marks)