Assignment 2 A172
Assignment 2 A172
Assignment 2 A172
Statement5;
1
The answer
1. According to the coding that has been given, the Statement 3 will
not 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:
For commercial vehicles, the toll charges are as follows (an extra charge of 10 sen is
added):
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.
3
assignment2UI.java
package my.assignment2;
import javax.swing.ButtonGroup;
public assignment2UI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed"
desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowCons
tants.EXIT_ON_CLOSE);
jLabel1.setText("************* Toll
Charge Calculator **************");
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);
}
});
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);
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)
.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)
.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) {
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>
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
17
Asg23.java
package assignmentFile;
import java.util.*;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
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