Saturday, September 7, 2019

MCA Package


A Package MCA which has one class Student. Accept student detail through parameterized constructor. Write display () method to display details. Create a main class which will use package and calculate total marks and percentage.


Packages in Java is a mechanism to encapsulate a group of classes, interfaces and sub packages. Many implementations of Java use a hierarchical file system to manage source and class files. It is easy to organize class files into packages. All we need to do is put related class files in the same directory, give the directory a name that relates to the purpose of the classes, and add a line to the top of each class file that declares the package name, which is the same as the directory name where they reside.
In java there are already many predefined packages that we use while programming.
For example: java.langjava.iojava.util etc.
However one of the most useful feature of java is that we can define our own packages

Advantages of using a package
·         Reusability:  Reusability of code is one of the most important requirements in the software industry. Reusability saves time, effort and also ensures consistency. A class once developed can be reused by any number of programs wishing to incorporate the class in that particular program.
·         Easy to locate the files.
·         In real life situation there may arise scenarios where we need to define files of the same name. This may lead to “name-space collisions”. Packages are a way of avoiding “name-space collisions”.
Types of package:

1.      Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages.
2.       User defined package: The package we create is called user-defined package.

Built-in Packages
These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported.
2)  java.io: Contains classed for supporting input / output operations.
3)  java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations.
4)  java.applet: Contains classes for creating Applets.
5)  java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc).
6)  java.net: Contain classes for supporting networking operations.


User defined package
Creating a Package
While creating a package, you should choose a name for the package and include a package statement along with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.
The package statement should 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.
If a package statement is not used then the class, interfaces, enumerations, and annotation types will be placed in the current default package.
To compile the Java programs with package statements, you have to use -d option as shown below.

javac -d Destination_folder file_name.java

Then a folder with the given package name is created in the specified destination, and the compiled class files will be placed in that folder.

Defining a Package:

This statement should be used in the beginning of the program to include that program in that particular package.
package  <package name>;
Use package keyword
Is very first statement in JAVA program
            package pkg_name;
if you want to create sub-packages
package rootPackage.subPackage1;
Must have exactly one public class
If you want more than one public class in same package then create different files for each public class.
Save JAVA program with public class class_name
.           public class class_name {
                        }

Example
Let us look at an example that creates a package called animals. It is a good practice to use names of packages with lower case letters to avoid any conflicts with the names of classes and interfaces.
Following package example contains interface named animals −

package animals;

interface Animal {
   public void eat();
   public void travel();
}
Now, let us implement the above interface in the same package animals 

package animals;
/* File name : MammalInt.java */

public class MammalInt implements Animal {

   public void eat() {
      System.out.println("Mammal eats");
   }

   public void travel() {
      System.out.println("Mammal travels");
   }

   public int noOfLegs() {
      return 0;
   }

   public static void main(String args[]) {
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
}
Now compile the java files as shown below −

$ javac -d . Animal.java
$ javac -d . MammalInt.java

Now a package/folder with the name animals will be created in the current directory and these class files will be placed in it.

You can execute the class file within the package and get the result as shown below.
Mammal eats
Mammal travels

The import Keyword
If a class wants to use another class in the same package, the package name need not be used. Classes in the same package find each other without any special syntax

Algorithm:-
  1. Start
  2. create package MCA.
  3. Define class Student with its attributes, parameterized  constructor and methods in package MCA.
  4. Create a main program with class name package_Demo
  5. Import MCA to main program
  6. Create object of Student class inside main class.
  7. Invoke display() method of Student class on that object to display total marks and percentage.
  8. stop

PROGRAM :-
//Save as student.java
package MCA;
import java.util.*;
public class student
{int o,y,t=0,i;
double p;
String n;
int r[]=new int [50];
public student(int rollno,String name)
{
y=rollno;
n=name;
System.out.println("Enter no of subject");
Scanner s = new Scanner(System.in);
o=s.nextInt();
System.out.println("enter marks");
for(i=0;i<o;i++)
{
 System.out.println("Sub :"+(i+1));
 r[i]=s.nextInt();
}
for(i=0;i<o;i++)
{
t=t+r[i];
}
System.out.println(" "+t);
p=(t*100)/(o*100.0);
}
public void display()
{
System.out.println("<========RESULT========>");
System.out.print("Name :"+n);
System.out.println("      Roll No :"+y);
for(i=0;i<o;i++)
{
 System.out.print("Sub :"+(i+1));
 System.out.println("  "+r[i]);
}
System.out.print("Total Marks: "+t);
System.out.print("        Percentage "+p+"%");
}
}

//save as e.java
import java.util.*;
import MCA.student;
class e
{
public static void main(String []args)
{
String a;int b;
Scanner s =new Scanner(System.in);
System.out.println("enter Name");
a=s.nextLine();
System.out.println("enter Roll no");
b=s.nextInt();
student t =new student(b,a);
t.display();
}
}

 OUTPUT :-
enter Name
Chetan
enter Roll no
65
Enter no of subject
2
enter marks
Sub :1
89
Sub :2
93
 182
<========RESULT========>
Name :Chetan      Roll No :65
Sub :1  89
Sub :2  93
Total Marks:182        Percentage 91.0%

Observations and learning:  

 Learn To create user defined packages and there use in real world.

 Conclusion:  
By using package in java we can solve the given problem

No comments:

Post a Comment