0% found this document useful (0 votes)
10 views28 pages

s3bsc_fair_record

The document provides an introduction to Java programming, covering its history, features, and types of programs such as standalone applications and applets. It includes examples of various Java programs demonstrating concepts like leap year checking, finding the greatest number, Fibonacci series, and more, along with their outputs. Additionally, it discusses Java's execution process and features like object-oriented programming, exception handling, and file handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views28 pages

s3bsc_fair_record

The document provides an introduction to Java programming, covering its history, features, and types of programs such as standalone applications and applets. It includes examples of various Java programs demonstrating concepts like leap year checking, finding the greatest number, Fibonacci series, and more, along with their outputs. Additionally, it discusses Java's execution process and features like object-oriented programming, exception handling, and file handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

INTRODUCTION TO JAVA LAB

History of Java
The history of Java starts with the Green Team. Java team members (also known as
Green Team), initiated this project to develop a language for digital devices such as
set-top boxes, televisions, etc. However, it was best suited for internet programming.
Later, Java technology was incorporated by Netscape.

The principles for creating Java programming were "Simple, Robust, Portable,
Platform-independent, Secured, High Performance, Multithreaded, Architecture
Neutral, Object-Oriented, Interpreted, and Dynamic". Java was developed by James
Gosling, who is known as the father of Java, in 1995. James Gosling and his team
members started the project in the early '90s.

Currently, Java is used in internet programming, mobile devices, games, e-business


solutions, etc.

Features of Java
The primary objective of Java programming language creation was to make it portable,
simple and secure programming language. Apart from this, there are also some
excellent features which play an important role in the popularity of this language. The
features of Java are also known as Java buzzwords.

A list of the most important features of the Java language is given below.

1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynamic

Two Types of Java Program


Java, a popular and versatile programming language, offers various ways to develop
and deploy applications. Two common methods of creating Java programs are
standalone applications and applets.

Standalone Applications

Standalone applications, also known as desktop applications or Java applications, are


programs that run independently on a user's computer. They are self-contained and don't
require a web browser or any other platform to execute.

Applets
Applets are Java programs that are designed to run within web browsers. They were
popular in the early days of the internet and provided a way to add interactive content
to web pages.

Execution of a Java program


The execution of a Java program consists of five steps which are

1. Creation of a Java Program


2. Compiling a Java program,
3. Loading the program into the memory by Java Virtual Machine,
4. Java Virtual Machine verification for bytecode,
5. Java Program execution
1. LEAP YEAR OR NOT
Aim: Program to check whether the given year is leap year or not
Program:
import java.util.*;
class LeapYear {
public static void main(String[] args){
int y;
System.out.println("Enter an Year : ");
Scanner sc = new Scanner(System.in);
y = sc.nextInt();
if (((y % 4 == 0) && (y % 100!= 0)) || (y%400 == 0))
System.out.println("Specified year is a leap year");
else
System.out.println("Specified year is not a leap year");
}
}

Output:
Enter an Year : 2024
Specified year is a leap year

Result : Program has been executed successfully executed and output is obtained.

2. GREATEST AMONG THREE NUMBERS


Aim: Program to find the greatest among three numbers
Program:
import java.util.*;
class Largest
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.println("Enter 3 numbers : ");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();

if (a > b && a > c){


System.out.println (a + " is the greatest");
}
else if (b > a && b > c){
System.out.println(b + " is the greatest");
}
else if (c > a && c > b){
System.out.println(c + " is the greatest");
}
else{
System.out.println("Any two values or all values are
equal");
}
}
}
Output:
1
3
4
4 is the greatest

Result : Program has been executed successfully executed and output is obtained.

3. FIBONACCI SERIES
Aim: Program to print the fibonacci series upto a certain limit
Program:
import java.util.*;
class Series{
public static void main(String args[])

{
int a=0,b = 1,c ,i,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the limit : ");
n = sc.nextInt();
System.out.print(a+" "+b);
for(i=2;i<n;++i)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}}

Output:
Enter the limit :
5
01123
Result : Program has been executed successfully executed and output is obtained.

4. PALINDROME
Aim: Program to check whether a number is palindrome or not
Program:
import java.util.*;
class Palindrome{
public static void main(String args[]){
int r,a,s=0,temp;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number : ");
a = sc.nextInt();
temp=a;
while(a>0){
r=a%10;
s=(s*10)+r;
a=a/10;
}
if(temp==s){
System.out.println("Palindrome number ");
}
else{
System.out.println("Not palindrome");
}
}
}

Output:
Enter the number :
151
Palindrome number
Result : Program has been executed successfully executed and output is obtained.

5. ARMSTRONG
Aim: Program to check whether a number is armstrong or not
Program:
import java.util.*;
public class Armstrong
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int temp, n, s=0, r;
System.out.println(“Enter the number : ”);
n = sc.nextInt();
temp = n;
while(n!=0)
{
r = n%10;
s = s + r*r*r;
n=n/10;
}
if(temp == s)
{
System.out.println(“Armstrong number”);
}
else
{
System.out.println(“Not a armstrong number”);
}
}
}

Output:
Enter the number :
153
Armstrong number

Result : Program has been executed successfully executed and output is obtained.
6. SUM OF DIGITS OF A NUMBER
Aim: Program to find sum of digits of a number
Program:
import java.util.*;
class sum
{
public static void main(String args[])
{
int n, d , s = 0;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number : ”);
n = sc.nextInt();
while(n>0)
{
d = n%10;
s = s+d;
n = n/10;

}
System.out.println(“The sum of digits of number = ”+s);

}
}
Output:
Enter the number :
86
The sum of digits of number = 14
Result : Program has been executed successfully executed and output is obtained.
7. CONSTRUCTOR
Aim: Program to define a class with a constructor and calculate the area of
a rectangle using method
Program:
class Rectangle
{
int length, breadth;
Rectangle(int l, int b)
{
length = l;
breadth = b;
}
void area()
{
int a = l*b;
System.out.println(“The area of rectangle = ” + a);
}
}
class Area
{
public static void main(String args[])
{
Rectangle r = new Rectangle(2, 6);
r.area();
}
}
Output:
The area of rectangle = 12
Result : Program has been executed successfully executed and output is obtained.

8. INHERITANCE
Aim: Program to display progress report of a student using inheritance
Program:
import java.util.*;
class Student
{
int rollno, m1, m2, total;
String name;
void total()
{
total = m1+m2;
}
}
class Report extends Student
{
void display()
{
System.out.println(“Name = ” + name);
System.out.println(“Rollno = ” + rollno);
System.out.println(“Total = ” + total);
}
public static void main(String args[])
{
Report r = new Report();
Scanner sc = new Scanner(System.in);
System.out.println(“Enter Name , Rollno, Mark1, and Mark2 ”);
r.name = sc.nextLine();
r.rollno = sc.nextInt();
r.m1 = sc.nextInt();
r.m2 = sc.nextInt();
r.total();
r.display();
}
}
Output:
Enter Name , Rollno, Mark1, and Mark2
Anandhu
8
24
26

Name = Anandhu
Rollno = 8
Total = 50
Result : Program has been executed successfully executed and output is obtained.

9. METHOD OVERRIDING
Aim: Program to demonstrate method overriding
Program:
class Bank
{
float getRateOfInterest()
{
return 0.0f;
}
}
class SBI extends Bank
{
float getRateOfInterest()
{
return 8.4f;
}
}
class ICICI extends Bank
{
float getRateOfInterest()
{
return 7.3f;
}
}
class AXIS extends Bank
{
float getRateOfInterest()
{
return 9.7f;
}
}
class Main
{
public static void main(String args[])
{
Bank b;
b = new SBI();
System.out.println(“SBI rate of interest : ” + b.getRateOfInterest());
b = new ICICI();
System.out.println(“ ICICI rate of interest : ” +

b.getRateOfInterest());
b = new AXIS();
System.out.println(“ AXIS rate of interest : ” +

b.getRateOfInterest());
}
}
Output:
SBI rate of interest : 8.4
ICICI rate of interest : 7.3
AXIS rate of interest : 9.7
Result : Program has been executed successfully executed and output is obtained.

10. METHOD OVERLOADING


Aim: Program to find area of different shapes using method overloading
Program:
class Shape
{
int area(int l, int b)
{
return l*b;
}
int area(int s)
{
return s*s;
}
double area(double r)
{
return (3.14*r*r);
}
public static void main(String args[])
{
int a , b;
double c;
Shape ob = new Shape();
a = ob.area(10, 2);
b = ob.area(4);
c = ob.area(3.2);
System.out.println(“Area of rectangle = ”+ a);
System.out.println(“Area of circle = ”+ c);
System.out.println(“Area of square = ”+ b);
}
}
Output:
Area of rectangle = 20
Area of circle = 32.15
Area of square = 16
Result : Program has been executed successfully executed and output is obtained.
11. INTERFACE
Aim: Program to find greatest among two numbers using interface
Program:
import java.util.*;
interface i
{
void great(int x, int y);
}
public class Greatest implements i
{
public void great(int x, int y)
{
if(x>y)
{
System.out.println(x + “ is greater than ”+ y);
}
else
{
System.out.println(y + “ is greater than ”+ x);
}
}

public static void main(String args[])


{
int x, y;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter two numbers : ”);
x = sc.nextInt();
y = sc.nextInt();
Greatest g = new Greatest();
g.great(x, y);
}
}
Output:
Enter two numbers :
4
6
6 is greater than 4
Result : Program has been executed successfully executed and output is obtained.

12. PACKAGE
Aim: Program to find factorial of a number using package
Program:
package p;
public class findfact
{
public void fact(int n)
{
int f = 1;
for(int i = 1; i<=n; i++)
{
f = f*i;
}
System.out.println(“fact = ”+f);
}
}
import p.*;
class demo
{
public static void main(String args[])
{
findfact ob = new findfact();
ob.fact(4);

}
}
Output:
Fact = 24
Result : Program has been executed successfully executed and output is obtained.

13. EXCEPTION HANDLING


Aim: Program to demonstrate exception handling
Program:
class Error
{
public static void main(String args[])
{
int a[2] = { 5, 10};
int b = 5;
try
{
int x =a[1]/b-a[0];
}
catch(ArithmeticException e)
{
System.out.println(“Arithmetic Exception”);
}
catch(ArrayIndexOutOfBoundException e)
{
System.out.println(“Array index error”);
}
int y = a[1]/a[0];
System.out.println(“y = ”+y);
}
}
Output:
Array index error
y=2
Result : Program has been executed successfully executed and output is obtained.

14. THREAD
Aim: Program to print odd and even numbers using thread concept
Program:
import java.util.*;
class even extends Thread
{
public void run()
{
int i;
for(i = 1 ; i<=10 ; i++)
{
if(i%2 == 0)
{
System.out.println(“i = ”+i);
}
}
}
}

class odd extends Thread


{
public void run()
{
int j;
for(j = 1 ; j<=10 ; j++)
{
if(j%2 != 0)
{
System.out.println(“j = ”+j);
}
}
}
}

class Threading
{
public static void main(String args[])
{

even e = new even();


odd o = new odd();
e.start();
o.start();
}
}
Output:
j=1
i=2
i=4
j=3
i=6
j=5
j=7
j=9
i=8
i = 10
Result : Program has been executed successfully executed and output is obtained.

15. FILE HANDLING


Aim: Program to create a text file and display its contents
Program:
import java.util.*;
import java.io.*;
class fileIO {
public static void main(String args[]) throws IOException
{

Scanner sc = new Scanner(System.in);


String text, msg;
System.out.println("Enter text : ");
text = sc.nextLine();
File file = new File("example.txt");
BufferedWriter input = new BufferedWriter(new FileWriter(file));
input.write(text);
System.out.println("Contents are added to the file");
input.close();
System.out.println("File closed");

try
{
BufferedReader out = new BufferedReader(new FileReader(file));
System.out.println("File content is : ");
while((msg = out.readLine()) != null){
System.out.println(msg);
}
out.close();
}
catch(IOException err)
{
System.out.print("[ERROR] Error occurred : " + err.getMessage());
}
}
}
Output:
Enter text :
Hello world
Contents are added to the file
File closed
File content is :
Hello world
Result : Program has been executed successfully executed and output is obtained.

16. STRING CLASS METHODS


Aim: Program to demonstrate methods of string class
Program:
import java.io.*;
import java.util.*;
public class str
{
public static void main(String args[])
{
String s = “HelloWorld”;
String s1 = “Hello”;
String s2 = “World”;
String s3 = “Hello World”;
System.out.println(“Length of ” + s + “ is ” + s.length());
System.out.println(“Character at 2nd position = ” + s.charAt(2));
System.out.println(“Substring from index 3 is ”+ s.substring(3));
System.out.println(“Concatenated string = ” + s1.concat(s2));
System.out.println(“Index of World is ” + s3.indexOf(“World”));
System.out.println(“Changing to lowercase ” + s.toLowerCase());
System.out.println(“Changing to uppercase ” + s3.toUpperCase());
}
}

Output:
Length of HelloWorld is 10
Character at 2nd position = l
Substring from index 3 is loWorld
Concatenated string = HelloWorld
Index of World is 6
Result : Program has been executed successfully executed and output is obtained.

17. HUMAN FACE


Aim: Program to draw human face
Program:
import java.applet.*;
import java.awt.*;
public class shape1 extends Applet
{
public void paint(Graphics g)
{
g.drawOval(70, 80, 80, 70);
g.drawOval(80, 100, 20, 20);
g.drawOval(120, 100, 20, 20);
g.drawLine(110, 110, 110, 120);
g.drawArc(134, 105, 25, 25, 80, -175 );
g.drawArc(95, 115, 25, 25, 180, 180);
g.drawArc(60, 105, 25, 25, 90, 190);
}
}
Result : Program has been executed successfully executed and output is obtained.

18. BORDER LAYOUT


Aim: Program to demonstrate borderlayout
Program:
import java.awt.*;
import javax.swing.*;
public class Border
{
Jframe f;
Border()
{
f = new Jframe();
JButton b1 = new JButton(“NORTH”);
JButton b2 = new JButton(“SOUTH”);

JButton b3 = new JButton(“EAST”);


JButton b4 = new JButton(“WEST”);
JButton b5 = new JButton(“CENTER”);
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH );
f.add(b3, BorderLayout.EAST );
f.add(b4, BorderLayout.WEST );
f.add(b5, BorderLayout.CENTER );
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String args[])
{
new Border();
}
}
Result : Program has been executed successfully executed and output is obtained.

19. EVENT HANDLING


Aim: Program to find sum of two numbers using event handling
Program:
import java.awt.*;
import java.awt.event.*;
class sumAwt extends Frame implements ActionListener
{
Button bt;
TextField t1, t2;
Label lb1, lb2, lb3;
Frame f;
sumAwt()
{
f = new Frame("Add two numbers");
lb1 = new Label("Enter first number : ");

lb1.setBounds(5, 50, 150, 30);


f.add(lb1);
t1 = new TextField();
t1.setBounds(200, 50, 150, 30);
f.add(t1);
lb2 = new Label("Enter second number : ");
lb2.setBounds(5, 110, 150, 30);
f.add(lb2);
t2 = new TextField();
t2.setBounds(200, 100, 150, 30);
f.add(t2);
lb3 = new Label("Result : ");
lb3.setBounds(90, 140, 150, 30);
f.add(lb3);
bt = new Button("+");
bt.setBounds(90, 200, 200, 30);
f.add(bt);
bt.addActionListener(this);
f.setLayout(null);
f.setSize(500, 500);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e)


{
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());
int c = 0;

if(e.getSource().equals(bt))
{
c = a + b;
lb3.setText(String.valueOf("Result : "+c));
}
}
public static void main(String args[])
{

new sumAwt();
}
}
Result : Program has been executed successfully executed and output is obtained.

You might also like