0% found this document useful (0 votes)
17 views2 pages

Sid Name Per Stream

The document defines a Student class with fields for student ID, name, percentage, and stream. It includes a constructor, toString method, hashCode method based on ID, and equals method that compares hashcodes. The MainRunner1 class uses a HashSet to store Student objects entered by the user, calling the readStudent method which prompts the user for input and returns a new Student object. It iterates through the HashSet printing out the Student details.

Uploaded by

Swarup Kanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Sid Name Per Stream

The document defines a Student class with fields for student ID, name, percentage, and stream. It includes a constructor, toString method, hashCode method based on ID, and equals method that compares hashcodes. The MainRunner1 class uses a HashSet to store Student objects entered by the user, calling the readStudent method which prompts the user for input and returns a new Student object. It iterates through the HashSet printing out the Student details.

Uploaded by

Swarup Kanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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