Tuesday, October 4, 2022

Practice Set on Java Package

- Create three classes Add, Sub, Mul and Calculator  into a package 

- Use a build-in package in java to write a class which displays a message ( by using sout ) after taking input from the user 

- Create a package in class with four package levels folder p1 , folder p2 , folder p3 & p5 with their respected method sum(), diff(), pro() and call all method into class Calculator.

..

Add.java

  1. package p1;  
  2. import java.util.*;  
  3. public class Add  
  4. {  
  5. int s;  
  6. public void sum()  
  7. {  
  8. System.out.print("Enter the first number: ");  
  9. Scanner scan=new Scanner(System.in);  
  10. int x=scan.nextInt();  
  11. System.out.print("Enter the second number: ");  
  12. Scanner scan1=new Scanner(System.in);  
  13. int y=scan1.nextInt();  
  14. s=x+y;  
  15. System.out.println("sum="+s);  
  16. }  
  17. }  

Sub.java

  1. package p2;  
  2. import java.util.*;  
  3. public class Sub  
  4. {  
  5. int d;  
  6. public void diff()  
  7. {  
  8. System.out.print("Enter the first number: ");  
  9. Scanner scan=new Scanner(System.in);  
  10. int x=scan.nextInt();  
  11. System.out.print("Enter the second number: ");  
  12. Scanner scan1=new Scanner(System.in);  
  13. int y=scan1.nextInt();  
  14. d=x-y;  
  15. System.out.println("Difference="+d);  
  16. }  
  17. }  

Mult.java

  1. package p3;  
  2. import java.util.*;  
  3. public class Mult  
  4. {  
  5. int m;  
  6. public void pro()  
  7. {  
  8. System.out.print("Enter the first number: ");  
  9. Scanner scan=new Scanner(System.in);  
  10. int x=scan.nextInt();  
  11. System.out.print("Enter the second number: ");  
  12. Scanner scan1=new Scanner(System.in);  
  13. int y=scan1.nextInt();  
  14. m=x*y;  
  15. System.out.println("Product="+m);  
  16. }  
  17. }  

Now, we are going to create the main class named Calculator. In this class, we have imported all the packages that we have created above. It includes all the classes in the Calculator class.

Calculator.java

  1. package p5;  
  2. //importing pre-defined package  
  3. import java.util.*;  
  4. //importing user-defined package  
  5. import p1.Add;  
  6. import p2.Sub;  
  7. import p3.Mult;  
  8. import p4.Div;  
  9. public class Calculator  
  10. {  
  11. public static void main(String args[])  
  12. {  
  13. System.out.print("Enter your choice: ");  
  14. Scanner scan=new Scanner(System.in);  
  15. int t=scan.nextInt();  
  16. switch(t)  
  17. {  
  18. case 1:  
  19. Add a=new Add();  
  20. a.sum();  
  21. break;  
  22. case 2:  
  23. Sub s=new Sub();  
  24. s.diff();  
  25. break;  
  26. case 3:  
  27. Mult m=new Mult();  
  28. m.pro();  
  29. break;  
  30. case 4:  
  31. Div d=new Div();  
  32. d.divd();  
  33. break;  
  34. }  
  35. }  
  36. }  

When we compile the above program, it creates corresponding .class files in packages named p1, p2, p3 and p5, respectively.

The .class files are generated. Now, we can run the above program.

Output:

Enter your choice: 3
Enter the first number: 2
Enter the second number: 23	
Product=46

..




No comments:

Post a Comment

techguruspeaks

  Core Java programming Introduction to Java Java Features Writing a Simple Java Program Java Keywords and Comments Variables, Identifiers &...