Вы находитесь на странице: 1из 27

/*Arithmetic Operators Example This example shows how to use Java

arithmetic operators like + (addition), (subtraction), * (multiplication) and /


(division). */

public class ArithmaticOperatorsExample


{
public static void main(String[] args)
{
System.out.println("Arithmatic operators example :");
int i = 50 + 20;
int j = i - 10;
int k = j * 2;
double l = k / 6;
System.out.println("i = " + i);
System.out.println("j = " + j);
System.out.println("k = " + k);
System.out.println("l = " + l);
}
}

Output:-

Arithmetic operators example :


i = 70
j = 60
k = 120
l = 20.0
/*Increment and Decrement Operators Example
This example shows how to use Java increment operator (++) and
decrement(--) operator.*/

public class IncrementDecrementOperatorExample

{
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.*/

public class SwapElementsExample


{

public static void main(String[] args)


{
int num1 = 10;
int num2 = 20;
System.out.println("Before Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);
//swap the value
swap(num1, num2);
}
private static void swap(int num1, int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
System.out.println("After Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);
}
}

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.*/

public class ReverseNumber {


public static void main(String[] args)
{

//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:-

Reversed Number is: 4321


/* Java Factorial Example This Java Factorial Example shows how to calculate
factorial of a given number using Java.*/

public class NumberFactorial

public static void main(String[] args)


{
int number = 5;

int factorial = number;


for(int i =(number - 1); i > 1; i--)
{
factorial = factorial * i;
}
System.out.println("Factorial of a number is " + factorial);
}
}

Output:-

Factorial of a number is 120


/* Free Flowing Switch Statement ExampleThis example shows how case
statements are executed */

public class Switchexample {


public static void main(String args[])
{
char choice;
System.out.println("Select your choice");
System.out.println("M-->Madras");
System.out.println("B--->Bombay");
System.out.println("C---> Calcutta ");
System.out.println("choice--->");
System.out.flush();
try
{
switch( choice = (char)System.in.read())
{
case 'M':
case 'm':
System.out.println("Madras : Booklet 5");
break;
case 'B':
case 'b':
System.out.println("Bombay : Booklet 9");
break;
case 'C':
case 'c':
System.out.println("Calcutta : Booklet15 ");
break;
default:System.out.println("Invalid Choice
(IC)");
}
}
catch (Exception e)
{
System.out.println("I/O error");
}
}
}
Output:-

Run 1
Select your choice
M-->Madras
B--->Bombay
C---> Calcutta
choice--->
m
Madras : Booklet 5

Run 2

Select your choice


M-->Madras
B--->Bombay
C---> Calcutta
choice--->
M
Madras : Booklet 5
/*Program Explaining if. else statement*/

public class Ifelseexample {


public static void main(String args[])
{
int number[]={50,65,56,71,81};
int even =0,odd=0;
for(int i=0;i< number.length;i++)
{
if((number[i]%2)==0)
{
even +=1;

}
else
{
odd +=1;
}
}
System.out.println("Even numbers:"+even+ "odd numbers:"+odd);
}
}

Output:-

Even numbers:2 odd numbers:3


/* Programming demonstrating while loop */

public class Whiletest


{
public static void main(String args[])
{
StringBuffer string=new StringBuffer();
char c;
System.out.println("Enter a string");
try
{
while((c = (char)System.in.read())!='\n')
{
string.append(c);

}
}
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. */

public class Elseifexample


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

int num1 = 324;


int num2 = 234;
if(num1 > num2)
{
System.out.println(num1 + " is greater than " + num2);
}
else if(num1 < num2){
System.out.println(num1 + " is less than " + num2);
}
else
{
System.out.println(num1 + " is equal to " + num2);
}
}
}

Output:-

324 is greater than 234


/*Applications of classes and objects*/

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 {

public static void main(String args[])


{
Sub s1=new Sub(100,200);
s1.display();
}
}

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[])
{

Results s1=new Results();


s1.getNumber(1234);
s1.getMarks(27.5F,33.0F);
s1.display();
}
}

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);
}

void test(int a, int b) {


System.out.println("a and b: " + a + " " + b);
}

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 */

public class MainClass


{
public static void main(String[] args)
{
int i = 1;
int j = 0;
try
{
System.out.println("Try block entered " + "i = " + i + "j = " + j);
System.out.println(i / j);
System.out.println("Ending try block");
}
catch (ArithmeticException e)
{
System.out.println("Arithmetic exception caught");
}
System.out.println("After try block");
return;
}
}

Output:-

Try block entered i = 1j = 0


Arithmetic exception caught
After try block
/*Program Demonstrating Multiple catch Blocks and Finally Block*/

public class Multiplecatch {

public static void main(String[] args) {


int a[]={5,10};
int b=5;
try{
int x=a[2]/b-a[1];

}
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:-

Arrray Index Error


finally block Entered
y=2
/*Creating A Thread Using the Thread Class */

public class MyThread extends Thread


{

public void run()


{
System.out.println("Thread executed!");
}

public static void main(String[] args)


{
Thread thread = new MyThread();
thread.start();
}
}

Output:-

Thread executed!
/*Implementing ‘Runnable’ Interface*/

public class MyRunnable implements Runnable


{

public void run()


{
System.out.println("Thread executed!");
}
public static void main(String[] args)
{
Thread thread = new Thread(new MyRunnable());
thread.start();
}
}

Output:-

Thread executed!
/*using Thread Methods yield (), stop (), sleep ().*/

class A extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1)yield();
System.out.println("\t from Thread A:i="+i);

}
System.out.println("exit form thread A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{

System.out.println("\t from Thread B:j="+j);


if(j==3) stop();

}
System.out.println("exit form thread B");

}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{

System.out.println("\t from Thread C:k="+k);


if(k==1)
{
try
{
sleep(1000);

}
catch (Exception e)
{

}
}
System.out.println("exit form thread B");

}
}
}

public class Threadexample


{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadc=new C();
System.out.println("Start Thread A");
threadA.start();
System.out.println("Start Thread B");
threadB.start();
System.out.println("Start Thread C");
threadc.start();
System.out.println("End of Main Method");
}
}

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);
}
}

<applet width=300 height=300 code="Linerect.class"> </applet>

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)

g.drawOval(120, i*60+10,50, 50);


else
g.fillOval(120,i*60+10,50,50);
}
}
}

Output:-

Вам также может понравиться