Vous êtes sur la page 1sur 19

STIA1123 Assignment 2

(Due Thursday 24 May 2018)


Answer all questions below. For every questions, you must submit a hardcopy and a
softcopy of your source code (.java file). Also, for the hardcopy, print some sample
outputs from the execution of your programs.
For the softcopy, submit your zipped (compressed) source codes through the UUM Online
Learning. The name of your zipped file should be in the format below:
Asg2_101010.rar (if your matric number is 101010)
------------------------------------------------------------------------------------------------------------
Reminder: Each source code submitted must be the result of your own work. Copied
source codes will earned you zero (0) mark.

TOPIC: Exception Handling Question:


Suppose that Statement2 causes an exception in the following try-catch block:
try{
Statement1;
Statement2;
Statement3;
}
catch (Exception1 ex1){….}

catch (Exception2 ex2){ throw


ex2;}
finally
{
Statement4; }

Statement5;

JUSTIFY your answer for the following questions:

1. Will Statement3 be executed?


2. If the exception is of type Exception1, will statement4 be executed, and will
statement5 be executed?
3. If the exception is of type Exception2, will statement4 be executed, and will
statement5 be executed?
4. If the exception is not Exception1 nor Exception2, will statement4 be executed, and
will statement5 be executed?
5. Will Statement5 be executed if the exception is not caught?
6. If the exception is caught in the catch, will Statement5 be executed?

1
The answer

1. According to the coding that has been given, the Statement 3 will
not be executed.

2. If the exception is of type Exception 1, then the Statement 4 and


Statement 5 still will be executed.

3. If the exception is of the Exception 2, then the Statement 4 still can


been executed but for Statement 5, it still cannot be executed.

4. If the exception is not Exception 1 or Exception 2, this two


statement for the Statement 4 and Statement 5 is still can be
executed.

5. For that situation, the Statement 5 can still be executed.

6. For that situation, the Statement 5 can still be executed for only
under exception in Statement 1 but for Statement 2, the Statement 5
cannot be executed.

2
TOPIC: GUI (Answer one of the following GUI questions)

Question 1:
A newly built 30.8km highway has 3 entry/exit points which is Bandar Aman, Hutan Kota
and Alor Paya. The toll charges between these points for private vehicles are as shown in
the table below:

Bandar Aman Hutan Kota Alor Paya


Bandar Aman RM1.20 RM1.70
Hutan Kota RM1.20 RM0.60
Alor Paya RM1.70 RM0.60

For commercial vehicles, the toll charges are as follows (an extra charge of 10 sen is
added):

Bandar Aman Hutan Kota Alor Paya


Bandar Aman RM1.30 RM1.80
Hutan Kota RM1.30 RM0.70
Alor Paya RM1.80 RM0.70

A toll calculation system is being built for use in all of the highway toll plazas. Given below
is the GUI for the system. The user needs to select the entry point and the exit point from
the combo boxes, named entryCB and exitCB, respectively. Then, the user must choose
the vehicle category (private or commercial) by clicking the related radio buttons,
privateRB or commRB. Next, when the user clicks the calculate button named calcBtn,
the program will display the output containing the route, the category and the calculated
charge in the text area, named outputTA (see below).

Based on the information given above, your task is to write the GUI program for the toll
charge calculator as shown above which has the event handling method for the calculate
button, calcBtn.

Name of source code file to be submitted: Asg22.java

3
assignment2UI.java
package my.assignment2;

import javax.swing.ButtonGroup;

public class assignment2UI extends


javax.swing.JFrame {

public assignment2UI() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed"
desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
entryCB = new
javax.swing.JComboBox<>();
exitCB = new javax.swing.JComboBox<>();
jLabel4 = new javax.swing.JLabel();
calcBtn = new javax.swing.JButton();
privateRB = new
javax.swing.JRadioButton();
commRB = new
javax.swing.JRadioButton();
jScrollPane1 = new
javax.swing.JScrollPane();
outputTA = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowCons
tants.EXIT_ON_CLOSE);

jLabel1.setText("************* Toll
Charge Calculator **************");

jLabel2.setText("Entry Point :");

jLabel3.setText("Exit Point :");

entryCB.setModel(new
javax.swing.DefaultComboBoxModel<>(new String[]
{ "Alor Paya", "Hutan Kota", "Bandar Aman" }));
entryCB.addActionListener(new
java.awt.event.ActionListener() {
4
public void
actionPerformed(java.awt.event.ActionEvent evt)
{
entryCBActionPerformed(evt);
}
});

exitCB.setModel(new
javax.swing.DefaultComboBoxModel<>(new String[]
{ "Bandar Aman", "Hutan Kota", "Alor Paya" }));
exitCB.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt)
{
exitCBActionPerformed(evt);
}
});

jLabel4.setText("Vehicle Category :");

calcBtn.setText("Calculate");
calcBtn.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt)
{
calcBtnActionPerformed(evt);
}
});

privateRB.setText("Private");
privateRB.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt)
{
privateRBActionPerformed(evt);
}
});

commRB.setText("Commercial");
commRB.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt)
{
commRBActionPerformed(evt);
}

5
});

outputTA.setColumns(20);
outputTA.setRows(5);
jScrollPane1.setViewportView(outputTA);

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLay
out.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()
.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swin
g.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swin
g.GroupLayout.Alignment.LEADING)

.addComponent(commRB)

.addComponent(jLabel4,
javax.swing.GroupLayout.PREFERRED_SIZE, 133,
javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap(javax.swing.GroupLayout.DEFAUL
T_SIZE, Short.MAX_VALUE))

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swin
g.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swin
g.GroupLayout.Alignment.LEADING, false)

.addGroup(javax.swing.GroupLayout.Alignment.TRA
ILING,
layout.createParallelGroup(javax.swing.GroupLay
out.Alignment.LEADING)

6
.addGroup(layout.createSequentialGroup()

.addComponent(jLabel2,
javax.swing.GroupLayout.PREFERRED_SIZE, 88,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(98, 98, 98)

.addComponent(jLabel3,
javax.swing.GroupLayout.PREFERRED_SIZE, 78,
javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addComponent(entryCB,
javax.swing.GroupLayout.PREFERRED_SIZE, 168,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(exitCB,
javax.swing.GroupLayout.PREFERRED_SIZE, 168,
javax.swing.GroupLayout.PREFERRED_SIZE)))

.addComponent(jLabel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGap(0, 0,
Short.MAX_VALUE))

.addComponent(jScrollPane1)

.addGroup(layout.createSequentialGroup()

.addComponent(privateRB)

.addPreferredGap(javax.swing.LayoutStyle.Compon
entPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)

.addComponent(calcBtn)))
.addContainerGap())))
);
layout.setVerticalGroup(

7
layout.createParallelGroup(javax.swing.GroupLay
out.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.Compon
entPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swin
g.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel3))

.addPreferredGap(javax.swing.LayoutStyle.Compon
entPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swin
g.GroupLayout.Alignment.BASELINE)
.addComponent(entryCB,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(exitCB,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.Compon
entPlacement.RELATED)
.addComponent(jLabel4,
javax.swing.GroupLayout.PREFERRED_SIZE, 27,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.Compon
entPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swin
g.GroupLayout.Alignment.BASELINE)
.addComponent(privateRB)
.addComponent(calcBtn))

.addPreferredGap(javax.swing.LayoutStyle.Compon
entPlacement.RELATED)
.addComponent(commRB)

8
.addPreferredGap(javax.swing.LayoutStyle.Compon
entPlacement.RELATED)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(javax.swing.GroupLayout.DEFAUL
T_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void
calcBtnActionPerformed(java.awt.event.ActionEve
nt evt) {

String entry = (String)


entryCB.getSelectedItem();
String exit = (String)
exitCB.getSelectedItem();

ButtonGroup group = new ButtonGroup();


group.add(privateRB);
group.add(commRB);

if (privateRB.isSelected()) {
if (entry.equals("Bandar Aman")) {
if (exit.equals("Hutan Kota"))
{
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Private " + "\nToll Charge=RM
1.20");
} else if (exit.equals("Alor
Paya")) {
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Private " + "\nToll Charge=RM
1.70");
}

if (entry.equals("Hutan Kota")) {

9
if (exit.equals("Bandar Aman"))
{
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Private " + "\nToll Charge=RM
1.20");
} else if (exit.equals("Alor
Paya")) {
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Private " + "\nToll Charge=RM
0.60");
}

} else if (entry.equals("Alor
Paya")) {
if (exit.equals("Bandar Aman"))
{
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Private " + "\nToll Charge=RM
1.70");
} else if (exit.equals("Hutan
Kota")) {
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Private " + "\nToll Charge=RM
0.60");
}
}

}
if (commRB.isSelected()) {
if (entry.equals("Bandar Aman")) {
if (exit.equals("Hutan Kota"))
{
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Commercial " + "\nToll Charge=RM
1.30");
} else if (exit.equals("Alor
Paya")) {
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "

10
+ exitCB.getSelectedItem().toString() +
"\nCategory: Commercial " + "\nToll Charge=RM
1.80");
}

if (entry.equals("Hutan Kota")) {
if (exit.equals("Bandar Aman"))
{
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Commercial " + "\nToll Charge=RM
1.30");
} else if (exit.equals("Alor
Paya")) {
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Commercial" + "\nToll Charge=RM
0.70");
}

} else if (entry.equals("Alor
Paya")) {
if (exit.equals("Bandar Aman"))
{
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Commercial " + "\nToll Charge=RM
1.80");
} else if (exit.equals("Hutan
Kota")) {
outputTA.setText("Route: "
+ entryCB.getSelectedItem().toString() + " to "
+ exitCB.getSelectedItem().toString() +
"\nCategory: Commercial " + "\nToll Charge=RM
0.70");
}
}

11
private void
privateRBActionPerformed(java.awt.event.ActionE
vent evt) {

private void
commRBActionPerformed(java.awt.event.ActionEven
t evt) {

private void
exitCBActionPerformed(java.awt.event.ActionEven
t evt) {
// TODO add your handling code here:
}

private void
entryCBActionPerformed(java.awt.event.ActionEve
nt evt) {

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed"
desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6)
is not available, stay with the default look
and feel.
* For details see
http://download.oracle.com/javase/tutorial/uisw
ing/lookandfeel/plaf.html
*/
try {
for
(javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels(
)) {
if
("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getCl
assName());
break;

12
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(assignment2U
I.class.getName()).log(java.util.logging.Level.
SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(assignment2U
I.class.getName()).log(java.util.logging.Level.
SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(assignment2U
I.class.getName()).log(java.util.logging.Level.
SEVERE, null, ex);
} catch
(javax.swing.UnsupportedLookAndFeelException
ex) {

java.util.logging.Logger.getLogger(assignment2U
I.class.getName()).log(java.util.logging.Level.
SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new
Runnable() {
public void run() {
new
assignment2UI().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton calcBtn;
private javax.swing.JRadioButton commRB;
private javax.swing.JComboBox<String>
entryCB;
private javax.swing.JComboBox<String>
exitCB;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;

13
private javax.swing.JScrollPane
jScrollPane1;
private javax.swing.JTextArea outputTA;
private javax.swing.JRadioButton privateRB;
// End of variables declaration
}

The Output

Interface

14
For Private

15
For Commercial

16
TOPIC: File (Answer one of the following File questions) Question
1:
Suppose you are given a text file that contains the names of people. Every name in the file
consists of a first name and last name. Unfortunately, the programmer who created the file
of names had a strange sense of humour and did not guarantee that each name was on a
single line of the file. Read this file of names and write them to a new text file, one name
per line with the words “First Name” preceding the first name and “Last
Name” preceding the last name.
For example, the input file contains
Adib Habbal
Jane
Black
The output file should be
First Name- Adib Last Name- Habbal
First Name- Jane Last Name- Black

(Hint: dealing with file requires you to use exception)

Name of source code file to be submitted: Asg23.java

17
Asg23.java
package assignmentFile;

import java.util.*;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Asg23 {

public static void main(String[] args) {


File recordFile = new File("recordFile.txt");
try {
PrintWriter result = new
PrintWriter(recordFile);
result.println("Adib Habbal");
result.println("Jane");
result.println("Black");
result.close();
} catch (IOException aa) {
System.out.println("*********** There is no
record! *************");
}

try {
Scanner sc = new Scanner(recordFile);
while (sc.hasNext()) {
String f_Name = sc.next();
String l_Name = sc.next();
System.out.println("FirstName- " + f_Name +
" LastName- " + l_Name);
}
sc.close();
} catch (FileNotFoundException bb) {
System.out.println("************ There is no
record! ***********");
}
}
}

18
The Output

In recordFile.txt

19

Vous aimerez peut-être aussi