-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStudent_string.java
More file actions
145 lines (131 loc) · 4.27 KB
/
Student_string.java
File metadata and controls
145 lines (131 loc) · 4.27 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.Scanner;
public class StudentString {
private int reg_no;
private String name;
private String date_joining;
private int semester;
private double gpa;
private double cgpa;
public int reg_no_calculator(int number) {
String temp = "";
for (int i = 8; i <= 9; i++) {
temp += (this.date_joining.charAt(i));
}
temp += number;
int res = Integer.parseInt(temp);
return res;
}
public StudentString() {
this.reg_no = 0;
this.name = "Unknown";
this.date_joining = "00-00-0000";
this.semester = 0;
this.gpa = 0.00;
this.cgpa = 0.00;
}
public StudentString(int reg_no, String name,int semester, double gpa, double cgpa) {
this.reg_no = reg_no;
this.name = name;
this.semester = semester;
this.gpa = gpa;
this.cgpa = cgpa;
}
public void display() {
System.out.println("Registration number - " + this.reg_no);
System.out.println("Name - " + this.name);
System.out.println("Date of joining - " + this.date_joining);
System.out.println("Semester - " + this.semester);
System.out.println("Gpa - " + this.gpa);
System.out.println("Cgpa - " + this.cgpa);
System.out.println("-----------------------------------------");
}
public void input_display(StudentString s,int number) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter data of the student");
s.reg_no = reg_no_calculator(number);
//sc.nextLine();
s.name = sc.nextLine();
s.date_joining = sc.nextLine();
s.semester = sc.nextInt();
s.gpa = sc.nextDouble();
s.cgpa = sc.nextDouble();
display();
System.out.println("-----------------------------------------");
}
void sort_semester(StudentString arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i; j < n; j++) {
if (arr[i].semester > arr[j].semester) {
StudentString temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void sort_cgpa(StudentString arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i; j < n; j++) {
if (arr[i].cgpa > arr[j].cgpa) {
StudentString temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void sort_name(StudentString arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i; j < n; j++) {
if ((arr[i].name.compareTo(arr[j].name)) > 0) {
StudentString temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void displayChar(char ch, StudentString arr[]) {
int len = arr.length;
for (int i = 0; i < len; i++) {
if (arr[i].name.charAt(0) == ch) {
display();
}
}
}
}
class StudentStringTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of students - ");
int length = sc.nextInt();
sc.nextLine();
StudentString s[] = new StudentString[length];
for(int i=0;i<length;i++)
{
s[i]=new StudentString();
s[i].input_display(s[i],i+10);
}
System.out.println("Sorted by semester");
s[0].sort_semester(s);
for (int i = 0; i < length; i++) {
s[i].display();
}
System.out.println("Sorted by cgpa");
s[0].sort_cgpa(s);
for (int i = 0; i < length; i++) {
s[i].display();
}
System.out.println("Sorted by name");
s[0].sort_name(s);
for (int i = 0; i < length; i++) {
s[i].display();
}
System.out.println("All students with particular letter.Enter Character - ");
char ch = sc.next().charAt(0);
s[0].displayChar(ch, s);
}
}