Fundamentals of Object Programming - CIT

advertisement
Cork Institute of Technology
Bachelor of Business (Honours) in Information Systems – Stage 2
(Bachelor of Business Studies in Information Systems – Stage 2)
(NFQ – Level 8)
Summer 2005
Fundamentals of Object Oriented Programming
(Time: 3 Hours)
Instructions
Answer any FOUR questions
All questions carry equal marks.
Q1. (a)
Examiners: Mr. Declan Forde
Mr. Eamonn Walsh
Briefly describe five primitive data types used in Java. Include an example of a
declaration of each type in your answer.
(b)
(5 Mark
Strings in Java are objects instantiated from the String class in the Java Class Library.
To what package does the String class belong?
Why is this package not imported into Java programs?
(c)
(2 Marks)
What is the output of the following program?
public class StringPlay {
public static void main(String[] args){
String phrase1 = “Write once”;
System.out.println(phrase1.length);
String phrase2 = phrase1.concat(“, run anywhere.”);
String phrase3 = phrase2.toUpperCase();
String phrase4 = phrase3.replace(‘E’,‘X’);
String phrase5 = phrase4.substring(3,18);
System.out.println(phrase2);
System.out.println(phrase3);
System.out.println(phrase4);
System.out.println(phrase5);
System.out.println(phrase5.length);
}
}
(6 Marks)
(d)
Write a Java application that will determine if a customer has exceeded the credit limit on
a credit card account. For each customer, the following facts are available:
account number; balance at the beginning of the month; total of all items charged by
the customer this month; total of all credits applied to the customer’s account this
month; the customer’s credit limit.
Use input dialogs (JOptionPane class) and wrapper classes to obtain these details from the
user. The program should calculate the new balance and determine if the new balance
exceeds the customer’s limit. For those customers whose credit limit is exceeded, the
program should display the message “Credit limit exceeded”.
(Required: Pseudocode, Fully commented Java code)
(12 Marks)
Q2. (a)
Triangles, circles, and squares are all geometric figures and therefore share some common
attributes and methods. They also have their own specific attributes and methods. It
should be possible to calculate the area of all geometric figures.
Triangle: Area = (base * height)/2
Circle: Area = Π * R2
(Π = 3.1416)
Square: Area = length * width
Draw a class tree depicting this situation.
(Include all instance data, methods and access modifiers for each class)
(10 Marks)
(b)
Write the program code for the classes in the class tree.
(10 Marks)
(c)
Create a simple driver program containing an array of references to geometric figure
objects, and use it to call the methods to calculate the area of different types of figures and
display the results.
(5 Marks)
2
Q3. (a)
Explain the terms public, private and protected in Java programs.
Include examples in your answers.
(b)
(7 Marks)
What is the output of each of the following segments of code:
(i)
int num = o, max = 20;
while(num < max)
{
System.out.println(num);
num += 4;
}
(ii)
int num = 1, max = 20;
while(num < max)
{
if (num % 2 == 0)
System.out.println(num);
num++;
}
(iii)
for (int num = 0; num <= 24; num += 2)
System.out.println(num);
(9 Marks)
(c)
What is the difference between a static variable and an instance variable?
(4 Marks)
(d)
Write a class that defines a typical bank account. Show how a static variable can be used
to keep track of the number of accounts created.
(Include the code body for the constructor only. For all other methods, the method headers
will suffice.)
(5 Marks)
3
Q4. (a)
Explain each of the following Java terms. Use code statements to illustrate your answer.
(i)
super
(ii)
implements
(iii)
this
(iv)
extends
(v)
constructor
(10 Marks)
(b) Write a statement that declares an integer array, myArray, and initialises it with the
values 3,8,-5,5,6,0,3,-2,8,9
(5 Mar
(c) What are the contents of the array after the following statements are executed?
myArray[2] = myArray[6] + 5;
int a = myArray[8];
myArray[7] = a + myArray[0] * myArray[0];
myArray[4] = myArray[4] + 1;
myArray[5] = myArray[3] + myArray[9];
myArray[3] = myArray[2 * myArray[0]];
(10 Marks)
4
Q5. (a)
Write a program, Awards.java, that
Asks a user what place they achieved in a competition.
Uses a switch statement to display an appropriate message, as follows:
o First place – “Gold”
o Second place – “Silver”
o Third place – “Bronze”
o Otherwise – “Thank you for your efforts. Your lucky number is XX”
(A user who doesn’t achieve a first, second, or third place is awarded a randomly
generated number in the range 1 – 100 inclusive. Use the Random class to generate the
random number. Use JOptionPane class to get user input and display the appropriate
messages. Ensure that all required import statements and relevant comments are included
in your program code. )
(b)
(15 Marks)
The figure shown is a UML sequence diagram depicting the flow of events in a Java
project that models a renovation scenario. Describe what is happening at each stage of the
diagram from start to finish. Number each activity in the sequence in the order of
occurrence.
(10 Marks)
client
new
theSurface
new
theFlooring
getNoOfMetres(theSurface)
getLength()
getWidth()
time
5
Q6. (a) Explain each of the following GUI terms
(i)
components
(ii)
events
(iii)
listeners
How are these concepts related in creating a Java program that uses a GUI?
(12 Marks)
(b) What packages from the Java Class Library are frequently imported when creating Java
GUIs?
(3 Marks)
(c) Explain the role of layout managers in creating Java GUIs. In your answer describe the
effect of each type of manager on an interface.
(8 Marks)
(d) Write a statement to follow the statement given, that changes the layout manager of the
panel instantiated below, to an alternative other than the default.
JPanel panel = new JPanel()
(2 Marks)
6
Download