Sei sulla pagina 1di 9

Pre-Lab

1.

package Demos;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Prelab3_1 {

public static void main(String args[]) throws FileNotFoundException

File f=new File("C:\\Users\\ADMIN\\eclipse-workspace\\CO1\\src\\Demos\\Data");

Scanner sc=new Scanner(f);

int a=sc.nextInt();

int b=sc.nextInt();

System.out.println(a+b);

For the above program create a file “Data” with 2 integers in it.

2.

package Demos;

public class Prelab_3_2_Student {

private long id;

private String name;

private String gender;

private String department;

public void setId(long id) {

this.id = id;

public void setName(String name) {


this.name = name;

public void setGender(String gender) {

this.gender = gender;

public void setDepartment(String department) {

this.department = department;

public String toString()

String
s=String.format("ID:%s%nName:%s%nGender:%s%nDepartment:%s",id,name,gender,department);

return s;

package Demos;

import javax.swing.JOptionPane;

public class Prelab3_2 {

public static void main(String args[])

Prelab_3_2_Student s=new Prelab_3_2_Student();

long id=Long.parseLong(JOptionPane.showInputDialog("Enter ID"));

String name=JOptionPane.showInputDialog("Enter name");

String gender=JOptionPane.showInputDialog("Enter Gender");

String department=JOptionPane.showInputDialog("Enter the department");

s.setId(id);

s.setName(name);

s.setGender(gender);
s.setDepartment(department);

System.out.println(s);

In-Lab

1.

package Demos;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Inlab_3_1 {

public static void main(String args[]) throws FileNotFoundException

File f1=new File("C:\\Users\\ADMIN\\eclipse-workspace\\CO1\\src\\Demos\\Names");

Scanner sc1=new Scanner(f1);

File f2=new File("C:\\Users\\ADMIN\\eclipse-workspace\\CO1\\src\\Demos\\Address");

Scanner sc2=new Scanner(f2);

System.out.println("ID\tName\tAddress");

while(sc1.hasNext())

sc2.nextLong(); //Ignore ID in Address file

System.out.println(sc1.nextLong()+" "+sc1.next()+" "+sc2.next());

For the above program, create 2 files “Names” and “Address”.


2.

package Demos;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.event.ActionListener;

public class Inlab_3_2 implements ActionListener {

JFrame f;

JLabel l1,l2,l3,l4;

JTextField t1,t2;

JButton b1,b2;

JRadioButton r1,r2;

ButtonGroup bg;

JComboBox cb;

public Inlab_3_2(){

f=new JFrame("Student Registration");

l1=new JLabel("ID");

l2=new JLabel("Name");

l3=new JLabel("Gender");

l4=new JLabel("Department");

t1=new JTextField(10);

t2=new JTextField(20);

r1=new JRadioButton("Male");

r2=new JRadioButton("Female");

bg=new ButtonGroup();

bg.add(r1);

bg.add(r2);

cb=new JComboBox();
cb.addItem("Select");

cb.addItem("CSE");

cb.addItem("ECE");

cb.addItem("ECSE");

cb.addItem("EEE");

b1=new JButton("Submit");

b2=new JButton("Reset");

f.setLayout(new FlowLayout());

f.setVisible(true);

f.add(l1);

f.add(t1);

f.add(l2);

f.add(t2);

f.add(l3);

f.add(r1);

f.add(r2);

f.add(l4);

f.add(cb);

f.add(b1);

f.add(b2);

b1.addActionListener(this);

b2.addActionListener(this);

public static void main(String args[])

Inlab_3_2 s=new Inlab_3_2();

public void actionPerformed(ActionEvent e)

if(e.getSource()==b1)

{
boolean filled=true;

String id=t1.getText();

if(id.trim().equals(""))

filled=false;

JOptionPane.showMessageDialog(t1,"Enter your ID");

String name=t2.getText();

if(name.trim().equals(""))

filled=false;

JOptionPane.showMessageDialog(t2,"Enter Your Name");

int a=cb.getSelectedIndex();

if(a==0)

filled=false;

JOptionPane.showMessageDialog(cb,"Select your department");

if(filled) {

System.out.println(t1.getText());

System.out.println(t2.getText());

System.out.println(cb.getSelectedItem().toString());

if(r1.isSelected())

System.out.println("Male");

if(r2.isSelected())

System.out.println("Female");

}
}

else if(e.getSource()==b2)

t1.setText("");

t2.setText("");

cb.setSelectedIndex(0);

r1.setSelected(true);

Post-Lab

1.

package stud;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class GUI implements ActionListener

JFrame f;

JButton b1,b2,b3,b4,b5;

JTextField t1,t2,t3;

JLabel l1,l2,l3;

public GUI()

f=new JFrame("Calculator");

l1=new JLabel("Input1");

l2=new JLabel("Input2");

l3=new JLabel("Output");
t1=new JTextField(10);

t2=new JTextField(10);

t3=new JTextField(10);

b1=new JButton("Add");

b2=new JButton("Sub");

b3=new JButton("Mul");

b4=new JButton("Div");

f.setLayout(new FlowLayout());

f.setVisible(true);

f.add(t1);

f.add(t2);

f.add(t3);

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

f.pack();

public void actionPerformed(ActionEvent e)

if(e.getSource()==b1)

int a=Integer.parseInt(t1.getText());

int b=Integer.parseInt(t2.getText());
int c=a+b;

t3.setText(Integer.toString(c));

else if(e.getSource()==b2)

int a=Integer.parseInt(t1.getText());

int b=Integer.parseInt(t2.getText());

int c=a-b;

t3.setText(Integer.toString(c));

else if(e.getSource()==b3)

int a=Integer.parseInt(t1.getText());

int b=Integer.parseInt(t2.getText());

int c=a*b;

t3.setText(Integer.toString(c));

else if(e.getSource()==b4)

int a=Integer.parseInt(t1.getText());

int b=Integer.parseInt(t2.getText());

int c=a/b;

t3.setText(Integer.toString(c));

public static void main(String args[])

new GUI();

Potrebbero piacerti anche