0% found this document useful (0 votes)
40 views7 pages

Javaprogram 9,10,11

This document demonstrates how to create a simple package in Java called "mypack" that contains a class "myfunction" with a method to check if a number is even. It then shows how to import and access that package and class in another Java program called "demo" to check if the number 5 is even.

Uploaded by

Abhishek Padul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views7 pages

Javaprogram 9,10,11

This document demonstrates how to create a simple package in Java called "mypack" that contains a class "myfunction" with a method to check if a number is even. It then shows how to import and access that package and class in another Java program called "demo" to check if the number 5 is even.

Uploaded by

Abhishek Padul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

# Java program to demonstrate the use of interfaces in java

interface Payable
{
public double getPayment();
}
class Invoice implements Payable
{
private String partNo;
private String partDesc;
private int qty;
private double ratePerUnit;

Invoice(String pNo, String Desc, int q, double rate)


{
partNo=pNo;
partDesc=Desc;
qty=q;
ratePerUnit=rate;
}
public String toString()
{
return String.format("%s: %s\n %s: %s\n %s: %d\n %s:%.2f",
"partNumber",partNo,"part Desc", partDesc, "Quantity",qty,
"Rate per Unit",ratePerUnit);
}
public double getPayment()
{
double pay=(double)qty*ratePerUnit;
return (pay);
}
}
class Employee implements Payable
{
private String empName;
private String empNo;
private double weeklySal;

Employee(String name, String No, double Sal)


{
empName=name;
empNo=No;
weeklySal=Sal;
}
public double getPayment()
{
return 4*weeklySal;
}
public String toString()
{
return String.format("%s: %s\n %s: %s\n %s: %.2f","Employee Name:
",empName,"Employee No: ",empNo,"Weekly Salary: ",weeklySal);
}
}

// Interface Test Class containing main method


public class Test_Interface
{
public static void main(String[] args)
{
Payable[] objects=new Payable[4];

objects[0]=new Invoice("P1001","RC Toy",1000,1200.8f);


objects[1]=new Invoice("P1002","Camera Drone",100,9200.4f);
objects[2]=new Employee("Aniket","EMP101",80000f);
objects[3]=new Employee("Affan","EMP102",100000f);

for(Payable obj: objects)


{
System.out.println("Details :\n"+obj);
System.out.println("Payment= "+obj.getPayment());
}
}
}
Output:
# Java program to demonstrate exception handling in java

import java.util.*;
public class multicatchloop
{
public static int div(int n,int d)
{
return n/d;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
boolean contuinueLoop = true;
do
{
try
{
System.out.println("numerator");
int n = sc.nextInt();
System.out.println("denominator");
int d = sc.nextInt();

int result = div(n,d);


System.out.printf("Result : %d/%d = %d\n",n,d,result);
contuinueLoop = false;
}
catch (InputMismatchException inputMismatchException)
{
System.err.printf("\nException : %s\n",inputMismatchException);
sc.nextLine();
System.out.println("You must enter integers . Please try again\n");
}
catch (ArithmeticException arithmeticException)
{
System.err.printf("\nException : %s\n",arithmeticException);
System.out.println("Zero is invalid denominator . Please try again\n");
}
}
while(contuinueLoop);
}
}
Output:
# Java program to create a simple package and access it in a program

package mypack;
public class myfunction{
public boolean iseven(int x)
{

if(x%2==0){
return true;
}
else{
return false;
}

}
}

import mypack.myfunction;
public class demo
{
public static void main(String[]args)
{
myfunction m1=new myfunction();
System.out.println("Is number 5 even="+m1.iseven(5));
}
}
Output:

You might also like