CompSci 251 Intermediate Computer Programming Spring 2017 Ja

CompSci 251: Intermediate Computer Programming Spring 2017 -Java

Lab 5

For this lab we will be looking at static, or class variables. Remember that where instances variables are 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).

Student

- uniqueId : int - id: int
- name: String

+ Student(name: String)

+ getName(): String
+ setName(name: String)

+ getId()

+ toString(): String
+ equals(other: Object): boolean

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 uniqueId. 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\".

Student

- uniqueId : int - id: int
- name: String

+ Student(name: String)

+ getName(): String
+ setName(name: String)

+ getId()

+ toString(): String
+ equals(other: Object): boolean

Student uniqueld: int - id: int name: String + Student (name: String) + getName): String +set Name(name: String) + getld +toString0: String + equals(other: Object): boolean

Solution

import java.util.*;

class Student
{
   private static int uniqueID;
   private int id;
   private String name;
  
   public Student(String name) //constructor
   {
       this.name = name;
       this.id = ++uniqueID;   //unique id is incremented every time a student object is created and is assigned to id
   }
  
   public String getName()
   {
       return name;
   }
  
   public void setName(String name)
   {
       this.name = name;
      
   }
  
   public int getId()
   {
       return id;
   }
  
   public String toString()
   {
       return \"\ Name : \"+name +\" id : \"+id;
   }
  
   public boolean Equals(Student s)
   {
       if(this.id == s.id && this.name.equals(s.name))
       return true;
       else
       return false;
   }
  
}

class TestStudent
{
   public static void main (String[] args)
   {
       Scanner scan = new Scanner(System.in);
       String option = \"loop\";
       String name;
      
       while(!option.equals(\"quit\"))
       {
           System.out.println(\"Enter the name of student to make its object\");
           name = scan.nextLine();
           Student s = new Student(name);
           System.out.println(s.toString()); //will generate unique ids for all students
          
           System.out.println(\"Do you want to continue?<type quit to exit>\");
           option = scan.nextLine();
           if(option.equals(\"quit\"))
           break;
       }
      
       //demostrate Equals()
           /*System.out.println(\"Enter the name of student to make its object\");
           name = scan.nextLine();
           Student s1 = new Student(name);
           System.out.println(s1.toString());
          
           System.out.println(\"Enter the name of student to make its object\");
           name = scan.nextLine();
           Student s2 = new Student(name);
           System.out.println(s2.toString());
          
           if(s1.Equals(s2))
           System.out.println(\"s1 and s2 are same\");//never true
           else
           System.out.println(\"s1 and s2 are not same\"); //always true */
   }
}

output:

CompSci 251: Intermediate Computer Programming Spring 2017 -Java Lab 5 For this lab we will be looking at static, or class variables. Remember that where instan
CompSci 251: Intermediate Computer Programming Spring 2017 -Java Lab 5 For this lab we will be looking at static, or class variables. Remember that where instan
CompSci 251: Intermediate Computer Programming Spring 2017 -Java Lab 5 For this lab we will be looking at static, or class variables. Remember that where instan

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site