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

Student

The document contains a Java class named 'Student' that serves as a blueprint for creating student objects, encapsulating their personal data, academic records, and methods for inputting and displaying this information. It includes functionalities for setting and printing student data, calculating percentages, and sorting marks based on various subjects. Additionally, the class demonstrates object-oriented programming principles such as encapsulation and method usage in a practical context.

Uploaded by

panav.golyan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Student

The document contains a Java class named 'Student' that serves as a blueprint for creating student objects, encapsulating their personal data, academic records, and methods for inputting and displaying this information. It includes functionalities for setting and printing student data, calculating percentages, and sorting marks based on various subjects. Additionally, the class demonstrates object-oriented programming principles such as encapsulation and method usage in a practical context.

Uploaded by

panav.golyan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

File: Untitled Document 1 Page 1 of 13

import java.util.*;
/**
* Write a description of class Student here.
*
* @author (Panav Golyan)
* @admn_no (1529)
* @class(10A)
*/
/**
* Class is a blueprint for making objects
* Objects are instances of a class
*
* Encapsulation - is packing the data and the fuctions acting on the data in one capsule
eg:class
*
* Encapsulation is a principle of object oriented programming
*
* Getter methods save value for instance
*
* Main is a special funtion that is static that is used to start the execution
*/
public class Student//User Defined data type for class Student
{
/**
* Escape sequence of characters is smth that conveys a special vslue . It is always
preceeded by a specified character. In java it
is back slash.eg:\t => tab ; \ is the escape character and t is the escape sequence
*/
static int std_no;//This variable stores the numeric value of the standard of the
student
char section; // his variable stores the section of the student
String name;//This variable stores the name of the student
String surname;//This variable stores the surname of the student
int admn_no;//This variable stores the numerical value of the admission number of the
student
float[] marks;//This variable stores the marks of the student
long contact_no; //This variable stores the contact number of the student
boolean assign;//This variable is the deciding factor if the student gets assigned or
not
Dates bdate ;//This variable stores the date

//This function sets the data of the student


void setData ()
{

Scanner sc = new Scanner (System.in);


System.out.print("Enter the name");
name = sc.next();
System.out.print("Enter the surname");
surname = sc.next();

System.out.print("Enter the contact_no");


contact_no = sc.nextLong();

bdate = new Dates();// creating an object hence the arrow outside as it is of type
Dates
bdate.readDate();

/**
* bdate.day = bday;//assigning value to the variables in the new created object
* bdate.month = month;
* bdate.year = year;
*/
}

//This function prints the personal data


File: Untitled Document 1 Page 2 of 13

void printPersonal(int day , int month , int year)


{

System.out.println("Name:" + " " + name);


System.out.println("Surname:" + " " + surname);
System.out.println("Contact no:" + " " + contact_no);
System.out.println("Admn_no:" + " " + admn_no);
//System.out.println("Birthdate:" + " " + bday + "/" + month + "/" + year);
bdate.differenceDates(day , month , year);
/**
* In this the bdate will have the instance variables which will be the birthdate
of the studentand today will have todays date
Hence we are creating two objects and printing the difference which is the age
*/
}

//This function prints in a different format


void printFinal(int day , int month , int year)
{
System.out.println("Admn_no:" + " " + "Name:" + " " + "Surname:" );
System.out.println( admn_no + " \t" +name + "\t " + surname + " \t " );
bdate.differenceDates(day , month , year);
}

//This function assings the admission number to the student


void setAdmn_no ()//this. keyword differnciates b/w local and instance
{
if(assign == false)
{
std_no +=1;
this.admn_no = std_no;
assign = true;
}
}

//This function sets the marks to the instance variable


void setMarks (float [] marks)
{
this.marks = marks;//pass by reference way , pass by value way is for loop but
null pointer will come so define length
}

//This function read marks from the user and assigns it to the instance variable
void readMarks()
{
int n;
Scanner sc = new Scanner (System.in);

System.out.print("Enter the number of the subjects for student "+ "\t");


n = sc.nextInt();
float [] marks = new float [n];
for(int i = 0; i < n ; i+=1)
{
System.out.print("Enter the marks of the subject"+(i+1)+ "\t");
marks[i] = sc.nextFloat();
}
setMarks(marks);//assigning the value of the local variable to the instance
variable by pass by reference
}

//This function computes the percentage of marks of the user


float computePercentage()
{
float percent = 0;
for(int i = 0; i< marks.length ; i+=1)
{
File: Untitled Document 1 Page 3 of 13

percent = percent + marks[i];


}
return ((percent/marks.length));
}

//This function prints the marks and the percentage of the student
void printMarks()
{
for(int i = 0; i<marks.length;i+=1)

System.out.print("Subject"+(i+1)+ "\t" );
System.out.print("Percentage:");

System.out.println();

for(int i = 0; i<marks.length;i+=1)
{
System.out.print( marks[i] + " \t");
}
System.out.print(computePercentage());
}

public static void main (String [] args)


{
Student s1;
s1 = new Student();

Student s2;
s2 = new Student();

Dates today;
today = new Dates();
System.out.println("Enter todays date:");
today.readDate();

System.out.println("Enter Data for student 1:");


s1.setData();
s1.readMarks();

System.out.println("Enter Data for student 2:");


s2.setData();
s2.readMarks();

System.out.println();
s1.setAdmn_no();
s1.computePercentage();

s2.setAdmn_no();
s2.computePercentage();

System.out.println("Data for Student 1:");


s1.printPersonal(today.day , today.month , today.year);
System.out.println();
s1.printMarks();
System.out.println();

System.out.println("-------------------------------------------------------------------------------------

System.out.println();
System.out.println("Data for Student 2:");
s2.printPersonal(today.day , today.month , today.year);
System.out.println();
s2.printMarks();
/**
* Output
File: Untitled Document 1 Page 4 of 13

* Data for Student 1:


Name: Panav
Surname: Golyan
Contact no: 7718640168
Admn_no: 1
The age is: 15years 6months 29days

Subject1 Subject2 Percentage:


97.0 98.0 97.50

---------------------------------------------------------------------------------------------

Data for Student 2:


Name: Shreyansh
Surname: Golyan
Contact no: 9749865342
Admn_no: 2
The age is: 19years 1months 28days

Subject1 Subject2 Percentage:


98.0 96.0 97.00
*/
}

/**
* Write a description of class student_marks here.
*
* @author (your name)
* @version (a version number or a date)
*/

//This function sorts the marks accorindng to Mathematics marks


void sortMaths (int[] roll_no , float[] marksMath , float[] marksEng , float[]
marksScience , float[] marksHCG)
{
int k = 0;
int n , r;
int i = 0;
float m , e , f , s , h ;

while(k <= marksMath.length)


{
i = 0;
while( i < marksMath.length - 1)
{
n = roll_no[i];
m = marksMath[i];
e = marksEng[i];
s = marksScience[i];
h = marksHCG[i];

if(m > marksMath[i+1] )


{
r = roll_no[i];
roll_no[i] = roll_no[i+ 1];
roll_no[i+1] = r;

f = marksMath[i] ;
marksMath[i] = marksMath[i + 1];
marksMath[i+1] = f;

f = marksEng[i];
marksEng[i] = marksEng[i+1];
marksEng[i+1] = f;
File: Untitled Document 1 Page 5 of 13

f = marksScience[i];
marksScience[i] = marksScience[i+1];
marksScience[i+1] = f;

f = marksHCG[i];
marksHCG[i] = marksHCG[i+1];
marksHCG[i+1] = f;
}
i++;
}
k++;
}
}

//This function sorts the marks accorindng to English marks


void sortEng (int[] roll_no , float[] marksMath , float[] marksEng , float[]
marksScience , float[] marksHCG)
{
int k = 0;
int n , r;
int i = 0;
float m , e , f , s , h ;

while(k <= marksEng.length)


{
i = 0;
while( i < marksEng.length - 1)
{
n = roll_no[i];
m = marksMath[i];
e = marksEng[i];
s = marksScience[i];
h = marksHCG[i];

if(e > marksEng[i+1] )


{
r = roll_no[i];
roll_no[i] = roll_no[i+ 1];
roll_no[i+1] = r;

f = marksMath[i] ;
marksMath[i] = marksMath[i + 1];
marksMath[i+1] = f;

f = marksEng[i];
marksEng[i] = marksEng[i+1];
marksEng[i+1] = f;

f = marksScience[i];
marksScience[i] = marksScience[i+1];
marksScience[i+1] = f;

f = marksHCG[i];
marksHCG[i] = marksHCG[i+1];
marksHCG[i+1] = f;
}
i++;
}
k++;
}
}

void sortScience (int[] roll_no , float[] marksMath , float[] marksEng , float[]


marksScience , float[] marksHCG)
{
int k = 0;
File: Untitled Document 1 Page 6 of 13

int n , r;
int i = 0;
float m , e , f , s , h ;

while(k <= marksScience.length)


{
i = 0;
while( i < marksScience.length - 1)
{
n = roll_no[i];
m = marksMath[i];
e = marksEng[i];
s = marksScience[i];
h = marksHCG[i];

if(s > marksScience[i+1] )


{
r = roll_no[i];
roll_no[i] = roll_no[i+ 1];
roll_no[i+1] = r;

f = marksMath[i] ;
marksMath[i] = marksMath[i + 1];
marksMath[i+1] = f;

f = marksEng[i];
marksEng[i] = marksEng[i+1];
marksEng[i+1] = f;

f = marksScience[i];
marksScience[i] = marksScience[i+1];
marksScience[i+1] = f;

f = marksHCG[i];
marksHCG[i] = marksHCG[i+1];
marksHCG[i+1] = f;
}
i++;
}
k++;
}
}

////This function sorts the marks accorindng to HCG marks


void sortHCG (int[] roll_no , float[] marksMath , float[] marksEng , float[]
marksScience , float[] marksHCG)
{
int k = 0;
int n , r;
int i = 0;
float m , e , f , s , h ;

while(k <= marksHCG.length)


{
i = 0;
while( i < marksHCG.length - 1)
{
n = roll_no[i];
m = marksMath[i];
e = marksEng[i];
s = marksScience[i];
h = marksHCG[i];

if(h > marksHCG[i+1] )


{
r = roll_no[i];
File: Untitled Document 1 Page 7 of 13

roll_no[i] = roll_no[i+ 1];


roll_no[i+1] = r;

f = marksMath[i] ;
marksMath[i] = marksMath[i + 1];
marksMath[i+1] = f;

f = marksEng[i];
marksEng[i] = marksEng[i+1];
marksEng[i+1] = f;

f = marksScience[i];
marksScience[i] = marksScience[i+1];
marksScience[i+1] = f;

f = marksHCG[i];
marksHCG[i] = marksHCG[i+1];
marksHCG[i+1] = f;
}
i++;
}
k++;
}
}

//This function sorts the marks accorindng to Roll number


void sortRollno (int[] roll_no , float[] marksMath , float[] marksEng , float[]
marksScience , float[] marksHCG)
{
int k = 0;
int n , r;
int i = 0;
float m , e , f , s , h ;

while(k <= roll_no.length)


{
i = 0;
while( i < roll_no.length - 1)
{
n = roll_no[i];
m = marksMath[i];
e = marksEng[i];
s = marksScience[i];
h = marksHCG[i];
if(n > roll_no[i+1] )
{
r = roll_no[i];
roll_no[i] = roll_no[i+ 1];
roll_no[i+1] = r;

f = marksMath[i] ;
marksMath[i] = marksMath[i + 1];
marksMath[i+1] = f;

f = marksEng[i];
marksEng[i] = marksEng[i+1];
marksEng[i+1] = f;

f = marksScience[i];
marksScience[i] = marksScience[i+1];
marksScience[i+1] = f;

f = marksHCG[i];
marksHCG[i] = marksHCG[i+1];
marksHCG[i+1] = f;
}
File: Untitled Document 1 Page 8 of 13

i++;
}
k++;
}
}

String [] Aggregate (int[] roll_no , float[] marksMath , float[] marksEng , float[]


marksScience , float[] marksHCG)
{
float marks = 0;
float k = 0;
float [] arr = new float[roll_no.length];
String [] agg = new String [roll_no.length];
for(int i = 0; i<roll_no.length;i+=1)
{
marks = marksMath[i] + marksEng[i] + marksScience[i] + marksHCG[i];
arr[i] = marks;
}

for(int i = 0; i < arr.length; i+=1)


{
k = (arr[i] / 4.0f);
if ( k >= 90)
{
agg[i] = "A";
}
else if( k >= 80 && k<90)
{
agg[i] = "B";
}
else if (k >= 70 && k< 80)
{
agg[i] = "C";
}
else
{
agg[i] = "D";
}
}
return agg;
}

//This function prints the roll number and the marks


void print(int[] roll_no , float[] marksMath , int order , float[] marksEng , float[]
marksScience , float[] marksHCG)
{
System.out.print("Roll no. \t");
System.out.println ("Maths: \t\tEnglish: \tScience: \t HCG: \tAggregate:" );
String [] arr = new String[roll_no.length];
arr = Aggregate(roll_no , marksMath , marksEng , marksScience ,marksHCG);
for(int c = 0; c < roll_no.length; c++)
{
System.out.print(roll_no[c] + "\t\t");
System.out.print(marksMath[c] + "\t\t");
System.out.print(marksEng[c] + "\t\t");
System.out.print(marksScience[c] + "\t\t");
System.out.print(marksHCG[c] + "\t\t");
System.out.print(arr[c]);
System.out.println();
}
}

//This function helps the user to sort accordingly


void sort (int[] roll_no , float[] marksMath , int order , float[] marksEng , float[]
marksScience , float[] marksHCG)
{
File: Untitled Document 1 Page 9 of 13

switch (order)
{
case 0:
sortRollno (roll_no ,marksMath ,marksEng ,marksScience ,marksHCG);
break;
case 1:
sortMaths (roll_no ,marksMath ,marksEng ,marksScience ,marksHCG);
break;
case 2:
sortEng (roll_no , marksMath , marksEng , marksScience ,marksHCG);
break;
case 3:
sortScience (roll_no ,marksMath ,marksEng ,marksScience ,marksHCG);
break;
case 4:
sortHCG (roll_no ,marksMath ,marksEng ,marksScience ,marksHCG);
break;
}
print(roll_no , marksMath,order,marksEng,marksScience,marksHCG);
}

//This is the main


void sortInteraction ()
{
int n = 0;
int k = 0;
int a = 0;
int r = 0;
Scanner sc = new Scanner(System.in);

System.out.print("Enter 0 to use the function and 1 to exit:");


a = sc.nextInt();

while(a != 1)
{

System.out.print("Enter the number of Students:");


n = sc.nextInt();

int [] roll_no = new int [n];


float [] marksMath = new float[n];
float [] marksEng = new float[n];
float [] marksScience = new float[n];
float [] marksHCG = new float[n];

for(int i = 0; i<roll_no.length;i+=1)
{
System.out.println("Enter the roll number of Student " + (i+1));
roll_no[i] = sc.nextInt();
System.out.println("Enter the Maths marks of Student " + (i+1));
marksMath[i] = sc.nextInt();
System.out.println("Enter the English marks of Student " + (i+1));
marksEng[i] = sc.nextInt();
System.out.println("Enter the Science marks of Student " + (i+1));
marksScience[i] = sc.nextInt();
System.out.println("Enter the HCG marks of Student " + (i+1));
marksHCG[i] = sc.nextInt();
}

System.out.print("Enter 0. to sort by roll no.");


System.out.print("Enter 1. to sort by Maths marks ");
System.out.print("Enter 2. to sort by Eng marks ");
System.out.print("Enter 3. to sort by Science marks ");
System.out.print("Enter 4. to sort by HCG marks");
k = sc.nextInt();
File: Untitled Document 1 Page 10 of 13

sort(roll_no , marksMath , k , marksEng , marksScience , marksHCG);


System.out.println();
System.out.print("Enter 0 to reuse the function for the same data or else
enter 1:");
r = sc.nextInt();
System.out.println();
while(r == 0)
{
System.out.print("Enter 0. to sort by roll no.");
System.out.print("Enter 1. to sort by Maths marks ");
System.out.print("Enter 2. to sort by Eng marks");
System.out.print("Enter 3. to sort by Science marks");
System.out.print("Enter 4. to sort by HCG marks");
k = sc.nextInt();

sort(roll_no , marksMath , k , marksEng , marksScience , marksHCG);

System.out.print("Enter 0 to reuse the function for the same data or else


enter 1:");
r = sc.nextInt();
}
System.out.println("Enter 0 to use the function for different data or 1 to
exit:");
a = sc.nextInt();
}
System.out.println();
System.out.print("Thank you for using the code:)");
/**
* Output:
* Enter 0 to use the function and 1 to exit:0
Enter the number of Students:2
Enter the roll number of Student 1
1529
Enter the Maths marks of Student 1
99
Enter the English marks of Student 1
98
Enter the Science marks of Student 1
90
Enter the HCG marks of Student 1
97
Enter the roll number of Student 2
1543
Enter the Maths marks of Student 2
99
Enter the English marks of Student 2
94
Enter the Science marks of Student 2
93
Enter the HCG marks of Student 2
90
Enter 0. to sort by roll no.Enter 1. to sort by Maths marks Enter 2. to sort by
Eng marks Enter 3. to sort by Science marks Enter 4. to sort by HCG marks0
Roll no. Maths: English: Science: HCG: Aggregate:
1529 99.0 98.0 90.0 97.0 A
1543 99.0 94.0 93.0 90.0 A

Enter 0 to reuse the function for the same data or else enter 1:1

Enter 0 to use the function for different data or 1 to exit:


1

Thank you for using the code:)


*/
}
File: Untitled Document 1 Page 11 of 13

//This reads marks and stores it in a 2D array with students as rows and marks as
columns
float [] [] read_marks ()
{
int n = 0;
int k = 0;

Scanner sc = new Scanner(System.in);


System.out.print("Enter the number of students:");
n = sc.nextInt();
System.out.print("Enter the number of subjects:");
k = sc.nextInt();
int [] roll_no = new int [n];
float[] [] marks = new float [n][k];

for(int i = 0; i<marks.length;i+=1)
{
System.out.print("Enter the roll_no of the student");
roll_no[i] = sc.nextInt();
for (int m = 0; m<marks[0].length;m+=1)
{
System.out.print("Enter the marks for subject " + (m+1));
marks[i][m] = sc.nextFloat();
}
}
return marks;
}

//This function sorts according to the order input


void sort2D (float [] [] marks , int [] roll_no , int order )//becuase of changing we
don't need to copy
{
float value = 0.0f;
int roll_val = 0;

for(int i = 0; i<roll_no.length;i++)
{
for(int c = 0; c<roll_no.length-1;c++)
{
if(order < marks.length)
{
if(marks[order][c] > marks[order][c+1])
{
for(int n = 0;n<marks.length;n++)
{
value = marks[n][c];
marks[n][c] = marks[n][c+1];
marks[n][c+1] = value;
}
roll_val = roll_no[i];
roll_no[i] = roll_no[i+1];
roll_no[i+1] = roll_val;
}
}
else
{
if(roll_no[c] > roll_no[c+1])
{
roll_val = roll_no[i];
roll_no[i] = roll_no[i+1];
roll_no[i+1] = roll_val;
for(int n = 0;n<marks.length;n++)
{
value = marks[n][c];
marks[n][c] = marks[n][c+1];
File: Untitled Document 1 Page 12 of 13

marks[n][c+1] = value;
}
}
}
}
}
}
//location of array gets copied so the whole array gets manipulated and not a copy
//pass by reference - address is copied
// pass by value - value is copied of primitive data type and string
void read2D (float[][] marks , int [] roll_no)//marks[0] is number of students as one
array consist of marks of one subject
{
Scanner sc = new Scanner (System.in);

for(int k = 0; k<marks[0].length;k+=1)
{
System.out.print("Enter the roll_no of the student"+ (k+1) + " ");
roll_no[k] = sc.nextInt();
for (int m = 0; m<marks.length;m+=1)
{
System.out.print("Enter the marks for subject "+ (m+1) + " ");
marks[m][k] = sc.nextFloat();
}
}
}

//This function prints the 2D array of marks and students


void print_2D(float[][] marks , int [] roll_no)
{
System.out.print("Roll no. \t");
System.out.println ("Maths: \t\tEnglish: \tScience: \t HCG: \t Second
Language:" );
for(int c = 0; c < marks[0].length; c++)
{
System.out.print(roll_no[c] + "\t\t");
for(int m = 0 ;m<marks.length;m++ )
{
System.out.print(marks[m][c] + "\t\t");
}
System.out.println();
}
}

//This is the main for the 2D array of marks and students


void _2Dinteraction()
{
int a , r , k;
int i = 0;
int n = 0;
int s = 0;
Scanner sc = new Scanner (System.in);
System.out.print("Enter 0 to use the function and 1 to exit:");
a = sc.nextInt();
while(a!= 1)
{
System.out.print("Enter the number of students:");
n = sc.nextInt();
System.out.print("Enter the number of subjects:");
s = sc.nextInt();

float[] [] marks = new float [s][n];


int [] roll_no = new int [n];

read2D(marks , roll_no);
System.out.print("Enter the index of sorting:");
File: Untitled Document 1 Page 13 of 13

i = sc.nextInt();
sort2D(marks , roll_no , i);

print_2D(marks , roll_no);
System.out.print("Enter 0 to reuse the function for the same data or else
enter 1:");
r = sc.nextInt();
while(r == 0)
{
System.out.println("Enter 5. to sort by roll no.");
System.out.println("Enter 0. to sort by Maths marks ");
System.out.println("Enter 1. to sort by Eng marks");
System.out.println("Enter 2. to sort by Science marks");
System.out.println("Enter 3. to sort by HCG marks");
System.out.print("Enter 4. to sort by Second Language marks");
k = sc.nextInt();

sort2D(marks , roll_no , k);


print_2D(marks , roll_no );
System.out.print("Enter 0 to reuse the function for the same data or else
enter 1:");
r = sc.nextInt();
}
System.out.print("Enter 0 to use the function for different data or 1 to
exit:");
a = sc.nextInt();
}
}
}

You might also like