Programming with Packages…!!!

In our earlier post we introduced you to package in Java.Today we shall make a program on this concept.The statement of the program is as follows:
“Write a program to keep student information in one package and student marks information in a class and try to access the data in the student information package and finally make a report with the complete information of the student.”
From the above statement we can understand that we need to create a package which all the information of the students.Then we need to use the data in student information package in a class which displays the final report about the students.
Before creating a package we need to create a folder of any desired name.Then we need to place the code of the package class in the folder and compile the package without execution(Because it does not contain main method).Now we shall write the package which contains the roll numbers of the students as the data in it.I’m going to store about five roll numbers of the students.

package mypackage;
public class studentinfo
{
  int[] roll_no = new int[6];
 public studentinfo()
  {
    for(int i=1;i<=5;i++)
      roll_no[i]=i;
   }
 public void display(int j)
  {
    System.out.print("\nStudent roll-no is:" + roll_no[j]);
   }
}

Here the method studentinfo() is a constructor we it calls implicitly when an object for class studentinfo is created.
Now we shall write program to use package.

import mypackage.*;
class studentmarks extends studentinfo
{
 public static void main(String arg[])
  {
    int[] marks = new int[6];
    int k=1;
    for(int i=91;i<=95;i++)
     {
       marks[k]=i;
       k++;
     }
    studentinfo ob = new studentinfo();
    System.out.print("               *****Student details are******");
    for(int j=1;j<=5;j++)
     {
       ob.display(j);
       System.out.print("\nStudent marks is:" + marks[j]+"\n\n");
     }
  }
}

Output:
packages...problem
You can download package and the program.
1.Package.
2.Program.

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s