Java Exp6 c28
Java Exp6 c28
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.
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.
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.
package animals;
interface Animal {
Now, let us implement the above interface in the same package animals
package animals;
void eat() {
System.out.println("Mammal eats");
} public void
travel() {
System.out.println("Mammal travels");
return 0;
}
public static void main(String args[]) {
m.eat();
m.travel();
$ 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
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();
B.4 Conclusion:
Ans- Writing a Package MCA which has one class Student and accepting student detail through
parameterized constructor.
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.
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.