0% found this document useful (0 votes)
110 views33 pages

Java Programs for Students and Shapes

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)
110 views33 pages

Java Programs for Students and Shapes

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

Program 1

[Link]

class Student

int rollno;

String name;

void putdata(int r,String n)

rollno = r;

name = n;

void getdata()

[Link]("Roll No : "+ rollno);

[Link]("Name : "+name);

class exam extends Student

double mark1,mark2,mark3,mark4,mark5,mark6;

void putmark(double m1,double m2,double m3,double m4,double m5,double m6)

mark1=m1;

mark2=m2;

mark3=m3;

mark4=m4;

mark5=m5;

mark6=m6;

void getmark()
{

getdata();

[Link]("Marks obtained");

[Link]("Mark 1 : "+mark1);

[Link]("Mark 2 : "+mark2);

[Link]("Mark 3 : "+mark3);

[Link]("Mark 4 : "+mark4);

[Link]("Mark 5 : "+mark5);

[Link]("Mark 6 : "+mark6);

class Result extends exam

double totalmarks;

void display()

totalmarks=mark1+mark2+mark3+mark4+mark5+mark6;

getmark();

[Link]("Total mark : "+totalmarks);

class MarksDemo

public static void main(String args[])

Result r1=new Result();

[Link](121, "Vishnu");

[Link](56.0, 82.0, 80.5, 70.5, 60.5, 85.5);

[Link]();
}

Output

Program 2

[Link]

import [Link].*;

class Methodoverload

void area(int x, int y)

int l = x,b = y,al;

al = l * b;

[Link]("Area of rectangle = "+al);

void area(double x,double y,double z)

double s,ar;
s=(x+y+z)/2;

ar=([Link](s*(s-x)*(s-y)*(s-z)));

[Link]("Area of triangle = "+ar);

class Overloadmethod

public static void main(String args[])

Methodoverload m1 = new Methodoverload();

Scanner s2=new Scanner([Link]);

[Link]("[Link] of rectangle");

[Link]("[Link] of triangle");

[Link]("Your choice please");

int c=[Link]();

switch (c) {

case 1: [Link](4,2);

break;

case 2: [Link](8.0,4.0,5.0);

break;

Output
Program 3

[Link]

import [Link].*;

class Addition

public int add(double a, double b)

int res=(int)a+(int)b;

return res;

public float add(int a, int b)

float res=(float)a+(float)b;

return res;

}
class Subtraction

public int sub(double a, double b)

int res=(int)a-(int)b;

return res;

public float sub(int a, int b)

float res=(float)a-(float)b;

return res;

class Multiplication

public int mul(double a, double b)

int res=(int)a*(int)b;

return res;

public float mul(int a, int b)

float res=(float)a*(float)b;

return res;

class Division

public int div(double a, double b)

int res=(int)a/(int)b;
return res;

public float div(int a, int b)

float res=(float)a/(float)b;

return res;

class AddMethod

public static void main(String args[])

Scanner s2=new Scanner([Link]);

[Link]("Enter 2 integer values : ");

int c=[Link]();

int d=[Link]();

[Link]("Enter 2 float values : ");

double a=[Link]();

double b=[Link]();

Addition ob=new Addition();

float res1=[Link](c,d);

[Link]("Addition of the two integer values : "+c+" + "+d+" = "+res1);

int res2=[Link](a,b);

[Link]("Addition of the two float values : "+a+" + "+b+" = "+res2);

Subtraction ob1=new Subtraction();

float res3=[Link](c,d);

[Link]("Subtraction of the two integer values : "+c+" - "+d+" = "+res3);

int res4=[Link](a,b);

[Link]("Subtraction of the two float values : "+a+" - "+b+" = "+res4);


Multiplication ob2=new Multiplication();

float res5=[Link](c,d);

[Link]("Multiplication of the two integer values : "+c+" * "+d+" = "+res5);

int res6=[Link](a,b);

[Link]("Multiplication of the two float values : "+a+" * "+b+" = "+res6);

Division ob3=new Division();

float res7=[Link](c,d);

[Link]("Division of the two integer values : "+c+" / "+d+" = "+res7);

int res8=[Link](a,b);

[Link]("Division of the two float values : "+a+" / "+b+" = "+res8);

Output

Program 4

[Link]
class A

int i,j;

A(int a,int b)

i=a;

j=b;

void show()

[Link]("i and j"+i+" "+j);

class B extends A

int k;

B(int a,int b,int c)

super(a,b);

k=c;

void show()

[Link]("k : "+k);

class Override

public static void main(String args[])

B subob=new B(1,2,3);
[Link]();

Output

Program 5

[Link]

package fact;

public class Factorial

public long factial(int a)

int f=1,n=a;

for(int i=2;i<=n;i++)

f=f*i;

return f;

[Link]

import [Link];
import [Link].*;

class PackageTest1

public static void main(String args[])

Factorial ob=new Factorial();

Scanner sc = new Scanner([Link]);

[Link]("Enter number");

int n=[Link]();

long f=[Link](n);

[Link]("Factorial of "+n+" is "+f);

Output

Program 6

[Link]

interface Shape

final static float pi=3.14f;

float calculateArea(float x);

class Circle implements Shape

{
public float calculateArea(float x)

return(pi*x*x);

class Sarea

public static void main(String args[])

Circle cir =new Circle();

float a=[Link](20);

[Link]("Area of circle with radius 20 is : "+a);

Output

Program 7

[Link]

import [Link].*;

interface Volume

final static float pi=3.14f;

public float calculateVolume(float x,float y);

abstract class Circle implements Volume

float r;
public abstract float calculateVolume(float x, float y);

void readRadius(float rad)

r=rad;

class Cylinder extends Circle

public float calculateVolume(float x, float y)

readRadius(y);

return pi*r*r*x;

class Varea

public static void main(String args[])

Scanner sc=new Scanner([Link]);

[Link]("Enter Radius : ");

float rad=[Link]();

[Link]("Enter height of Cylinder : ");

float h=[Link]();

Cylinder cl=new Cylinder();

float v=[Link](h,rad);

[Link]("Vloume of cylinder is "+v);

Output
Program 8

[Link]

import [Link].*;

class Exceptionsum

public static void main(String args[])

Scanner sc=new Scanner([Link]);

[Link]("Enter frist no : ");

int a=[Link]();

[Link]("Enter second no : ");

int b=[Link]();

int sum=a+b;

[Link]("Sum is : "+sum);

try

if(a>b)

throw new MyException("Throws user defined Exception");

else

[Link](a+ " is less than "+b);

catch(MyException e )

[Link]("Caught my Exception : " +a+ " is greater than "+b);

[Link]([Link]());
}

class MyException extends Exception

public MyException(String message)

super(message);

Output

Program 9

[Link]

import [Link].*;

class ExceptionExp

public static void main(String args[])

Scanner sc=new Scanner([Link]);


[Link]("Enter value of x ");

int x=[Link]();

float y=x/(x-5);

[Link]("Value of expression "+y);

try

if (x<=5)

throw new MyException("Throws user defined exception");

else

[Link]("x is greater than 5 : "+x);

catch(MyException e)

[Link]("Caught my exception "+x+" is less than 5");

[Link]([Link]());

class MyException extends Exception

public MyException(String message)

super(message);

Output
Program 10

[Link]

class Odd extends Thread

public void run()

[Link]("Odd number are ");

for (int i=1;i<10;i++)

try

if (i%2!=0)

[Link]("Odd number = "+i);

[Link](100);

catch (InterruptedException e)

[Link]("Odd child interrupted");


}

[Link]("Odd child exiting");

class Even extends Thread

public void run()

[Link]("Even number are ");

for (int i=2;i<10;i++)

try

if (i%2==0)

[Link]("Even number = "+i);

[Link](100);

catch (InterruptedException e)

[Link]("Even child interrupted");

[Link]("Even child exiting");

class Oddeven

public static void main (String args[])


{

Odd o=new Odd();

[Link]();

Even ev=new Even();

[Link]();

[Link]("Main thread exiting");

Output

Program 11

[Link]

class Prime extends Thread

public void run()

[Link]("Prime numbers are ");

for (int n=2;n<=10;n++)

{
boolean flag = true;

try

for (int i=2;i<=n/2;i++)

if(n%i==0)

flag = false;

break;

if(flag)

[Link]("prime numbers = "+n);

[Link](100);

catch (Exception e)

[Link]("Prime child interrupted");

[Link]("prime child Exiting");

class Even extends Thread

public void run()

[Link]("Even number are ");

for (int i=2;i<=10;i++)


{

try

if (i%2==0)

[Link]("even number = "+i);

[Link](100);

catch (Exception e)

[Link]("even child interrupted");

[Link]("even child exiting");

class Primeeven

public static void main(String args[])

Prime p = new Prime();

[Link]();

Even ev = new Even();

[Link]();

[Link]("main thread exiting");

Output
Program 12

[Link]

import [Link].*;

import [Link].*;

public class RectParam extends Applet

String Str1,Str2;

public void init()

Str1=getParameter("String1");

Str2=getParameter("String2");

public void paint(Graphics g)

[Link](Str1,10,100);

[Link](10,10,60,50);

[Link](Str2,10,180);

[Link](10,200,50,50);

}
[Link]

<html>

<head>

<title></title>

</head>

<body>

<applet code=[Link] width="400" height="200">

<param name="String1" value="Rectangle" >

<param name="String2" value="Circle">

</applet>

</body>

</html>

Output

Program 13
[Link]

import [Link].*;

import [Link].*;

public class House extends Applet

public void paint(Graphics g)

int x []={150,300,225};

int y []={150,150,25};

[Link](150,150,150,200);

[Link](200,200,50,150);

[Link](200,75,50,50);

[Link](x,y,3);

[Link]

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<applet code="[Link]" width="400" height="450"></applet>

</body>

</html>

Output
Program 14

[Link]

import [Link].*;

import [Link].*;

public class Star extends Applet

public void paint (Graphics g)

int xpoints[]={55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };

int ypoints[]={0, 36, 36, 54, 96, 72, 96, 54, 36, 36};

int num=[Link];
[Link](xpoints,ypoints,num);

[Link]

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<applet code="[Link]" width="230" height="210"></applet>

</body>

</html>

Output

Program 15

[Link]

import [Link].*;

import [Link].*;

public class Smiley extends Applet

{
public void paint(Graphics g)

[Link](80,70,150,150);

[Link](130,180,50,20,180,180);

[Link]([Link]);

[Link](120,120,15,15);

[Link](170,120,15,15);

[Link]

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<applet code="[Link]" width="230" height="210"></applet>

</body>

</html>

Output
Program 16

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class SumGUI

public static void main(String args[])

JFrame f= new JFrame("Sum");

[Link](true);

Container c =[Link]();

[Link](300, 300);

[Link](new FlowLayout());

JLabel jl1=new JLabel("First No ");

[Link](jl1);

JTextField t1=new JTextField(15);

[Link](t1);
JLabel jl2=new JLabel ("Second No ");

[Link](jl2);

JTextField t2=new JTextField(15);

[Link](t2);

JLabel jl3=new JLabel ("Result ");

[Link](jl3);

JTextField t3=new JTextField(15);

[Link](t3);

[Link](false);

JButton jb=new JButton ("SUBMIT");

[Link](jb);

[Link](new ActionListener ()

public void actionPerformed(ActionEvent ae)

int x=[Link]([Link]());

int y=[Link]([Link]());

int z=x+y;

[Link]([Link](z));

);

Output
Program 17

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class Interest

public static void main(String args[])

JFrame f=new JFrame("Sum");

[Link](true);

Container c= [Link]();

[Link](300, 300);

[Link](new FlowLayout());

JLabel jl1=new JLabel("Principle amount(p)");

[Link](jl1);

JTextField t1=new JTextField(15);

[Link](t1);

JLabel jl2=new JLabel("Number of time Period (N) : in years");

[Link](jl2);
JTextField t2=new JTextField(15);

[Link](t2);

JLabel jl3=new JLabel("Interest Rate (R):%");

[Link](jl3);

JTextField t3=new JTextField(15);

[Link](t3);

JLabel jl4=new JLabel("Result");

[Link](jl4);

JTextField t4=new JTextField(15);

[Link](t4);

[Link](false);

JButton jb=new JButton("SUBMIT");

[Link](jb);

[Link](new ActionListener()

public void actionPerformed(ActionEvent ae )

int p=[Link]([Link]());

int n=[Link]([Link]());

int r=[Link]([Link]());

int I=p*n*r/100;

[Link]([Link](I));

);

Output
Program 18

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class TextDelete

public static void main(String args[])

JFrame f= new JFrame("Sum");

[Link](true);

Container c=[Link]();

[Link](300, 300);

[Link](new FlowLayout());

JLabel jl1=new JLabel("Text Area");

[Link](jl1);

JTextArea t1=new JTextArea(20,20);

[Link](t1);

JLabel jl2=new JLabel("Enter text to be deleted");

[Link](jl2);

JTextField t2=new JTextField(15);


[Link](t2);

JButton jb=new JButton("DELETE TEXT");

[Link](jb);

[Link](new ActionListener()

public void actionPerformed(ActionEvent ae)

String str=[Link]();

[Link]([Link]().replace(str," "));

);

Output

You might also like