SlideShare a Scribd company logo
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 :-

More Related Content

PDF
DCN Practical
DOCX
Java practical
DOC
Final JAVA Practical of BCA SEM-5.
PDF
Important java programs(collection+file)
DOCX
Java programs
PDF
Java_practical_handbook
PDF
Java programs
PPTX
Java practice programs for beginners
DCN Practical
Java practical
Final JAVA Practical of BCA SEM-5.
Important java programs(collection+file)
Java programs
Java_practical_handbook
Java programs
Java practice programs for beginners

What's hot (19)

KEY
Why Learn Python?
PDF
Java practical(baca sem v)
PDF
Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...
PDF
Java Puzzle
DOCX
Java PRACTICAL file
PDF
Spock framework
PDF
Java puzzles
PPTX
Unit Testing with Foq
PPTX
Java Language fundamental
PDF
Spock: A Highly Logical Way To Test
PDF
Sam wd programs
PPTX
Smarter Testing With Spock
PPT
Networking Core Concept
PDF
JVM Mechanics
ODT
Java practical
PDF
Spock Framework
PPT
JDBC Core Concept
PDF
Using Fuzzy Code Search to Link Code Fragments in Discussions to Source Code
PPTX
Java весна 2013 лекция 2
Why Learn Python?
Java practical(baca sem v)
Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...
Java Puzzle
Java PRACTICAL file
Spock framework
Java puzzles
Unit Testing with Foq
Java Language fundamental
Spock: A Highly Logical Way To Test
Sam wd programs
Smarter Testing With Spock
Networking Core Concept
JVM Mechanics
Java practical
Spock Framework
JDBC Core Concept
Using Fuzzy Code Search to Link Code Fragments in Discussions to Source Code
Java весна 2013 лекция 2
Ad

Similar to Core java pract_sem iii (20)

PDF
Java practical N Scheme Diploma in Computer Engineering
DOCX
Java Program
PDF
PDF
Basic program in java
PDF
Sharable_Java_Python.pdf
PDF
Java for android developers
DOC
CS2309 JAVA LAB MANUAL
PDF
java-programming.pdf
PDF
Microsoft word java
PDF
JAVA PRACTICE QUESTIONS v1.4.pdf
DOCX
Java programming lab_manual_by_rohit_jaiswar
PDF
Big Java Early Objects 5th Edition Horstmann Solutions Manual
PDF
Ee java lab assignment 3
PDF
Java programming lab manual
PDF
SE-IT JAVA LAB SYLLABUS
DOCX
DOCX
Java file
DOCX
Java file
PDF
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
PPTX
Presentation1 computer shaan
Java practical N Scheme Diploma in Computer Engineering
Java Program
Basic program in java
Sharable_Java_Python.pdf
Java for android developers
CS2309 JAVA LAB MANUAL
java-programming.pdf
Microsoft word java
JAVA PRACTICE QUESTIONS v1.4.pdf
Java programming lab_manual_by_rohit_jaiswar
Big Java Early Objects 5th Edition Horstmann Solutions Manual
Ee java lab assignment 3
Java programming lab manual
SE-IT JAVA LAB SYLLABUS
Java file
Java file
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
Presentation1 computer shaan
Ad

More from Niraj Bharambe (18)

PDF
Tybsc cs dbms2 notes
PDF
T.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai University
PDF
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
PDF
Advanced java practical semester 6_computer science
DOCX
Core java tutorial
DOCX
Java unit 4_cs_notes
DOCX
Unit 1st and 3rd notes of java
DOCX
J2 ee tutorial ejb
DOCX
Xml 150323102007-conversion-gate01
DOCX
Htmlnotes 150323102005-conversion-gate01
DOCX
Htmlcolorcodes 150323101937-conversion-gate01
DOCX
Definition
PDF
Sixth sense technology
DOC
collisiondetection
DOCX
Html color codes
DOCX
Embedded System Practical manual (1)
DOCX
Data Warehousing Practical for T.Y.I.T.
PDF
Forouzan appendix
Tybsc cs dbms2 notes
T.Y.B.S.CS Advance Java Practicals Sem 5 Mumbai University
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Advanced java practical semester 6_computer science
Core java tutorial
Java unit 4_cs_notes
Unit 1st and 3rd notes of java
J2 ee tutorial ejb
Xml 150323102007-conversion-gate01
Htmlnotes 150323102005-conversion-gate01
Htmlcolorcodes 150323101937-conversion-gate01
Definition
Sixth sense technology
collisiondetection
Html color codes
Embedded System Practical manual (1)
Data Warehousing Practical for T.Y.I.T.
Forouzan appendix

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Business Ethics Teaching Materials for college
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Anesthesia in Laparoscopic Surgery in India
Insiders guide to clinical Medicine.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
102 student loan defaulters named and shamed – Is someone you know on the list?
Week 4 Term 3 Study Techniques revisited.pptx
PPH.pptx obstetrics and gynecology in nursing
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Final Presentation General Medicine 03-08-2024.pptx
Business Ethics Teaching Materials for college
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
TR - Agricultural Crops Production NC III.pdf
VCE English Exam - Section C Student Revision Booklet
O7-L3 Supply Chain Operations - ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Renaissance Architecture: A Journey from Faith to Humanism
Anesthesia in Laparoscopic Surgery in India

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"); } } }
  • 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(); } } }
  • 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]); } }
  • 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(); } }}
  • 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: 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(); } }
  • 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{ 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()); } }
  • 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 = 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())
  • 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(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); } }
  • 24. public static void main(String...s) { new Calculator(); } } OUTPUT :-