Student
Student
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
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 read marks from the user and assigns it to the instance variable
void readMarks()
{
int n;
Scanner sc = new Scanner (System.in);
//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());
}
Student s2;
s2 = new Student();
Dates today;
today = new Dates();
System.out.println("Enter todays date:");
today.readDate();
System.out.println();
s1.setAdmn_no();
s1.computePercentage();
s2.setAdmn_no();
s2.computePercentage();
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
---------------------------------------------------------------------------------------------
/**
* Write a description of class student_marks here.
*
* @author (your name)
* @version (a version number or a date)
*/
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++;
}
}
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++;
}
}
int n , r;
int i = 0;
float m , e , f , s , h ;
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++;
}
}
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++;
}
}
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++;
}
}
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);
}
while(a != 1)
{
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();
}
Enter 0 to reuse the function for the same data or else enter 1:1
//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;
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;
}
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();
}
}
}
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();