Implementation Of Packages

/* Implementation Of Packages :: Import a package in a CLASS and use it. */

Java Code for First.java

package pack;

public class First
{
    static double amt,sim,comp,princ = 1000,rate = 0.09;
    statici nt  n=2;

    public First()
    {
        System.out.println("\n...Welcome to First Class\n");
    }

public static void DisplayData()
{
System.out.println("Principle Amount\t:"+princ+"\nRate Of Interest\t:"+rate*100+"%");
System.out.println("Term\t\t\t:"+n);
}
public static void SimpleInterest()
{
System.out.println("\nDisplaying Simple Interest...");
try 
{
Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
catch(InterruptedException e)
{
e.printStackTrace();
sim = (princ*n*rate);
System.out.println("\tInterest\t: "+sim);
}

public static void CompInterest()
{
    System.out.println("\nDisplaying Compound Interest...");
try 
{
Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
catch(InterruptedException e)
{
e.printStackTrace();
double ans = princ*(1+rate/n);
comp = Math.pow(ans,(double)n*4.0);
System.out.println("\tInterest\t: "+comp);
}
/* public static void main(String []args)
{
First f= new First();
f.DisplayData();
f.SimpleInterest();
f.CompInterest();
}*/
}

/* The socond file imports the first file and calculates the interst*/
/*Second.java*/

import pack.First;

class Second
{
    public static void main(String[] args)
    {
        System.out.println("Running Class Second..");
        First first = new First();
        first.DisplayData();
        first.SimpleInterest();
        first.CompInterest();
    }
}

-------------------- Output----------------------------------
Running Class Second..
Welcome to First Class
Principal Amount :1000.0
Rate Of Interest :9.0%
Term :2
Displaying Simple Interest...
Interest : 180.0
Displaying Compound Interest...
Interest : 1.4221006128366082E24

Previous
Next Post »