Java Programs: Packages & Interfaces | PDF | Area | Class (Computer Programming)
0% found this document useful (0 votes)
120 views

Java Programs: Packages & Interfaces

The document contains examples of Java programs implementing packages and interfaces. The first example shows a Student class, Exam interface, and Result class that extends Student and implements Exam. The Result class calculates and displays a student's percentage. The second example defines an Employee class, Gross interface, and Salary class that extends Employee and implements Gross to calculate and display an employee's gross salary. The remaining examples demonstrate additional uses of packages, interfaces, inheritance and polymorphism in Java programs.

Uploaded by

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

Java Programs: Packages & Interfaces

The document contains examples of Java programs implementing packages and interfaces. The first example shows a Student class, Exam interface, and Result class that extends Student and implements Exam. The Result class calculates and displays a student's percentage. The second example defines an Employee class, Gross interface, and Salary class that extends Employee and implements Gross to calculate and display an employee's gross salary. The remaining examples demonstrate additional uses of packages, interfaces, inheritance and polymorphism in Java programs.

Uploaded by

Raj Debadwar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Java Programs

Chapter 3: Packages & interfaces


1. Write a program to implement following hierarchy:

Code:

import java.lang.*;
import java.util.*;
interface Exam {
public void percent_cal();
}
class Student {
String name;
int roll_no;
float marks1,marks2;
Scanner obj=new Scanner(System.in);
void accept() {
System.out.print("\nEnter the name of student : ");
name=obj.next();
System.out.print("\nEnter the roll_no of student : ");
roll_no=obj.nextInt();
System.out.print("\nEnter the marks 1 of student : ");
marks1=obj.nextInt();
System.out.print("\nEnter the marks 2 of student : ");
marks2=obj.nextInt();
}
}
class Result extends Student implements Exam {
public void percent_cal() {
System.out.print("Percentage : "+(marks1+marks2)/2);
}
void display() {
System.out.print("\nStudent name : "+name);
System.out.print("\nStudent Roll no. : "+roll_no);
System.out.print("\nStudent marks 1 : "+marks1);
System.out.print("\nStudent marks 2 : "+marks2);
// System.out.print("\nStudent percentage : "+p);
}
public static void main(String args[]) {
Result rd=new Result();
rd.accept();
rd.percent_cal();
rd.display();
}
}
Output:
2. Define a class ‘Employee’ having data members name and basic-salary.
Also declare an interface ‘Gross’ having method to calculate gross salary.
Derive a class ‘Salary’ using class ‘Employee’ and interface ‘Gross’ and
display the name, basic-salary and gross-salary.
Code:

import java.lang.*;
import java.util.*;
class Employee{
Scanner obj=new Scanner(System.in);
String name;
double basic_salary,expense;
void accept() {
System.out.print("\nEnter the name of employee : ");
name=obj.next();
System.out.print("\nEnter the basic salary of employee : ");
basic_salary=obj.nextDouble();
System.out.print("\nEnter the Expense : ");
expense=obj.nextInt();
}
}
interface Gross{
public void gross_salary();
}
class Salary extends Employee implements Gross{
public void gross_salary() {
System.out.print("Gross Salary : "+(basic_salary+expense));
}
void display() {
System.out.print("\nEmployee name : "+name);
System.out.print("\nEmployee salary : "+basic_salary);
System.out.print("\nEmployee Expence : "+expense);
}
public static void main(String args[]) {
Salary rd=new Salary();
rd.accept();
rd.gross_salary();
rd.display();
}
}
Output:
3. Design a package containing a class which defines a method to find area of
rectangle. Import it in Java application to calculate area of a rectangle.
Code:
Package Creation :

package rectangle;
public class area {
public void aor(int l,int b) {
int a=l*b;
System.out.print("\nArea of triangle : "+a);
}
}
Package Implementation :
import java.util.*;
import rectangle.area;
class rectdemo{
public static void main(String args[]) {
int l,b;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the length of triangle : ");
l=obj.nextInt();
System.out.print("\nEnter the beadth of triangle : ");
b=obj.nextInt();
area r=new area();
r.aor(l,b);
}
}
Output:
4. Implement the inheritance given below by assuming suitable methods.

Code:
import java.util.*;
class Person{
String name,address;
Scanner obj=new Scanner(System.in);
void get() {
System.out.print("\nEnter the name of employee : ");
name=obj.next();
System.out.print("\nEnter the address of employee : ");
address=obj.next();
}
}
interface Manager{
public int salary=10000;
public void accept();
public void salary();
}
class Employee extends Person implements Manager{

public void accept(){}


public void salary() {
System.out.print("\nEmployee salary : "+salary);
}
void display() {
System.out.print("\nEmployee name : "+name);
System.out.print("\nEmployee address : "+address);
}
public static void main(String args[]) {
Employee d=new Employee();
d.get();
d.accept();
d.salary();
d.display();
}

}
Output:
5. Design a package containing a class which defines a method for arithmetic
operations. Import it in Java application and perform the operations.
Code:
Creating Package:
package arithmetic;
public class arithmetic_operations
{
public void addition(int x,int y)
{
int r=x+y;
System.out.print("\nAddition of "+x+" and "+y+" is "+r);
}
public void subtraction(int x,int y)
{
int r=x-y;
System.out.print("\nSubtraction of "+x+" and "+y+" is "+r);
}
public void multiplication(int x,int y)
{
int r=x*y;
System.out.print("\nMultiplication of "+x+" and "+y+" is "+r);
}
public void division(int x,int y)
{
double r=x/y;
System.out.print("\nDivision of "+x+" and "+y+" is "+r);
}

}
Implementing Package :
import java.lang.*;
import java.util.*;
import arithmetic.arithmetic_operations;
class arithmeticdemo
{
public static void main(String args[])
{
int a,b;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the first number : ");
a=obj.nextInt();
System.out.print("\nEnter the second number : ");
b=obj.nextInt();
arithmetic_operations rd=new arithmetic_operations();
rd.addition(a,b);
rd.subtraction(a,b);
rd.multiplication(a,b);
rd.division(a,b);
}
}
Output:
6. Define a package named ‘useFul’ with a class names ‘UseMe’ having
following methods:
1)area()- To calculate the area of given shape.
2)salary()- To calculate the salary given basic Salary,da,hRA.
3)percentage()-To calculate the percentage given total marks and marks
obtained.
4)Develop a program named ‘Package Use’ to import the above package
‘useFul’ and use the method area().
5) Develop a program named ‘manager’

Code:
Package Creation:
package useFull;
import java.util.*;
public class UseMe
{
Scanner obj=new Scanner(System.in);
public static void area()
{
class method{

void aos(int a)
{
System.out.print("\nArea of square with length "+a+" is
"+(a*a));
}
void aor(int a,int b)
{
System.out.print("\nArea of reactangle with dimensions
"+a+" & "+b+" is "+(a*b));
}
void aoc(int r)
{
double a=3.14*r*r;
System.out.print("\nArea of circle with radius "+r+" is
"+a);
}
void aot(int a,int b)
{
float ar=(a*b)/2;
System.out.print("\nArea of triangle with dimensions
"+a+" &"+b+" is "+ar);
}
}
Scanner obj=new Scanner(System.in);
method m=new method();
System.out.print("\n1.Square\n2.Rectangle\n3.Circle\
n4.Triangle\nSelect the shape\n");
int ch=obj.nextInt();
UseMe u=new UseMe();
switch(ch)
{
case 1:System.out.print("\nEnter the length of side of
square : ");
int s=obj.nextInt();
m.aos(s);
break;
case 2:System.out.print("\nEnter the dimensions of
rectangle : ");
int l=obj.nextInt();
int b=obj.nextInt();
m.aor(l,b);
break;
case 3:System.out.print("\nEnter the radius of circle : ");
int r=obj.nextInt();
m.aoc(r);
break;
case 4:System.out.print("\nEnter the dimensions of
triangle : ");
int ba=obj.nextInt();
int w=obj.nextInt();
m.aot(ba,w);
break;
}
}
public void salary()
{
int ba,da,hra;
System.out.print("\nEnter the basic salary : ");
ba=obj.nextInt();
System.out.print("\nEnter the dearness allownce : ");
da=obj.nextInt();
System.out.print("\nEnter the house rent allowance : ");
hra=obj.nextInt();
System.out.print("\nThe total Gross salary if employee is : "+
(ba+da+hra));
}
public void percentage()
{
int n,sum=0;
float p;
System.out.print("\nEnter the total number of subjects : ");
n=obj.nextInt();
int m[]=new int[n];
System.out.print("\nEnter the marks of "+n+" subjects : ");
for(int i=0;i<n;i++)
{
m[n]=obj.nextInt();
}
for(int i=0;i<n;i++)
{
sum=sum+m[i];
}
p=sum/n;
{
System.out.print("\nPercentahe of student : "+p);
}
}
}
Package Implementation:
1]
import useFull.UseMe;
class packageUse
{
public static void main(String args[])
{
UseMe o=new UseMe();
o.area();
}
}
Output:

2]
import useFull.UseMe;
class manager
{
public static void main(String args[])
{
UseMe obj=new UseMe();
obj.salary();
}
}
Output:

You might also like