Java Prog
Java Prog
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
Output:
15
5
50
2
0
/*** Write a java program to check whether the given year is leap or not ******/
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args){
int year;
System.out.println("Enter an Year :: ");
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
Output:
Enter an Year ::
2020
else
System.out.println(num3+" is the largest Number");
}
}
Output:
20 is the largest Number
/*** Write a java program to find the reverse of a given number ****/
class ReverseNumberDemo
int num=123456789;
while( num != 0 )
num = num/10;
Output:
public class PrimeExample{
public static void main(String args[]){
int i,m=0,flag=0;
int n=3;//it is the number to be checked
m=n/2;
if(n==0||n==1){
System.out.println(n+" is not prime number");
}else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
if(flag==0) { System.out.println(n+" is prime number"); }
}//end of else
}
Output:
3 is prime number
/*** Write a java program to print Fibonacci Series ********/
class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
Output:
0 1 1 2 3 5 8 13 21 34
/*** Write a java program to check whether the given number is palindrome or
not ****/
class PalindromeExample{
int r,sum=0,temp;
temp=n;
while(n>0){
sum=(sum*10)+r;
n=n/10;
if(temp==sum)
else
System.out.println("not palindrome");
Output:
palindrome number
/*** Write a java program on switch case to display month*******/
Output:
7 - July
/*** Write a java program to print the elements of a given array *****/
//Initialize array
Output:
1 2 3 4 5
/*** Write a java program to find the sum of elements in the given array ****/
//Initialize array
int sum = 0;
Output:
//Initialize array
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println();
//Displaying elements of array after sorting
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output:
52871
12578
/*** Write a java program on matrix addition *******/
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(c[i][j]+" ");
System.out.println();//new line
}}
Output:
268
486
469
/*** Write a java program on matrix multiplication *********/
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
System.out.print(c[i][j]+" ");
System.out.println();//new line
}}
Output:
666
12 12 12
18 18 18
/*** Write a java program to read the data from the keyboard ******/
import java.util.Scanner;
sc.close();
Output:
Enter a number: 89
class A{
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
Output: sonoo
jaiswal
abc
/*** Write a java program on Constructors ********/
class Student4{
int id;
String name;
id = i;
name = n;
s1.display();
s2.display();
Output:
111 Karan
222 Aryan
/*** Write a java program on method Overloading *******/
class Adder{
class TestOverloading1{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Output:
22
33
/*** Write a java program on single inheritance ********/
class Employee{
float salary=40000;
int bonus=10000;
output:
class Shape {
System.out.println("Inside display");
System.out.println("Inside area");
System.out.println("Inside volume");
cube.display();
cube.area();
cube.volume();
Output:
Inside display
Inside area
Inside volume
/*** Write a java program on Hierarchical inheritance ********/
class Animal{
void eat(){System.out.println("eating...");}
void bark(){System.out.println("barking...");}
void meow(){System.out.println("meowing...");}
class TestInheritance3{
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
meowing...
eating...
/*** Write a java program on Multiple inheritance ********/
interface Printable{
void print();
interface Showable{
void print();
obj.print();
Output:
Hello
/*** Write a java program on Hybrid inheritance ********/
interface read
{
public void reading();
}
interface work
{
public void working();
}
emp.hoursWorked = 12;
emp.firstName = "John";
emp.lastName = "Smith";
emp.age = 27;
System.out.println(emp.comuptePay());
}
}
Output:
60000
/*** Write a java program on method overriding********/
class Bank{
class Test2{
Output:
//save as Simple.java
package mypack;
System.out.println("Welcome to package");
Output:
Welcome to package
/*** Write a java program on exception handling *********/
try{
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
Output:
statements **/
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
Output:
System.out.println("thread is running...");
t1.start();
Output:
thread is running...
/*** Write a java program to create a thread by implementing Runnable interface
***/
System.out.println("thread is running...");
t1.start();
Output:
thread is running...
/*** Write a java program on thread Priorities *******/
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
Output:
class Customer{
int amount=10000;
if(this.amount<amount){
System.out.println("Less balance; waiting for deposit...");
try{wait();}catch(Exception e){}
}
this.amount-=amount;
System.out.println("withdraw completed...");
}
class Test{
public static void main(String args[]){
final Customer c=new Customer();
new Thread(){
public void run(){c.withdraw(15000);}
}.start();
new Thread(){
public void run(){c.deposit(10000);}
}.start();
}
}
Output:
going to withdraw...
Less balance; waiting for deposit...
going to deposit...
deposit completed...
withdraw completed
/*** Write a java program on JDBC *********/
import java.sql.*;
class OracleCon{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
Statement stmt=con.createStatement();
while(rs.next())
con.close();