Skip to content

Instantly share code, notes, and snippets.

@kshitijvarshne1
Created January 18, 2021 13:18
Show Gist options
  • Save kshitijvarshne1/41019c6bebf6371ab93d076c2ee1b122 to your computer and use it in GitHub Desktop.
Save kshitijvarshne1/41019c6bebf6371ab93d076c2ee1b122 to your computer and use it in GitHub Desktop.
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 18-Jan-21
* Time: 11:26 AM
* File: Student.java
*/
package jan18_21_NK.two;
import java.util.Objects;
public class Student {
private static String universityName;
private static String nameOfStream;
static {
universityName = "GLA";
nameOfStream = "B.Tech";
}
private int id;
private int marks;
private String studentName;
public Student(int id, int marks, String studentName) {
this.id = id;
this.marks = marks;
this.studentName = studentName;
}
public String getUniversityName() {
return universityName;
}
public String getNameOfStream() {
return nameOfStream;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return getId() == student.getId() && getMarks() == student.getMarks() && Objects.equals(getStudentName(), student.getStudentName());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getMarks(), getStudentName());
}
@Override
public String toString() {
return "Student { " +
"id=" + id +
", marks=" + marks +
", studentName='" + studentName +
" }";
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 18-Jan-21
* Time: 11:28 AM
* File: StudentMain.java
*/
package jan18_21_NK.two;
import java.util.InputMismatchException;
import java.util.Scanner;
public class StudentMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value for all the object :- ");
int limit = sc.nextInt();
int id = 0;
int marks = 0;
String studentName = null;
Student[] studentArray = new Student[limit];
for (int i = 0; i < limit; i++) {
try {
id = sc.nextInt();
marks = sc.nextInt();
sc.nextLine();
studentName = sc.nextLine();
} catch (InputMismatchException a) {
System.out.println("Stop the program and again provide the value for object");
a.printStackTrace();
}
Student obj = new Student(id, marks, studentName);
studentArray[i] = obj;
}
System.out.println("Enter any valur for start the program rather than -1 :-");
int var = sc.nextInt();
System.out.println("enter the value for start the program" +
"\n"
+ "enter 1 for find the average of all marks" + '\n'
+ "enter 2 for show all the objects" + '\n'
+ "enter -1 for terminate the program");
while (var != -1) {
var = sc.nextInt();
switch (var) {
case 1:
int sum = 0;
for (int i = 0; i < limit; i++) {
sum += studentArray[i].getMarks();
}
float average = sum / limit;
System.out.println("Average :- " + average);
break;
case 2:
for (int i = 0; i < limit; i++) {
System.out.println(studentArray[i]);
}
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment