Escolar Documentos
Profissional Documentos
Cultura Documentos
Practical 1
Part A
1. Match the following terms with the descriptions given below:
Descriptions:
(a) an instance of a class
(b) contains statements that represent the action that an object or class takes
when a message is sent to it
(c) a method defined for a class
(d) defines how a collection of objects behave and what information they
maintain
(e) a class that contains features that are common to several other classes
related in an inheritance hierarchy
(f) a value that maintains information for a specific instance of a class
(g) a class that inherits features from its ancestor
(h) a method that is defined for an object
(i) a data value that is passed to a method for its processing
javax.swing.JFrame window;
window = new javax.swing.JFrame();
window.setSize(300,100);
window.setTitle("My First Java Program in Lab");
window.setVisible(true);
}
}
page 1/3
UCCC2013 Object-Oriented Programming 201101
2. Look in the JavaPrograms folder. Do you notice another file added? When did
this happen?
3. In the program, the last message sent to the JFrame object makes the window
visible.
Modify this message so that the JFrame object will not be visible when you run
the program. Compile (step E) and run (step F) your program again.
4. In the program, notice that the JFrame class is fully qualified by its package
name, namely javax.swing. Add an import statement to the program so that
the class can be referred to as simply JFrame. Compile and run your modified
program and make sure that it behaves the same.
5. Change the size of the JFrame in the program to other values of your choice.
Compile and run the program and observe the results of your changes.
class SecondProgram {
firstPart = JOptionPane.showInputDialog(null,
"What is the first part of your name?");
lastPart = JOptionPane.showInputDialog(null,
"What is the second part of your name?");
JOptionPane.showMessageDialog(null, "Your name is " +
firstPart + lastPart);
}
}
page 2/3
UCCC2013 Object-Oriented Programming 201101
1. Using JCreator.
A. Start JCreator. Click the Finish button for the Wizard.
B. Create the above program as follows:
(i) Select File→New→File.
(ii) For File Type, select Java File.
(iii) For Name, enter SecondProgram.
(iv) For Location, select the JavaPrograms folder.
(v) Click the Finish button.
(vi) Copy and paste the program.
C. Save the program by selecting File→Save.
D. Compile the program by selecting Build→Compile File.
E. Run the program by selecting Build→Execute File.
You can close the window that this program creates by clicking on the 'X'
in the top right corner of the window. You also need to press any key to
terminate your program.
2. Notice that there is a problem with the output: there is no space between the
first and second parts of your name. Modify the code so that the output will
have a space between them.
3. Instead of displaying your name, modify your program so that it displays your
initials, with dots after them (but no spaces). For example, if the inputs to your
program are "Bee" as the first part and "Lee" as the second part, the output
should be "Your initials are B.L.".
[Hint: Use one of the String class methods.]
4. Modify your program to have a single input dialog to enter your name, with a
single space between the first and second parts. For example, you might enter
"Bee Lee" as your name. The program then displays your initials.
2. Write a Java program that displays a frame window 300 pixels wide and 200
pixels high with the title My First Frame. Place the frame so that its top, left
corner is at a position 100 pixels from the left of the screen and 150 pixels from
the top of the screen. To position a window at a specified location, you use the
setLocation method, as in
//frame refers to a JFrame object
frame.setLocation(50,50);
Experiment to determine how the two arguments in the setLocation method
affect the positioning of the window.
page 3/3