Академический Документы
Профессиональный Документы
Культура Документы
Output:-
{
public static void main(String[] args)
{
int i = 10;
int j = 10;
i++;
j++;
System.out.println("i = " + i);
System.out.println("j = " + j);
int k = i++;
int l = ++j;
System.out.println("k = " + k);
System.out.println("l = " + l);
}
}
Output:-
i = 11
j = 11
k = 11
l = 12
/*Swap Numbers Java Example This Swap Numbers Java Example shows how
to swap value of two numbers using java.*/
Output:-
Before Swapping
Value of num1 is :10
Value of num2 is :20
After Swapping
Value of num1 is :20
Value of num2 is :10
/*Reverse Number using Java This Java Reverse Number Example shows how to
reverse a given number.*/
//original number
int number = 1234;
int reversedNumber = 0;
int temp = 0;
while(number > 0)
{
//use modulus operator to strip off the last digit
temp = number%10;
//create the reversed number
reversedNumber = reversedNumber * 10 + temp;
number = number/10;
}
//output the reversed number
System.out.println("Reversed Number is: " + reversedNumber);
}
}
Output:-
Output:-
Run 1
Select your choice
M-->Madras
B--->Bombay
C---> Calcutta
choice--->
m
Madras : Booklet 5
Run 2
}
else
{
odd +=1;
}
}
System.out.println("Even numbers:"+even+ "odd numbers:"+odd);
}
}
Output:-
}
}
catch(Exception e)
{
System.out.println("Error in input");
}
System.out.println("you have entered...");
System.out.println(string);
Output:-
Enter a string
bharat
you have entered...
bharat
/* Compare Two Numbers Java Example This Compare Two Numbers Java
Example shows how to compare two numbers using if else if statements. */
Output:-
class Rectangle
{
int length,width;
void getData(int x,int y)
{
length=x;
width=y;
}
int rectArea()
{
int area=length*width;
return(area);
}
}
class Rectarea
{
public static void main(String args[])
{
int area1,area2;
Rectangle rect=new Rectangle();
Rectangle rect2=new Rectangle();
rect.length=15;
rect.width=10;
area1=rect.length*rect.width;
rect2.getData (20,12);
area2=rect2.rectArea();
System.out.println("Area1="+area1);
System.out.println("Area2="+area2);
}
Output:-
Area1=150
Area2=240
/*Applications of Constructors*/
class ConstructorsExample {
int length,width;
public ConstructorsExample(int x,int y)
{
length=x;
width=y;
}
int rectArea()
{
return(length*width);
}
}
class RectangleArea
{
public static void main(String args[])
{
ConstructorsExample con=new ConstructorsExample(15,10);
int area1=con.rectArea();
System.out.println("Area1="+area1);
}
}
Output:-
Area1=150
/* Application of single Inheritance*/
class Room
{
int length;
int breadth;
Room(int x,int y)
{
length= x;
breadth = y;
}
int area()
{
return (length *breadth);
}
}
class BedRoom extends Room
{
int height;
BedRoom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return (length *breadth*height);
}
}
public class InherTest {
public static void main(String args[])
{
BedRoom room1 =new BedRoom(14,12,10);
int area1=room1.area();
int volume1=room1.volume();
System.out.println("Area1="+area1);
System.out.println("Volume1="+volume1);
}
}
Output:-
Area1=168
Volume1=1680
/*Illustration of method overloading*/
class Super
{
int x;
Super(int x)
{
this.x=x;
}
void display()
{
System.out.println("Super x="+x);
}
}
class Sub extends Super
{
int y;
public Sub(int x,int y)
{
super(x);
this.y=y;
}
void display()
{
System.out.println("Super x="+x);
System.out.println("Sub y="+y);
}
}
public class Overridetest {
Output:-
Super x=100
Sub y=200
/*Implementing Multiple Inheritance*/
class Student
{
int rollNumber;
void getNumber(int n)
{
rollNumber=n;
}
void putNumber()
{
System.out.println("Roll no:"+rollNumber);
}
}
class Test extends Student
{
float part1,part2;
void getMarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putMarks()
{
System.out.println("Marks Obtained");
System.out.println("Part 1="+part1);
System.out.println("Part 2="+part2);
}
}
interface Sports
{
float sportWt=6.0F;
}
class Results extends Test implements Sports
{
float total;
public void putWt()
{
System.out.println("Sports Wt="+sportWt);
}
void display()
{
total =part1+part2+sportWt;
putNumber();
putMarks();
putWt();
System.out.println("Total Score="+total);
}
}
public class MultipleInheritance
{
public static void main(String args[])
{
Output:-
Roll no:1234
Marks Obtained
Part 1=27.5
Part 2=33.0
Sports Wt=6.0
Total Score=66.5
/* Demonstrate method overloading.*/
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
void test(int a) {
System.out.println("a: " + a);
}
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}
Output:-
No parameters
a: 10
a and b: 10 20
double a: 123.2
Result of ob.test(123.2): 15178.240000000002
/* HANDLING EXCEPTIONS IN JAVA */
Output:-
}
catch(ArithmeticException e)
{
System.out.println("Division By Zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Arrray Index Error");
}
catch(ArrayStoreException e)
{
System.out.print("wrong Data type");
}
finally
{
System.out.println("finally block Entered");
}
int y=a[1]/a[0];
System.out.println("y="+y);
}
}
Output:-
Output:-
Thread executed!
/*Implementing ‘Runnable’ Interface*/
Output:-
Thread executed!
/*using Thread Methods yield (), stop (), sleep ().*/
}
System.out.println("exit form thread A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
}
System.out.println("exit form thread B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
}
catch (Exception e)
{
}
}
System.out.println("exit form thread B");
}
}
}
Output:-
Start Thread A
Start Thread B
Start Thread C
End of Main Method
from Thread C:k=1
from Thread B:j=1
from Thread B:j=2
from Thread B:j=3
from Thread A:i=1
from Thread A:i=2
from Thread A:i=3
from Thread A:i=4
from Thread A:i=5
exit form thread A
exit form thread B
from Thread C:k=2
exit form thread B
from Thread C:k=3
exit form thread B
from Thread C:k=4
exit form thread B
from Thread C:k=5
exit form thread B
/*Drawing Lines and Rectangles Using Applet In Graphics Programming*/
import java.applet.Applet;
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);
}
}
Output:-
/*Using Control Loops in Applets*/
import java.applet.*;
import java.applet.*;
import java.awt.Graphics;
public class Controlloop extends Applet
{
public void paint(Graphics g)
{
for(int i=0;i<=4;i++)
{
if((i%2) == 0)
Output:-