0% found this document useful (0 votes)
150 views

Parvezjava

The abstract class Shape has subclasses Triangle, Rectangle, and Circle. The abstract class Shape defines an area() method that is implemented differently in each subclass to calculate the area of the specific shape. A test class demonstrates calling the area() method on objects of each subclass.

Uploaded by

Parvez Mir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

Parvezjava

The abstract class Shape has subclasses Triangle, Rectangle, and Circle. The abstract class Shape defines an area() method that is implemented differently in each subclass to calculate the area of the specific shape. A test class demonstrates calling the area() method on objects of each subclass.

Uploaded by

Parvez Mir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1.

Basic program in java

1. Write a java program which checks whether entered number is prime or


not?

import java.io.*;
import java.util.Scanner;
public class Pra_1
{

public static void main(String a[])


{
boolean Prime=false;
Scanner scan=new Scanner(System.in);
System.out.print("Enter Number:");
int n=scan.nextInt();
scan.close();
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
Prime=true;
break;
}
}
if(!Prime)
{
System.out.println(n+" Is Prime Number");
}
else
{
System.out.println(n+" Not a Prime Number");
}

}
}

● OUTPUT

Enrollment no:170220107103
2. Declare a class called Coordinate to have 3 dimensional Cartesian
coordinates. Define following methods:
– Constructor(s)
– add coordinates to add two Coordinate objects and to produce resultant
object. Define method main to show use of above methods.

import java.io.*;
class Coordinate
{
int a,b,c;
Coordinate()
{
a=11;
}
Coordinate(int y)
{
b=y;
}
Coordinate(Coordinate x1,Coordinate x2)
{
c=x1.a+x2.b;
System.out.print("Addition of two coordinates is:"+c);
}
}
public class Pra_2
{

public static void main(String[] args)


{
Coordinate c1=new Coordinate();
Coordinate c2=new Coordinate(50);
Coordinate c3=new Coordinate(c1,c2);
}
}

● OUTPUT

Enrollment no:170220107103
3. Declare a class called employee having employee_id and employee_name
as members. Extend class employee to have a subclass called salary having
designation and monthly_salary as members. Define following:
– Required constructors
– A method to find and display all details of employees drawing salary more
than Rs. 20000/-
– Method main for creating an array for storing these details given as
command line arguments and showing usage of above methods.

import java.util.Scanner;
class Employee
{
String[] employee_id;
String[] employee_name;
}
class Salary extends Employee
{
String[] Designation;
double[] monthly_salary;
Salary(int j)
{
employee_name=new String[j];
employee_id=new String[j];
Designation=new String[j];
monthly_salary= new double[j];
}
void display(int j)
{
System.out.println("---------------------------------------------------------------");
System.out.println("---------------------------------------------------------------");
System.out.println("\t Details of employee who have salary above 20000");
System.out.println("---------------------------------------------------------------");
System.out.println("---------------------------------------------------------------\n \n");
System.out.format("%-15s %-15s %-25s %-10s %n","employee id","employee name","employee
Designation","Monthly Salary");
System.out.println("----------------------------------------------------------------------------");
for(int i=0;i<j;i++)
{
if(monthly_salary[i]>=20000)
{
System.out.format("%-15s %-15s %-25s %-10s
%n",employee_id[i],employee_name[i],Designation[i],monthly_salary[i]);
}
}
}
public static void main(String [] args)
{
Scanner jaimin=new Scanner(System.in);
int length=args.length;

Enrollment no:170220107103
Salary obj = new Salary(length);
if(length==0)
{
System.out.println("please enter employee id");
}
for(int i=0;i<length;i++)
{
obj.employee_id[i]=args[i];
System.out.println("\n\n enter the details of \""+args[i]+"\" employee id");
System.out.print("\n name of employee -->");
obj.employee_name[i]=jaimin.next();
System.out.print("\n Designation of employee -->");
obj.Designation[i]=jaimin.next();
System.out.print("\nMonthly salary of employee -->");
obj.monthly_salary[i]=jaimin.nextDouble();
}
obj.display(length);
}
}

● OUTPUT

Enrollment no:170220107103
4. Declare a class called author having author_name as private data member.
Extend author class to have two sub classes called book_publication &
paper_publication. Each of these classes have private member called title.
Show usage of dynamic method dispatch (dynamic polymorphism) to display
book or paper publications of a given author. Use command line arguments
for inputting data.

class author
{
protected String author_name;
public void get_aname(String k) { this.author_name = k; }
public void display(){}
public void get_title(String k) {}
}
class book_publication extends author
{
protected String title;
public void get_title(String k) { this.title=k; }
public void display()
{
System.out.println("The book's author name is : " + author_name + " and the booktitle is : " +
this.title );
}
}
class paper_publication extends author
{
protected String title;
public void get_title(String k) { this.title=k; }
public void display()
{
System.out.println("The paper's author name is : " + author_name + " and the papertitle is :
" + this.title );
}
}
public class Pra_4

{
public static void main(String args[])
{
author ref;
book_publication b1 = new book_publication();
paper_publication p1 = new paper_publication();
ref=b1;
ref.get_aname(args[0]);
ref.get_title(args[1]);
ref.display();
ref=p1;
ref.get_aname(args[2]);
ref.get_title(args[3]);

Enrollment no:170220107103
ref.display();
}
}

● OUTPUT

Enrollment no:170220107103
5. Declare a class called coordinate to represent 3 dimensional Cartesian
coordinates( x, y and z). Define following methods:
– constructor
– display, to print values of members
– add_coordinates, to add three such coordinate objects to produce a
resultant coordinate object. Generate and handle exception if x, y and z
coordinates of the result are zero.
– main, to show use of above methods.

import java.lang.*;
class coordinate extends Exception
{
int result;
int x,y,z;
coordinate()
{
x=y=z=0;
}
coordinate(int x,int y,int z)
{
this.x=x;
this.y=y;
this.z=z;
}
void display()
{
System.out.println("X:"+x);
System.out.println("Y:"+y);
System.out.println("Z:"+z);
}
void add_coordinate() throws coordinate
{
result=x+y+z;
System.out.println("Sum of x,y,z:"+result);
if(result==0)
{
throw new coordinate();
}
}
}
public class Pra_5
{
public static void main(String args[])
{
coordinate c=new coordinate(10,11,50);
c.display();
try
{
c.add_coordinate();

Enrollment no:170220107103
}
catch(coordinate ce)
{
System.out.println("Exception!!!");
}
}
}

● OUTPUT

Enrollment no:170220107103
6. Define the Rectangle class that contains:
Two double fields x and y that specify the center of the rectangle, the data
field width and height
A no-arg constructor that creates the default rectangle with (0,0) for (x,y) and
1 for both width and height
A parameterized constructor creates a rectangle with the specified x,y,height
and width.
A method getArea() that returns the area of the rectangle.
A method getPerimeter() that returns the perimeter of the rectangle.
A method contains(double x, double y) that returns true if the specified point
(x,y) is inside this rectangle.
Write a test program that creates two rectangle objects. One with default
values and other with user specified values. Test all the methods of the class
for both the objects.

class Rectangle
{
double x,y,height,width;
Rectangle()
{
x=y=0;
height=width=1;
}
Rectangle(double x,double y,double h,double w)
{
this.x=x;
this.y=y;
height=h;
width=w;
}
double getArea()
{
return (height*width);
}
double getPerimeter()
{
return (2*height+width);
}
boolean check(double x,double y)
{
double pointx=x;
double pointy=y;
if(pointx<(this.x+(this.width)) && pointy>(this.y-(this.width)) &&
pointy<(this.y+(this.height)) && pointy>(this.y-(this.height)))
{
return true;
}

Enrollment no:170220107103
else
return false;
}
}
public class Pra_6
{
public static void main(String args[])
{
Rectangle r=new Rectangle();
Rectangle r1=new Rectangle(5,4,32,24);
System.out.println("Area of Rectangle:"+r.getArea());
System.out.println("Perimeter of Rectangle:"+r.getPerimeter());
System.out.println("Point inside Rectangle:"+r.check(0,0));
System.out.println("Area of Rectangle:"+r1.getArea());
System.out.println("Perimeter of Rectangle:"+r1.getPerimeter());
System.out.println("Point inside Rectangle:"+r1.check(6,4));
}
}

● OUTPUT

Enrollment no:170220107103
2. Abstract Class and Interface

1. Describe abstract class called Shape which has three subclasses say
Triangle, Rectangle, and Circle. Define one method area () in the abstract
class and override this area () in these three subclasses to calculate for
specific object i.e. area () of Triangle subclass should calculate area of triangle
etc. Same for Rectangle and Circle.

abstract class shape


{
abstract void area();
}
class Triangle extends shape
{
void area()
{
double length=11;
double width=50;
double height=40;
double l=(length+width+height)/2;
double ans=Math.sqrt(l*(l-length)*(l-width)*(l-height));
System.out.println("Area of Triangle:"+ans);
}
}
class Rectangle extends shape
{
void area()
{
int length=11;
int width=50;
System.out.println("Area of Rectangle:"+length*width);
}
}
class Circle extends shape
{
void area()
{
double pi=3.14;
double r=50;
System.out.println("Area of Circle:"+pi*r*r);
}
}
public class Pra_2_1
{
public static void main(String args[])
{
shape s=new Triangle();

Enrollment no:170220107103
s.area();
shape s1=new Rectangle();
s1.area();
shape s2=new Circle();
s2.area();
}
}

● OUTPUT

Enrollment no:170220107103
2. Write a program that illustrates interface inheritance. Interface P is
extended by P1 and P2. Interface P12 inherits from both P1 and P2.Each
interface declares one constant and one method. class Q implements
P12.Instantiate Q and invoke each of its methods. Each method displays one
of the constants.

interface P
{
final int a=87;
void display_a();
}
interface P1 extends P
{
final int b=42;
void display_b();
}
interface P2 extends P
{
final int c=11;
void display_c();
}
interface P12 extends P1,P2
{
final int d=50;
void display_d();
}
class Q implements P12
{
public void display_a()
{
System.out.println("In Interface P value of A:"+a);
}
public void display_b()
{
System.out.println("In Interface P1 value of B:"+b);
}
public void display_c()
{
System.out.println("In Interface P2 value of C:"+c);
}
public void display_d()
{
System.out.println("In Interface P12 value of D:"+d);
}
}
public class Pra_2_2
{
public static void main(String arg[])
{
Q q1=new Q();

Enrollment no:170220107103
q1.display_a();
q1.display_b();
q1.display_c();
q1.display_d();
}
}

● OUTPUT

Enrollment no:170220107103
3. Write a program that illustrates interface inheritance. Interface A is
extended by A1 and A2. Interface A12 inherits from both P1 and P2.Each
interface declares one constant and one method. Class B implements
A12.Instantiate B and invoke each of its methods. Each method displays one
of the constants.

interface A
{
final int a=10;
void display_a();
}
interface A1 extends A
{
final int b=20;
void display_b();
}
interface A2 extends A
{
final int c=30;
void display_c();
}
interface A12 extends A1,A2
{
final int d=40;
void display_d();
}
class B implements A12
{
public void display_a()
{
System.out.println("In Interface A value of A:"+a);
}
public void display_b()
{
System.out.println("In Interface A1 value of B:"+b);
}
public void display_c()
{
System.out.println("In Interface A2 value of C:"+c);
}
public void display_d()
{
System.out.println("In Interface A12 value of D:"+d);
}
}
public class Pra_2_3
{
public static void main(String arg[])
{
B b1=new B();

Enrollment no:170220107103
b1.display_a();
b1.display_b();
b1.display_c();
b1.display_d();
}
}

● OUTPUT

Enrollment no:170220107103
4. The Transport interface declares a deliver () method. The abstract class
Animal is the super class of the Tiger, Camel, Deer and Donkey classes. The
Transport interface is implemented by the Camel and Donkey classes. Write a
test program that initialize an array of four Animal objects. If the object
implements the Transport interface, the deliver () method is invoked.

interface Transport
{
void deliver();
}
abstract class Animal
{
abstract void display();
}
class Tiger extends Animal
{
public void display()
{
System.out.println("Animal Tiger");
}
}
class Camel extends Animal implements Transport
{
public void display()
{
System.out.println("Animal Camel");
}
Camel()
{
display();
deliver();
}
public void deliver()
{
System.out.println("Deliver Camel");
}
}
class Deer extends Animal
{
public void display()
{
System.out.println("Animal Deer");
}
}
class Donkey extends Animal implements Transport
{
public void display()
{
System.out.println("Animal Donkey");
}

Enrollment no:170220107103
Donkey()
{
display();
deliver();
}
public void deliver()
{
System.out.println("Deliver Donkey");
}
}
public class Pra_2_4
{
public static void main(String args[])
{
Tiger t=new Tiger();
Camel c=new Camel();
Deer d=new Deer();
Donkey dn=new Donkey();
t.display();
d.display();
}
}

● OUTPUT

Enrollment no:170220107103
5. The abstract Vegetable class has three subclasses named Potato, Brinjal
and Tomato. Write an application that demonstrates how to establish this
class hierarchy. Declare one instance variable of type String that indicates the
color of a vegetable. Create and display instances of these objects. Override
the toString() method of Object to return a string with the name of the
vegetable and its color.

abstract class Vegetable
{
public String color;
}
class Potato extends Vegetable
{
public String toString()
{
color="Brown-skinned Color";
return ("Potato:"+color);
}
}
class Brinjal extends Vegetable
{
public String toString()
{
color="Purple Color";
return ("Brinjal:"+color);
}
}
class Tomato extends Vegetable
{
public String toString()
{
color="Red Color";
return ("Tomato:"+color);
}
}
public class Pra_2_5
{
public static void main(String args[])
{
Potato p=new Potato();
Brinjal b=new Brinjal();
Tomato t=new Tomato();
System.out.println(p);
System.out.println(b);
System.out.println(t);
}
}

Enrollment no:170220107103
● OUTPUT

Enrollment no:170220107103

You might also like