You are on page 1of 8

2020F-BCE-159 Section A Syed Muhammad Hasan Danish

PRACTICE ASSIGNMENT

Source Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class C_conv {
public static void converter()
{
// Creating a new frame using JFrame
JFrame f = new JFrame("Currency CONVERTER");
// Creating three labels
JLabel l1, l2,l3;
// Creating three text fields.
JTextField t1, t2,t3;
// Creating four buttons
JButton b1, b2, b3,b4;
// Naming the labels and setting
// the bounds for the labels
l1 = new JLabel("US Dollar:");
l1.setBounds(20, 40, 60, 30);
l2 = new JLabel("Singaporean Dollar:");
l2.setBounds(20, 70, 60, 30);
l3 = new JLabel("Euro:");
l3.setBounds(20, 100, 60, 30);
// bounds for the text fields
t1 = new JTextField("");
t1.setBounds(80, 40, 70, 30);
t2 = new JTextField("");
t2.setBounds(80, 70, 70, 30);
t3 = new JTextField("");
t3.setBounds(80, 100, 70, 30);
// one button to close
// and setting the button bounds
b1 = new JButton("USD");
b1.setBounds(150, 50, 60, 15);
b2 = new JButton("SGD");
b2.setBounds(150, 80, 60, 15);
b4 = new JButton("Euro");
b4.setBounds(150, 110, 60, 15);
b3 = new JButton("clear");
b3.setBounds(150, 150, 80, 30);
// Adding action listener

Page 1 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Converting to float
float d
= Float.parseFloat(t1.getText());
// Converting USD to SGD
float d1 = (float) (d * 1.41);
//converting USD to euro
float d2 = (float) (d*0.92);
String str1 = String.valueOf(d1);
String.format("%.2f", d1);
String.format("%.2f", d2);
String str2 = String.valueOf(d2);
// Placing value of d1 & d2 in the text box
t2.setText(str1);
t3.setText(str2);
}
});
// Adding action listener
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Converting to float
float d2
= Float.parseFloat(t2.getText());
// converting SGD to EURO
float d3 = (float) (d2 * 0.65);
//converting SGD to USD
float d6 = (float) (d2 * 1.41);
String.format("%.2f", d3);
String.format("%.2f", d6);
String str2 = String.valueOf(d3);
String str4 = String.valueOf(d6);

// Placing it in the text box


t3.setText(str2);
t1.setText(str4);
}
});

// Adding action listener


b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Converting to float
float d4
= Float.parseFloat(t3.getText());

Page 2 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish

// Converting euro to usd


float d5 = (float) (d4 *1.41);
// Converting euro to SGD
float d7 = (float) (d4 *0.65);
String.format("%.2f", d7);
String.format("%.2f", d5);
String str3 = String.valueOf(d5);
String str5 = String.valueOf(d7);
// Placing it in the text box
t1.setText(str3);
t2.setText(str5);
}
});

// Adding the created objects


// to the form
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(t3);
f.add(l3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);

f.setLayout(null);
f.setSize(400, 300);
f.setVisible(true);

// Action listener to clear the text fields


b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JButton b3 = new JButton("Clear");
t1.setText(null);
t2.setText(null);
t3.setText(null);
}
});
}
// Driver code
public static void main(String args[])

{
converter();

Page 3 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish

}
}

Output:

Source Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;

class Snake1task2{
private JFrame f;
private JButton g;
private JLabel a,b,c;
private JTextField d,e;
Snake1task2() {
f = new JFrame("Snake Eyes");
a = new JLabel("Dice One");
b = new JLabel("Dice 2");
c = new JLabel("Snake Eyes");
g = new JButton("Roll again");
g.addActionlistener(this);
f.setSize(500, 400);
f.setLayout(new FlowLayout());
f.add(c);
f.add(a);
f.getContentPane().setBackground(Color.pink);
d_a = new JTextField();
e_b = new JTextField();
d_a.setEditabe(false);

Page 4 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish

e_b.setEditable(false);
f.add(d_a);
f.add(b);
f.add(e_b);
f.add(g);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Random r=new Random();
int a= r.nextInt(6-1+1)+1;
int b=r.nextInt(6-1+1)+1;
d_a.setText(""+a);
e_b.setText(""+b);
if(a && b ==1) {
JOptionPane.showMessageDialog(null, "You got 1", "Congrats");
JOptionPane.PLAIN_MESSAGE;
}}}
class Main
public static void main(String[]args){
Snake1task2 eyes=new Snake1task2();
}}
Output:

Source Code:
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

Page 5 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
public class Bmi_Calculator {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
String height="",weight="",gen="",result="";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Bmi_Calculator window = new Bmi_Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Bmi_Calculator() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 310, 310);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

// public void SliderExample() {


JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
slider.setMinorTickSpacing(2);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
// JSlider.setBounds(86, 12, 142, 33);
JPanel panel = new JPanel();
panel.add(slider);
frame.getContentPane().add(slider);
JLabel lblNewLabel = new JLabel("BMI CALCULATOR");
lblNewLabel.setBounds(86, 12, 142, 33);
frame.getContentPane().add(lblNewLabel);
JLabel lblHeight = new JLabel("Height(cm)");
lblHeight.setBounds(25, 57, 87, 15);
frame.getContentPane().add(lblHeight);
textField = new JTextField();

Page 6 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish

textField.setBounds(113, 57, 87, 19);


frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel lblWeight = new JLabel("Weight(Kg)");
lblWeight.setBounds(25, 96, 87, 15);
frame.getContentPane().add(lblWeight);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(113, 94, 87, 19);
frame.getContentPane().add(textField_1);
public void calculate_bmi() {
if(textField.getText().isEmpty() || textField_1.getText().isEmpty() || gen.isEmpty())
{error();
return;
}
double h,w,r;
height = textField.getText();
weight = textField_1.getText();
day = dates[comboBox.getSelectedIndex()];
month = months[comboBox_1.getSelectedIndex()];
year = years[comboBox_2.getSelectedIndex()];
h=Double.parseDouble(height);
w=Double.parseDouble(weight);
r=w/h2 * 10000
DecimalFormat df = new DecimalFormat("###.##");
result="";
result+=String.valueOf(df.format(r));
JFrame f =new JFrame();
JOptionPane.showMessageDialog(f,result);
}
public void error() {
JFrame f =new JFrame();
JOptionPane.showMessageDialog(f,"Fill all the details","Alert",JOptionPane.WARNING_MESSAGE);
}

Output:

Page 7 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish

Page 8 of 8

You might also like