0% found this document useful (0 votes)
16 views

CCP 54

Uploaded by

ahmed72246
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CCP 54

Uploaded by

ahmed72246
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

CCP
OOP

Submitted By:
Ahmed Abdullah (054)

Submitted to:
Miss Samia Ejaz

Dated:
June 7, 2024

Department of Computer Science,


HITEC University, Taxila
Lab Task No 04

Complex Computing Problem


For Weather station, a system (program) is required that can convert temperature between

Celsius, Kelvin, and Fahrenheit. Develop a file-based GUI system using object-oriented

concepts in Java language that contain two screens.

Output:
Lab Task No 04
Lab Task No 04
Lab Task No 04
MAIN CLASS:

import javax.swing.*;
import java.awt.*;

public class Main {


public static void main(String[] args) {

JFrame frame = new JFrame();


frame.setSize(500,650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setLocationRelativeTo(null);

frame.setResizable(false);
panel panel = new panel(frame);
frame.add(panel);
frame.setVisible(true);

}
}

Second Class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class panel extends JPanel implements ActionListener {

JRadioButton button1;
JRadioButton button2;
JRadioButton button3;
JRadioButton button4;
JRadioButton button5;
JRadioButton button6;
JButton submit;
JLabel label1;
JLabel label2;
ButtonGroup group;

panel(JFrame frame) {
this.setPreferredSize(new Dimension(500, 650));
this.setLayout(null);

label1 = new JLabel("WEATHER SCALE CONVERSION SYSTEM");


label1.setFont(new Font("Arial", Font.BOLD, 20));
label1.setBounds(50, 20, 400, 30);
Lab Task No 04
add(label1);

label2 = new JLabel("-: Please Select Appropriate Option :-");


label2.setFont(new Font("Arial", Font.PLAIN, 17));
label2.setBounds(100, 60, 400, 30);
add(label2);

button1 = new JRadioButton("1: Celsius to Fahrenheit");


button2 = new JRadioButton("2: Celsius to Kelvin");
button3 = new JRadioButton("3: Kelvin to Celsius");
button4 = new JRadioButton("4: Kelvin to Fahrenheit");
button5 = new JRadioButton("5: Fahrenheit to Celsius");
button6 = new JRadioButton("6: Fahrenheit to Kelvin");

button1.setBounds(170, 100, 400, 30);


button2.setBounds(170, 140, 400, 30);
button3.setBounds(170, 180, 400, 30);
button4.setBounds(170, 220, 400, 30);
button5.setBounds(170, 260, 400, 30);
button6.setBounds(170, 300, 400, 30);

button1.setFocusable(false);
button2.setFocusable(false);
button3.setFocusable(false);
button4.setFocusable(false);
button5.setFocusable(false);
button6.setFocusable(false);

group = new ButtonGroup();


group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);
group.add(button5);
group.add(button6);

add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(button6);

submit = new JButton("Submit");


submit.setBounds(200, 350, 100, 50);
submit.setFocusable(false);
submit.addActionListener(this);
add(submit);
}

@Override
public void actionPerformed(ActionEvent e) {
Lab Task No 04
secondwindow sec;

if (button1.isSelected()) {
sec = new secondwindow(1);
} else if (button2.isSelected()) {
sec = new secondwindow(2);
} else if (button3.isSelected()) {
sec = new secondwindow(3);
} else if (button4.isSelected()) {
sec = new secondwindow(4);
} else if (button5.isSelected()) {
sec = new secondwindow(5);
} else if (button6.isSelected()) {
sec = new secondwindow(6);
}
}
}

Third class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class secondwindow extends JPanel implements ActionListener {


int task;
JFrame frame2 = new JFrame();
JLabel label1;
JLabel label2;
JTextField textField;
int result;
JButton button;
JButton savefile;
JLabel resultLabel; // Declare resultLabel here

public secondwindow(int i) {
task = i;
frame2.setSize(500, 650);
frame2.setLocationRelativeTo(null);
frame2.setResizable(false);

this.setPreferredSize(new Dimension(250, 250));


this.setLayout(new FlowLayout(FlowLayout.CENTER, 200, 30));

label1 = new JLabel();


Lab Task No 04
label2 = new JLabel();
label1.setText("WEATHER SCALE CONVERSION SYSTEM");
label2.setText("-: Please Enter Appropriate Data :-");
label1.setFont(new Font("Arial", Font.BOLD, 20));
label2.setFont(new Font("Arial", Font.PLAIN, 17));
add(label1);
add(label2);
textField = new JTextField();
textField.setPreferredSize(new Dimension(100, 50));
add(textField);
savefile = new JButton("Save to File");
savefile.setPreferredSize(new Dimension(100, 50));
savefile.setFocusable(false);
savefile.addActionListener(this);
add(savefile);

button = new JButton("Convert");


button.setPreferredSize(new Dimension(100, 50));
button.setFocusable(false);
button.addActionListener(this);
add(button);

resultLabel = new JLabel();


resultLabel.setFont(new Font("Arial", Font.BOLD, 17));

add(resultLabel);

frame2.add(this);
frame2.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==button){
String data = textField.getText();

try {
int data1 = Integer.parseInt(data);
switch (task) {

case 1:
result = (int) ((data1 * 9.0 / 5.0) + 32);
resultLabel.setText("Temperature ("+ data1+") Celsius in
Fahrenheit is:" + result);

break;
case 2:

result = data1 + 273;


resultLabel.setText("Temperature ("+ data1+") Celsius in
Kelvin is:" + result);
Lab Task No 04

break;
case 3:

result = data1 - 273;


resultLabel.setText("Temperature ("+ data1+") Kelvin in
Celsius is:" + result);

break;
case 4:

result = (int) ((data1 * 9.0 / 5.0) - 459.67);


resultLabel.setText("Temperature ("+ data1+") Kelvin in
Fahrenheit is:" + result);

break;
case 5:

result = (int) ((data1 - 32) * 5.0 / 9.0);


resultLabel.setText("Temperature ("+ data1+") Fahrenheit
in Celsius is:" + result);

break;
case 6:

result = (int) ((data1 + 459.67) * 5.0 / 9.0);


resultLabel.setText("Temperature ("+ data1+") Fahrenheit
in Kelvin is:" + result);

break;
}

} catch (NumberFormatException ex) {


JOptionPane.showMessageDialog(frame2, "Please enter a valid
integer.");

}
} else if (e.getSource()==savefile) {
try (BufferedWriter writer = new BufferedWriter(new
FileWriter("conversionHistory.txt", true))) {
writer.write(resultLabel.getText());
writer.newLine();
JOptionPane.showMessageDialog(frame2,"Result saved to
file");
}
catch (IOException f){
System.err.println("error writing to file" +
f.getMessage());
}
}
}
Lab Task No 04

You might also like