PRACTICAL 1
AIM :- Accept integer values for a, b and c which are coefficients of quadratic equation. Find the solution
of quadratic equation.
Source Code:-
import java.util.Scanner;
public class pract1
{
public static void main(String[] args)
{
int a, b, c;
double root1, root2, d;
Scanner s = new Scanner(System.in);
System.out.println("Given quadratic equation:ax^2 + bx + c");
System.out.print("Enter a:");
a = s.nextInt();
System.out.print("Enter b:");
b = s.nextInt();
System.out.print("Enter c:");
c = s.nextInt();
System.out.println("Given quadratic equation:"+a+"x^2 + "+b+"x + "+c);
d = b * b - 4 * a * c;
if(d > 0)
{
System.out.println("Roots are real and unequal");
root1 = ( - b + Math.sqrt(d))/(2*a);
root2 = (-b - Math.sqrt(d))/(2*a);
System.out.println("First root is:"+root1);
System.out.println("Second root is:"+root2);
}
else if(d == 0)
{
System.out.println("Roots are real and equal");
root1 = (-b+Math.sqrt(d))/(2*a);
System.out.println("Root:"+root1);
}
else
{
System.out.println("Roots are imaginary");
}
}
}
OUTPUT :-
PRACTICAL 2
AIM :- Accept two n x m matrices. Write a Java program to find addition of these matrices
Source Code:-
import java.util.Scanner;
class pract2
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
second[c][d] = in.nextInt();
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to subtract matrices
System.out.println("Sum of entered matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"t");
System.out.println();
}
}
}
OUTPUT :-
PRACTICAL 3
AIM :- Accept n strings. Sort names in ascending order.
Source Code:-
import java.util.Scanner;
public class pract3
{
public static void main(String[] args)
{
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]);
}
}
OUTPUT :-
PRACTICAL 4
AIM :- Create a package: Animals. In package animals create interface Animal with suitable
behaviors. Implement the interface Animal in the same package animals.
Source Code:-
Animals.java (interface):
interface Animals {
void callSound();
int run();
}
Feline.java (abstract class):
abstract class Feline implements Animals {
@Override
public void callSound() {
System.out.println("roar");
}
}
Canine.java (abstract class):
abstract class Canine implements Animals {
@Override
public void callSound() {
System.out.println("howl");
}
}
Lion.java (class):
class Lion extends Feline {
@Override
public void callSound() {
super.callSound();
}
@Override
public int run() {
return 40;
}
}
Cat.java (class):
class Cat extends Feline {
@Override
public void callSound() {
System.out.println("meow");
}
@Override
public int run() {
return 30;
}
}
Wolf.java (class):
class Wolf extends Canine {
@Override
public void callSound() {
super.callSound();
}
@Override
public int run() {
return 20;
}
}
Dog.java (class):
class Dog extends Canine {
@Override
public void callSound() {
System.out.println("woof");
super.callSound();
}
@Override
public int run() {
return 10;
}
}
Main.java:
public class Main {
public static void main(String[] args) {
Animals[] animals = new Animals[4];
animals[0] = new Cat();
animals[1] = new Dog();
animals[2] = new Wolf();
animals[3] = new Lion();
for (int i = 0; i < animals.length; i++) {
animals[i].callSound();
}
}}
OUTPUT :-
PRACTICAL 5
AIM :- Demonstrate Java inheritance using extends keyword.
Source Code:-
Animal.java:
public class Animal {
public Animal() {
System.out.println("A new animal has been created!");
}
public void sleep() {
System.out.println("An animal sleeps...");
}
public void eat() {
System.out.println("An animal eats...");
}
}
Bird.java:
public class Bird extends Animal {
public Bird() {
super();
System.out.println("A new bird has been created!");
}
@Override
public void sleep() {
System.out.println("A bird sleeps...");
}
@Override
public void eat() {
System.out.println("A bird eats...");
}
}
Dog.java:
public class Dog extends Animal {
public Dog() {
super();
System.out.println("A new dog has been created!");
}
@Override
public void sleep() {
System.out.println("A dog sleeps...");
}
@Override
public void eat() {
System.out.println("A dog eats...");
}
}
MainClass.java:
public class MainClass {
public static void main(String[] args) {
Animal animal = new Animal();
Bird bird = new Bird();
Dog dog = new Dog();
System.out.println();
animal.sleep();
animal.eat();
bird.sleep();
bird.eat();
dog.sleep();
dog.eat();
}
}
OUTPUT :-
PRACTICAL 6
AIM :- Demonstrate method overloading and method overriding in Java
Source Code:-
Method overloading:-
class Polymorphism
{
void add(int a, int b)
{
System.out.println("Sum of two="+(a+b));
}
void add(int a, int b,int c)
{
System.out.println("Sum of three="+(a+b+c));
}
}
class pract6_overloading
{
public static void main(String args[])
{
Sum s=new Sum();
s.add(10,15);
s.add(10,20,30);
}
}
OUTPUT :-
Method overriding:-
class Bank{
int getRateOfInterest()
{
return 0;
}
}
class SBI extends Bank
{
int getRateOfInterest()
{
return 8;
}
}
class ICICI extends Bank
{
int getRateOfInterest()
{
return 7;
}
}
class AXIS extends Bank
{
int getRateOfInterest()
{
return 9;
}
}
class pract6_overriding
{
public static void main(String args[])
{
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
OUTPUT :-
PRACTICAL 7
AIM :- Demonstrate creating your own exception in Java.
Source Code:-
class NumberRangeException extends Exception
{
String msg;
NumberRangeException()
{
msg = new String("Enter a number between 20 and 100");
}
}
public class My_Exception
{
public static void main (String args [ ])
{
try
{
int x = 10;
if (x < 20 || x >100) throw new NumberRangeException( );
}
catch (NumberRangeException e)
{
System.out.println (e);
}
}
}
OUTPUT :-
PRACTICAL 8
AIM :- Using various swing components design Java application to accept a student's resume.
(Design form)
Source Code:-
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Frame1 extends Frame implements ActionListener
{
String msg="";
Button btnNew,btnSubmit,btnView;
Label lblName,lblAge,lblAddr,lblGender,lblQua;
TextField txtName,txtAge;
TextArea txtAddr,txtAns;
CheckboxGroup ChkGrp;
Checkbox chkMale,chkFemale;
Checkbox chkMca,chkBca,chkBba,chkMba;
Frame1(String name)
{
super(name);
setLayout(new GridLayout(3,2));
lblName = new Label("Name: ");
lblAge = new Label("Age: ");
lblAddr = new Label("Address : ");
lblGender = new Label("Gender: ");
lblQua = new Label("Qualification: ");
txtName = new TextField(20);
txtAge = new TextField(20);
txtAddr = new TextArea();
ChkGrp = new CheckboxGroup();
chkMale = new Checkbox("Male",ChkGrp,false);
chkFemale = new Checkbox("Female",ChkGrp,false);
chkMca = new Checkbox("MCA");
chkBca = new Checkbox("BCA");
chkMba = new Checkbox("MBA");
chkBba = new Checkbox("BBA");
btnNew = new Button("NEW");
btnSubmit = new Button("SUBMIT");
btnView = new Button("VIEW");
btnNew.addActionListener(this);
btnSubmit.addActionListener(this);
btnView.addActionListener(this);
add(lblName);
add(txtName);
add(lblAge);
add(txtAge);
add(lblAddr);
add(txtAddr);
add(lblGender);
add(chkMale);
add(chkFemale);
add(lblQua);
add(chkBca);
add(chkBba);
add(chkMca);
add(chkMba);
add(btnNew);
add(btnSubmit);
add(btnView);
txtAns = new TextArea();
add(txtAns);
}
@Override
public void actionPerformed(ActionEvent ae)
{
String s="";
boolean b;
FileInputStream Fin;
DataInputStream dis;
FileOutputStream Fout;
DataOutputStream dos;
try
{
Fout = new FileOutputStream("Biodata.txt",true);
dos = new DataOutputStream(Fout);
String str = ae.getActionCommand();
if(str.equals("SUBMIT"))
{
s=txtName.getText().trim();
dos.writeUTF(s);
dos.writeInt(Integer.parseInt(txtAge.getText()));
s=txtAddr.getText();
dos.writeUTF(s);
if(chkMale.getState())
dos.writeUTF("Male ");
if(chkFemale.getState())
dos.writeUTF("Female ");
s="";
if(chkMca.getState())
s="MCA ";
if(chkBca.getState())
s+="BCA ";
if(chkBba.getState())
s+="BBA ";
if(chkMba.getState())
s+="MBA ";
s+="!";
dos.writeUTF(s);
Fout.close();
}
if(str.equals("VIEW"))
if(str.equals("NEW"))
{
txtName.setText("");
txtAge.setText("");
txtAddr.setText("");
chkMale.setState(false);
chkFemale.setState(false);
chkMca.setState(false);
chkBca.setState(false);
chkBba.setState(false);
chkMba.setState(false);
}
}
catch(Exception e)
{
System.out.println("The Exception Is : " +e);
}
}
}
class pract8
{
public static void main(String args[])
{
try{
Frame1 F = new Frame1("Biodata");
F.setSize(400,400);
F.show();
}catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT :-
PRACTICAL 9
AIM :- Write a Java List example and demonstrate methods of Java List interface.
Source Code:-
import java.util.*;
public class Pract9 {
public static void main(String[] args) {
List a1 = new ArrayList();
a1.add("Zara");
a1.add("Mahnaz");
a1.add("Ayan");
System.out.println(" ArrayList Elements");
System.out.print("t" + a1);
List l1 = new LinkedList();
l1.add("Zara");
l1.add("Mahnaz");
l1.add("Ayan");
System.out.println();
System.out.println(" LinkedList Elements");
System.out.print("t" + l1);
}
}
OUTPUT :-
PRACTICAL 10
AIM :- Design simple calculator GUI application using AWT components
Source Code:-
import java.awt.*;
import java.awt.event.*;
class Calculator implements ActionListener
{
//Declaring Objects
Frame f=new Frame();
Label l1=new Label("First Number");
Label l2=new Label("Second Number");
Label l3=new Label("Result");
TextField t1=new TextField();
TextField t2=new TextField();
TextField t3=new TextField();
Button b1=new Button("Add");
Button b2=new Button("Sub");
Button b3=new Button("Mul");
Button b4=new Button("Div");
Button b5=new Button("Cancel");
Calculator()
{
//Giving Coordinates
l1.setBounds(50,100,100,20);
l2.setBounds(50,140,100,20);
l3.setBounds(50,180,100,20);
t1.setBounds(200,100,100,20);
t2.setBounds(200,140,100,20);
t3.setBounds(200,180,100,20);
b1.setBounds(50,250,50,20);
b2.setBounds(110,250,50,20);
b3.setBounds(170,250,50,20);
b4.setBounds(230,250,50,20);
b5.setBounds(290,250,50,20);
//Adding components to the frame
f.add(l1);
f.add(l2);
f.add(l3);
f.add(t1);
f.add(t2);
f.add(t3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
f.setLayout(null);
f.setVisible(true);
f.setSize(400,350);
}
public void actionPerformed(ActionEvent e)
{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
if(e.getSource()==b1)
{
t3.setText(String.valueOf(n1+n2));
}
if(e.getSource()==b2)
{
t3.setText(String.valueOf(n1-n2));
}
if(e.getSource()==b3)
{
t3.setText(String.valueOf(n1*n2));
}
if(e.getSource()==b4)
{
t3.setText(String.valueOf(n1/n2));
}
if(e.getSource()==b5)
{
System.exit(0);
}
}
public static void main(String...s)
{
new Calculator();
}
}
OUTPUT :-

Core java pract_sem iii

  • 1.
    PRACTICAL 1 AIM :-Accept integer values for a, b and c which are coefficients of quadratic equation. Find the solution of quadratic equation. Source Code:- import java.util.Scanner; public class pract1 { public static void main(String[] args) { int a, b, c; double root1, root2, d; Scanner s = new Scanner(System.in); System.out.println("Given quadratic equation:ax^2 + bx + c"); System.out.print("Enter a:"); a = s.nextInt(); System.out.print("Enter b:"); b = s.nextInt(); System.out.print("Enter c:"); c = s.nextInt(); System.out.println("Given quadratic equation:"+a+"x^2 + "+b+"x + "+c); d = b * b - 4 * a * c; if(d > 0) { System.out.println("Roots are real and unequal"); root1 = ( - b + Math.sqrt(d))/(2*a); root2 = (-b - Math.sqrt(d))/(2*a); System.out.println("First root is:"+root1); System.out.println("Second root is:"+root2); } else if(d == 0) { System.out.println("Roots are real and equal"); root1 = (-b+Math.sqrt(d))/(2*a); System.out.println("Root:"+root1); } else { System.out.println("Roots are imaginary"); } } }
  • 2.
  • 3.
    PRACTICAL 2 AIM :-Accept two n x m matrices. Write a Java program to find addition of these matrices Source Code:- import java.util.Scanner; class pract2 { public static void main(String args[]) { int m, n, c, d; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows and columns of matrix"); m = in.nextInt(); n = in.nextInt(); int first[][] = new int[m][n]; int second[][] = new int[m][n]; int sum[][] = new int[m][n]; System.out.println("Enter the elements of first matrix"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) first[c][d] = in.nextInt(); System.out.println("Enter the elements of second matrix"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) second[c][d] = in.nextInt(); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to subtract matrices System.out.println("Sum of entered matrices:-"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) System.out.print(sum[c][d]+"t"); System.out.println(); } } }
  • 4.
  • 5.
    PRACTICAL 3 AIM :-Accept n strings. Sort names in ascending order. Source Code:- import java.util.Scanner; public class pract3 { public static void main(String[] args) { int n; String temp; Scanner s = new Scanner(System.in); System.out.print("Enter number of names you want to enter:"); n = s.nextInt(); String names[] = new String[n]; Scanner s1 = new Scanner(System.in); System.out.println("Enter all the names:"); for(int i = 0; i < n; i++) { names[i] = s1.nextLine(); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (names[i].compareTo(names[j])>0) { temp = names[i]; names[i] = names[j]; names[j] = temp; } } } System.out.print("Names in Sorted Order:"); for (int i = 0; i < n - 1; i++) { System.out.print(names[i] + ","); } System.out.print(names[n - 1]); } }
  • 6.
  • 7.
    PRACTICAL 4 AIM :-Create a package: Animals. In package animals create interface Animal with suitable behaviors. Implement the interface Animal in the same package animals. Source Code:- Animals.java (interface): interface Animals { void callSound(); int run(); } Feline.java (abstract class): abstract class Feline implements Animals { @Override public void callSound() { System.out.println("roar"); } } Canine.java (abstract class): abstract class Canine implements Animals { @Override public void callSound() { System.out.println("howl"); } } Lion.java (class): class Lion extends Feline { @Override public void callSound() { super.callSound(); } @Override public int run() { return 40; } } Cat.java (class): class Cat extends Feline { @Override public void callSound() {
  • 8.
    System.out.println("meow"); } @Override public int run(){ return 30; } } Wolf.java (class): class Wolf extends Canine { @Override public void callSound() { super.callSound(); } @Override public int run() { return 20; } } Dog.java (class): class Dog extends Canine { @Override public void callSound() { System.out.println("woof"); super.callSound(); } @Override public int run() { return 10; } } Main.java: public class Main { public static void main(String[] args) { Animals[] animals = new Animals[4]; animals[0] = new Cat(); animals[1] = new Dog(); animals[2] = new Wolf(); animals[3] = new Lion(); for (int i = 0; i < animals.length; i++) { animals[i].callSound(); } }}
  • 9.
  • 10.
    PRACTICAL 5 AIM :-Demonstrate Java inheritance using extends keyword. Source Code:- Animal.java: public class Animal { public Animal() { System.out.println("A new animal has been created!"); } public void sleep() { System.out.println("An animal sleeps..."); } public void eat() { System.out.println("An animal eats..."); } } Bird.java: public class Bird extends Animal { public Bird() { super(); System.out.println("A new bird has been created!"); } @Override public void sleep() { System.out.println("A bird sleeps..."); } @Override public void eat() { System.out.println("A bird eats..."); } } Dog.java: public class Dog extends Animal { public Dog() { super(); System.out.println("A new dog has been created!"); } @Override public void sleep() { System.out.println("A dog sleeps..."); } @Override public void eat() {
  • 11.
    System.out.println("A dog eats..."); } } MainClass.java: publicclass MainClass { public static void main(String[] args) { Animal animal = new Animal(); Bird bird = new Bird(); Dog dog = new Dog(); System.out.println(); animal.sleep(); animal.eat(); bird.sleep(); bird.eat(); dog.sleep(); dog.eat(); } }
  • 12.
  • 13.
    PRACTICAL 6 AIM :-Demonstrate method overloading and method overriding in Java Source Code:- Method overloading:- class Polymorphism { void add(int a, int b) { System.out.println("Sum of two="+(a+b)); } void add(int a, int b,int c) { System.out.println("Sum of three="+(a+b+c)); } } class pract6_overloading { public static void main(String args[]) { Sum s=new Sum(); s.add(10,15); s.add(10,20,30); } } OUTPUT :-
  • 14.
    Method overriding:- class Bank{ intgetRateOfInterest() { return 0; } } class SBI extends Bank { int getRateOfInterest() { return 8; } } class ICICI extends Bank { int getRateOfInterest() { return 7; } } class AXIS extends Bank { int getRateOfInterest() { return 9; } } class pract6_overriding { public static void main(String args[]) { SBI s=new SBI(); ICICI i=new ICICI(); AXIS a=new AXIS(); System.out.println("SBI Rate of Interest: "+s.getRateOfInterest()); System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest()); System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest()); } }
  • 15.
  • 16.
    PRACTICAL 7 AIM :-Demonstrate creating your own exception in Java. Source Code:- class NumberRangeException extends Exception { String msg; NumberRangeException() { msg = new String("Enter a number between 20 and 100"); } } public class My_Exception { public static void main (String args [ ]) { try { int x = 10; if (x < 20 || x >100) throw new NumberRangeException( ); } catch (NumberRangeException e) { System.out.println (e); } } } OUTPUT :-
  • 17.
    PRACTICAL 8 AIM :-Using various swing components design Java application to accept a student's resume. (Design form) Source Code:- import java.io.*; import java.awt.*; import java.awt.event.*; class Frame1 extends Frame implements ActionListener { String msg=""; Button btnNew,btnSubmit,btnView; Label lblName,lblAge,lblAddr,lblGender,lblQua; TextField txtName,txtAge; TextArea txtAddr,txtAns; CheckboxGroup ChkGrp; Checkbox chkMale,chkFemale; Checkbox chkMca,chkBca,chkBba,chkMba; Frame1(String name) { super(name); setLayout(new GridLayout(3,2)); lblName = new Label("Name: "); lblAge = new Label("Age: "); lblAddr = new Label("Address : "); lblGender = new Label("Gender: "); lblQua = new Label("Qualification: "); txtName = new TextField(20); txtAge = new TextField(20); txtAddr = new TextArea(); ChkGrp = new CheckboxGroup(); chkMale = new Checkbox("Male",ChkGrp,false); chkFemale = new Checkbox("Female",ChkGrp,false); chkMca = new Checkbox("MCA"); chkBca = new Checkbox("BCA"); chkMba = new Checkbox("MBA"); chkBba = new Checkbox("BBA"); btnNew = new Button("NEW"); btnSubmit = new Button("SUBMIT"); btnView = new Button("VIEW"); btnNew.addActionListener(this); btnSubmit.addActionListener(this); btnView.addActionListener(this); add(lblName);
  • 18.
    add(txtName); add(lblAge); add(txtAge); add(lblAddr); add(txtAddr); add(lblGender); add(chkMale); add(chkFemale); add(lblQua); add(chkBca); add(chkBba); add(chkMca); add(chkMba); add(btnNew); add(btnSubmit); add(btnView); txtAns = newTextArea(); add(txtAns); } @Override public void actionPerformed(ActionEvent ae) { String s=""; boolean b; FileInputStream Fin; DataInputStream dis; FileOutputStream Fout; DataOutputStream dos; try { Fout = new FileOutputStream("Biodata.txt",true); dos = new DataOutputStream(Fout); String str = ae.getActionCommand(); if(str.equals("SUBMIT")) { s=txtName.getText().trim(); dos.writeUTF(s); dos.writeInt(Integer.parseInt(txtAge.getText())); s=txtAddr.getText(); dos.writeUTF(s); if(chkMale.getState())
  • 19.
    dos.writeUTF("Male "); if(chkFemale.getState()) dos.writeUTF("Female "); s=""; if(chkMca.getState()) s="MCA"; if(chkBca.getState()) s+="BCA "; if(chkBba.getState()) s+="BBA "; if(chkMba.getState()) s+="MBA "; s+="!"; dos.writeUTF(s); Fout.close(); } if(str.equals("VIEW")) if(str.equals("NEW")) { txtName.setText(""); txtAge.setText(""); txtAddr.setText(""); chkMale.setState(false); chkFemale.setState(false); chkMca.setState(false); chkBca.setState(false); chkBba.setState(false); chkMba.setState(false); } } catch(Exception e) { System.out.println("The Exception Is : " +e); } } } class pract8 { public static void main(String args[])
  • 20.
    { try{ Frame1 F =new Frame1("Biodata"); F.setSize(400,400); F.show(); }catch(Exception e) { System.out.println(e); } } } OUTPUT :-
  • 21.
    PRACTICAL 9 AIM :-Write a Java List example and demonstrate methods of Java List interface. Source Code:- import java.util.*; public class Pract9 { public static void main(String[] args) { List a1 = new ArrayList(); a1.add("Zara"); a1.add("Mahnaz"); a1.add("Ayan"); System.out.println(" ArrayList Elements"); System.out.print("t" + a1); List l1 = new LinkedList(); l1.add("Zara"); l1.add("Mahnaz"); l1.add("Ayan"); System.out.println(); System.out.println(" LinkedList Elements"); System.out.print("t" + l1); } } OUTPUT :-
  • 22.
    PRACTICAL 10 AIM :-Design simple calculator GUI application using AWT components Source Code:- import java.awt.*; import java.awt.event.*; class Calculator implements ActionListener { //Declaring Objects Frame f=new Frame(); Label l1=new Label("First Number"); Label l2=new Label("Second Number"); Label l3=new Label("Result"); TextField t1=new TextField(); TextField t2=new TextField(); TextField t3=new TextField(); Button b1=new Button("Add"); Button b2=new Button("Sub"); Button b3=new Button("Mul"); Button b4=new Button("Div"); Button b5=new Button("Cancel"); Calculator() { //Giving Coordinates l1.setBounds(50,100,100,20); l2.setBounds(50,140,100,20); l3.setBounds(50,180,100,20); t1.setBounds(200,100,100,20); t2.setBounds(200,140,100,20); t3.setBounds(200,180,100,20); b1.setBounds(50,250,50,20); b2.setBounds(110,250,50,20); b3.setBounds(170,250,50,20); b4.setBounds(230,250,50,20); b5.setBounds(290,250,50,20); //Adding components to the frame f.add(l1); f.add(l2); f.add(l3); f.add(t1);
  • 23.
    f.add(t2); f.add(t3); f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); f.setLayout(null); f.setVisible(true); f.setSize(400,350); } public void actionPerformed(ActionEvente) { int n1=Integer.parseInt(t1.getText()); int n2=Integer.parseInt(t2.getText()); if(e.getSource()==b1) { t3.setText(String.valueOf(n1+n2)); } if(e.getSource()==b2) { t3.setText(String.valueOf(n1-n2)); } if(e.getSource()==b3) { t3.setText(String.valueOf(n1*n2)); } if(e.getSource()==b4) { t3.setText(String.valueOf(n1/n2)); } if(e.getSource()==b5) { System.exit(0); } }
  • 24.
    public static voidmain(String...s) { new Calculator(); } } OUTPUT :-