Java Programming
Java Programming
Java Programming
Programming Section
Vocti Chakma
9/13/2015
Page 2
Program:
public class Subtraction {
public static void main(String arg[])
{
int a=10; int b=20;
int c=a-b;
System.out.println("Subtraction="+c);
}
}
OUTPUT:
Subtraction=-10
Page 3
3*Write a Java program for multiplication two numbers.
Program:
public class Multiplication {
public static void main(String arg[])
{
int a,b,c;
a=100;
b=20;
c=a*b;
System.out.println("Multiplication is="+c);
}
}
OUTPUT:
Multiplication=2000
4* Write a Java program for Division two numbers.
Program:
public class Division {
public static void main(String arg[])
{
int a,b,c;
a=1000;
b=220;
c=a/b;
System.out.println("Division is="+c);
}
}
OUTPUT:
Division is= 4
Page 4
5*Write a program for modulus two numbers.
Program:
public class Modulas {
public static void main(String arg[])
{
int a,b,c;
a=10;
b=02;
c=a%b;
System.out.println("Modulus is="+c);
}
}
OUTPUT:
Modulus is=0.
6*Write a Java program for adding two number. Numbers will input by user.
Program:
import java.util.Scanner;
public class Summation1 {
public static void main(String arg[])
{
Scanner sn= new Scanner(System.in);
int a,b,c;
System.out.println("Enter The Value of a & b:");
a=sn.nextInt();
b=sn.nextInt();
c=a+b;
System.out.println("Summation is="+c);
}
}
OUTPUT:
Enter The Value of a & b:
10
5
Summation is=15
Page 5
7* Write a Java program for Subtraction two numbers. Numbers will input by user..
Program:
import java.util.Scanner;
public class Subtraction1 {
public static void main(String arg[])
{
Scanner sn= new Scanner(System.in);
int a,b,c;
System.out.println("Enter The Value of a & b:");
a=sn.nextInt();
b=sn.nextInt();
c=a-b;
System.out.println("Subtraction is="+c);
}
}
OUTPUT:
Enter The Value of a & b:
20
10
Subtraction is=10.
8*Write a Java program for multiplication two numbers. Numbers will input by user.
import java.util.Scanner;
public class Multiplication1 {
public static void main(String arg[])
{
Scanner sn= new Scanner(System.in);
int a,b,c;
System.out.println("Enter The Value of a & b:");
a=sn.nextInt();
b=sn.nextInt();
c=a*b;
System.out.println("Multiplication is="+c);
}
}
OUTPUT:
Enter The Value of a & b:
10
Page 6
2
Multiplication=20
9*Write a Java program for Division two numbers. Numbers will input by user..
Program:
import java.util.Scanner;
public class Division1 {
public static void main(String arg[])
{
Scanner sn= new Scanner(System.in);
int a,b,c;
System.out.println("Enter The Value of a & b:");
a=sn.nextInt();
b=sn.nextInt();
c=a/b;
System.out.println(Division is="+c);
}
}
OUTPUT:
Enter The Value of a & b:
10
2
Division is=5
10*Write a Java program for modulus two numbers. Numbers will input from user.
Program:
import java.util.Scanner;
public class Modulas1 {
public static void main(String arg[])
{
Scanner sn= new Scanner(System.in);
int a,b,c;
System.out.println("Enter The Value of a & b:");
a=sn.nextInt();
b=sn.nextInt();
c=a%b;
System.out.println("Modulus is="+c);
}
}
Page 7
OUTPUT:
Enter The Value of a & b:
10
2
Modulus is=0.
Page 8
11*Write a Java program for find out large number from 2 numbers. Numbers Will input by
user.
Program:
import java.util.Scanner;
public class Maximum {
public static void main(String arg[])
{
Scanner sn=new Scanner(System.in);
int a,b;
System.out.println("Enter the value of a & b:");
a=sn.nextInt();
b=sn.nextInt();
if(a>b)
{
System.out.println("Maximum number is="+a);
}
else
System.out.println("Maximum number is="+b);
}
}
OUTPUT:
Enter The Value of a & b:
12
54
Maximum number is =54
Page 9
12*Write a Java program for find out small number from 2 numbers.
Program:
Import java.util.Scanner;
public class Minimum {
public static void main(String arg[])
{
Scanner sn=new Scanner(System.in);
int a,b;
System.out.println("Enter the value of a & b:");
a=sn.nextInt();
b=sn.nextInt();
if(a>b)
{
System.out.println("Minimum number is="+b);
}
else
System.out.println("Minimum number is="+a);
}
}
OUTPUT:
Enter The Value of a & b:
11
10
Minimum number is=10
Page 10
13*Write a java program for find out maximum number from 3 numbers.
Program:
import java.util.Scanner;
public class maximum3 {
public static void main(String arg[])
{
Scanner sn=new Scanner(System.in);
int a,b,c;
System.out.println("Enter the value of a & b:");
a=sn.nextInt();
b=sn.nextInt();
c=sn.nextInt();
if(a>b && a>c)
{
System.out.println(a+" is largest than " +b +" and "+c);
}
else if(b>a && b>c)
System.out.println(b+" is the largest number than"+a+"and"+c);
else
System.out.println(c+" is largest number");
}
}
OUTPUT:
Enter The Value of a & b:
10
14
17
17 is largest from 10 & 14.
Page 11
14*Write a Java program for find out minimum number from 3 numbers.
Program:
import java.util.Scanner;
public class minimum3 {
public static void main(String arg[])
{
Scanner sn=new Scanner(System.in);
int a,b,c;
System.out.println("Enter the value of a b & c:");
a=sn.nextInt();
b=sn.nextInt();
c=sn.nextInt();
if(a<b && a<c)
{
System.out.println(a+" is smallest than " +b +" and "+c);
}
else if(b<a && b<c)
System.out.println(b+" is the smallest number than"+a+"and"+c);
else
System.out.println(c+" is smallest number");
}
}
OUTPUT:
Enter The Value of a b & c:
10
42
74
10 is smallest than 42 and 74.
Page 12
Page 13
System.out.println("Enter Your Value For Division:");
b=sn.nextInt();
c=sn.nextInt();
d=b/c;
System.out.println("Division is:" + d);
}
else if("5".equals(a))
{
System.out.println("Enter Your Value For Modulus:");
b=sn.nextInt();
c=sn.nextInt();
d=b%c;
System.out.println("Modulas is:" + d);
}
else
{
System.out.println("INVALID INPUT!!!");
}
}
}
OUTPUT:
1.Addition 2.Subtraction 3.Multiplication 4.Division 5.Modulus.
1
Enter Your Value For Summation:
10
47
Summation is=57
2*Write a Java program for Addition, Subtraction, Multiplication, Division & Modulus By Using
Switch Case Statement.
Program:
package Vocti;
import java.util.Scanner;
public class Switch {
public static void main(String args[])
{
Scanner sn=new Scanner(System.in);
int a;
int b,c,d;
Page 14
System.out.println("Enter Your Choice.");
System.out.println("1.Addition 2.Subtraction 3.Multiplication 4.Division 5.Modulus");
a=sn.nextInt();
switch(a)
{
case 1:
System.out.println("Enter Your Value For Summation:");
b=sn.nextInt();
c=sn.nextInt();
d=b+c;
System.out.println("Summation is:" + d);
break;
case 2:
System.out.println("Enter Your Value For Subtraction:");
b=sn.nextInt();
c=sn.nextInt();
d=b-c;
System.out.println("Subtraction is:" + d);
break;
case 3:
System.out.println("Enter Your Value For Multiplication:");
b=sn.nextInt();
c=sn.nextInt();
d=b*c;
System.out.println("Multiplication is:" + d);
break;
case 4:
System.out.println("Enter Your Value For Division:");
b=sn.nextInt();
c=sn.nextInt();
d=b/c;
System.out.println("Division is:" + d);
break;
case 5:
System.out.println("Enter Your Value For Modulus:");
b=sn.nextInt();
c=sn.nextInt();
d=b%c;
System.out.println("Modulas is:" + d);
break;
default:
System.out.println("INVALID CHOICE!!");
}
}
}
Page 15
Page 16
OUTPUT:
1.Addition 2.Subtraction 3.Multiplication 4.Division 5.Modulus.
2
Enter Your Value For Subtraction:
15
12
Subtraction is=3.
Page 17
Use Of Loop.
1*Write a Java program for print number using loop.
Program:
package Vocti;
public class print {
public static void main(String arg[])
{
int i;
for(i=1;i<=10;i++)
{
System.out.print( "\t "+i);
}
}
}
OUTPUT:
1 2 3 4 5 7 8 9 10
2* Write a Java program for print number using for loop. Number will decrease..
Program:
package Vocti;
public class print {
public static void main(String arg[])
{
int i;
for(i=1;i>=10;i++)
{
System.out.print( "\t "+i);
}
}
}
OUTPUT:
10 9 8 7 6 5 4 3 2 1
Page 18
3* Write a Java program for print prime number using for loop.
Program:
package Vocti;
import java.util.Scanner;
public class prime {
public static void main(String args[])
{
int a,i;
int n;
Scanner sn=new Scanner(System.in);
System.out.println("Enter Your Number:");
n=sn.nextInt();
for(a=2;a<=n;a++)
{
for(i=2;i<a;i++)
{
if(a%i==0)
break;
}
if(a==i)
{
System.out.println(" " + i+" ");
}
}
}
}
OUTPUT:
Enter Your Value: 10
2
3
5
7
Page 19
10
Page 20
6*write a Java program for print number using do while loop.
Program
Package javaapp
public class dowhile {
public static void main(String args[])
{
int a=1;
do
{
a++;
System.out.println("\t"+a);
}
while(a<=10);
}
}
OUTPUT:
1 2 3 4 5 6 7 8 9 10
Page 21
Even or Odd
4*Write a Java program for print Even or Odd number.
Program:
package Vocti;
import java.util.Scanner;
public class Even {
public static void main(String args[])
{
Scanner sn=new Scanner(System.in);
int a;
System.out.println("Please Input Your Number:");
a=sn.nextInt();
if(a%2==0)
{
System.out.println("Your Number is Even.");
}
else
{
System.out.println("Your Number is Odd");
}
}
}
OUTPUT:
Please Input Your Number:
10
Your Number is Even.
Page 22
Use Of Array
1*Write a simple Java program for array example using for loop.
Program:
package Vocti;
public class Array_Example {
public static void main(String arg[])
{
int mark[]={23,54,34,10,15,30,25};
for(int i=0;i<=6;i++)
{
System.out.println("Mark["+i+"]="+mark[i]);
}
}
}
OUTPUT:
Mark[0]=23
Mark[1]=54
Mark[2]=34
Mark[3]=10
Mark[4]=15
Mark[5]=30
Mark[6]=25
2*Write a Java program for print array example without using loop.
Program:
package Vocti;
public class Array_Example2 {
public static void main(String agrs[])
{
int Roll[]={1,2,3,4,5};
System.out.println("Roll[0]="+Roll[0]);
System.out.println("Roll[1]="+Roll[1]);
System.out.println("Roll[2]="+Roll[2]);
System.out.println("Roll[3]="+Roll[3]);
}
}
Page 23
OUTPUT:
Roll[0]=1
Roll[1]=2
Roll[2]=3
Roll[3]=4
Roll[4]=5
3*Write a Java program for print string in array..
Program:
package Vocti;
public class Array_Atring {
public static void main(String args[])
{
//String name[];
//String name = name[];
String name[]={"Pranoy","Vocti","Gyanna"};
System.out.println("Name is[0]="+name[0]);
System.out.println("Name is[1]="+name[1]);
System.out.println("Name is[2]="+name[2]);
}
}
OUTPUT:
Name is[0]=Pranoy
Name is[1]=Vocti
Name is[2]=Gyanna
4*Write a Java program for print number in array. Number will input by user.
Program:
package Vocti;
import java.util.Scanner;
public class Inpu_Array {
public static void main(String arg[])
{
Scanner sn=new Scanner(System.in);
int i,n;
System.out.println("Enter Your Value:");
n=sn.nextInt();
int roll[]=new int[n];
//input intu array
System.out.println("Enter values:");
for(i=0;i<n;i++)
Page 24
{
roll[i]=sn.nextInt();
}
//print array with index
System.out.println("Values are:");
for(i=0;i<n;i++)
{
System.out.println("Roll["+ i +"]=" + roll[i]);
}
}
}
OUTPUT:
Enter Your Value:
5
Enter values:
10 12 14 15 14
Values are:
Roll[0]=10
Roll[1]=12
Roll[2]=14
Roll[3]=15
Roll[4]=14
5*Write a Java Program for two dimensional Array.
Program:
package Vocti;
import java.util.Scanner;
public class two_dimentional {
public static void main(String args[]) {
Scanner sn = new Scanner(System.in);
int i, j;
System.out.println("Enter Values:");
int roll[][] = new int[3][3];
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
roll[i][j] = sn.nextInt();
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
System.out.println("Roll[" + i + "][" + j + "]=" + roll[i][j]);
Page 25
}
}
//} catch (Exception e) {
// System.out.println("Error " + e);
}
}
Page 26
OUTPUT:
Enter Values:
11 12 13 14 15 16 17 18 19
Roll[0][0]=11
Roll[0][1]=12
Roll[0][2]=13
Roll[1][0]=14
Roll[1][1]=15
Roll[1][2]=16
Roll[2][0]=17
Roll[2][1]=18
Roll[2][2]=19
Page 27
Exception Handling
1*Write a Java program for Exception handling.
Program:
package Vocti;
//import java.util.Scanner;
public class Exeption {
public static void main(String args[]) {
Scanner sn = new Scanner(System.in);
int i, j;
try {
System.out.println("Enter Values:");
int roll[][] = new int[3][3];
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
roll[i][j] = sn.nextInt();
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
System.out.println("Roll[" + i + "][" + j + "]=" + roll[i][j]);
}
}
} catch (Exception e) {
System.out.println("Error " + e);
}
}
}
OUTPUT:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot
find symbol
symbol: class Scanner
location: class Vocti.Exeption
at Vocti.Exeption.main(Exeption.java:17)
Java Result: 1
Page 28
Page 29
System.out.println("Largest Number:");
if(no1>no2)
{
System.out.println(no1 + " is grether than "+no2);
}
else
{
System.out.println(no2+" is Grather than "+no1);
}
System.out.println("Smallest number:");
{
if(no1<no2)
//System.out.println("Smallest Number");
{
System.out.println(no1+"is less than"+no2);
}
else
{
System.out.println(no2+"is less than"+no1);
}
}
//return 0;
return 0;
}
double sum(double a1)
{
if(a1%2==0)
// ;
{
System.out.println("Even Number:");
System.out.println(a1+" is a even number");
}
else
{
System.out.println("Odd Number:");
System.out.println(a1+"is odd number");
}
return 0;
}
Page 30
}
public class overloading {
//private static double a1;
public static void main(String arg[])
{
//over s=new over();
Scanner sn=new Scanner (System.in);
System.out.println("Enter The Value Of a & b:");
int a=sn.nextInt();
int b=sn.nextInt();
System.out.println("Enter The value of no1 & no2:");
float no1=sn.nextFloat();
float no2=sn.nextFloat();
Page 31
Result is:1
Largest Number:
8.0 is Grather than 5.0
Smallest number:
5.0 is less than8.0
Odd Number:
41.0 is odd number
2* Write a Java program for prime & find out large with overloading method.
Program:
package Object_Oriented;
import java.util.Scanner;
class over1{
int count=0;
int show(int n)
{
{
int count=0;
for(int i=1;i<n;i++)
{
count++;
}
//return 0;
}
if(count<2)
{
System.out.println(n+ "is a prime number");
}
if(count>2)
{
System.out.println(n+"is not a prime number");
}
return 0;
}
int show(int a,int b,int c)
{
if((a>b) && (a>c))
{
System.out.println(a+"is large than "+b+"and"+c);
}
else if((b>a) && (b>c))
{
System.out.println(b +"is large than " +a+"and"+c);
}
else{
Page 32
System.out.println(c +"is a large number.");
}
return 0;
}
}
public class Overlodding2 {
public static void main(String args[])
{
Scanner sn=new Scanner(System.in);
System.out.println("Enter Your Number:");
int n=sn.nextInt();
System.out.println("Enter The Value Of a b c:");
int a=sn.nextInt();
int b=sn.nextInt();
int c=sn.nextInt();
over1 s=new over1();
s.show(n);
s.show(a, b, c);
}
}
OUTPUT:
Enter Your Number:
47
Enter The Value Of a b c:
21 23 22
47 is a prime number
23 is large than 21and 22
Page 33
Inheritance
1*Write a Java program using single Inheritance.
Program:
package Inheritance;
import java.util.Scanner;
class Student{
String Roll;
String Name;
}
class Result extends Student
{
float Mark;
void GetData(String x,String y,float z)
{
Roll=x;
Name=y;
Mark=z;
}
void Display()
{
System.out.println("Roll is :" +Roll);
System.out.println("Name is :" +Name);
System.out.println("Mark is :" +Mark);
}
}
public class Inherit {
public static void main(String arg[])
{
Scanner sn=new Scanner(System.in);
System.out.println("Enter Your Roll:");
String x=sn.nextLine();
System.out.println("Enter Your Name:");
String y=sn.nextLine();
System.out.println("Enter Your Mark:");
float z=sn.nextFloat();
Result R= new Result();
R.GetData(x, y, z);
R.Display();
}
}
Page 34
OUTPUT:
Enter Your Roll:
633518
Enter Your Name:
Vocti
Enter Your Mark:
58
Roll is :633518
Name is :Vocti
Mark is :58.0
2*Write a Java program for multi level inheritance.
Program:
package Inheritance;
/**
*
* @author vocti
*/
class Student1{
int Roll;
String Name;
void GetData()
{
Roll=633518;
Name="Vocti";
}
void Didplay()
{
System.out.println("Inside Class Student");
System.out.println("Roll is="+Roll);
System.out.println("Name is="+Name);
}
}
class Exam extends Student1
{
float Mark;
void GetData()
{
Mark=80.75f;
}
void Didplay()
{
System.out.println("Inside Exam Class");
System.out.println("Mark is:"+Mark);
}
}
class result extends Exam
Page 35
{
Student1 s=new Student1();
Exam e=new Exam();
void GetData()
{
s.GetData();
e.GetData();
}
void Display()
{
s.Didplay();
e.Didplay();
}
}
public class Multiple_inh {
public static void main(String arg[])
{
result R =new result();
R.GetData();
R.Display();
}
}
OUTPUT:
Inside Class Student
Roll is=633518
Name is=Vocti
Inside Exam Class
Mark is:80.75
3*Write a Java program use hierarchical inheritance.
Program:
package Inheritance;
/**
*
* @author vocti
*/
class Students{
int Roll;
String Name;
void GetName()
{
Name="Vocti";
System.out.println("Name is="+Name);
}
Page 36
}
class Academic extends Students
{
int Roll;
void GetRoll()
{
Roll=633518;
System.out.println("Roll is="+Roll);
}
}
class Result1 extends Students
{
float Mark;
void GetMark()
{
Mark =80.5f;
System.out.println("Mark is:"+Mark);
}
}
public class Hierar {
public static void main(String arg[])
{
Academic A =new Academic();
A.GetRoll();
Students S=new Students();
S.GetName();
Result1 R=new Result1();
R.GetMark();
}
}
OUTPUT:
Roll is633518
Name is=Vocti
Mark is:80.5
Page 37
Abstract
1*Write a Java program use Abstract.
Program:
abstract class Student
{
public int Roll;
public String Name;
abstract void GetData();
}
class Result extends Student
{
float Mark;
void GetData()
{
Roll=633518;
Name="Vocti";
Mark=52.2f;
}
void Display()
{
System.out.println("Roll is : "+Roll);
System.out.println("Name: "+Name);
System.out.println("Mark is : "+Mark);
}
}
public class AbstractClassExample {
public static void main(String arg[])
{
Result R=new Result();
R.GetData();
R.Display();
}
}
OUTPUT:
Roll is : 633518
Name: Vocti
Mark is : 52.2
Page 38
Interface
1*Write a Java program use interface.
Program:
interface Student
{
static final int Roll=633518;
static final String Name="Vocti";
void Display();
}
class Result implements Student
{
public float Mark;
public void Display()
{
System.out.println("Roll is: "+Roll);
System.out.println("Name is: "+Name);
}
public void GrtMark()
{
Mark=87.0f;
}
public void Show()
{
System.out.println("Mark is: "+Mark);
}
}
public class InterfaceExample {
public static void main(String arg[])
{
Result R=new Result();
R.GrtMark();
R.Display();
R.Show();
}
}
OUTPUT:
Roll is: 633518
Name is: Vocti
Mark is: 87.0
Page 39
Multithread
1*Write a Java program use Multithread.
Program:
package MultiThread;
class Super extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("\t From Thread A: i = "+i);
}
System.out.println("Exit From A ");
}
}
class Sub1 extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("From Thread B: j= "+j);
}
System.out.println("Exit From B ");
}
}
class Sub2 extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\t From Thread C : k = "+k);
}
}
}
public class ThreadTest {
public static void main(String arg[])
{
Page 40
Super threadSuper=new Super();
Sub1 threadSub1=new Sub1();
Sub2 threadSub2=new Sub2();
System.out.println("Start thread i");
threadSuper.start();
System.out.println("Start thread j");
threadSub1.start();
System.out.println("Start thread k");
threadSub2.start();
}
}
OUTPUT:
Start thread i
Start thread j
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 From A
Start thread k
From Thread C : k = 1
From Thread C : k = 2
From Thread C : k = 3
From Thread C : k = 4
From Thread C : k = 5
From Thread B: j= 1
From Thread B: j= 2
From Thread B: j= 3
From Thread B: j= 4
From Thread B: j= 5
Exit From B
2*Write a Java program use multithread.
Program:
package MultiThread;
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);
Page 41
}
System.out.println("Exit From A ");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("From Thread B: j= "+j);
if(j==3) stop();
}
System.out.println("Exit From 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 from C: ");
}
}
public class ThreadExample {
public static void main(String arg[])
{
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");
Page 42
threadB.start();
System.out.println("Start thread C");
threadC.start();
System.out.println("End of Main thread");
}
}
OUTPUT:
Start thread A
Start thread B
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 From A
Start thread C
End of Main thread
From Thread C : k = 1
From Thread B: j= 1
From Thread B: j= 2
From Thread B: j= 3
From Thread C : k = 2
From Thread C : k = 3
From Thread C : k = 4
From Thread C : k = 5
Exit from C: