Question

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 uniqueld: int - id: int name: String + Student (name: String) + getName): String +set Name(name: String) + getld +toString0: String + equals(other: Object): boolean

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

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 "\nName : "+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:

Enter the name of student to make its object
Steve jobs
Name : Steve jobs id : 1
Do you want to continue?<type quit to exit>yes
Enter the name of student to make its object
Adam Jones
Name : Adam Jones id : 2
Do you want to continue?<type quit to exit>yes
Enter the name of student to make its object
Kathy Williams
Name : Kathy Williams id : 3
Do you want to continue?<type quit to exit>yes
Enter the name of student to make its object
John Miller
Name : John Miller id : 4
Do you want to continue?<type quit to exit>quit
Add a comment
Know the answer?
Add Answer to:
CompSci 251: Intermediate Computer Programming Spring 2017 -Java Lab 5 For this lab we will be...
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
  • 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...

  • Please comment the important lines in the .java file as shown in the template. The important...

    Please comment the important lines in the .java file as shown in the template. The important lines including but not limited to i) variables, ii) for-loop, iii) while-loop, iv) if-else statement, iv) methods. Please use your own words to describe what is your purpose to write this line. Panther ID - 002357394 Purpose: Instance variables belong to the instance of a class, i.e., an object. Every object of that class has its own copy of that instance variable. Changes made...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

  • Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed...

    Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed to the constructor Methods: Accessors int getID( ) String getName( ) Class Roster: This class will implement the functionality of all roster for school. Instance Variables a final int MAX_NUM representing the maximum number of students allowed on the roster an ArrayList storing students Constructors a default constructor should initialize the list to empty strings a single parameter constructor that takes an ArrayList<Student> Both...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • This is the question about object-oriend programming(java) please show the detail comment and prefect code of...

    This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • Programming assignment for Java: Do not add any other instance variables to any class, but you...

    Programming assignment for Java: Do not add any other instance variables to any class, but you can create local variables in a method to accomplish tasks. Do not create any methods other than the ones listed below. Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...

  • Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the...

    Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the TA class to represent the course the student is TA'ing. 1.3) Provide a constructor for the class so the code in the PeopleFactory will work as provided. 1.4) Provide a toString() method for the TA  class so that TA's will output as shown in the sample code. Provide a class Staff so that you can un-comment the lines of code in the PeopleFactory class that...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

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