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

JAVA Programs With Output

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

JAVA Programs With Output

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

Program 1

MarksDemo.java

class Student

int rollno;

String name;

void putdata(int r,String n)

rollno = r;

name = n;

void getdata()

System.out.println("Roll No : "+ rollno);

System.out.println("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();

System.out.println("Marks obtained");

System.out.println("Mark 1 : "+mark1);

System.out.println("Mark 2 : "+mark2);

System.out.println("Mark 3 : "+mark3);

System.out.println("Mark 4 : "+mark4);

System.out.println("Mark 5 : "+mark5);

System.out.println("Mark 6 : "+mark6);

class Result extends exam

double totalmarks;

void display()

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

getmark();

System.out.println("Total mark : "+totalmarks);

class MarksDemo

public static void main(String args[])

Result r1=new Result();

r1.putdata(121, "Vishnu");

r1.putmark(56.0, 82.0, 80.5, 70.5, 60.5, 85.5);

r1.display();
}

Output

Program 2

Overloadmethod.java

import java.util.*;

class Methodoverload

void area(int x, int y)

int l = x,b = y,al;

al = l * b;

System.out.println("Area of rectangle = "+al);

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

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

ar=(Math.sqrt(s*(s-x)*(s-y)*(s-z)));

System.out.println("Area of triangle = "+ar);

class Overloadmethod

public static void main(String args[])

Methodoverload m1 = new Methodoverload();

Scanner s2=new Scanner(System.in);

System.out.println("1.Area of rectangle");

System.out.println("2.Area of triangle");

System.out.println("Your choice please");

int c=s2.nextInt();

switch (c) {

case 1: m1.area(4,2);

break;

case 2: m1.area(8.0,4.0,5.0);

break;

Output
Program 3

AddMethod.java

import java.util.*;

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(System.in);

System.out.println("Enter 2 integer values : ");

int c=s2.nextInt();

int d=s2.nextInt();

System.out.println("Enter 2 float values : ");

double a=s2.nextDouble();

double b=s2.nextDouble();

Addition ob=new Addition();

float res1=ob.add(c,d);

System.out.println("Addition of the two integer values : "+c+" + "+d+" = "+res1);

int res2=ob.add(a,b);

System.out.println("Addition of the two float values : "+a+" + "+b+" = "+res2);

Subtraction ob1=new Subtraction();

float res3=ob1.sub(c,d);

System.out.println("Subtraction of the two integer values : "+c+" - "+d+" = "+res3);

int res4=ob1.sub(a,b);

System.out.println("Subtraction of the two float values : "+a+" - "+b+" = "+res4);


Multiplication ob2=new Multiplication();

float res5=ob2.mul(c,d);

System.out.println("Multiplication of the two integer values : "+c+" * "+d+" = "+res5);

int res6=ob2.mul(a,b);

System.out.println("Multiplication of the two float values : "+a+" * "+b+" = "+res6);

Division ob3=new Division();

float res7=ob3.div(c,d);

System.out.println("Division of the two integer values : "+c+" / "+d+" = "+res7);

int res8=ob3.div(a,b);

System.out.println("Division of the two float values : "+a+" / "+b+" = "+res8);

Output

Program 4

Override.java
class A

int i,j;

A(int a,int b)

i=a;

j=b;

void show()

System.out.println("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()

System.out.println("k : "+k);

class Override

public static void main(String args[])

B subob=new B(1,2,3);
subob.show();

Output

Program 5

Factorial.java

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;

PackageTest1.java

import fact.Factorial;
import java.util.*;

class PackageTest1

public static void main(String args[])

Factorial ob=new Factorial();

Scanner sc = new Scanner(System.in);

System.out.println("Enter number");

int n=sc.nextInt();

long f=ob.factial(n);

System.out.println("Factorial of "+n+" is "+f);

Output

Program 6

Sarea.java

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=cir.calculateArea(20);

System.out.println("Area of circle with radius 20 is : "+a);

Output

Program 7

Varea.java

import java.util.*;

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(System.in);

System.out.println("Enter Radius : ");

float rad=sc.nextFloat();

System.out.println("Enter height of Cylinder : ");

float h=sc.nextFloat();

Cylinder cl=new Cylinder();

float v=cl.calculateVolume(h,rad);

System.out.println("Vloume of cylinder is "+v);

Output
Program 8

Exceptionsum.java

import java.util.*;

class Exceptionsum

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter frist no : ");

int a=sc.nextInt();

System.out.println("Enter second no : ");

int b=sc.nextInt();

int sum=a+b;

System.out.println("Sum is : "+sum);

try

if(a>b)

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

else

System.out.println(a+ " is less than "+b);

catch(MyException e )

System.out.println("Caught my Exception : " +a+ " is greater than "+b);

System.out.println(e.getMessage());
}

class MyException extends Exception

public MyException(String message)

super(message);

Output

Program 9

ExceptionExp.java

import java.util.*;

class ExceptionExp

public static void main(String args[])

Scanner sc=new Scanner(System.in);


System.out.println("Enter value of x ");

int x=sc.nextInt();

float y=x/(x-5);

System.out.println("Value of expression "+y);

try

if (x<=5)

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

else

System.out.println("x is greater than 5 : "+x);

catch(MyException e)

System.out.println("Caught my exception "+x+" is less than 5");

System.out.println(e.getMessage());

class MyException extends Exception

public MyException(String message)

super(message);

Output
Program 10

Oddeven.java

class Odd extends Thread

public void run()

System.out.println("Odd number are ");

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

try

if (i%2!=0)

System.out.println("Odd number = "+i);

Thread.sleep(100);

catch (InterruptedException e)

System.out.println("Odd child interrupted");


}

System.out.println("Odd child exiting");

class Even extends Thread

public void run()

System.out.println("Even number are ");

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

try

if (i%2==0)

System.out.println("Even number = "+i);

Thread.sleep(100);

catch (InterruptedException e)

System.out.println("Even child interrupted");

System.out.println("Even child exiting");

class Oddeven

public static void main (String args[])


{

Odd o=new Odd();

o.start();

Even ev=new Even();

ev.start();

System.out.println("Main thread exiting");

Output

Program 11

Primeeven.java

class Prime extends Thread

public void run()

System.out.println("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)

System.out.println("prime numbers = "+n);

Thread.sleep(100);

catch (Exception e)

System.out.println("Prime child interrupted");

System.out.println("prime child Exiting");

class Even extends Thread

public void run()

System.out.println("Even number are ");

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


{

try

if (i%2==0)

System.out.println("even number = "+i);

Thread.sleep(100);

catch (Exception e)

System.out.println("even child interrupted");

System.out.println("even child exiting");

class Primeeven

public static void main(String args[])

Prime p = new Prime();

p.start();

Even ev = new Even();

ev.start();

System.out.println("main thread exiting");

Output
Program 12

RectParam.java

import java.awt.*;

import java.applet.*;

public class RectParam extends Applet

String Str1,Str2;

public void init()

Str1=getParameter("String1");

Str2=getParameter("String2");

public void paint(Graphics g)

g.drawString(Str1,10,100);

g.drawRect(10,10,60,50);

g.drawString(Str2,10,180);

g.drawOval(10,200,50,50);

}
RectParam.html

<html>

<head>

<title></title>

</head>

<body>

<applet code=RectParam.class width="400" height="200">

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

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

</applet>

</body>

</html>

Output

Program 13
House.java

import java.awt.*;

import java.applet.*;

public class House extends Applet

public void paint(Graphics g)

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

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

g.drawRect(150,150,150,200);

g.drawRect(200,200,50,150);

g.drawOval(200,75,50,50);

g.drawPolygon(x,y,3);

House.html

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<applet code="House.class" width="400" height="450"></applet>

</body>

</html>

Output
Program 14

Star.java

import java.awt.*;

import java.applet.*;

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=xpoints.length;
g.drawPolygon(xpoints,ypoints,num);

Star.html

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<applet code="Star.class" width="230" height="210"></applet>

</body>

</html>

Output

Program 15

Smiley.java

import java.awt.*;

import java.applet.*;

public class Smiley extends Applet

{
public void paint(Graphics g)

g.drawOval(80,70,150,150);

g.drawArc(130,180,50,20,180,180);

g.setColor(Color.gray);

g.fillOval(120,120,15,15);

g.fillOval(170,120,15,15);

Smiley.html

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<applet code="Smiley.class" width="230" height="210"></applet>

</body>

</html>

Output
Program 16

SumGUI.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class SumGUI

public static void main(String args[])

JFrame f= new JFrame("Sum");

f.setVisible(true);

Container c =f.getContentPane();

f.setSize(300, 300);

c.setLayout(new FlowLayout());

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

c.add(jl1);

JTextField t1=new JTextField(15);

c.add(t1);
JLabel jl2=new JLabel ("Second No ");

c.add(jl2);

JTextField t2=new JTextField(15);

c.add(t2);

JLabel jl3=new JLabel ("Result ");

c.add(jl3);

JTextField t3=new JTextField(15);

c.add(t3);

t3.setEditable(false);

JButton jb=new JButton ("SUBMIT");

c.add(jb);

jb.addActionListener(new ActionListener ()

public void actionPerformed(ActionEvent ae)

int x=Integer.parseInt(t1.getText());

int y=Integer.parseInt(t2.getText());

int z=x+y;

t3.setText(String.valueOf(z));

);

Output
Program 17

Interest.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Interest

public static void main(String args[])

JFrame f=new JFrame("Sum");

f.setVisible(true);

Container c= f.getContentPane();

f.setSize(300, 300);

c.setLayout(new FlowLayout());

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

c.add(jl1);

JTextField t1=new JTextField(15);

c.add(t1);

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

c.add(jl2);
JTextField t2=new JTextField(15);

c.add(t2);

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

c.add(jl3);

JTextField t3=new JTextField(15);

c.add(t3);

JLabel jl4=new JLabel("Result");

c.add(jl4);

JTextField t4=new JTextField(15);

c.add(t4);

t4.setEditable(false);

JButton jb=new JButton("SUBMIT");

c.add(jb);

jb.addActionListener(new ActionListener()

public void actionPerformed(ActionEvent ae )

int p=Integer.parseInt(t1.getText());

int n=Integer.parseInt(t2.getText());

int r=Integer.parseInt(t3.getText());

int I=p*n*r/100;

t4.setText(String.valueOf(I));

);

Output
Program 18

TextDelete.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class TextDelete

public static void main(String args[])

JFrame f= new JFrame("Sum");

f.setVisible(true);

Container c=f.getContentPane();

f.setSize(300, 300);

f.setLayout(new FlowLayout());

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

c.add(jl1);

JTextArea t1=new JTextArea(20,20);

c.add(t1);

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

c.add(jl2);

JTextField t2=new JTextField(15);


c.add(t2);

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

c.add(jb);

jb.addActionListener(new ActionListener()

public void actionPerformed(ActionEvent ae)

String str=t2.getText();

t1.setText(t1.getText().replace(str," "));

);

Output

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