Sem 5‎ > ‎JAVA LAB‎ > ‎

Px11: WAP to create Student class and Marks class in two different packages

posted Aug 22, 2012, 10:08 AM by Neil Mathew   [ updated Aug 22, 2012, 10:09 AM ]


SOURCE CODE OF STUDENT CLASS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
        //package declaration first
package StudyHard;
 
        //Import statements
import java.io.*;
import java.lang.*;
 
        //Class
public class Student
{
String name;
long rollno;
 
public void getStudentDetails()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        //remember to remove the ln from SOP
System.out.print(" Enter Student Name: ");
name=br.readLine();
System.out.print(" Enter Roll No: ");
rollno=Long.parseLong(br.readLine());
}
 
        //public so that another package can access it 
        //(if no inheritance)
public void showStudentDetails()
{
System.out.println("\n Student Name: "+name);
System.out.println(" Enrollment No: "+rollno);
}
 
        /*Since this class is made to be accessed by another, 
        main() is not necessary, but to access this class directly,
        main function is necessary */
 
public static void main(String args[])throws IOException
{
Student ob=new Student();
ob.getStudentDetails();
ob.showStudentDetails();
}
 
}
 


SOURCE CODE OF MARKS CLASS:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package TopperMaterial;
 
import java.io.*;
import java.lang.*;
import StudyHard.*;
 
 
class Marks
{
        //array declarations different in java
int marks[] = new int[3];
double avg;
 
void getMarksDetails()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 
        //remember to remove ln
System.out.print(" Enter Marks of Physics: ");
marks[0] = Integer.parseInt(br.readLine());
 
System.out.print(" Enter Marks of Chemistry: ");
marks[1] = Integer.parseInt(br.readLine());
 
System.out.print(" Enter Marks of Mathematics: ");
marks[2] = Integer.parseInt(br.readLine());
 
avg = marks[0]+marks[1]+marks[2];
avg/=3;
 
}
 
void showMarksDetails()
{
System.out.println("\n Marks of Physics: "+marks[0]);
System.out.println(" Marks of Chemistry: "+marks[1]);
System.out.println(" Marks of Mathematics: "+marks[2]);
 
System.out.println(" Average Marks:"+avg);
}
 
public static void main(String args[]) throws IOException
{
        //The functions of Student should be public so 
        //that they can be accessed by the different package
        //(if no inheritance)
Student Sob=new Student();
Marks Mob=new Marks();
 
System.out.println("\n Enter Details: \n");
Sob.getStudentDetails();
Mob.getMarksDetails();
 
System.out.println("\n Showing Details: \n");
Sob.showStudentDetails();
Mob.showMarksDetails();
}
}


OUTPUT:




c:\>cd Java

 

c:\Java>cd RandomFolder

 

c:\Java\RandomFolder>path=C:\Java\jdk1.6.0_30\bin;

 

c:\Java\RandomFolder>javac -d . Student.java

 

c:\Java\RandomFolder>javac -d . Marks.java

 

c:\Java\RandomFolder>java TopperMaterial.Marks

 

 Enter Details:

 

 Enter Student Name: Neil Mathew

 Enter Roll No: 3305

 Enter Marks of Physics: 80

 Enter Marks of Chemistry: 85

 Enter Marks of Mathematics: 87

 

 Showing Details:

 

 

 Student Name: Neil Mathew

 Enrollment No: 3305

 

 Marks of Physics:80

 Marks of Chemistry:85

 Marks of Mathematics:87

 Average Marks:84.0

 

 


Comments