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

Java Practicals

Uploaded by

Xenon Y
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)
54 views

Java Practicals

Uploaded by

Xenon Y
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/ 27

public class hello class add2

{ {

public static void main(String args[]) public static void main(String args[])

{ {

System.out.println("Hello World"); int a=Integer.parseInt(args[0]);

} int b=Integer.parseInt(args[1]);

} int c=a+b;

System.out.println("sum of "+a+" and


"+b+" is "+c);

}
class add

public static void main(String args[])

int a=22, b=33;

int c=a+b;

System.out.println("sum of "+a+" and


"+b+" is "+c);

}
import java.util.Scanner; class use

public class add3 {

{ public static void main(String args[])

public static void main(String[] args) {

{ boolean a=true;

Scanner scanner = new boolean b=false;


Scanner(System.in);
System.out.println("a&&b:"+(a&&b));

System.out.println("a||b:"+(a||b));
System.out.print("enter the first
System.out.println("!(a&&b):"+!(a&&
integer: ");
b));
int num1 =
}
scanner.nextInt();System.out.print("e
nter the second integer: "); }

int num2 = scanner.nextInt();

int sum = num1 + num2;

System.out.println("Sum of the two


integers: "+sum);

scanner.close();

}
class prc3

public static void main(String args[])

int num=Integer.parseInt(args[0]);
if(num>=0)

System.out.println(num +" is Positive


no.");

if(num%2==0)

System.out.println(num +" is Even


no.");

else

System.out.println(num +" is Odd


no.");

else

{ System.out.println(num + " is
Negative no.");

if(num%2==0)

{ System.out.println(num + " is Even


no.");
public class prc4 { case 'i':

public static void main(String[] args) case 'o':


{
case 'u':
int number =
System.out.println(letter + "
Integer.parseInt(args[0]);
is a vowel.");

break;
String result = (number % 2 == 0)
default:
? "even" : "odd";
System.out.println(letter + "
is not a vowel.");
System.out.println("The number
break;
" + number + " is " + result + ".");
}
}
}
}
}

public class vowel {

public static void main(String[] args)


{

char letter = 'a';

switch (letter) {

case 'a':

case 'e':
public class prc5i { for (int i = 1; i <= rows; i++) {

public static void main(String[] args) for (int j = 1; j <= rows - i; j++) {
{
System.out.print(" ");
for (int i = 1; i <= 20; i++) {
}
if (i % 2 == 0) {
for (int k = 1; k <= i; k++) {
System.out.println(i);
System.out.print("*");
}
}
}
System.out.println();
}
}
}
}

public class prc5ii {

public static void main(String[] args)


{

int rows = 5; // You can change


this value to display a different size
pyramid
public class prc6 {

public static void main(String[] args)


{

int num = 1;

do {

System.out.print(num);

num++;

} while (num <= 50);

public class prc6ii

public static void main(String[] args)

int num = 1;

do
{
System.out.println(num);
num++;
} while (num < 11)

}
public class prc7i { public class prc7ii {

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

byte num1 = 10; // implicit typecasting in action

int num2 = 20; int num1 = 9;

float num2 = 5.4;

int sum = num1 + num2;

float sum = num1 + num2;

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

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

} }

}
class prc9

public static void main(String args[])

int x = 200;

double y=12.24;

byte a=(byte)x;

long b=(long)x;

int c=(int)y;

System.out.println("int x="+x);

System.out.println("int x to byte="+a);

System.out.println("int x to long="+b);

System.out.println("double y="+y);

System.out.println("double y to int="+c);

}
class prc9i

public static void main(String args[])

int a=10;

byte b=20;

System.out.println("Before typecasting : ");

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

double c=a;

long d=b;

System.out.println("After type casting :");

System.out.println("c= "+c+" d "+d);

}
class foreach1

public static void main(String args[])

String[] f1 ={"one", "two","three"};

System.out.println("\n foreachLop!");

for(String fe:f1)

System.out.println(fe);

}
class Complex Number{
private double real;
private double imaginary;
public ComplexNumber()
This.real=0;
this imaginary=0;
}
public ComplexNumber(double real, double Imaginary)
this.real=real;
this imaginary=imaginary;
public ComplexNumber
add(ComplexNumber num){
double realSum=this.real+num.real;
double imaginarySum = this.imaginary+num.imaginary;
return new ComplexNumber(realSum, ImaginarySum);
}
public String toString) {
return this.real+"+"+this.imaginary+"i";
}
}
public class prac10{
public static void main(String[] args) {
Complex Number num1=new Complex Number(10, 12);
Complex Number num2 new Complex Number(50, 69),
ComplexNumber sum num1.add(num2);
System.out.println("Sum is: "+sum);
}
}
class box
{
int l,b, area ;
box ()
{
l = 20
b = 15
}
box( int x ,int y)
{
I=x
b=y
}
void area()
{
area =l* b;
System.out.println("Area of rectangele is: "+area);
}
public static void main(String args[])
{
System.out.println("Default constructure :");
box B= newbox ()
B.area();
System.out.println("para constructure:");
box B1 = new box(50,10);
B1.area();
}
}
class strcls
{
public static void main(String arr[])
{
String s1="Hello";
String s2="Welcome to Java";
System.out.println(s1);
System.out.println(s2);
System.out.println(s1.charAt(4));
System.out.println(s1.length());

System.out.println(s1.substring(1));

System.out.println(s2.substring(3,7));

System.out.println();

System.out.println(s1.startsWith("He"));

System.out.println(s1.endsWith("ca"));

System.out.println(s1.equals("Java"));

System.out.println(s2.equals("to"));

System.out.println(s2.equalsIgnoreCase("to"))
;

System.out.println(s1.indexOf('H'));

System.out.println(s2.lastIndexOf('a'));
class strbf
{
public static void main(String arr[])

StringBuffer s1=new StringBuffer("Welcome To Java Programming !!!");

String s2=" DEV here";

System.out.println("\n");

System.out.println(s1);

System.out.println("\n");

s1.setCharAt(2,'y');

System.out.println(s1);

System.out.println("\n");

System.out.println(s1.append(s2));

System.out.println("\n");

System.out.println(s1.insert(10,s2));
}
}

System.out.println("Hi".concat(s2));

System.out.println(s1.compareTo(s2));

System.out.println(s1.toLowerCase());

System.out.println(s1.replace('I','m'));

System.out.println(s1.contains("java"));

System.out.println(" Hello hi ".trim());


}
}
import java.util.*;

import java.io.*;

class Vec1

public static void main(String args[])

Vector v=new Vector();

v.add(10);

v.insertElementAt(20,1);

v.addElement(30);

System.out.println("Vector Elements:"+v);

v.add(40);

v.add(50);

v.add(60);

v.add(70);

v.add(80);

System.out.println("Elements Are:"+v);

v.removeElement(50);

v.removeElementAt(4);

System.out.println("Elements Are:"+v);

v.removeAllElements();

}
import java.io.*;

import java.util. Vector; class v2

public static void main(String args[])

Vector v=new Vector();

v.add(new Double(10.896));

v.add(new Integer("20"));

v.add(new Float("30.56"));

v.add(new Character('a'));

v.add(new String("Raj"));

v.insert ElementAt("java",2);

System.out.println("Elements are:"+v);

}
class wra
{
public static void main(String args[])
{
String str1=”32”;
String s2="5";
Float f=Float.valueOf(str1);
Integer i=integer.valueOf(s2);
System.out.println(f);
System.out.println(i);
}
} class wrp3
{
public static void main(String args[])
{
Double o1=new Double(10.34);
double d=ol.doubleValue();
class wrp2 System.out.println("d: “+d)
{ }
public static void main(String args[]) }
{
int i=55;
String s1=integer.toString(i);
String s2 Integer.toHexString(i);
String s3 Integer.toOctalString(i);
String s4=Integer.toBinaryString(i);
System.out.println("String:"+s1);
System.out.println("HexString: "+s2);
System.out.println("Octal String: "+s3);
System.out.println("Binary String: “s4);
}
}
PRACTICAL NO 17

class bankA
class superc
{
Class superc
void display()
{
{
Void display()
super.display();
{
System.out.println("This is Super Class");
System.out.println(“This is Super Class”);
}
}
}
}
class super3
Class super3
{
{
public static void main(string args[])
public static void main(string args[])
{
{
sub s1=new sub();
bankC c1=new bankC();
s1.display();
cl.dis();
}
}
}
}
{
void dis()
{
System.out.println("Rate Of interest Of Bank A:
6%");
}
}
class bankB extends bankA
{
void dis()
{
super.dis();
System.out.println("Rate Of Interest Of Bank B:
4%");
}
}
class bankC extends bankB
{
void dis()
{
super dis();
System.out.println("Rate Of Interest Of Bank C:
3%);
}
}
class animal
{
void display()
{
super.display();
System.out.println("This is Super Class");
}
}
class super3
{
public static void main(string args[])
{
sub s1=new sub();
s1.display();
}
}
{

void move()
{

System.out.println("This is Super Class")


}
}

class dog extends animal


{

void move()
{
super.move();

System.out.println("This is Subclass");
}
}

class super1

public static void main(String args[])


{

dog d1=new dog();

d1.move();
}
}
Practical no 19

interface A
import java util *; class prc19b
{
interface A public static void main(string args[])
{
{ C c1=new C();
c1.acc();
void AreaOfRec(); c1.AreaOfRec();
c1.AreaOfCircle();
} }
}
class C implements A,B
{
void show();
{
}
int r,l,b; class B implements A
{
Scanner sc=new Scanner(System.in); public void show()
{
void acc() System.out.println("This is Class B which
implements interface A");
{ }
}
System.out.println("Enter Radius:"); class prc19
{
r=sc.nextint(); public static void main(string args[])
{
System.out.println("Enter Length:"); System.out.println("This is Main Method !!");
B b1=new B();
l=sc.nextInt();
b1.show();
}
System.out.println("Enter Breadth :");
}
b=sc.nextInt();

public void AreaOfRec()

System.out.println("Area of
Rectangle:"+(3.14*r*r));

}
Practical No 20

package p1;
public class A
{
public void dis()
System.out.println("\n This is Pack1");
}
}
package p2;
public class B
{
public void show()
{
system.out.println("\n This is Pack2");
}
}
import p1.A;
import p2.A;

class Packages
{
public static void main(string args[])
{
System.out.println("This is Main Class !!");
A a1=new A();
a1.dis()
B b1=new B();
b1.show();
}
}
PRACTICAL NO 18
class student System.out.println("Rollno of
student:"+rollno);
{
System.out.println("Total marks of
String name;
student:"+t);
int rollno;
}
}
void gets(String n, int r)
}
class prc18
name=n;
{
rollno=r;
public static void main(String args*+)
}
{

} result R =new result();

class test extends student R.gets("Pratham",2245);

{ R.getT(20,20);

int m1,m2; R.display();

void getT(int x,int y) }

{ }

m1=x;
m2=y;
}
}
class result extends test
{
int t;
void display()
}
t=m1+m2;
System.out.println("Name of
student:"+name);
Practical 23,24,25

import java.util.*; import java.util.*;


import java.lang.Exception;
class throwEx extends Exception import java.lang.Exception;
{ class throwEx1 extends Exception
throwEx(String msg) {
{ throwEx1(String msg)
super(msg); {
} super(msg);
} }
class prc23 }
{ class prc24
public static void main(String args[]) {
{ public static void main(String args[])
System.out.println("Enter your Age:"); {
Scanner sc=new Scanner(System.in); System.out.println("Enter Password:");
int age=sc.nextInt(); Scanner sc=new Scanner(System.in);
try String passw=sc.next();
{ try
if(age<0) {
throw new throwEx("Your Age is negative"); if(!passw.equals("CD2"))
else throw new throwEx1("password is Incorrect");
System.out.println("Correct Age"); else
} System.out.println("Correct Password");
catch(throwEx e) }
{ catch(throwEx1 e)
System.out.println(e.getMessage()); {
System.out.println(e.getMessage());
} }
finally finally
{ {
System.out.println("I will always execute ...As I am System.out.println("i am Finally Block");
Finally Block"); }
} }
} }
}
Practical 26 ,27

import java.util.*;
import java.lang.Exception; import java.util.*;
class prc26 import java.lang.Exception;
{ class NotMatchException extends Exception
static void fun() throws ArithmeticException {
{ NotMatchException(String msg)
int a,b,c; {
a=10; super(msg);
b=0; }
try }
{ class prc27
c=a/b; {
} public static void main(String args[])
catch(ArithmeticException e) {
{ System.out.println("Enter String:");
System.out.println("Exception caught!"+e); Scanner sc=new Scanner(System.in);
} String s1=sc.next();
} try
public static void main(String args[]) {
{ if(!s1.equals("INDIA"))
fun(); throw new NotMatchException("incorrect String");
} else
} System.out.println("Correct String");
}
catch(NotMatchException e)
{
System.out.println(e.getMessage());
}
}
}
Practical No 28

import java.applet.*;
import java.applet.*; import java.awt.*;
import java.awt.*;
public class Ex1 extends Applet public class AL extends Applet
{ {
String s1;
String s1;
public void init()
public void init()
{
{ System.out.println("Initializing an
s1=getParameter("Name"); applet");
} }
public void paint(Graphics g) public void start()
{ {
g.drawString(s1,50,30); System.out.println("Starting an
} applet");
} }
public void stop()
/*<applet code="Ex1" width=500 height=500> {
System.out.println("Stoping an
applet");
<param name="Name" value="Welcome to the
}
World of Applet"> public void destroy()
{
</applet>*/ System.out.println("Destroying an
applet");
}
public void paint(Graphics g)
{
g.drawString("Welcome",50,30);
}
}
/*<applet code="AL" width=500
height=500>

</applet> */
import java.applet.*;
import java.awt.*;
public class Voting extends Applet
{
String s1;

public void init()


{
s1=getParameter("Age");
}
public void paint(Graphics g)
{
int Age=Integer.parseInt(s1);
if(Age<18)
g.drawString("Not Eligible For Voting",50,40);
else
g.drawString("Eligible For Voting",50,40);
}
}

/* <applet code="Voting" width=500 height=500>

<param name="Age" value="20">

</applet>*/

You might also like