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...
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...
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...
Use object-oriented programming to write an application for a company that uses a subscription-based business model...
Use object-oriented programming to write an application for a company that uses a subscription-based business model to loan out clothes such as office and party wear to female clients. A subscription costs $69 per 4 weeks. A client can make unlimited loan requests during her subscription, as long as she has no previous loan that has not been returned. There can be only one piece of clothes in each loan request. The chosen piece of clothes takes a day to...
**[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...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...