Question

In Java create the equals() and toString() method for the Student Class Two students objects are...

In Java create the equals() and toString() method for the Student Class

Two students objects are considered equal if their id is the same. The toString() method should print out the name and id of the object.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Here is your Student.java file, in which toString() and equals are implemented*/

import java.util.Scanner;
public class Student
{         int id;String name;
public void toString(Student o) {
System.out.println(id+" "+name+" "+o.id+" "+o.name) ;
}
public Boolean equals(Student o) {
if(id==o.id)
return true;
else
return false;
}
   public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   Student m[]=new Student[2];
       System.out.println("Enter student id and name of 1st student:");
m[0]=new Student();
       m[0].id=sc.nextInt();
       sc.nextLine();
       m[0].name=sc.nextLine();
       System.out.println("Enter student id and name of 1st student:");
       m[1]=new Student();
               m[1].id=sc.nextInt();
               sc.nextLine();
       m[1].name=sc.nextLine();
       if(m[0].equals(m[1]))
       m[0].toString(m[1]);
   }
}

Add a comment
Know the answer?
Add Answer to:
In Java create the equals() and toString() method for the Student Class Two students objects are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a java project and create Student class. This class should have the following attributes, name...

    Create a java project and create Student class. This class should have the following attributes, name : String age : int id : String gender : String gpa : double and toString() method that returns the Student's detail. Your project should contain a TestStudent class where you get the student's info from user , create student's objects and save them in an arralist. You should save at least 10 student objects in this arraylist using loop statement. Then iterate through...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class...

    PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class has 4 attributes: (1) student ID: protected, an integer of 10 digits (2) student name: protected (3) student group code: protected, an integer (1 for undergraduates, 2 for graduate students) (4) student major: protected (e.g.: Business, Computer Sciences, Engineering) Class Student declaration provides a default constructor, get-methods and set-methods for the attributes, a public abstract method (displayStudentData()). PART II: Create a Java class named...

  • Create Student class•Change header so that Student extends Person•A Student has-an additional instance variable, major of...

    Create Student class•Change header so that Student extends Person•A Student has-an additional instance variable, major of type String.•Add the instance variable, its getters and setters. •Add a toString method to the Student class. It should reuse the toString method of Person. •Add two constructors: –No args–Constructor that receives the student’s name, birth year and Major as parameters•Create a class TestInheritance3–Create 2 Student objects–Create an Instructor object–Print the information about the two students and the instructor using the getters of Person,...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • In Java Create an array of Student objects. Display the students by age in UNSORTED order....

    In Java Create an array of Student objects. Display the students by age in UNSORTED order. Apply the Quick sort and Display the students by age in SORTED order. public class TestQuickStudent { public static void main (String [ ] args ) { /* ***Create your array of Student objects with AT LEAST 5 names */ System.out.println ( " ____________________________");    /* ***LOOP through your array and print out each UNSORTED check by student age */ System.out.println ( " ____________________________");...

  • 2. Create ONE java file in Dr Java according the specifications below: A. Create a Country...

    2. Create ONE java file in Dr Java according the specifications below: A. Create a Country class a. Each Country object of the Country class should know (state) i. its name ii. its area (in square miles) iii. its population b. Each Country object should be able to (behavior) i. set initial population (setter) ii. get density (number of people per square mile) (getter) iii. adjust the set population by some amount (setter) iv. provide its name, area, and population...

  • Equals and Copy method activity Implement a Dog class with the following features: + Attributes: ...

    Java Equals and Copy method activity Implement a Dog class with the following features: + Attributes: age, gender, weight + Method: constructor, copy constructor, equals (if same weight and gender), toString + A main method/driver that create an array of 5 Dog objects (obtain input from user), and then display the objects that are "equal" Equals and Copy method activity Implement a Dog class with the following features: + Attributes: age, gender, weight + Method: constructor, copy constructor, equals (if...

  • In Java, Please do not forget the ToString method in the Student class! In this exercise,...

    In Java, Please do not forget the ToString method in the Student class! In this exercise, you will first create a Student class. A Student has two attributes: name (String) and gradePointAverage (double). The class has a constructor that sets the name and grade point average of the Student. It has appropriate get and set methods. As well, there is a toString method that prints the student's name and their grade point average (highest is 4.0) You will then write...

  • The object class should be a Student with the following attributes: id: integer first name: String...

    The object class should be a Student with the following attributes: id: integer first name: String last name: String write the accessors, mutators, constructor, and toString(). In your main test class you will write your main method and do the following things: Create an array of Student objects with at least 5 students in your array. Write a sort method that must be your original work and included in the main class. The sort method will sort based on student...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT