Você está na página 1de 3

UCCC2013 Object-Oriented Programming 201101

Practical 1
Part A
1. Match the following terms with the descriptions given below:

Terms: method, argument, object, class, class method, instance method,


instance value, superclass, subclass

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

Part B (Refer to the following program for this part)


class FirstProgram {

public static void main (String [] args) {

javax.swing.JFrame window;
window = new javax.swing.JFrame();

window.setSize(300,100);
window.setTitle("My First Java Program in Lab");
window.setVisible(true);
}
}

1. Using Notepad and Java Development Kit (jdk).


A. Create a folder on your Desktop with the name JavaPrograms.
B. Create the above program using Notepad as follows:
(i) Click Start→All Programs→Accessories→Notepad.
(ii) Copy and paste the program.
C. Save the program as follows:
(i) Click File→Save.
(ii) For Save in, select the JavaPrograms folder.
(iii) For Save as type, select All Files.
(iv) Enter the file name FirstProgram.java.
D. Open a Command Prompt Window and set the environment as follows:
(i) Click Start→All Programs→Accessories→Command Prompt.
(ii) Enter the following command to change to the JavaPrograms
directory:
cd Desktop\JavaPrograms

page 1/3
UCCC2013 Object-Oriented Programming 201101

(iii) Enter the following two commands to set the environment:


set path=C:\Program Files\Java\jdk1.6.0_15\bin
set classpath=.
The first command sets the PATH environment variable so that we
can execute the Java compiler and interpreter from the current
directory. The second command tells the compiler and interpreter
where to find the source files. The period (.) means the current
directory.
Note: You only need to do this step once.
E. Compile the program by entering the command:
javac FirstProgram.java
F. Run the program by entering the command:
java FirstProgram
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 Ctrl-C to
terminate your program.

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.

Part C (Refer to the following program for this part)


import javax.swing.*;

class SecondProgram {

public static void main (String [] args) {


String firstPart;
String lastPart;

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.

Part D (Additional Exercises)


1. Write a Java program that displays today’s date in two formats, for example:
• 18 January 08
• 18/1/2008
[Hint: Use Date and SimpleDateFormat classes. Remember to use import
statements.]

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

Você também pode gostar