0% found this document useful (0 votes)
17 views8 pages

BIT202344995 Assignment 2

Uploaded by

8t8svswp8q
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

BIT202344995 Assignment 2

Uploaded by

8t8svswp8q
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

NAME: ANTONY NGURU CHEGE

ADM NO: BIT/2023/44995

DATE: 09/04/24

BIT/2023/44995
1. Using GUI, write a program code that uses four checkboxes for selecting four
units in BSC IT of your choice.
import javax.swing.*;

import java.awt.event.*;

class BscIT extends JFrame implements ActionListener {

private JCheckBox unit1Checkbox, unit2Checkbox, unit3Checkbox, unit4Checkbox;

private JButton selectButton;

public BscIT() {

setTitle("BSC IT Unit Selection");

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

unit1Checkbox = new JCheckBox("Programming");

unit2Checkbox = new JCheckBox("Database Management");

unit3Checkbox = new JCheckBox("Network Security");

unit4Checkbox = new JCheckBox("Web Development");

selectButton = new JButton("Select");

selectButton.addActionListener(this);

panel.add(unit1Checkbox);

panel.add(unit2Checkbox);

panel.add(unit3Checkbox);

panel.add(unit4Checkbox);

panel.add(selectButton);

add(panel);

BIT/2023/44995
setVisible(true);

public void actionPerformed(ActionEvent ) {

if (e.getSource() == selectButton) {

String selectedUnits = "Selected Units:\n";

if (unit1Checkbox.isSelected()) {

selectedUnits += unit1Checkbox.getText() + "\n";

if (unit2Checkbox.isSelected()) {

selectedUnits += unit2Checkbox.getText() + "\n";

if (unit3Checkbox.isSelected()) {

selectedUnits += unit3Checkbox.getText() + "\n";

if (unit4Checkbox.isSelected()) {

selectedUnits += unit4Checkbox.getText() + "\n";

JOptionPane.showMessageDialog(this, selectedUnits);

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

new BscIT();

});

BIT/2023/44995
2. Write a simple MIDlet application to display your name and Registration
number on the screen.
import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class Mydetails extends MIDlet {

private Display display;

private Form infoForm;

private StringItem nameItem;

private StringItem regNumberItem;

public Mydetails() {

infoForm = new Form("My Information");

nameItem = new StringItem("Name:", "Antony Chege");

regNumberItem = new StringItem("Registration Number:", "854010");

infoForm.append(nameItem);

infoForm.append(regNumberItem);

public void startApp() {

display = Display.getDisplay(this);

display.setCurrent(infoForm);

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

BIT/2023/44995
3. Write a MIdlet application to create a list of all the subjects pursued in your
year of study.
import javax.microedition.lcdui.List;

import javax.microedition.lcdui.Choice;

import javax.microedition.lcdui.Display;

import javax.microedition.midlet.MIDlet;

public class Subjects extends MIDlet {

private List subjectList;

public Subjects() {

subjectList = new List("Select a unit", Choice.MULTIPLE);

subjectList.append("Mobile Development", null);

subjectList.append("Operating systems", null);

subjectList.append("Software and Hardware Installation", null);

subjectList.append("Introduction to Business", null);

subjectList.append("Financial accounting", null);

subjectList.append("Probability and Statistics", null);

subjectList.append("TQM", null);

public void startApp() {

Display display = Display.getDisplay(this);

display.setCurrent(subjectList);

public void pauseApp() {

public void destroyApp(boolean unconditional) {

BIT/2023/44995
4. Write a MIDlet application with the following functionalities.
a. Add record
b. Delete record
c. Replace record
This MIDlet application consists of a form with a text field (recordField) to input records and a list
(recordList) to display the records. It provides functionalities to add, delete, and replace records. The
records are stored in a vector (records). When a record is added, deleted, or replaced, the list of records
is updated accordingly by calling the updateRecordList() method.

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import java.util.Vector;

public class RecordManagerMIDlet extends MIDlet implements CommandListener {

private Display display;

private Form mainForm;

private Command addCommand;

private Command deleteCommand;

private Command replaceCommand;

private Command exitCommand;

private TextField recordField;

private List recordList;

private Vector records;

public RecordManagerMIDlet() {

mainForm = new Form("Record Management");

addCommand = new Command("Add", Command.OK, 1);

deleteCommand = new Command("Delete", Command.OK, 2);

replaceCommand = new Command("Replace", Command.OK, 3);

exitCommand = new Command("Exit", Command.EXIT, 4);

recordField = new TextField("Record:", "", 50, TextField.ANY);

BIT/2023/44995
recordList = new List("Records", List.IMPLICIT);

mainForm.append(recordField);

mainForm.addCommand(addCommand);

mainForm.addCommand(deleteCommand);

mainForm.addCommand(replaceCommand);

mainForm.addCommand(exitCommand);

mainForm.setCommandListener(this);

records = new Vector();

public void startApp() {

display = Display.getDisplay(this);

display.setCurrent(mainForm);

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable d) {

if (c == exitCommand) {

destroyApp(true);

notifyDestroyed();

} else if (c == addCommand) {

String newRecord = recordField.getString();

addRecord(newRecord);

recordField.setString("");

} else if (c == deleteCommand) {

int selectedIndex = recordList.getSelectedIndex();

if (selectedIndex >= 0 && selectedIndex < records.size()) {

records.removeElementAt(selectedIndex);

updateRecordList();

BIT/2023/44995
}

} else if (c == replaceCommand) {

int selectedIndex = recordList.getSelectedIndex();

String updatedRecord = recordField.getString();

if (selectedIndex >= 0 && selectedIndex < records.size() && !updatedRecord.equals("")) {

records.setElementAt(updatedRecord, selectedIndex);

updateRecordList();

recordField.setString("");

private void addRecord(String newRecord) {

if (!newRecord.equals("")) {

records.addElement(newRecord);

updateRecordList();

private void updateRecordList() {

recordList.deleteAll();

for (int i = 0; i < records.size(); i++) {

recordList.append((String) records.elementAt(i), null);

display.setCurrent(recordList);

BIT/2023/44995

You might also like