Page 199 - CTS - CSA TP - Volume 2
P. 199

COMPUTER SOFTWARE APPLICATION - CITS



           EXERCISE 115 : Create and use a package in JAVA



            Objectives

           At the end of this exercise you shall be able to
           •  develop Java programs to Create and use a package in JAVA.

           Requirements

           Tools/Materials
           •  PC / laptop with windows OS
           •   SDK software
           •  Test editor (Visual studio/ subline/ notepad)

           Procedure


           TASK 1: Create and use a package
           package p2;
           import java.util.Scanner;
           public class Sub {

               int d;
               public void diff() {
                   Scanner scan = new Scanner(System.in);
                   System.out.print(“Enter the first number: “);
                   int x = scan.nextInt();



                   System.out.print(“Enter the second number: “);
                   int y = scan.nextInt();


                   d = x - y;

                   System.out.println(“Difference = “ + d);


                   // Close the Scanner to release resources
                   scan.close();
               }

               // Main method for testing
               public static void main(String[] args) {
                   Sub sub = new Sub();
                   sub.diff();
               }

           }







                                                           184
   194   195   196   197   198   199   200   201   202   203   204