0% found this document useful (0 votes)
85 views

Java File

This document contains 23 Java programs demonstrating various programming concepts like: 1) Basic Java program structure and output 2) Using methods, classes, objects, parameters and return values 3) Inheritance, polymorphism, abstraction through interfaces and abstract classes 4) Exception handling using try-catch blocks and throwing custom exceptions 5) Arithmetic operations and conditional logic using if-else statements The programs cover basic to more advanced Java concepts like single and multi-level inheritance, method overriding, abstract classes, interfaces and exception handling.

Uploaded by

jaspreet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Java File

This document contains 23 Java programs demonstrating various programming concepts like: 1) Basic Java program structure and output 2) Using methods, classes, objects, parameters and return values 3) Inheritance, polymorphism, abstraction through interfaces and abstract classes 4) Exception handling using try-catch blocks and throwing custom exceptions 5) Arithmetic operations and conditional logic using if-else statements The programs cover basic to more advanced Java concepts like single and multi-level inheritance, method overriding, abstract classes, interfaces and exception handling.

Uploaded by

jaspreet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 68

1/* A simple java program.

*/
class abc
{
public static void main(String args[])
{
System.out.println("Hello, this is my First Java
Program");
}
}

OUTPUT:

2 /* Find out the square root of no.*/


class abc
{
public static void main(String args[])
{
double x=25;
double y;
y=Math.sqrt(x);
System.out.println("y="+y);
}
}

OUTPUT:

3 /*A program using two classes*/

class room
{
float length;
float breadth;
void getdata(float a,float b)
{
length=a;
breadth=b;
}
}
class roomarea
{
public static void main(String args[])
{
float area;
room obj= new room();
3
obj.getdata(10,20); // function call
area= obj.length*obj.breadth;

System.out.println("Area="+area);
}
}

OUTPUT:

4 /*Declaring class, member, object,


Accessing member?*/

class Rectangle
{
int length, breadth;
void getdata(int x, int y)
{
length = x;
breadth = y;
}
int rectArea()
{
int area = length * breadth;
return (area);
}
}
class RoomArea
5
{
public static void main(String args[])
{

int area1, area2;


Rectangle rect1=new Rectangle();
Rectangle rect2=new Rectangle();
rect1.length=15;
rect1.breadth=10;
area1=rect1.length*rect1.breadth;
rect2.getdata(30,40);
area2=rect2.rectArea();
System.out.println("Area1 ="+area1);
System.out.println("Area2 ="+area2);
}
}

OUTPUT:

5 /*Program to Default Constructor*/


class display

{
int a=10;
int b=20;
int c;
display()
{
c=30;
}
void show()
{
System.out.println("A="+a);
System.out.println("B="+b);
System.out.println("C="+c);
}
7
}
class demo
{
public static void main(String args[])
{

display obj=new display();


obj.show();
}
}

OUTPUT:

6 /*Program to Parameterized
constructor*/
class room

{
int length,breadth,res;
room(int x,int y)
{
length=x;
breadth=y;
}
void roomarea()
{
res=length*breadth;
System.out.println("area="+res);
}
}
class roomarea
9
{
public static void main(String args[])
{
room obj=new room(10,20);
obj.roomarea();

}
}

OUTPUT:

10

7 /* Program to copy constructor*/


class room
{
int length,breadth;

room(int l, int b)
{
length=l;
breadth=b;
}
room(room obj)
{
System.out.println("copy constructor");
length=obj.length;
breadth=obj.breadth;
}
int area()
11
{
return(length*breadth);
}
}
class roomarea
{
public static void main(String args[])

{
room firstroom=new room(10,20);
room secondroom=new room(firstroom);
System.out.println("The area
is="+firstroom.area());
System.out.println("The area
is="+secondroom.area());
}
}

OUTPUT:

12

8 /*Program to Nested of Mathod*/


class nesting
{
int x, y;
nesting(int a, int b)

{
x = a;
y = b;
}
int largest()
{
if (x >= y)
return(x);
else
return(y);
}
void display()
13
{
int large = largest();
System.out.println("largest value="+large);
}
}
class nestingdemo
{

public static void main(String args[])


{
nesting obj=new nesting(34,86);
obj.display();
}
}

OUTPUT:

14

9 /*Program to Single inheritance*/


class base
{
int num1;
void baseshow()
{
System.out.println("num1="+num1);

}
}
class derived extends base
{
int num2;
void product()
{
System.out.println("Product of 2no's="+
(num1*num2));
}
void derivedshow()
15
{
System.out.println("num2="+num2);
}
}
class inheritancedemo
{
public static void main(String args[])
{

derived obj=new derived();


obj.num1=10;
obj.baseshow();
obj.derivedshow();
obj.product();
}
}

OUTPUT:

16

10 /*Program to Single Inheritance using


Two
Constructor in Classes*/
class room
{
int length;
int breadth;

room(int l, int b)
{
length=l;
breadth=b;
}
void area()
{
System.out.println("Area of room="+
(length*breadth));
}
}
class bedroom extends room
17
{
int height;
bedroom(int l,int b,int h)
{
super(l,b);
height=h;
}

void volume()
{
System.out.println("Value of room="+
(length*breadth*height));
}
}
class inheritdemo
{
public static void main(String args[])
{
bedroom obj=new bedroom(10,20,30);
18
obj.area();
obj.volume();
}
}

OUTPUT:

19

11 /*Program to Method Overriding in


single
inheritance.*/
class base
{
int x;
base(int x)

{
this.x=x;
}
void display()
{
System.out.println("Value of x="+x);
}
}
class derived extends base
{
int y;
20
derived(int x,int y)
{
super(x);
this.y=y;
}
void display()
{
System.out.println("value of x="+x);

System.out.println("Value of y="+y);
}
}
class overridedemo
{
public static void main(String args[])
{
derived obj = new derived(100,200);
obj.display();
}
21
}

OUTPUT:

22

12 /*Program to Abstract Method and


Class*/
abstract class share
{
abstract void area();
abstract void circumference();
}
class rectangle extends share
{

private double length, breadth;


rectangle(double x, double y)
{
length=x;
breadth=y;
}
public void area()
{
System.out.println("Area of rectangle="+
(length*breadth));
23
}
public void circumference()
{
System.out.println("Circumference of
rectangle="+2*(length*breadth));
}
}
class circle extends share
{

private double radious;


circle(double r)
{
radious = r;
}
public void area()
{
System.out.println("Area of circle="+
(Math.PI*radious*radious));
}
24
public void circumference()
{
System.out.println("Circumference of
circle="+2*(Math.PI*radious));
}
}
class abstractdemo
{
public static void main(String args[])

{
share s;
rectangle r=new rectangle(10,20);
s=r;
s.area();
s.circumference();
circle c = new circle (30);
s=c;
s.area();
25
s.circumference();
}
}

OUTPUT:

26

13 /*Program to Implement Interface in


java.*/
interface area
{
final static float PI=3.14f;
float compute(float x, float y);
}
class rectangle implements area
{

public float compute(float x, float y)


{
return(x*y);
}
}
class circle implements area
{
public float compute(float x, float y)
{
27
return(PI*x*x);
}
}
class interfacetest
{
public static void main(String [] args)
{
rectangle rect= new rectangle();
circle cir= new circle();
area a1;

a1 = rect;
System.out.println("Area of
rectangle="+a1.compute(10,20));
a1=cir;
System.out.println("Area of
circle="+a1.compute(10,0));
}
}

28

OUTPUT:

29

14 /*Program to Handle exception using


try/catch.*/
class abc
{
public static void main(String [] args)
{
int a=10, b=5, c=5,x,y;
try
{

x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
y = a/(b+c);
System.out.println("y="+y);
}
30
}

OUTPUT:

31

15 /*Program to use nested try.*/


class abc
{
public static void main(String [] args)
{
try
{
int a=2, b=4, c=2,x=7,z;
int p[] = {2};

p[3] = 33;
try
{
z=x/((b*b)-(4*a*c));
System.out.println("The value of z is ="+ z);
}
catch(ArithmeticException e)
32
{
System.out.println("Division by zero in
Arithmatic expression");
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index is out-ofbounds");
}
}
}

OUTPUT:

33

16 /*Program to using Multiple catch.*/


class abc
{
public static void main(String args[])
{
int a[] = {5,10};
int b = 5;
try
{
int x = a[2]/b-a[1];

}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
34
System.out.println("Array Index error");
}
catch(ArrayStoreException e)
{
System.out.println("Wrong data type");
}
int y = a[1]/a[0];
System.out.println("y = " +y);
}
}

OUTPUT:

35

17 /*Program to Multiple catch using


finally
Statement.*/
class abc
{
public static void main(String args[])
{
int a[]={5,10};
int b=5;
try
{

int x = a[2]/b-a[1];
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
36
{
System.out.println("Array index error");
}
catch(ArrayStoreException e)
{
System.out.println("wrong data type");
}
finally
{
int y=a[1]/a[0];
System.out.println("y ="+y);
}

}
}

OUTPUT:

37

18 /*Program to using Throwing our own


exception.*/
import java.lang.Exception;
class myException extends Exception
{
myException(String message)
{
super(message);
}
}
class testmyException
{

public static void main(String args[])


{
int x =5,y=1000;
try
{
float z =(float) x / (float) y;
38
if(z < 0.01)
{
throw new myException("Number is too
small");
}
}
catch(myException e)
{
System.out.println("Caught my Exception");
}
finally
{

System.out.println("I am always here");


}
}
}

39

OUTPUT:

40

19 /* Write a Program to using of


Throws.*/
class Examplethrows
{
static void divide_m() throws ArithmeticException
{
int x= 22,y=0,z;
z=x/y;
}
public static void main(String args[])
{
try
{

divide_m();
}
catch(ArithmeticException e)
{
System.out.println("caught the exception"+e);
41
}
}
}

OUTPUT:

42

20 /*Program to Demonstrate the basic


arithmetic
operation*/
class abc
{
public static void main(String args[])
{
System.out.println("Integer Arithmetic");
int a = 1+1;
int b=a*3;
int c=b/4;
int d=c-4;

int e=-d;
System.out.println("a =" +a);
System.out.println("b =" +b);
System.out.println("c =" +c);
System.out.println("d =" +d);
43
System.out.println("e =" +e);
System.out.println("Floating Point Arithmetic");
double da=1+1;
double db=da*3;
double dc=db/4;
double dd=dc-a;
double de=dd;
System.out.println("da="+da);
System.out.println("db="+db);
System.out.println("dc="+dc);
System.out.println("dd="+dd);
System.out.println("de="+de);
}
}

44

OUTPUT:

45

21 /*Program to counting with If


Statement.*/
class iftest
{
public static void main(String args[])
{
int i,count,count1,count2;
float[] weight ={45.f, 55.0f, 47.0f, 51.0f, 54.0f};
float[] height ={176.5f, 174.2f, 168.0f, 170.7f,
169.0f};
count = 0;
count1=0;
count2=0;
for(i=0;i<=4;i++)
{

if (weight[i] < 50.0 && height[i] > 170.0)


{
count1=count1+1;
}
46
count=count+1;
}
count2=count-count1;
System.out.println("Number of persons with...");
System.out.println("Weight<50 and Heigth>170
="+count);
System.out.println("Other="+count2);
}
}

OUTPUT:

47

22 /*Program to Experimenting with


if.else
Statement.*/
class ifelsetest
{
public static void main(String args[])
{
int number[] ={50,65,56,71,81};
int even = 0, odd = 0;
for(int i = 0; i < number.length; i++)
{
if((number[i] % 2) == 0)

{
even += 1;
}
else
{
48
odd += 1;
}
}
System.out.println("Even Numbers: " +even +
"Odd Numbers : " + odd);
}
}

OUTPUT:

49

23 /*Program to using Nesting if..else


statement.*/
class ifelsetest
{
public static void main(String args[])
{
int a=325,b=712,c=478;
System.out.println("Largest value is : ");
if(a > b)
{
if(a > c)
{
System.out.println(a);

}
else
{
System.out.println(c);
}
50
}
if(c > b)
{
System.out.println(c);
}
else
{
System.out.println(b);
}
}
}

OUTPUT:

51

24 /*Program to Demonstration of else if


ladder*/
class ifelsetest
{
public static void main(String args[])
{
int rollnumber[]={111, 222, 333, 444};
int marks[]={81, 75, 43, 58 };
for (int i=0; i< rollnumber.length; i++)
{
if(marks[i] > 79)
System.out.println(rollnumber[i] + " Honours");

else if(marks[i]>59)
System.out.println(rollnumber[i] + " I
Division");
else if(marks[i]>49)
System.out.println(rollnumber[i] + " II
Division");
else
52
System.out.println(rollnumber[i] + " fail");
}
}
}

OUTPUT:

53

25 /*Program to Using while loop.*/


class abc
{
public static void main(String args[])
{
StringBuffer string = new StringBuffer();
char c;
System.out.println("Enter a string");
try
{
while((c = (char)System.in.read()) != '\n');

{
string.append(c);
}
}
catch(Exception e)
{
54
System.out.println("Error in input");
}
System.out.println("You have entered...");
System.out.println(string);
}
}

OUTPUT:

55

26 /*Program to Printing multiplication


table using
dowhile loop*/
class dowhiletest
{
public static void main(String args[])
{
int row, column, y;
System.out.println("Mulitiplication Table");
row = 1;
do

{
column = 1;
do
{
y = row * column;
System.out.println("area="+y);
56
column = column + 1;
}
while (column <= 3);
System.out.println("\n");
row = row + 1;
}
while(row <= 3);
}
}

OUTPUT:

57

27 /*Program to Hello java program.*/


import java.awt.*;
import java.applet.*;
class HelloJava extends Applet
{
public void paint (Graphics g)
{
g.drawString("Hello java", 10, 100);
}
}

The HTML file for a simple applet


program.
<applet code="HelloJava" width=200 height=60>
</applet>

58

OUTPUT:

59

28 /*Program to Applet HelloJavaParam*/


import java.awt.*;
import java.applet.*;
public class HelloJavaParam extends Applet
{
String str;
public void init( )
{
str = getParameter("string");
if(str == null)

str = "Java";
str = "Hello" + str;
}
public void paint (Graphics g)
{
g.drawString(str,10,100);
}
60
}

The HTML file for HelloJavaParam applet.


<HTML>
<!Parameter HTML file>
<HEAD>
<TITTLE>wELCOME to Java Applets</TITLE>
</HEAD>
<BODY>
<APPLET CODE = HelloJavaParam.class

WIDTH = 400
HEIGHT = 200>
<PARAM NAME = "string"
VALUE ="Applet!">
</APPLET>
</BODY>
</HTML>

61
OUTPUT:

62
29 /*Program to Drawing lines and
rectangles.*/
import java.awt.*;
import java.applet.*;
public class LineRect
extends Applet
{
public void paint (Graphics g)
{
g.drawLine(10, 10, 50, 50);
g.drawRect(10, 60, 40, 30);

g.fillRect(60, 10, 30, 80);


g.drawRoundRect(10, 100, 80, 50, 10,10);
g.fillRoundRect(20, 110, 60, 30, 5, 5);
g.drawLine(100, 10, 230, 140);
g.drawLine(100, 140, 230, 10);
}
}
63

The HTML file for using Drawing lines


and rectangle.
<APPLET
CODE=LineRect.class
width=250
Height=200>
</APPLET>

OUTPUT:

64

30 /*Program to Applet for drawing a


human face.*/
import java.awt.*;
import java.applet.*;
public class face extends Applet
{
public void paint (Graphics g)
{
g.drawOval(40, 40, 120, 150);
g.drawOval(57, 75, 30, 20);

g.drawOval(110, 75, 30, 20);


g.fillOval(68, 81, 10, 10);
g.fillOval(121, 81, 10, 10);
g.drawOval(85, 100, 30, 30);
g.fillArc(60, 125, 80, 40, 180, 180);
g.drawOval(25, 92, 15, 30);
g.drawOval(160, 92, 15, 30);
}
65
}

The HTML file for using Drawing lines


and rectangle.
<APPLET
CODE = face.class
WIDTH = 250
HEIGHT = 200>
</APPLET>

OUTPUT:

66

31 /*Program to Drawing polygons.*/


import java.awt.*;
import java.applet.*;
public class Poly extends Applet
{
int x1[]={20, 120, 220, 20};
int y1[]={20, 120, 20, 20};
int n1=4;
int x2[]={120, 220, 220, 120};

int y2[]={120, 20, 220, 120};


int n2=4;
public void paint (Graphics g)
{
g.drawPolygon(x1, y1, n1);
g.fillPolygon(x2, y2, n2);
}
}
67

The HTML file for using Drawing Polygon.


<APPLET
CODE = Poly.class
WIDTH = 450
HEIGHT = 300>
</APPLET>

OUTPUT:

68

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy