PACKAGES USING JAVA


ALGORITHM:
Step  1: Start the process.
Step  2: Create a package called “sample” using mkdir command in the command prompt.
Step  3: Change the directory from parent directory to the newly created sub directory using   
              cd command.
Step  4: Inside the package create two files containing two different classes called
             personal_details and mark_details.
Step  5: Calculate the total, average and grade of the student inside the class mark_details.
Step  6: Make the scope of both the two classes and all the functions defined inside these two 
             classes as “public”.
Step  7: Inherit the personal_details class inside the mark_details class using “extends”
             keyword.
Step  8: Inside the parent directory create a class called “pack” and include the package header
             file for using the classes available in the sample package.
Step  9: Create object for mark_details class and invoke all the functions using the object.
Step 10: Save and execute the program.
Step 11: Stop the process.
 
PROGRAM:
package sample;
import java.io.*;
public class personal_details
{
String name;
String address;
String phone_no;
int mark[]=new int[5];
int total=0,i;
float avg;
public void get_data()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter Name:");
name=d.readLine();
System.out.println("Enter Address:");
address=d.readLine();
System.out.println("Enter Phone Number:");
phone_no=d.readLine();
}
public void display()throws IOException
{
System.out.println("Name:"+name);
System.out.println("Address:"+address);
System.out.println("Phone Number:"+phone_no);
}
}
package sample;
import java.io.*;
public class mark_details  extends personal_details
{
String reg_no;
int i;
char grade;
public void get_data()throws IOException
{
super.get_data();
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter Register Number:");
reg_no=d.readLine();
System.out.println("Enter Marks:");
for(i=0;i<5;i++)
mark[i]=Integer.parseInt(d.readLine());
}
public void calc_total()throws IOException
{
for(int i=0;i<5;i++)
total+=mark[i];
avg=total/5;
}
public void calc_grade()throws IOException
{
if(mark[0]<50 || mark[1]<50 || mark[2]<50 || mark[3]<50 || mark[4]<50)
grade='F';
else
{
if(avg>=50 && avg<60)
grade='C';
else if(avg>=60 && avg<70)
grade='B';
else if(avg>=70 && avg<80)
grade='A';
else if(avg>=80 && avg<90)
grade='S';
else
grade='O';
}
}
public void display()throws IOException
{
super.display();
System.out.println("Register Number:"+reg_no);
System.out.println("Marks:");
for(int i=0;i<5;i++)
System.out.println(+mark[i]);
System.out.println("Total:"+total);
System.out.println("Average:"+avg);
System.out.println("Grade:"+grade);
}
}
import java.io.*;
import sample.*;
class pack
{
public static void main(String args[])throws IOException
{
mark_details ob=new mark_details();
ob.get_data();
ob.calc_total();
ob.calc_grade();
ob.display();
}
}

0 comments: