Sid Name Per Stream

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

public class Student

{
int sid;
String name;
double per;
String stream;
public Student(int sid, String name, double per, String stream)
{
this.sid = sid;
this.name = name;
this.per = per;
this.stream = stream;
}
@Override
public String toString()
{
return "Student [" + sid + "," + name + "," + per + "," + stream
+ "]";
}
public int hashCode()
{
return sid; // based on id
//return name.hashCode(); // based on Name
//return ((Double)per).hashCode(); // based on per
}
public boolean equals(Object obj)
{
return this.hashCode()==obj.hashCode();
}
}
import java.util.HashSet;
import java.util.Scanner;

public class MainRunner1


{
static Student readStudent()
{
Scanner sc= new Scanner(System.in);
System.out.println(" Enter the id: ");
int id= sc.nextInt();
System.out.println(" Enter the Name: ");
String name=sc.next();
System.out.println(" Enter the percentage: ");
double per=sc.nextDouble();
System.out.println(" Enter the stream: ");
String stream=sc.next();

return new Student(id, name, per, stream);


}

public static void main(String[] args)


{
HashSet students= new HashSet();
Scanner sc= new Scanner(System.in);
do
{
Student std= readStudent();
students.add(std);
System.out.println(" do you have more Student");
String res=sc.next();
if(res.equalsIgnoreCase("no"))
break;
}while(true);

System.out.println("<-----Students INFO----->");
for(Object obj:students)
{
System.out.println(obj);
}
}
}

You might also like