Professional Documents
Culture Documents
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);
Page 2 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish
f.setLayout(null);
f.setSize(400, 300);
f.setVisible(true);
{
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);
Page 6 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish
Output:
Page 7 of 8
2020F-BCE-159 Section A Syed Muhammad Hasan Danish
Page 8 of 8