Java Programming Assignments

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Java Programming Lecturer

John Maina

Java Programming Assignments


1) Write a program that accepts a list of integers and computes their sum. The program
should allow the user to enter any number of integers but an input of zero should
terminate the list. For example if the user enters 2 5 6 12 8 2 0 the sum should be 35.
If he/she enters 2 4 5 0 the sum should be 11 and if he enters 5 4 6 0 12 43 2 the sum
should 15 i.e. only numbers before zero are summed. The rest are ignored and the
program terminated. (5 marks)
2) Using nested loops, write code to produce the following output. (8 marks)
NB: Use only two loops.
12345678
1234567
123456
12345
1234
123
12
1
3) Differentiate between the following as used in Java: -
a) Abstract and Concrete methods. (4 marks)
b) Instance and Static members of a class. (2 marks)
4) Write a complete Java program illustrating the difference between instance and static
members of class. Indicate with comments where each is used and explain the
significance of making the instance or static. (6 marks)
5) You have been asked to implement a user-interface component ButtonCanvas which
is both a button that can be clicked by the user and a canvas which the user can draw
on. Your existing code contains a Button class and a Canvas class. Both of this extend
GuiComponent.
a) ButtonCanvas should be built using multiple inheritance. What does this mean in
this context? (3 marks)
b) Give two reasons why this might be desirable. (3 marks)
c) Give two complexities that arise in this case (3 marks)
d) Java interfaces originally contained only abstract methods and static final fields.
How did this restrictions avoid the complexities of extending multiple classes?
(3 marks)
e) Recent versions of Java added default methods to interfaces. What is the impact
of this with respect to multiple inheritance? (3 marks)
6) Write a program that accepts a set of integers (the user decides how many) and then
stores them in an array. The main function then passes these values one by one to a
method called get_even which returns 1 if the integer is even and 0 if it is odd. The

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

There is a total of 2 odd numbers and their sum is 42.


There is a total of 4 even numbers and their sum is 124.
NB: All data input and output should be done in main. Don’t use % any where in the
main function (% can be used in the method). (10 marks)
7) Write a program that accepts the amount of money deposited in a bank account, the
annual interest rate and the target amount (The amount of money the account holder
wants to have in the account after a period of time) and then calculates the number of
years it will take for the money to accumulate to the targeted amount. NB: 1) The
interest being earned is Compound Interest. 2) Don’t use the formula for calculating
compound interest.
For example if the money deposited is 10000 and the target amount is 20000 and the
account earns an interest rate (compound) of 10% pa, then the output should be: -
It will take 8 years for your money to reach your target.
By the end of this period, the amount in your account will be 21435.89 (10 marks)

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)

* * * * * All the best – JM * * * * *

You might also like