0% found this document useful (0 votes)
97 views

Java Exp6 c28

The document describes creating a user-defined package in Java called MCA that contains a Student class. The Student class accepts student details through a parameterized constructor and has a display() method. A main class called package_Demo imports the MCA package, creates a Student object, calculates the student's total marks and percentage by calling methods, and displays the output. The student wrote code for the Student class within the MCA package and for the main package_Demo class, tested it by running and received the expected output of student details, marks, and percentage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Java Exp6 c28

The document describes creating a user-defined package in Java called MCA that contains a Student class. The Student class accepts student details through a parameterized constructor and has a display() method. A main class called package_Demo imports the MCA package, creates a Student object, calculates the student's total marks and percentage by calling methods, and displays the output. The student wrote code for the Student class within the MCA package and for the main package_Demo class, tested it by running and received the expected output of student details, marks, and percentage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PART A

(PART A: TO BE REFFERED BY STUDENTS)


Experiment No. 06
Aim: Write 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.
Objective: To create user defined packages and there use in real world.
Outcome: Students created packages for various problems used in real world
Theory:
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.lang, java.io, java.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
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded at the end of the practical)
Roll. No. C28 Name: Prathmesh Krishna Gaikwad
Class: SE-C Batch: B-1
Date of Experiment: 29/10/2021 Date of Submission: 6/11/2021
Experiment No.: 06 Grade:
B.1 Software Code written by student:

package MCA;
public class Student
{
int r,maths,chem,pyhs,eng,bio;
String nm;
double marks=0;
double percentage;
public Student(int roll,String n,int m,int p,int c,int b,int e)
{
r=roll;
nm=n;
maths=m;
chem=c;
pyhs=p;
eng=e;
bio=b;
}
public void calculate()
{
marks=(double)maths+chem+pyhs+eng+bio;
percentage=(marks*100)/500;
}
public void display()
{
System.out.println("Roll NO.: "+r);
System.out.println("Name: "+nm);
System.out.println("-------------------");
System.out.println(" Marks ");
System.out.println("English : "+eng);
System.out.println("Mathematics : "+maths);
System.out.println("Physics : "+pyhs);
System.out.println("Chemistry : "+chem);
System.out.println("Biology : "+bio);
System.out.println("-------------------");
System.out.print("TotalMarks: ");
System.out.printf("%.0f",marks);
System.out.println();
System.out.println("Percentage: "+percentage+"%");
System.out.println("-------------------");
}
}

Main:
import MCA.*;
import java.util.*;
class package_Demo
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int m,p,c,e,b;
System.out.print("Enter the Roll No.= ");
int roll = sc.nextInt();
System.out.print("Enter the Name = ");
String name = sc.next();
System.out.println();
System.out.print("Enter the marks of Mathematics : ");
m=sc.nextInt();
System.out.print("Enter the marks of Physics : ");
p=sc.nextInt();
System.out.print("Enter the marks of Chemistry : ");
c=sc.nextInt();
System.out.print("Enter the marks of English : ");
e=sc.nextInt();
System.out.print("Enter the marks of Biology : ");
b=sc.nextInt();

Student s1= new Student(roll,name,m,p,c,e,b);


System.out.println();
System.out.println("Student Information,Marks and
Percentage are as follows:");
s1.calculate();
s1.display();
}
}
B.2 Input and Output:

B.3 Observations and learning:


Ans-Learning parameterized constructors.

B.4 Conclusion:
Ans- Writing a Package MCA which has one class Student and accepting student detail through
parameterized constructor.

B.5 Question of Curiosity

1.What are packages ? what is use of packages ?


Ans-
A)Package in Java is a mechanism to encapsulate a group of classes, sub packages and
interfaces. Packages are used for: Preventing naming conflicts
B)Packages (sometimes called namespaces) are used to give a separate identity (name)
to a group of classes. (Note: Packages are primarily used in Java and Actionscript.) For
example, you are writing a fishing program and a finance program. Both programs use a
Bank class but for entirely different reasons.
2.What is difference between importing "java.applet.Applet" and "java.applet.* "?
Ans- When you import java.applet.*, it means you are importing all the classes available
in the java.applet package. When you import java.applet.Applet, it means you are
importing the single class. It is better to import the classes that you need instead of
importing all the classes in the package.When you import java.applet.*, it means you are
importing all the classes available in the java.applet package. When you import
java.applet.Applet, it means you are importing the single class. It is better to import the
classes that you need instead of importing all the classes in the package.

3.Which package is always imported by default?


Ans- The java. lang package is always imported by default.

4.Can I import same package/class twice? Will the JVM load the package twice at
runtime?
Ans- Yes, we can import a class twice in Java, it doesn't create any issues but,
irrespective of the number of times you import, JVM loads the class only once.

5.Does importing a package imports the sub packages as well? E.g. Does importing
com.bob.* also import com.bob.code.*?
Ans- No we will have to import the sub packages explicitly. Importing com.bob.* will
import classes in the package bob only. It will not import any class in any of its sub
packages.

6.Explain the usage of Java packages.


Ans- Packages are used in Java in order to prevent naming conflicts, to control access,
to make searching/locating and usage of classes, interfaces, enumerations and
annotations easier, etc.

7.Are the imports checked for validity at compile time? e.g. will the code containing an
import such as java.lang.BOB compile?
Ans- the imports are checked for the semantic validity at compile time. The code containing
above line of import will not compile. It will throw an error saying, can not resolve symbol
symbol : class BOB
location: package io
import java.io.BOB;
8.Name few classes that are part of java.io package ?
Ans- Following are the important classes in Java.io package:
i)BufferedInputStream.
ii)BufferedOutputStream.
iii)BufferedReader.
iv)BufferedWriter.
v)ByteArrayInputStream.
vi)ByteArrayOutputStream.
vii)CharArrayReader.
viii)CharArrayWriter – Set1 Set2.

You might also like