Lab 5: Packages And Array In Java
Lab 5: Packages And Array In Java
Package in Java
Definition: A package is a grouping of related types providing access protection and namespace management. Note that types refer to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this lesson simply as classes and interfaces.
Why we need to create a package
Purpose: To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages.
Case: Suppose you write a group of classes that represent graphic objects, such as circles, rectangles, lines, and points. You also write an interface, Draggable
, that classes implement if they can be dragged with the mouse.
//in the Draggable.java file public interface Draggable { ... } //in the Graphic.java file public abstract class Graphic { ... } //in the Circle.java file public class Circle extends Graphic implements Draggable { . . . } //in the Rectangle.java file public class Rectangle extends Graphic implements Draggable { . . . } //in the Point.java file public class Point extends Graphic implements Draggable { . . . } //in the Line.java file public class Line extends Graphic implements Draggable { . . . }
You should bundle these classes and the interface in a package
for several reasons, including the following:
- You and other programmers can easily determine that these types are related.
- You and other programmers know where to find types that can provide graphics-related functions.
- The names of your types won’t conflict with the type names in other packages because the package creates a new namespace.
- You can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package.
Creating a Package in Java
To create a package, you choose a name for the package
(naming conventions are discussed in the next section) and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package. For instance, The package statement (for example, package graphics
😉 must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. Here, the package name is ‘graphics’.
//in the Draggable.java file package graphics; public interface Draggable { . . . } //in the Graphic.java file package graphics; public abstract class Graphic { . . . } //in the Circle.java file package graphics; public class Circle extends Graphic implements Draggable { . . . } //in the Rectangle.java file package graphics; public class Rectangle extends Graphic implements Draggable { . . . } //in the Point.java file package graphics; public class Point extends Graphic implements Draggable { . . . } //in the Line.java file package graphics; public class Line extends Graphic implements Draggable { . . . }
Note: If you do not use a package statement
, your type ends up in an unnamed package which is like default. Honestly speaking, an unnamed package is only for small or temporary applications or when you are just beginning the development process. Otherwise, classes and interfaces belong in named packages.
Case 0: All the import statement must be written above of the class keyword
//save as Temp.java import java.awt.*; // "import" keyword is responsible for importing package import java.util.*; // ‘*’ includes all the classes & interfaces of that package class Temp{ public static void main(String s[]){ Frame f=new Frame(); // “Frame” is class of awt package Date d=new Date(); // “Date” is a class of util package System.out.println(d); // reference variable d of class is containing the current date and time } } /* Output: F:\coreJAVA\9Packages>javac Temp.java F:\coreJAVA\9Packages>java Temp Wed Aug 11 10:15:40 IST 1993 */
Case 1: Without any import statement
//save as Temp2.java class Temp2{ public static void main(String s[]){ // Fully qualified class path java. uti l.Date d= new java.util.Date(); System.out.println(d); } } /* Output: F:\coreJAVA\9Packages>javac Temp2.java F:\coreJAVA\9Packages>java Temp2 Wed Aug 11 10:15:40 IST 2020 */
Array in Java
Normally, an ARRAY
is a collection of similar type of elements that have a contiguous memory location.Java array is an object which contains elements of a similar data type. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
Case 0: Creating a dynamic array
// save as Array1.java class Array1{ public static void main(String s[]){ int x[]=new int[10]; // creating array as dynamically for(int i=0;i<x.length;i++){ // for loop to store array x[i]=i+1; } System.out.print("Array is:"); // to print in same line for(int i=0;i<x.length;i++){ // for loop to print array System.out.print(" "+x[i]); // to print array } System.out.println("\nsize of array is: "+ x.length); // to print size of array } }
Output
Javac Array1.java Java Array1 Array is: 1 2 3 4 5 6 7 8 9 10 size of array is : 10
Case 1: Finding a maximum value of an array
// save as Array2.java class Array2{ public static void main(String s[]){ int x[]={100,20,30,40,50,900};// array initialization for(int i=0;i<x.length;i++){ System.out.println(x[i]); } //finding the maximum value of array int max = x[0]; for(int i = 0; i < x.length; i++) { // comparison between among numbers if(max < x[i]){ max = x[i]; } } //printing the maximum value System.out.println("Maximum value:"+max); } } /* Output: F:\coreJAVA\10ARRAY>javac Array2.java F:\coreJAVA\10ARRAY>java Array2 100 20 30 40 50 900 Maximum value: 900 */
Case 2: Array declaration in constructor
//save as Array3.java class Array3{ int []a; Array3(int z){ // constructor a=new int[z]; System.out.print("Array is:"); // to print array for(int i=0;i<a.length;i++){ a[i]=i+1; System.out.print(" "+a[i]); } } public static void main (String ...s){ Array3 arr=new Array3(12); } } /* Output: F:\coreJAVA\10ARRAY>javac Array3.java F:\coreJAVA\10ARRAY>java Array3 Array is: 1 2 3 4 5 6 7 8 9 10 11 12 */
Case 3: Passing array to method:
// save as Array4.java class Array4{ //creating a method which receives an array as a parameter static void min(int arr[]){ int min=arr[0]; for(int i=1;i<arr.length;i++)// finding minimum if(min>arr[i]) min=arr[I]; //to print minimum value System.out.println("minimum elements of an array is "+min); } public static void main(String args[]){ //declaring and initializing an array int a[]={33,3,4,50,60,80,10}; min(a);//passing array to method } } /* Output: F:\coreJAVA\10ARRAY>javac Array4.java F:\coreJAVA\10ARRAY>java Array4 minimum elements of an array is 3 */
Case 4: Creation of Anonymous Array
//save as AnonymousArray.java public class AnonymousArray{ /* creating a method which receives an array as a parameter static method can be referenced from static context */ static void printArray(int arr[]){ for(int i=0;i<arr.length;i++) System.out.println(arr[i]);// printing array } public static void main(String args[]){ //passing anonymous array to method printArray(new int[]{10,22,44,66}); } } /* Output: F:\coreJAVA\10ARRAY>javac AnonymousArray.java F:\coreJAVA\10ARRAY>java AnonymousArray 10 22 44 */
Case 5: ArrayIndexOutOfBoundsException in a Java Array
//save as TestArrayException.java class TestArrayException{ public static void main(String args[]){ int arr[]={50,60,70,80}; for(int i=0;i<=arr.length;i++){ System.out.println(arr[i]); } } }
Output
F:\coreJAVA\image>javac TestArrayException.java F:\coreJAVA\image>java TestArrayException 50 60 70 80 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at TestArrayException.main(TestArrayException.java:6)
Case 6: Array of Arrays
//save as Array5.java class Array5 { public static void main(String args[]) { // declaring and initializing 2D array int arr[][] = { { 1, 2, 3 }, { 2, 4, 5 }, { 4, 4, 5 } }; // printing 2D array for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } } /* Output: F:\coreJAVA\10ARRAY>javac AnonymousArray.java F:\coreJAVA\10ARRAY>java AnonymousArray 1 2 3 2 4 5 4 4 5 */
Case 7: Addition of Array
//save as Array6.java class Array6{ public static void main(String s[]){ int a[][]={{2,4,6},{3,5,7}}; // initializing 2D array int b[][]={{1,6,3},{5,10,5}}; int c[][]=new int[2][3]; // creating variable to store the result System.out.println("sum of two matrices:"); for(int i=0;i<2;i++){ // logic of adding two matrices for(int j=0;j<3;j++){ c[i][j]=a[i][j]+b[i][j]; System.out.print(" "+c[i][j]+" "); } System.out.println(); // to change line } } }
Output
javac Array6.java jav Array6 sum of two matrices: 3 10 9 8 15 12
Case 8: Multiplication of Array
// save as MatrixMulti.java class MatrixMulti{ public static void main(String s[]){ int a[][]={{1,2,3},{2,2,2},{3,3,3}}; int b[][]={{1,2,1},{2,2,2},{3,3,3}}; int c[][]=new int[3][3]; // matrix of 3*3 for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ c[i][j]=0; for(int k=0;k<3;k++){ c[i][j]=c[i][j]+a[i][k]*b[k][j]; // logic of array multiplication } System.out.print(c[i][j]+" "); } System.out.println(); } } }
Output
javac MatrixMulti.java java MatrixMulti 14 15 14 12 14 12 18 21 18