Question

Consider the Student class. Each student has a unique yorkID in the format of York-X, where...

Consider the Student class. Each student has a unique yorkID in the format of York-X, where X is the serial number, starting from 1. That is, the first Student object gets yorkID York-1, the second object gets York-2.

A) Add field(s) if you think you need one. Write "None" if you don't need one.

B) Complete the constructor so that the first Student object gets York ID York-1, the second Student object gets York-2 etc.

i dont have time. Please be quick

thanks

Homework Answers

Answer #1

I am going to answer the question subpart by subpart as it is asked. Before I answer this question, let me clarify that I'm going to use the programming language Java since we haven't been told the programming language but I'll try to make the logic behind this solution clear so that solution could be changed easily.

A)

Yes, the student class would need one more field that is a static field, we can name it however we want but I'm going to name it id and I will instantiate with the value 1 since our yorkId starts with value York-1. A static variable is a type of variable which is shared across all the instances of a class.

B)

Instead of writing only the constructor, I will write the whole class so that there is no confusion in the end.

Student Class :

public class Student
{
// Fields required
String yorkId;
static int id=1;
  
// Constructor
public Student(){
yorkId="York-"+id;
id++;
}
  
// Main function to create students and see the yorkId
   public static void main(String[] args) {
       Student s1 = new Student();
       Student s2 = new Student();
       Student s3 = new Student();
       System.out.println("The unique ID of first student is " + s1.yorkId);
       System.out.println("The unique ID of second student is " + s2.yorkId);
       System.out.println("The unique ID of third student is " + s3.yorkId);
   }
}


Code screenshot :

Code output :

The explanation for the constructor :

We are incrementing the static variable Id when an object gets created and then we can concatenate the variable id and "York-" to form our unique id.

Solution ends.

Please comment and let me know if you have any further doubts. Please upvote this answer if you like it.

Thank you.

Have a good day.

Know the answer?
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
Similar Questions
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...
In Java Let’s say we’re designing a Student class that has two data fields. One data...
In Java Let’s say we’re designing a Student class that has two data fields. One data field, called id, is for storing each student’s ID number. Another data field, called numStudents, keeps track of how many Student objects have been created. For each of the blanks, indicate the appropriate choice. id should be   public/private   and   static/not static . numStudents should be   public/private   and   static/not static . The next three questions use the following class: class Counter {     private int...
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements....
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements. But in a queue, the elements are retrieved in a first-in first-out fashion. The class contains: An int[] data field named elements that stores the int values in the queue. A data field named size that stores the number of elements in the queue. A constructor that creates a Queue object with default capacity 8. The method enqueue(int v) that adds v into the...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these classes: Product, Customer, and Store. All data members of each class should be marked as **private** (a leading underscore in the name). Since they're private, if you need to access them from outside the class, you should do so via get or set methods. Any get or set methods should be named per the usual convention ("get_" or "set_" followed by the name of...
IT 168                                         &nb
IT 168                                                                                          Fall 2020 Program 4 Due Date: October 21 at 11:55 PM Problem: A simulation program is needed to determine if the ISU Quiz Bowl Team should comprise mainly of genius students or regular students. A primary requirement of this program that is different from your earlier assignments is that you are required to create 2 different Java classes in the design of your program. Here are some other design details that may be helpful: Design: The classes should...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If you can't implement all of the features of Project described in this document, implement what you can. Start by implementing the StockItem class, and its derived classes. Then add in the InventoryManager class later. In each case, the test files must be in separate classes. UML Diagram: Use Violet or other drawing software to draw the UML diagrams of each class that you use...
c++ Program Description You are going to write a computer program/prototype to process mail packages that...
c++ Program Description You are going to write a computer program/prototype to process mail packages that are sent to different cities. For each destination city, a destination object is set up with the name of the city, the count of packages to the city and the total weight of all the packages. The destination object is updated periodically when new packages are collected. You will maintain a list of destination objects and use commands to process data in the list....
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do depending upon what the user enters, please refer to the examples below to use to test your program. Run your final program one time for each scenario to make sure that you get the expected output. Be sure to format the output of your program so that it follows what is included in the examples. Remember, in all examples bold items are entered by...