0% found this document useful (0 votes)
2 views16 pages

Lab ProgramsJava

The document contains a series of Java programs demonstrating various programming concepts such as finding the largest number, reversing a number, checking for prime numbers, and implementing constructors and method overloading. It also covers exception handling, user-defined exceptions, palindrome checking, threading, applets, and creating a GUI for student registration. Each program is presented with its code and purpose, showcasing fundamental Java programming techniques.

Uploaded by

kavyasanthosh107
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views16 pages

Lab ProgramsJava

The document contains a series of Java programs demonstrating various programming concepts such as finding the largest number, reversing a number, checking for prime numbers, and implementing constructors and method overloading. It also covers exception handling, user-defined exceptions, palindrome checking, threading, applets, and creating a GUI for student registration. Each program is presented with its code and purpose, showcasing fundamental Java programming techniques.

Uploaded by

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

Lab Programs

1. Program to find biggest of 3 numbers

import java.util.Scanner;

class LargestNumber

public static void main(String[] args)

int a,b,c;

Scanner s=new Scanner(System.in);

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

a=s.nextInt();

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

b=s.nextInt();

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

c=s.nextInt();

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

System.out.println("Largest number:" + a);

else if (b>=a && b>=c)

System.out.println("Largest number:" + b);

else

System.out.println("Largest number:" + c);

2. Program to reverse a number

import java.util.Scanner;

class ReverseNumber

public static void main(String[] args)


{

int n, rev=0;

Scanner s=new Scanner(System.in);

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

n=s.nextInt();

while(n!=0)

int d=n%10;

rev=rev*10+d;

n=n/10;

System.out.println("Reversed Number:" + rev);

3. Program to find the given number is prime or not

import java.util.Scanner;

class PrimeNumber

public static void main(String[] args)

int n, count=0;

Scanner s=new Scanner(System.in);

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

n=s.nextInt();

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

if(n%i ==0)

count++;

if(count>2)

System.out.println("Given number is Not Prime");


else

System.out.println("Given number is Prime");

4. Write java programs that implement default constructor

import java.util.Scanner;

class Rectangle

double width;

double height;

Rectangle()

width=10;

height=20;

double area()

return width*height;

class DefaultConstructor

public static void main(String[] args)

Rectangle r1=new Rectangle();

System.out.println(r1.area());

}
5. Write java programs that implement constructor overloading

import java.util.Scanner;

class Rectangle

double width;

double height;

Rectangle()

width=10;

height=20;

Rectangle(double w,double h)

width=w;

height=h;

double area()

return width*height;

class ConstructorOverloading

public static void main(String[] args)

Rectangle r1=new Rectangle();

System.out.println("Area of First Rectangle=" + r1.area());

Rectangle r2=new Rectangle(5,25);

System.out.println("Area of Second Rectangle=" + r2.area());

}
6. Write a java program to find the smallest of given list integers using array

import java.util.Scanner;

class SmallestNumber

public static void main(String[] args)

Scanner s=new Scanner(System.in);

System.out.println("How many elements do you want:");

int n=s.nextInt();

int a[]=new int[n];

System.out.println("Enter " + n + " elements");

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

a[i]=s.nextInt();

int min=a[0];

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

if(a[i]<min)

min=a[i];

System.out.println("Smallest Number:" + min);

7. Write a java program for demonstrating nested classes.

class Outer

void test()

Inner i=new Inner();

i.display();

class Inner

void display()
{

System.out.println("This is Inner Class");

} } }

class NestedClasses

public static void main(String args[])

Outer o=new Outer();

o.test();

8. Write a java program to implement method overloading

class Rect

double width;

double height;

double area(double w,double h)

width=w;

height=h;

return width*height;

class MethodOverloading

public static void main(String[] args)

Rect r1=new Rect();

System.out.println("Area of Rectangle=" + r1.area(10,20));

}
9. Write a program to demonstrate inheritance.

class eight

int clno;

String name;

class nine extends eight

int m1,m2,m3,total;

void initialize()

{ clno=1;

name="Arun";

m1=25;m2=35;m3=34;

total=m1+m2+m3;

void display()

System.out.println(clno +

"\t"+name+"\t"+m1+"\t"+m2+"\t"+m3+"\t"+total);

public static void main(String args[])

{ nine n=new nine();

n.initialize();

n.display();

}
10. Write a java program to implement method overriding

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 OverrideMethod

public static void main(String args[]) {

B b = new B(1, 2, 3);

b.show();

A a=new A(10,20);

a.show();

} }
11. Write a java program for creating a package and using a package.

package Arithmetic;

public class mathsop

public int add(int a,int b)

return a+b;

public int sub(int a,int b)

return a-b;

public int mult(int a,int b)

return a*b;

public double div(int a,int b)

return a/b;

Note: Store the above file in Arithmetic folder.

import Arithmetic.mathsop;

import java.util.Scanner;

public class PackageExample

public static void main(String[] args)

System.out.println("Enter the values of A and B:");

Scanner s=new Scanner(System.in);

int a=s.nextInt();
int b=s.nextInt();

mathsop m=new mathsop();

System.out.println("Addition=" + m.add(a,b) + "\nSubtraction=" +

m.sub(a,b) + "\nMultiplication=" + m.mult(a,b)+ "\nDivision=" + m.div(a,b));

Note: Store this file outside Arithmetic folder.

12. Write a java program using Exception handling mechanism.

import java.util.Scanner;

class ExceptionExample

public static void main(String as[])

Scanner s=new Scanner(System.in);

System.out.println("Enter A and B");

int a=s.nextInt();

int b=s.nextInt();

try

System.out.println("Division="+ (a/b));

catch(ArithmeticException e)

System.out.println("Division by zero is not possible");

}
13. Write a java program to demonstrate user defined exception handling mechanism.

import java.util.Scanner;

class UserDefinedException

public static void main(String[] args)

try

Scanner s=new Scanner(System.in);

System.out.println("Enter the marks in Java:");

int mk=s.nextInt();

if (mk>100)

throw new MarksOutOfBound("Marks should not be greater than 100.");

System.out.println("Entered marks: " + mk+ " is valid");

catch (MarksOutOfBound mb)

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

class MarksOutOfBound extends Exception

MarksOutOfBound(String s)

super(s);

}
14. Write a java program that checks whether a given string is a palindrome or not

import java.util.Scanner;

class StringPalindrome

public static void main(String[] args)

Scanner s=new Scanner(System.in);

System.out.println("Enter the String:");

String str=s.next();

System.out.println("String is:" + str);

String reversestring="";

for (int i = str.length() - 1; i >= 0; i--)

reversestring = reversestring + str.charAt(i);

System.out.println("String is:" + reversestring);

if (str.equals(reversestring))

System.out.println("The string is a palindrome.");

else

System.out.println("The string isn't a palindrome.");

15. Write a java program for creating new thread by extending Thread class

class A extends Thread

public void run()

System.out.println("Thread A Started");

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

System.out.println("Thread A -> No = " +i);


}

System.out.println("Exit from Thread A ");

class B extends Thread

public void run()

System.out.println("Thread B Started");

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

System.out.println("Thread B -> No = " +i);

System.out.println("Exit from Thread B ");

class ThreadProgram2

public static void main(String args[])

A obja=new A();

B objb=new B();

System.out.println("start thread A");

obja.start();

System.out.println("start thread B");

objb.start();

}
16. Write java programs to create a simple Applet

Applet File: (SimpleApplet.java)

import java.awt.*;

import java.applet.*;

public class SimpleApplet extends Applet

public void paint (Graphics g)

g.drawString("Hello java", 10, 100);

HTML File: (AppletExample.java)

<HTML>

<HEAD>

<TITLE>

Welcome to java applet

</TITLE>

</HEAD>

<BODY>

<APPLET CODE = "SimpleApplet.class"

WIDTH = 400

HEIGHT = 200>

</APPLET>

</BODY>

</HTML>

17. Write java programs to create swing based Applet

Swing File:

import javax.swing.JApplet;

import java.awt.*;

public class SwingApplet extends JApplet

{
public void paint (Graphics g)

g.drawOval(25,25,100,200); // (x,y, width,height)

HTML File:

<HTML>

<HEAD>

<TITLE>

Welcome to java applet

</TITLE>

</HEAD>

<BODY>

<APPLET CODE = "SwingApplet.class"

WIDTH = 400

HEIGHT = 200>

</APPLET>

</BODY>

</HTML>

18. Create a GUI that accepts details for a student registration

for a seminar.

import java.awt.*;

class twelve

Frame f1;

Panel p1;

Label l1,l2,l3;

TextField t1,t2,t3;

Button b1,b2;

twelve()

{
f1=new Frame("Registration form");

p1=new Panel();

l1=new Label("Name please");

l2=new Label("College name");

l3=new Label("Emaila ddress");

t1=new TextField(10);

t2=new TextField(10);

t3=new TextField(10);

b1=new Button("Submit");

b2=new Button("Reset");

p1.add(l1);

p1.add(t1);

p1.add(l2);

p1.add(t2);

p1.add(l3);p1.add(t3);p1.add(b1);p1.add(b2);

f1.add(p1);

f1.setSize(300,300);

f1.setVisible(true);

public static void main(String args[])

twelve tt=new twelve();

You might also like