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

Java Lab Manual

The document demonstrates different Java programming concepts like data types, operators, scope and lifetime of variables, classes, objects, methods, method overloading, and constructor overloading. Code examples and outputs are provided for each concept.

Uploaded by

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

Java Lab Manual

The document demonstrates different Java programming concepts like data types, operators, scope and lifetime of variables, classes, objects, methods, method overloading, and constructor overloading. Code examples and outputs are provided for each concept.

Uploaded by

marow75067
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Data types

1 a)

AIM:

To Write a Java program to demonstrate the use of different data types in java.

ALGORITHM:

1) Start
2) import java package
3) Declare and define the class as data under public mode.
4) Declare with various data types such as byte, short, int, float, double, Boolean and char.
5) Use the print statement to display all the value declared using different data types
6) Close the main () function
7) Close the class declared.
8) Stop the program.

Source Code: -

public class Main


{
public static void main(String[] args)
{
byte b=100;
short s=123;
int v=1234532;
int calc=9876543;
long amountVal=1234567891;
float rate=12.25F;
double val=12345.234D;
boolean flag=true;
boolean bval=false;
char ch=88;
char ch2='A';
System.out.println("Byte Value = "+b);
System.out.println("Short Value = "+s);
System.out.println("int Value = "+v);
System.out.println("int calc = "+calc);
System.out.println("long AmountVal = "+amountVal);
System.out.println("float instrest Value = "+rate);
System.out.println("double Value = "+val);
System.out.println("boolean Flag Value = "+flag);
System.out.println("boolean Value = "+bval);
System.out.println("char a Value = "+ch);
System.out.println("char b Value = "+ch2);
}
}

Output: -

Byte Value = 100


Short Value = 123
int Value = 1234532
int calc = 9876543
long AmountVal = 1234567891
float instrest Value = 12.25
double Value = 12345.234
boolean Flag Value = true
boolean Value = false
char a Value = X
char b Value = A

Result: -
Thus the Above Program to demonstrate Different types of data types in java is Executed
Successfully.

1.B Operators

Aim: -To write java program to demonstrate Different types of operators in java

Algorithm: -

1) Start
2) import java package
3) Declare and define the class name operators.
4) declare variables needed as x, a, b, c.
5) Using print statements perform
5.1) All increments And Decrement Operators to X variables
5.2) To perform Operations between a, b and c variables with arithmetic, relational and bitwise
operators
5.3) To perform Operations between a, b and c variables with conditional and ternary operators
6) Using min variables perform ternary operation between a and b variables using ternary operator
7) Finally a and b values are incremented by using addition assignment operator
8) Print the updated a ,b and c values
9) Stop

Source Code: -
import java.io.*;
public class Main
{
public static void main (String[] args)
{
int x=1,y=2,a=3,b=4,c=5;
boolean d=true;
boolean e=true;
boolean f=false;
System.out.println(x+y); //Addition
System.out.println(x-y); //Subtraction
System.out.println(x*y); //Multiplication
System.out.println(x/y); //Division
System.out.println(x%y); //Modulus
System.out.println(d&&e); //True as both are true
System.out.println(d&&f); //False as c is false
System.out.println(d||f); //True as a is true
System.out.println(d==e); //True as both are equal
System.out.println(d!=e); //False as both are equal
System.out.println(d!=f); //True as both are not equal
System.out.println(a<c); //True as a is less than c
System.out.println(a<=c); //True as a is less than c
System.out.println(!d); //inverted from true to false
System.out.println(a>c); //False, a is not greater than c
System.out.println(a>=b); // a is greater or equal to b
System.out.println(!f); //True as f is false
System.out.println(x++); //Result Shown before Increment
System.out.println(++x); //Incremented ,result is shown
System.out.println(--x); //Decremented, result is shown
}
}

Output: -

3
-1
2
0
1
true
false
true
true
false
true
true
true
false
false
false
true
1
3
2
Result: - Thus the Above Program to demonstrate Different types of Operators in java is Executed
Successfully

1. C Scope & Lifetime variable


Aim: -
To write java program to demonstrate scope and lifetime of variables.

Algorithm: -

1) Start
2) Declare and define the class name as Scope
3) Declare and define the main () function under public mode
4) Declare variables and initialize it, inside the main ().
5) Check for the condition if (x==10).
5.1) initialize other variable y=20
5.2) display the values for x and y using the print statement
5.3) Assign the x as y*2 , x=x*2;
6) Display the value of x
7) Close the main () function and class
8) stop.

Source Code: -
import java.io.*;
class Main
{
public static void main (String args[])
{
int x=10,y=0;
if (x==10)
{
y=20;
System.out.println("x and y:"+ x + " and "+y);
}
x=y*2;
System.out.println(" X is "+x);
}

Output: -
x and y: 10 and 20
X is 40

Result: -
Thus the Above Program to demonstrate scope and lifetime variable is Executed Successfully.
Class, Object And Methods

2a) Aim:

To write a java program to demonstrate the use of classes, object and methods.

Algorithm:

1. Start
2. Declare class name as main.
3. Declare main function and create object.
4. Print any line and invoke it.
5. Create another class and declare function.
6. Print the statement printing from inside method.
7. Stop.

Source code:

class Main
{
public static void main(String args[])
{
Output obj=new Output();
System.out.println("about to encounter a method");
obj.method();
System.out.println("method was executed");
}
}
class Output
{
void method()
{
System.out.println("printing from inside method");
}
}

Output:

About to encounter a method


Printing from inside method
method was executed

Result:
Thus the above java program to demonstrate the use of classes, objects and methods has been executed
successfully.

Constructors
2b)
Aim:
To write a java program to demonstrate the use of constructors.

Algorithm:

1. Start.
2. Declare and define the class as default.
3. Declare the variables as a,b,c.
4. Declare and define the default() function to declare the a,b values as a constructor.
5. Declare the display () function to display values and close function.
6. Declare the main () function.
7. Create an objects for the class default and call the display () to call the function.
8. Close the main () and class definition.
9. Stop.
Source code:

class Default
{
int a,b,c;
Default(int i,int j)
{
a=i;
b=j;
c=a+b;
}
public static void main(String args[])
{
Default d=new Default(2,2);
d. display ();
}
void display()
{
System.out.println("a="+a+"\nb="+b+"\nsum="+c);
}
}

Output:

a=2
b=2
sum=4

Result:
Thus, the above program to demonstrate the use of constructor has been successfully executed.

3 a)

Aim:-
To create a java program to demonstrate the concept of method overloading
Algorithm:-

1. Start
2. Declare the class name as example.
3. Declare the method with parameters as public void sum(int a,int b).
4. Initialize c=a+b and display c value.
5. Declare another method with parameters in the public mode
Public void sum(int a,int b,int c)
6. Initialize x=a+b+c and display the x value
7. Declare main() and create object
8. Invoke all the functions with arguments.
9. Close the main()
10. Stop

Source Code:-

class Example
{
void sum(int a,int b)
{
int c=a+b;
System.out.println("sum="+c);
}
void sum(int a,int b,int c)
{
int x=a+b+c;
System.out.println("sum="+x);
}
public static void main(String args[])
{
Example e=new Example();
e.sum(1,2);
e.sum(1,2,3);
}
}

Output

Sum=3
Sum=6

Result
Thus the above java program to demonstrate the concept of method overloading has been executed successfully

3 b)
Aim:-
To write a java program to demonstrate the concept of constructor overloading .

Algorithm:-

1 .Start
2. Declare the class name as student
3. Declare the variables needed under the default mode
4. Declare and define the constructor as student
4.1 assign the variables in the constructor using this keyword.
4.2 as this.rollno, this .name , this.fee
4.3 Close the constructor
5. Declare and define another constructor with the same name used before with different
parameters and display values
6. Declare and define another class as test for main()and create an object for invoking the constructor and call
display()
8. Close the class
9. Stop
Source Code:-

class Student
{
int roll_no;
String regno;
String name;
float fee;
Student(int roll_no,String name,float fee)
{
System.out.println("first constructor");
this.roll_no=roll_no;
this.name=name;
this.fee=fee ;
System.out.println(roll_no+" "+fee);
}
Student(String regno,int fee)
{
System.out.println("second constructor");
this.regno =regno;
this.fee=fee;
System.out.println(regno+" "+fee);
}

}
class Test{
public static void main(String args[])
{
Student s1=new Student(111,"anvir",2000f);
Student s2=new Student("207513333",3000);
}
}

Output:

First constructor
111 anvir 2000.0
Second constructor
20753300 3000

Result:

Thus the above java program to demonstrate the concept of constructor overloading using this keyword has been
executed successfully executed
4a)

Aim:-

To create the java program to demonstrate the concept of command line argument

Algorithm:-

1.Start
2.Declare class name and command line
3. declare the main()
4.under main(), print the arguments
5.your first argument is +args(0)
6.close the main
7.Stop

Source Code:

import java.io.*;
class Commandline
{
public static void main(String args[])
{
System.out.println("your first argument is:"+args(0));
}
}
Compile:- javac Commandline

Run:-java Commandline harshey’s_mix line

Output :-

Your first argument is harshey’s_mix

Result:

Thus the above java program to read and write different types of data using command line argument
has been executed successfully

4 b)

Aim:-
To write a java program to read and write different types of data using scanner class.

Algorithm:-

1. Start
2. import java.util.scanner declare class name as student
3. Declare main(),under main() declare the declare the details of the student like roll no , stdname , branch
,percentage
4. Declare scanner as scanner sc=new scanner(System.in)
5. Enter student rollno
5.1. rolno=sc.nextInt()
5.2. for nextline declare scnextLine()
6. Enter student name ,branch ,percentage.
7. stdname =sc.nextFloat().
8. print all the details of the student.
9. stop

Source Code:-

import java.util.*;
class Student
{
public static void main(String args[])
{
int rollno;
String stname;
String branch;
float percentage;
Scanner sc=new Scanner(System.in);
System.out.println("enter student rollno");
rollno=sc.nextInt();
System.out.println("enter the student name");
stname=sc.nextLine();
System.out.println("enter the branch");
branch=sc.nextLine();
sc.nextInt();
System.out.println("enter the percentage");
percentage=sc.nextFloat();
System.out.println("rollnois :"+rollno+"\nname is :"+stname+"\nbranch is :"+branch+"\n percentage
is :" + percentage);
}
}

Output:-

roll no is : 38
nemeis :hema
branch is :csm
percentage is :90.00

Result:-

Thus the above java program to read and write different types of data using scanner class has executed
successfully

Fibonacci series without using Recursion

5.a)

Aim:-
To write a java program for Fibonacci series without using Recursion.
Algorithm:-
1. Start
2. Declare class name as Fibonacci
3. Declare main (),declare variables and initialize the values to
Them
3.1. int n3
3.2. int n1=0
3.3 int n2=1
4. Define the loop as
4.1. for(i=2;i<count; i++)
5. Print the values
5.1. n3 =n1+n2
5.2. n1 =n2
5.3. n2 =n3
6. Close the main ()
7. Stop.

Source code:-

import java.io.*;
class Fibonacci {
public static void main(String args[])
{
int n1=0,n2=1,n3,count=10;
System.out.println (" "+n1);
System.out.println (" "+n2);
for(int i=2;i<count; i++)
{
n3=n2+n1;
System.out.println (" "+n3);
n1=n2;
n2=n3;
}
}
}

Output:-

0
1
1
2
3
5
8
13
21
34

Result:-

Thus, the above java program of Fibonacci series without using recursion has been executed
successfully.

Fibonacci series using recursion


5. a (ii)
Aim:- To write a java program that uses recursive function to print Fibonacci series.

Algorithm:-
1. Start
2. Declare class name as Fibonacci
3. Initialize the values to the variable under static mode
4. Static int n1=0,n2=1,n3=0
4.1. static void print(int count)
5. declare if condition if(count>0) Assign the variables as follows
5.1. n3=n1+n2
5.2. n1=n2
5.3. n2=n3
6. print n3 value initialize count=10
6.1. print(count-1)value
7. declare main()
7.1. print the n1 and n2 values
7.2. print(count-2)value which at as recursion function

7.3. Close the main()

8. Stop

Source code:-
import java.io.*;
class Fibonacci
{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count)
{
if(count>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci (count-1);
}
}
public static void main(String args[])
{
int count=10;
System.out.print(n1+" "+n2); //printing 0 and 1
printFibonacci (count-2); //n-2 because 2 numbers are already printed
}
}

Output:

0 1 1 2 3 5 8 13 21 34

Result:- Thus, the above java program of Fibonacci series with recursion has been executed successfully.
Prime numbers

5. (b)

Aim:-
To check a java program that prompts the use for an integer and prints out all prime numbers up to that
integer.

Algorithm:-

1. 1.start
2. 2.declare class name as Prime number
3. 3.declare main function & initialize variable n & p
4. create object & print(“enter a number”);
5. check condition
6. For(int i=2; i<n; i++)
7. for(int j=2; j<1, j++)
7.1.1. 6.1if( i % j ==0)
7.1.1.1. then p=1
7.2. if (p==0) then print i value
8. stop

Source code:

import java.util.Scanner;

class Primenumbers
{
public static void main(String args[])
{
int n;
int p;
Scanner s=new Scanner( System.in);
System.out.println("Enter a number: ");
n=s.nextInt();
for(int i=2;i<n; i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
System.out.println(i);
}
}
}

Output:- enter a number : 14


2
3
5
7

Result:
Thus, the above java program has been executed successfully

6(A) Palindrome

Aim:-
Write a java Program to check weather a string is palindrome or not.

Algorithm:-

1) Start
2) import java.util.Scanner
3) declare the class name as palindrome And main function, initialize variables
3.1) string a,b;
4) Enter the Variables by using scanner statement
4.1) scanner S = new scanner(System.in);
5)Enter the string to check.
5.1) a=S.nextLine();
5.2) initialize the n variable with the Length of a
6) perform the condition for(int i=0 ; i>=0 ; i--).
6.1) Initialize b=b+a.charAt(i).
7) Check if (a.equalsIgnoreCase(b))
7.1) print string is palindrome
7.2) else
7.3)print string is palindrome
8.) Stop.

Source Code:-

import java.util.*;
public class Palindrome
{
public static void main(String args[])
{
String a, b = "";
Scanner s = new Scanner(System.in);
System.out.print("Enter the string you want to check:");
a = s.nextLine();
int n = a.length();
for(int i = n - 1; i >= 0; i--)
{
b = b + a.charAt(i);
}
if(a.equalsIgnoreCase(b))
{
System.out.println("The string is palindrome.");
}
else
{
System.out.println("The string is not palindrome.");
}
}
}

Output: -

Enter The String You Want to Check: Madam


The String is palindrome

Result:-
Thus The Above Program to check weather a string is palindrome or not is Executed Successfully.

6(B) Ascending Order

Aim:-
To Write a java Program for sorting a given list of names in Ascending order

Algorithm:-

1) Start
2) import java.util.Scanner
3) declare the class in public mode for the main function section
4) Declare the Variables needed and create a object for scanner class
5) give the integer type statement using the object created for the scanner class
6) create an array object & create another scanner class object
7) to sort the given names, follow the steps below.
7.1) for (int i=0 ; i<n ; i++)
7.1.2) initialize or assign name[i]=S1.nextLine();
7.2)for (j=i+1 ; j<n ; j++)
7.2.1) check if( names[i].CompareTo(names[j])>0)
7.2.1.1)assign temp = names[i];
7.2.1.2) assign temp = names[j];
7.2.1.3) assign names[j] = temp;
8.) print the given strings using the below statements
8.1) for (int i=0 ; i<n-1 ; i++)
8.1.1) print names[i]
8.2) print names [n-1]
9) STOP
Source Code:-

import java.util.Scanner;
public class Alphabetical_Order
{
public static void main(String[] args)
{
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]);
}
}
Output: -
Enter Number of name you want to enter :
Enter the names:
Andrea
Zombie
Priya

Names in Sorted Order:


Andrea
Priya
Zombie

Result:-

Thus The Above Program for sorting a given list of names in Ascending order is Executed
Successfully.

7(a) Multi level inheritance

AIM:-
To wrote a java program to perform multilevel inheritance.

ALGORITHM:-

1. Start.
2. Create a main class called Box.
2.1. Declare the variables needed under private mode.
2.2. Create a constructor with arguments of objects for BOX.
2.2.1. assign the values for the variables using the object.
2.3. Create another constructor with no arguments.
2.3.1. Assign the -1 value to all the variables.
2.4. Create another parameterized constructor.
2.4.1. Assign the parameters values to the respective variables.
2.5. Create a volume function with double return value.
2.5.1. return the width*height*depth value.
2.6. Create another parameterized constructor with len arguments.
2.6.1. Assign the len value to the variables.
2.7. Close the class BOX.
3. Create a subclass BOXWEIGHT for the super class BOX.
3.1. Declare the variables needed.
3.2. Create a constructor with argument with created object for subclass.
3.2.1. Call the object of super class used in constructor using super().
3.2.2. Assign the value for the variable using the objects.
3.3. Create another constructor which is parameterized for all the variables.
3.3.1. Call the similar constructor in the super class using super().
3.3.2. Assign the remained parameter to the variable.
3.4. Create another default constructor which calls the default super class constructor and assign the variable to
-1.
3.5. Create a parameterized constructor and call the super class constructor using super (len) and assign the
variables as m.
3.6 close the subclass BOXWEIGHT.
4. Create another subclass ship for BOXWEIGHT class.
4.1. Declare the variables needed.
4.2. Create a parameterized constructor with object for ship class and call the super (ob) function and assign
the variables as using object.
4.3. Create a default constructor which calls the super() in super class and assign the cost variable as -1.
4.4. Create another parameterized constructor to call the super(m,w,d,h) in super class and assign c value to the
variable.
4.5. Create another parameterized constructor to call super( len, m) in super class and assign c value to the
variable.
4.6 close the class.
5. Create another super class for main function
5.1. Create an object for the ship class as a and pass the parameters for constructor through the object.
5.2. Gives the parameters to the vol variable using object with volume
5.3. Print the values needed.
5.4. Print the values needed.
6. Stop.

Source code:

class Box
{
double width;
double height;
double depth;
Box(Box ob)
{
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = -1;
height = -1;
depth = -1;
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width * height * depth;
}
}

class BoxWeight extends Box


{
double weight;
BoxWeight(double w, double h, double d, double m)
{
super(w, h, d);
weight = m;
}
BoxWeight(BoxWeight ob)
{
super(ob);
weight = ob.weight;
}
BoxWeight()
{
super();
weight = -1;
}
BoxWeight(double len, double m)
{
super(len);
weight = m;
}
}
class DemoSuper
{
public static void main(String args[])
{
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
BoxWeight mybox3 = new BoxWeight(); // default
BoxWeight mycube= new BoxWeight(3, 2);
BoxWeight myclone= new BoxWeight(mybox1);
double vol;
vol= mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol= mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
vol= mybox3.volume();
System.out.println("Volume of mybox3 is " + vol);
System.out.println("Weight of mybox3 is " + mybox3.weight);
System.out.println();
vol= myclone.volume();
System.out.println("Volume of mycloneis " + vol);
System.out.println("Weight of mycloneis " + myclone.weight);
System.out.println();
vol= mycube.volume();
System.out.println("Volume of my cube is " + vol);
System.out.println("Weight of my cube is " + mycube.weight);
System.out.println();
}
}

OUTPUT:-

Shipment - 100
cost - 4.0
weight - 3.4
RESULT:-

Thus the above java program to perform multilevel inheritance has been successfully executed.
7 B) Area of objects using overridden method

AIM:-
To write a java program that calculates the area of objects using overridden method.

ALGORITHM:-

1. Start
2. Create a main class as Figure.
2.1 Declare the variables needed.
2.2 Create a parameterized constructor top assign the arguments values to the variables.
2.3 Create area () function with double return type that computes the area of an object.
2.4. Close the class.
3. Create a subclass rectangle for super class figure.
3.1 Create a parameterized constructor which calls the super class constructor.
3.2. Create a overridden method for area () which computes the area of the rectangle and returns a value.
3.3. Close the sub class.
4. Create another subclass triangle for super class figure.
4.1. Create a parameterized constructor which calls the super class constructor.
4.2. Create a overridden method for area() which computes the area of the triangle and returns a value.
4.3. Close the sub class.
5. Create another main class to define main () function.
5.1. Create an object for all the three classes and pass the values to the constructors through arguments.
5.2. Create a reference variables for the figure class, using the objects created for the classes, create and assign
the reference variables to the respective class objects and call the area() functions.
6. Stop.

Source code:
import java.io.*;
class Figure
{
double dim1;
double dim2;
Figure (double a, double b)
{
dim1 = a;
dim2 = b;
}
double area()
{
System.out.println("Area for figure is undefined.");
return 0;
}
}
class Rectangle extends Figure
{
Rectangle (double a, double b)
{
super (a, b);
}
// override area for rectangle
double area()
{
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure
{
Triangle (double a, double b)
{
super(a, b);
}
// override area for right triangle
double area()
{
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class FindAreas
{
public static void main(String args[]) throws IOException
{
Figure f=new Figure(10,10);
Rectangle r = new Rectangle(9,5);
Triangle t = new Triangle(10,8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area());
}
}

OUTPUT;-
Inside Area for Rectangle.
Area is 45.0
Inside Area for Triangle.
Area is 40.0
Area for figure is undefined.
Area is 0.0

RESULT:-
Thus, the java program that calculates the area of objects using overriding method has been
successfully executed.

8(a)

AIM:-
To write a java program to abstract class and methods to show the number of sides of a given
geometrical object.

ALGORITHM:-

1. 0.Start
2. 1.Create an abstract mai class (or) super class shape which includes an empty method named
numberofsides()
3. 2.Create a subclass Trapezoid for superclass shape.
3.1. 2.1 define the numberofsides() under public mode to print the number of sides of trapezoid.
3.2. 2.2 close the subclass.
4. 3.Create another subclass Triangle for super class shape.
4.1. 3.1 define the numberofsides() under public mode to print mode to print the number of sides of
4.1.1. Trianlge.
4.2. 3.2 close the subclass
5. 4.create another subclass Hexagon for super class shape.
5.1. 4.1 define the numberofsides() under public mode to print the number of sides of hexagon.
5.2. 4.2close the subclass.
6. 5.create another super class side to define main() function.
6.1. 5.1 create objects for all the subclass and access the numberofsides() through the objects created.
6.2. 5.2 close the class
7. 6.Stop

Source Code:

import java.io.*;

abstract class Shape


{
abstract void numberofsides();
}
class Trapezoid extends Shape
{
void numberofsides()
{
System.out.println("number of sides of trapezoid is:4");
}
}
class Hexagon extends Shape
{
void numberofsides()
{
System.out.println("number of sides of hexagon is:6");
}
}
class Triangle extends Shape
{
void numberofsides()
{
System.out.println("number of sides of Triangle is:3");
}
}
class Sides
{
public static void main(String args[])
{
Shape a=new Shape();
Trapezoid s=new Trapezoid();
s.numberofsides();
Hexagon s1=new Hexagon();
s1.numberofsides();
Triangle s2=new Triangle();
s2.numberofsides();
}
}
OUTPUT:-

Number of sides in Trapezoid is 4


Number of sides in Triangle is 3
Number of sides in Hexagon is 6

RESULT:-
Thus, the above java program that use abstract class and method the number of sides of a given
geometrical object has been executed successfully.

8(b)
Aim:-
To write a java program which includes class, abstract class and interface
ALGORITHM:-

0.Start
1.Create an abstract main (or) super class A and declare an abstract method called mth1().
1.1.close the abstract class.
2.Create an interface with name B, and declare the meth2() function.
2.1.close the interface.
3.create an another class C which extends A class and implements B class
3.1.define the meth1() function under public mode which prints some desired statements.
3.2.define the meth2() function under public mode which points the desired statements.
3.3.close the class
4.create an another super class named All. To define the main()
4.1.Create an object for the C class
4.2.Call the meth1() and meth2() functions using the objects created.
4.3.close the class All .
5.Stop

Source Code:
abstract class A
{
abstract void meth1();
}
interface B
{
void meth2();
}
class C extends A implements B
{
public void meth1()
{
System.out.println("this is abstract");
}
public void meth2()
{
System.out.println("this is an interface");
}
}
class Demo
{
public static void main(String args[])
{
C obj=new C();
obj.meth1();
obj.meth2();
}
}
OUTPUT:-

This is abstract
This is an Interface

RESULT:-
Thus, the above java program which includes class abstract class, and interface has been executed
successfully.

8(c)
AIM:-
To write a java program for creation of user defined package and accessing members present in package.
ALGORITHM:-
1.Start
2.create a package pack1
2.1.Create a class as student under public mode.
2.1.1.declare the variables needed.
3.create another package pack2
3.1.create an Interface for sports
3.1.1.declare an abstract method for show().
4.create another package namely record.
4.1.import the student class from pack1 and sport class from pack2.
4.2.create a class sports1 that implements sports interface.
4.2.1.define the abstract method.
4.3.Stop.
5.Create another class record. To define main().
5.1.Create the objects for the students and sports classes.
5.2print the variables information using objects created.
5.3.close the class.
Source Code:
package pack1;
public class Student
{

public int rol_no=18;


public char name[]={'s','e','k','h','a','r'};
}

package pack2;
public interface Sports
{
void show();

//Program to implement packages


package Record;
import pack1.*;
import pack2.*;
class sports1 implements Sports
{
public void show()
{
System.out.println("21/3/2011 - volley ball");
System.out.println("22/3/2011 - Cricket");
System.out.println("23/3/2011 - Table tennis");
}
}

class Report
{
public static void main(String args[])
{
Student s=new Student();
sports1 s1=new sports1();
System.out.print("Rollnumber: "+s.rol_no+"\nname ");
System.out.print(s.name);
s1.show();
}
}

Output:

Roll number is :28


name is :Harshi
18-01-2022 - Kabaddi
25-11-2021 – Cricket

RESULT:-
Thus the above java program for creation of user defined package and accessing members present in
package has been executed successfully.

You might also like