Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Associated with a particular instance, static variables are associated with the

ID: 3795388 • Letter: A

Question

Associated with a particular instance, static variables are associated with the particular class. Specifically, if there are n instances of a class, there are n copies of an instance variable, but exactly one copy of a static variable (even when n = 0). Create a Student class conforming to the diagram above. Each student instance has a distinct ID which is not the same as any other student. In order to ensure we don't assign duplicate IDs, we keep track of all the IDs we've assigned in a static variable unique ID. Every time a student is created, this value is changed. For the equals method, it returns true when the current object and the argument hold other Person object have the same id in their id instance variables and have the same names. Create a driver that asks the user, in a loop, for a student's name. You should create an instance of your Student class and print the new student to the console. You should stop when the user enters the word "quit".

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

public class Student {

  

   // instance variable

   private int id;

   private String name;

   private static int uniqueId = 0;

  

   public Student(){

       id = ++uniqueId;

       name = "";

   }

   public int getId() {

       return id;

   }

   public String getName() {

       return name;

   }

  

   public void setName(String name) {

       this.name = name;

   }

  

   @Override

   public String toString() {

       return "Id: "+id+", Name: "+name;

   }

  

   @Override

   public boolean equals(Object obj) {

       if(obj instanceof Student){

           Student t = (Student)obj;

           if(name.equals(t.name))

               return true;

       }

      

       return false;

   }

}

import java.util.Scanner;

public class StudentDriver {

  

   public static void main(String[] args) {

      

       Scanner sc = new Scanner(System.in);

       String name;

       while(true){

           System.out.print("Enter your name (quit to stop): ");

           name = sc.nextLine();

          

           if("quit".equalsIgnoreCase(name.trim()))

               break;

          

           // creating student object

           Student s = new Student();

           s.setName(name);

          

           System.out.println(s);

       }

   }

}

/*

Sample run:

Enter your name (quit to stop): Pravesh Kuamr

Id: 1, Name: Pravesh Kuamr

Enter your name (quit to stop): Alex

Id: 2, Name: Alex

Enter your name (quit to stop): Vikash Kumar

Id: 3, Name: Vikash Kumar

Enter your name (quit to stop): quit

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote