Professional Documents
Culture Documents
PR732 - Assignment 1
PR732 - Assignment 1
Table of Contents
QUESTION 1: ........................................................................................................................................... 1
Write an application for a construction company to handle a customer’s order to build a new home.
Use separate Button Groups to allow the customer to select one of four models, the number of
bedrooms, and a garage type. Assume that the models are the Aspen, R110 000; the Brittany, R120
000; the Colonial, R120 000; or the Dartmoor, R130 000. Assume that any model can have two,
three, or four bedrooms and that each bedroom adds R10 500 to the base price. Assume that the
garage type can accommodate zero, one, two, or three cars, and that each car adds R7775 to the
price. The interface should be as shown below. .................................................................................... 1
Screenshots of Output: ........................................................................................................................... 1
Code: ....................................................................................................................................................... 2
QUESTION 2: ........................................................................................................................................... 5
Create a an interface that contains two JTextFields, a JButton, and three JLabels. When the user
types an employee’s first and last names (separated by a space) in a JTextField, the employee’s job
title is displayed in a second JTextField. Include two JLabels to describe the JTextFields used for data
entry. Also include a third JLabel that displays the employee’s title or an error message if no match is
found for the employee. Use parallel arrays to store the employees’ names and job titles. ................ 5
Screenshots of Output: ........................................................................................................................... 5
Code: ....................................................................................................................................................... 6
QUESTION 3: ......................................................................................................................................... 10
Create a GUI application in Java that allows users to chat with each other over a network using
sockets. The application should consist of two parts: a client and a server…. ..................................... 10
Screenshot of Output:........................................................................................................................... 10
Code: ..................................................................................................................................................... 10
REFERENCES:......................................................................................................................................... 15
Page | 0
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
QUESTION 1:
Write an application for a construction company to handle a customer’s order to build a new
home. Use separate Button Groups to allow the customer to select one of four models, the
number of bedrooms, and a garage type. Assume that the models are the Aspen, R110 000;
the Brittany, R120 000; the Colonial, R120 000; or the Dartmoor, R130 000. Assume that any
model can have two, three, or four bedrooms and that each bedroom adds R10 500 to the
base price. Assume that the garage type can accommodate zero, one, two, or three cars, and
that each car adds R7775 to the price. The interface should be as shown below.
Screenshots of Output:
Page | 1
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public JMyNewHome() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
calculateButton.addActionListener(this);
Page | 2
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
add(modelComboBox);
add(bedroomComboBox);
add(garageComboBox);
add(calculateButton);
add(totalLabel);
pack();
setLocationRelativeTo(null);
@Override
SwingUtilities.invokeLater((null));{
Page | 3
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
app.setVisible(true);
};
Page | 4
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
QUESTION 2:
Create a an interface that contains two JTextFields, a JButton, and three JLabels. When the
user types an employee’s first and last names (separated by a space) in a JTextField, the
employee’s job title is displayed in a second JTextField. Include two JLabels to describe the
JTextFields used for data entry. Also include a third JLabel that displays the employee’s title
or an error message if no match is found for the employee. Use parallel arrays to store the
employees’ names and job titles.
Screenshots of Output:
Page | 5
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public EmployeeInfo() {
// Initialize the JFrame
setTitle("Employee Information");
Page | 6
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4, 2));
Page | 7
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
// Method to find the job title based on first and last names
private String findJobTitle(String firstName, String lastName) {
for (int i = 0; i < employeeNames.length; i++) {
if (employeeNames[i].equalsIgnoreCase(firstName + " " + lastName)) {
return jobTitles[i];
}
}
return null; // The Employee is not found
}
Page | 8
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
@Override
public void run() {
EmployeeInfo empInfo = new EmployeeInfo();
empInfo.setVisible(true);
}
});
}
}
Page | 9
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
QUESTION 3:
Create a GUI application in Java that allows users to chat with each other over a network
using sockets. The application should consist of two parts: a client and a server….
Screenshot of Output:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
Page | 10
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
/**
*/
public ChatGUI() {
super("Chat Application");
setLayout(new FlowLayout());
add(loginLabel);
add(loginField);
add(loginButton);
loginButton.addActionListener(new ActionListener() {
if (!isConnected) {
Page | 11
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
login(name);
});
add(textField);
textField.setEnabled(false);
add(sendButton);
sendButton.addActionListener(new ActionListener() {
if (isConnected) {
String s = textField.getText();
try {
out.writeUTF(s);
out.flush();
ex.printStackTrace();
textField.setText("");
});
add(textArea);
textArea.setEditable(false);
Page | 12
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
try {
in = new DataInputStream(socket.getInputStream());
out.writeUTF(name);
out.flush();
isConnected = true;
textField.setEnabled(true);
while (true) {
try {
String s = in.readUTF();
textArea.append(s + "\n");
ex.printStackTrace();
});
t.start();
ex.printStackTrace();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Page | 13
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
gui.setSize(400, 500);
gui.setVisible(true);
Page | 14
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1
REFERENCES:
1. CITATION Far \l 1033 ]
Page | 15