0% found this document useful (0 votes)
104 views15 pages

Java Packages and Interfaces Examples

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views15 pages

Java Packages and Interfaces Examples

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Programs

Chapter 3: Packages & interfaces


1. Write a program to implement following hierarchy:

Code:

import [Link].*;
import [Link].*;
interface Exam {
public void percent_cal();
}
class Student {
String name;
int roll_no;
float marks1,marks2;
Scanner obj=new Scanner([Link]);
void accept() {
[Link]("\nEnter the name of student : ");
name=[Link]();
[Link]("\nEnter the roll_no of student : ");
roll_no=[Link]();
[Link]("\nEnter the marks 1 of student : ");
marks1=[Link]();
[Link]("\nEnter the marks 2 of student : ");
marks2=[Link]();
}
}
class Result extends Student implements Exam {
public void percent_cal() {
[Link]("Percentage : "+(marks1+marks2)/2);
}
void display() {
[Link]("\nStudent name : "+name);
[Link]("\nStudent Roll no. : "+roll_no);
[Link]("\nStudent marks 1 : "+marks1);
[Link]("\nStudent marks 2 : "+marks2);
// [Link]("\nStudent percentage : "+p);
}
public static void main(String args[]) {
Result rd=new Result();
[Link]();
rd.percent_cal();
[Link]();
}
}
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 [Link].*;
import [Link].*;
class Employee{
Scanner obj=new Scanner([Link]);
String name;
double basic_salary,expense;
void accept() {
[Link]("\nEnter the name of employee : ");
name=[Link]();
[Link]("\nEnter the basic salary of employee : ");
basic_salary=[Link]();
[Link]("\nEnter the Expense : ");
expense=[Link]();
}
}
interface Gross{
public void gross_salary();
}
class Salary extends Employee implements Gross{
public void gross_salary() {
[Link]("Gross Salary : "+(basic_salary+expense));
}
void display() {
[Link]("\nEmployee name : "+name);
[Link]("\nEmployee salary : "+basic_salary);
[Link]("\nEmployee Expence : "+expense);
}
public static void main(String args[]) {
Salary rd=new Salary();
[Link]();
rd.gross_salary();
[Link]();
}
}
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;
[Link]("\nArea of triangle : "+a);
}
}
Package Implementation :
import [Link].*;
import [Link];
class rectdemo{
public static void main(String args[]) {
int l,b;
Scanner obj=new Scanner([Link]);
[Link]("\nEnter the length of triangle : ");
l=[Link]();
[Link]("\nEnter the beadth of triangle : ");
b=[Link]();
area r=new area();
[Link](l,b);
}
}
Output:
4. Implement the inheritance given below by assuming suitable methods.

Code:
import [Link].*;
class Person{
String name,address;
Scanner obj=new Scanner([Link]);
void get() {
[Link]("\nEnter the name of employee : ");
name=[Link]();
[Link]("\nEnter the address of employee : ");
address=[Link]();
}
}
interface Manager{
public int salary=10000;
public void accept();
public void salary();
}
class Employee extends Person implements Manager{

public void accept(){}


public void salary() {
[Link]("\nEmployee salary : "+salary);
}
void display() {
[Link]("\nEmployee name : "+name);
[Link]("\nEmployee address : "+address);
}
public static void main(String args[]) {
Employee d=new Employee();
[Link]();
[Link]();
[Link]();
[Link]();
}

}
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;
[Link]("\nAddition of "+x+" and "+y+" is "+r);
}
public void subtraction(int x,int y)
{
int r=x-y;
[Link]("\nSubtraction of "+x+" and "+y+" is "+r);
}
public void multiplication(int x,int y)
{
int r=x*y;
[Link]("\nMultiplication of "+x+" and "+y+" is "+r);
}
public void division(int x,int y)
{
double r=x/y;
[Link]("\nDivision of "+x+" and "+y+" is "+r);
}

}
Implementing Package :
import [Link].*;
import [Link].*;
import arithmetic.arithmetic_operations;
class arithmeticdemo
{
public static void main(String args[])
{
int a,b;
Scanner obj=new Scanner([Link]);
[Link]("\nEnter the first number : ");
a=[Link]();
[Link]("\nEnter the second number : ");
b=[Link]();
arithmetic_operations rd=new arithmetic_operations();
[Link](a,b);
[Link](a,b);
[Link](a,b);
[Link](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 [Link].*;
public class UseMe
{
Scanner obj=new Scanner([Link]);
public static void area()
{
class method{

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

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

Common questions

Powered by AI

Polymorphism via interfaces "Exam" and "Gross" is evident as different classes implement these interfaces and provide their own definitions of methods like "percent_cal" and "gross_salary". Classes like "Result" and "Salary" use these interfaces to define unique behaviors for calculating results and salaries, allowing different implementations for different contexts while adhering to the interface contract, thereby enabling flexible and dynamic behaviors in code execution .

In Java, interfaces like "Exam" and "Gross" facilitate polymorphism by allowing classes to implement these interfaces and thereby adhere to a contract. In the provided examples, the interface "Exam" has a method "percent_cal" which is implemented by the class "Result", allowing different behaviors while ensuring any class implementing this interface will adhere to the method specified by "Exam". Similarly, the "Gross" interface defines a way to calculate gross salary, and it is implemented by the class "Salary", allowing for polymorphic behavior across different employment contexts .

Interfaces and packages significantly enhance code reusability and reliability in software design. Interfaces, like "Exam" and "Gross", define contracts that multiple classes can implement, ensuring consistency and enabling polymorphic substitutions, thus promoting reusability across various contexts. Packages like "rectangle" and "arithmetic" logically organize classes, managing namespaces and dependencies, which improves reliability by reducing errors related to duplicated code and namespace collisions. By encapsulating functionality, they also simplify maintenance and updates across software systems .

Interfaces in Java, such as "Exam" and "Gross", support the principle of "write once, run anywhere" by defining a set of methods that classes agree to implement, without dictating how they should be implemented. This abstraction separates the interface definition from the implementation details, allowing classes to be used interchangeably in different environments. As the implementation details are platform-independent, the same bytecode can run on any machine equipped with a Java Virtual Machine (JVM), honoring Java's architectural neutrality .

Using an interface like "Gross" to calculate salaries offers flexibility and encourages loose coupling. Interfaces allow "Salary" to implement multiple behaviors across different classes. However, unlike abstract classes, interfaces cannot have fields or implemented methods until Java 8. If shared fields or methods with default behaviors are necessary, an abstract class would be more suitable. The choice depends on whether the design requires multiple inheritance, possible with interfaces, or shared state, better managed by abstract classes .

Packages in Java, as shown with "rectangle" and "arithmetic", are used to group related classes and interfaces together, enabling better modularity and namespace management. The "rectangle" package contains a class that calculates the area of a rectangle, while the "arithmetic" package contains a class that performs basic arithmetic operations. Importing these packages allows external classes to use these functionalities, promoting code reusability and organization .

Inheritance in Java is demonstrated by the "Employee" class, which extends the "Person" class and implements the "Manager" interface. This design allows "Employee" to inherit properties like "name" and "address" and methods like "get" from "Person" while also implementing behavior defined by the "Manager" interface, such as a fixed "salary" value. This approach combines inheritance and interface implementation to build complex class hierarchies and to ensure that an "Employee" is a "Person" by relationship and a "Manager" by capability, promoting code reuse and polymorphic behavior .

Java interfaces like "Exam" and "Gross" facilitate dynamic behavior by defining methods that can be implemented by any class, allowing instructions to be executed in various forms depending on the object's runtime class. This enables late binding where the specific implementation is determined at runtime, thus allowing flexibility and adaptability in the program's behavior as seen in classes such as "Result" and "Salary" that provide distinct implementations of interface methods while maintaining a shared interface reference .

Encapsulating arithmetic operations in a separate package, like in the "arithmetic" package, enhances maintainability, reusability, and organization of the code. It allows a clear separation of concerns where arithmetic logic is maintained independently of business logic. This modular approach facilitates easier updates and debugging for arithmetic logic without affecting the main application. It also allows other applications to reuse these operations by simply importing the package, promoting code reuse and reducing redundancy .

Packages, like "rectangle" and "useFul", play a crucial role in organizing code in large Java applications by grouping related classes into namespaces, reducing naming conflicts, and enhancing code readability and maintainability. By encapsulating related functionalities, packages allow developers to separate functionality into discrete units that can be developed, tested, and maintained independently. This modular structure promotes code reusability and allows multiple developers to collaborate on large projects more efficiently by minimizing the risk of conflicts across the codebase .

You might also like