Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

AZEEM SATHAR 401907116 PROG 732 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 class JMyNewHome extends JFrame implements ActionListener {

private final String[] models = {"Aspen", "Brittany", "Colonial", "Dartmoor"};

private final int[] modelPrices = {110000, 120000, 120000, 130000};

private final int bedroomPrice = 10500;

private final int garagePrice = 7775;

private JComboBox<String> modelComboBox;

private JComboBox<Integer> bedroomComboBox;

private JComboBox<Integer> garageComboBox;

private JLabel totalLabel;

public JMyNewHome() {

setTitle("New Home Construction Order");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new FlowLayout());

modelComboBox = new JComboBox<>(models);

bedroomComboBox = new JComboBox<>(new Integer[]{2, 3, 4});

garageComboBox = new JComboBox<>(new Integer[]{0, 1, 2, 3});

JButton calculateButton = new JButton("Calculate Total");

calculateButton.addActionListener(this);

totalLabel = new JLabel("Total Estimated Price: R0");

Page | 2
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1

add(new JLabel("Choose a Model:"));

add(modelComboBox);

add(new JLabel("Choose Number of Bedrooms:"));

add(bedroomComboBox);

add(new JLabel("Choose Garage Type:"));

add(garageComboBox);

add(calculateButton);

add(totalLabel);

pack();

setLocationRelativeTo(null);

@Override

public void actionPerformed(ActionEvent e) {

int modelIndex = modelComboBox.getSelectedIndex();

int selectedBedrooms = (int) bedroomComboBox.getSelectedItem();

int selectedGarage = (int) garageComboBox.getSelectedItem();

int totalPrice = modelPrices[modelIndex] + selectedBedrooms * bedroomPrice + selectedGarage


* garagePrice;

totalLabel.setText("Total Estimated Price: R" + totalPrice);

public static void main(String[] args) {

SwingUtilities.invokeLater((null));{

Page | 3
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1

JMyNewHome app = new JMyNewHome();

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 class EmployeeInfo extends JFrame {


/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField firstNameField;
private JTextField jobTitleField;
private JButton searchButton;
private JLabel firstNameLabel;
private JLabel jobTitleLabel;
private JLabel resultLabel;

// Parallel arrays to store employee names and job titles


private String[] employeeNames = {
"Sash Adari", "Azeem Sathar", "Ismaaeel Shaik"
};
private String[] jobTitles = {
"Scientist", "Infrastructure Engineer", "System Administrator"
};

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));

// Create and initialize components


firstNameLabel = new JLabel("First and Last Name:");
firstNameField = new JTextField();
jobTitleLabel = new JLabel("Job Title:");
jobTitleField = new JTextField();
searchButton = new JButton("Search");
resultLabel = new JLabel();

// Add components to the JFrame


add(firstNameLabel);
add(firstNameField);
add(jobTitleLabel);
add(jobTitleField);
add(searchButton);
add(resultLabel);

// Add ActionListener to the searchButton


searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String fullName = firstNameField.getText();
String[] names = fullName.split(" ");
if (names.length == 2) {
String firstName = names[0];

Page | 7
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1

String lastName = names[1];


String jobTitle = findJobTitle(firstName, lastName);
if (jobTitle != null) {
jobTitleField.setText(jobTitle);
resultLabel.setText("");
} else {
jobTitleField.setText("");
resultLabel.setText("Employee not found.");
}
} else {
jobTitleField.setText("");
resultLabel.setText("Please enter both first and last names.");
}
}
});
}

// 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
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {

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.*;

public class ChatGUI extends JFrame {

Page | 10
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1

/**

*/

private static final long serialVersionUID = 1L;

private JTextField textField;

private JTextArea textArea;

private JButton sendButton;

private JButton loginButton;

private JTextField loginField;

private JLabel loginLabel;

private Socket socket;

private DataInputStream in;

private DataOutputStream out;

private boolean isConnected = false;

public ChatGUI() {

super("Chat Application");

setLayout(new FlowLayout());

loginLabel = new JLabel("Login:");

add(loginLabel);

loginField = new JTextField(20);

add(loginField);

loginButton = new JButton("Login");

add(loginButton);

loginButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (!isConnected) {

String name = loginField.getText();

Page | 11
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1

login(name);

});

textField = new JTextField(20);

add(textField);

textField.setEnabled(false);

sendButton = new JButton("Send");

add(sendButton);

sendButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (isConnected) {

String s = textField.getText();

try {

out.writeUTF(s);

out.flush();

} catch (IOException ex) {

ex.printStackTrace();

textField.setText("");

});

textArea = new JTextArea(20, 30);

add(textArea);

textArea.setEditable(false);

Page | 12
AZEEM SATHAR 401907116 PROG 732 ASSIGNMENT 1

private void login(String name) {

try {

socket = new Socket("localhost", 8765);

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

out.writeUTF(name);

out.flush();

isConnected = true;

textField.setEnabled(true);

Thread t = new Thread(new Runnable() {

public void run() {

while (true) {

try {

String s = in.readUTF();

textArea.append(s + "\n");

} catch (IOException ex) {

ex.printStackTrace();

});

t.start();

} catch (IOException ex) {

ex.printStackTrace();

public static void main(String[] args) {

ChatGUI gui = new ChatGUI();

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 ]

2. GeeksforGeeks. (2016). Socket Programming in Java - GeeksforGeeks. [online]


Available at: https://www.geeksforgeeks.org/socket-programming-in-java/.

3. Harold, E., n.d. Java network programming.

4. Ntu.edu.sg. (2018). GUI Programming - Java Programming Tutorial. [online] Available


at: https://www3.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html.

5. Pandey, H., n.d. Java programming.

6. Sarang, P., 2012. Java programming. New York: McGraw-Hill.

Page | 15

You might also like