//Constructor
class TestConstructor
{
int a,b;
TestConstructor()
{
System.out.println("Default Constructor!!");
a=10;
b=20;
System.out.println("value of a="+a);
System.out.println("value of b="+b);
}
}
class MainConstructor
{
public static void main(String args[])
{
TestConstructor obj=new TestConstructor();
}
}
//Single Inheritance
class Animal
{
void eat()
{
System.out.println("Eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("Barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
//Super Keyword
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("eating bread...");
}
void bark()
{
System.out.println("barking...");
}
void work()
{
super.eat();
bark();
}
}
class TestSuper2
{
public static void main(String args[])
{
Dog d=new Dog();
d.work();
}
}
//Calculate Area of Circle using Interface
interface Area
{
public double Compute(float x);
}
class circle implements Area
{
public double Compute(float x)
{
return(3.14*x*x);
}
}
class InterfaceTest
{
public static void main(String args[])
{
circle c1=new circle();
Area a1;
a1=c1;
System.out.println("Area of circle is "+a1.Compute(3.0f));
}
}
//Abstract Class
class MyShape
{
void displayArea()
{
System.out.println("this is display area method of class MyShape");
}
}
interface MyImage
{
public void drawImage();
public void inputImage();
}
interface Mycolor
{
public void inputcolor();
}
public class InterfaceTest05 extends MyShape implements MyImage,Mycolor
{
public void inputImage()
{
System.out.println("now we are overring the draw image method interface MyImage");
}
public void drawImage()
{
System.out.println("now we are overring the draw image method interface MyImage");
}
public void inputcolor()
{
System.out.println("now we are overring the input image method of Interface Mycolor MyImage");
}
public void display()
{
System.out.println("disp function of class InterfaceTest05");
}
public static void main(String args[])
{
InterfaceTest05 obj=new InterfaceTest05();
obj.drawImage();
obj.inputImage();
obj.inputcolor();
obj.displayArea();
obj.display();
}
}
//Interface
import java.util.*;
interface Account
{
void set();
void display();
}
interface Person
{
void store();
void disp();
}
public class customer implements Account,Person
{
String Name;
float r,u;
int acc_no;
int balance;
public void set()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the name :");
Name=s.next();
System.out.println("Enter the account number :");
acc_no=s.nextInt();
System.out.println("Enter the balance amount :");
balance=s.nextInt();
System.out.println("Enter the rate :");
r=s.nextInt();
}
public void disp(){
System.out.println("Name :"+Name);
System.out.println("Account Number :"+acc_no);
System.out.println("Balance :"+balance);
u=(balance*r)/100;
System.out.println("The Interest amount :"+u);
}
public void display()
{
System.out.println("Program has started ");
}
public void store()
{
}
public static void main(String args[])
{
customer c=new customer();
c.display();
c.set();
c.store();
c.disp();
}
}
//Abstract Class
abstract class Shape1
{
public abstract float calculateArea();
public void displayArea()
{
System.out.println("Display area of shape class");
}
}
class draw
{
public void drawShape()
{
}
}
class AbstractCircle extends Shape1
{
float radius;
public float calculateArea()
{
return((radius*radius)*(22/7));
}
}
class Shape
{
public static void main(String ss[])
{
AbstractCircle c=new AbstractCircle();
c.radius=10f;
float Area;
c.displayArea();
Area=c.calculateArea();
System.out.println("Area of Circle is"+Area);
}
}
//Package
//info.java file outside the package folder
//main java file to compile and run
import student.details;
class info
{
public static void main(String args[])
{
details d=new details();
d.get();
d.display();
}
}
//details.java file inside the student folder
package student;
public class details
{
public String nm;
public int rollno;
public void get()
{
nm="raj";
rollno=10;
}
public void display()
{
System.out.println("name as the student="+nm);
System.out.println("Rollno="+rollno);
}
}
//B.java file outside the pack folder
//main java file to compile and run
import pack.A;
class B
{
public static void main(String args[])
{
A obj=new A();
obj.msg();
}
}
//A.java file inside the pack folder
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
Exception
//implementation of try catch block
class error2
{
public static void main(String args[])
{
int a=25,b=5,c=0;
try
{
c=a/b;
}
catch(ArithmeticException e)
{
System.out.println("Division by 0 error");
System.out.println("Type of error"+e);
}
System.out.println("The value of a is"+a);
System.out.println("The value of b is"+b);
System.out.println("The value of c is"+c);
}
}
//implementation of try with multiple catch block
import java.util.Scanner;
class error5
{
public static void main(String args[])
{
int a[]={12,13,15,23,44};
int b,c,d;
int i;
try
{
Scanner s =new Scanner (System.in);
System.out.println("Enter the two numbers");
c=s.nextInt();
d=s.nextInt();
b=c/d;
System.out.println("Content of array elements are as follows");
for(i=0;i<5;i++)
System.out.println(a[i]);
}
catch(ArithmeticException e)
{
System.out.println("Division by 0 error");
System.out.println("Type of error"+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Overflow");
System.out.println("Type of error"+e);
}
finally
{
System.out.println("I am always there");
}
}
}
//implementation of try-catch n finally blck
class error3
{
public static void main(String args[])
{
int a[]={12,13,15,23,44};
int i;
try
{
System.out.println("Content of array elements are as follows");
for(i=0;i<7;i++)
System.out.println(a[i]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Overflow");
System.out.println("Type of error"+e);
}
finally
{
System.out.println("I am always there");
}
}
}
//catching coomandline arguments
//try with multiple catch blocks
class error1
{
public static void main(String args[])
{
int i;
try{
System.out.println("Command line arguments are as follows");
for(i=0;i<5;i++)
System.out.println(Integer.parseInt(args[i]));
}
catch(NumberFormatException e)
{
System.out.println("Invalid Data");
System.out.println("Type error"+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Overflow");
System.out.println("Type of error"+e);
}
finally
{
System.out.println("I am always there");
}
}
}
Calculator
import java.util.*;
public class javaprogram
{
public static void main(String args[])
{
float a,b,res;
char choice,ch;
Scanner scan=new Scanner(System.in);
do
{
System.out.print("1.Addition \n");
System.out.print("2.Subtraction \n");
System.out.print("3.Multiplication \n");
System.out.print("4.Division \n");
System.out.print("5 Exit \n");
System.out.print("Enter your choice: ");
choice=scan.next().charAt(0);
switch(choice)
{
case '1' : System.out.print("Enter Two Number: ");
a=scan.nextFloat();
b=scan.nextFloat();
res=a+b;
System.out.print("Result="+res);
break;
case '2' : System.out.print("Enter Two Number: ");
a=scan.nextFloat();
b=scan.nextFloat();
res=a-b;
System.out.print("Result="+res);
break;
case '3' : System.out.print("Enter Two Number: ");
a=scan.nextFloat();
b=scan.nextFloat();
res=a*b;
System.out.print("Result="+res);
break;
case '4' : System.out.print("Enter Two Number: ");
a=scan.nextFloat();
b=scan.nextFloat();
res=a/b;
System.out.print("Result="+res);
break;
case '5' : System.exit(0);
break;
default:System.out.print("Wrong choice!!");
}
System.out.println("\n------------------------\n");
}
while(choice!=5);
}
}
Program to demonstrate java list interface
import java.util.*;
public class collectionDemo
{
public static void main(String args[])
{
List a1=new ArrayList();
a1.add("ZARA");
a1.add("MAHNAZ");
a1.add("ARYAN");
System.out.println("Array List Elements");
System.out.println("\t"+a1);
List l1=new LinkedList();
l1.add("zara");
l1.add("mahnaz");
l1.add("aryan");
System.out.println("Linked list elements");
System.out.println("\t"+l1);
}
}
Program to create your own exception in java
class NumberRangeException extends Exception
{
String msg;
NumberRangeException()
{
msg=new String("ENter a number between 20 and 100");
}
}
public class My_Exception
{
public static void main(String args[])
{
try
{
int x=10;
if(x<20 || x>100)throw new NumberRangeException();
}
catch(NumberRangeException e)
{System.out.println(e);
}
}
}
Accept integer values for a, b and c which are coefficients of quadratic equation. Find the solution of quadratic equation
import java.util.Scanner;
public class Quadratic_Equation
{
public static void main(String[] args)
{
int a, b, c;
double root1, root2, d;
Scanner s = new Scanner(System.in);
System.out.println("Given quadratic equation:ax^2 + bx + c");
System.out.print("Enter a:");
a = s.nextInt();
System.out.print("Enter b:");
b = s.nextInt();
System.out.print("Enter c:");
c = s.nextInt();
System.out.println("Given quadratic equation:"+a+"x^2 + "+b+"x + "+c);
d = b * b - 4 * a * c;
if(d > 0)
{
System.out.println("Roots are real and unequal");
root1 = ( - b + Math.sqrt(d))/(2*a);
root2 = (-b - Math.sqrt(d))/(2*a);
System.out.println("First root is:"+root1);
System.out.println("Second root is:"+root2);
}
else if(d == 0)
{
System.out.println("Roots are real and equal");
root1 = (-b+Math.sqrt(d))/(2*a);
System.out.println("Root:"+root1);
}
else
{
System.out.println("Roots are imaginary");
}
}
}
Accept two n x m matrices. Write a Java program to find addition of these matrices.
import java.io.*;
class MathAddition
{
public static void main(String args[])throws IOException
{
int m,n,i,j;
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the number of rows for matrix");
n=Integer.parseInt(dis.readLine());
System.out.println("Enter the number of columns for matrix");
m=Integer.parseInt(dis.readLine());
int a[][]=new int[n][m];
int b[][]=new int[n][m];
int c[][]=new int[n][m];
System.out.println("Enter elements for first"+n+"x"+m+"matrix");
for(i=0; i < n; i++)
{
for(j=0;j<m;j++)
{
a[i][j]=Integer.parseInt(dis.readLine());
}
}
System.out.println("Enter elements for second"+n+"x"+m+"matrix");
for(i=0;i<n;i++)
{
for(j=0; j < m; j++)
{
b[i][j]=Integer.parseInt(dis.readLine());
}
}
System.out.println("Enter elements for first"+n+"x"+m+"matrix");
for(i=0;i<n;i++)
{
for(j=0; j < m; j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("Matrix after addition:");
for(i=0;i < n; i++)
{
for(j=0; j < m; j++)
{
System.out.println(c[i][j]+"\t");
}
System.out.println("\n");
}
}
}
Accept n strings. Sort names in Descending order
class sorting
{
public static void main(String[] input)
{
int k=input.length;
String temp=new String();
String names[] = new String[k+1];
for(int i=0; i<k; i++)
{
names[i] = input[i];
}
for(int i=0; i<k; i++)
for(int j=i+1; j<k; j++)
{
if(names[i].compareTo(names[j])<0)
{
temp=names[i];
names[i]=names[j];
names[j]=temp;
}
}
System.out.println("\nSorted Descending Order is ");
for(int i=0;i<k;i++)
{
System.out.println(names[i]);
}
}
}
Demonstrate method overloading
class Calculate
{
void Sum(int a,int b)
{
System.out.println("Sum is"+(a+b));
}
void Sum(float a,float b)
{
System.out.println("Sum is"+(a+b));
}
public static void main(String[] args)
{
Calculate cal=new Calculate();
cal.Sum(8,5);
cal.Sum(4.5f,3.8f);
}
}
Illustrate use of THIS keyword
class Student
{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee)
{
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display()
{
System.out.println(rollno+" "+name+" "+fee);
}
}
class TestThis2
{
public static void main (String args[])
{
Student S1=new Student(111,"ankit",5000f);
Student S2=new Student(112,"Sumit",6000f);
S1.display();
S2.display();
}
}
//method overloading
import java.util.Scanner;
class student
{
int rno;
String name;
void accept()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter Roll no.");
rno=s.nextInt();
System.out.println("Enter name");
name=s.next();
}
}
class marks extends student
{
int sub1,sub2,sub3;
void getmarks()
{
Scanner p=new Scanner(System.in);
System.out.println("Enter the marks of three subjects");
sub1=p.nextInt();
sub2=p.nextInt();
sub3=p.nextInt();
}
}
class result extends marks
{
float avg;
void showresult()
{
avg=(sub1+sub2+sub3)/3;
System.out.println("name="+name);
System.out.println("roll no="+rno);
System.out.println("sub1="+sub1);
System.out.println("sub2="+sub2);
System.out.println("sub3="+sub3);
if(avg>=40)
{
System.out.println("Student is pass");
}
else
{
System.out.println("Student is fail");
}
}
}
class multilevel2
{
public static void main(String args[])
{
result r=new result();
r.accept();
r.getmarks();
r.showresult();
}
}
Comments
Post a Comment