简体   繁体   中英

How to construct a user(/input)-specified number of objects

I'm currently doing an intro level undergrad CS course (learning basics of 'program'&'class' building using Java).

The relevant part of my personal (&job related) project: I have a list of zip codes associated with One county.

I'm gonna define a class called 'County'. Then I'm gonna use this class to construct an object of type County, called 'middlesex'.

ie: County middlesex = new County();

Now, in this object, I want to construct a number of objects of class-type ZipCode.

ie: ZipCode objName = new ZipCode();

(Each such ZipCode object is gonna contain certain instance data).

My problem is this. Assume that I don't know how many zipcodes are contained in the Middlesex county. However, I have a .txt file that contains just a list of all the zipcodes of Middlesex county.

Let's say there are n number of zipcodes in this list.

In the object 'middlesex', of class-type 'County', I want to set up a loop. This loop will scan each zipcode in the list, then construct an object of class-type ZipCode for each zipcode.

Thus the loop may go thru n iterations, and construct n objects of type ZipCode.

Thus, for every iteration of the loop, a unique object-reference-name must be created (corresponding to the particular zipcode in the list).

Part of this problem (but distinct and optional), is that I want to know how (if possible), I can set up a structure that allows an inputted (scanned) string to be used as the name of an object-reference.

I apologize if I've made incorrect terminology use. I know that many are gonna suggest arrays. I haven't learned about them yet, but I gotta read about them over this weekend for school. I'm just gonna try to figure this out for a day or two, and then just move on to using arrays to perform this task.

So, if I've made any sense to anyone, is what I'm trying to do possible without arrays?

Thank u.

You're describing a very basic scenario, one where one object contains (possibly) many references to objects of a 2nd type, what we call a constructor called "composition" where the relationship here is a "has-a" relationship, County has-a (or has-many) zip codes

As opposed to using inheritance to wrongly try to solve this, the "inheritance" relationship or the "is-a" relationship -- County is not a zip code and zip code is not a county.

The code to create this can be very simple, something like:

import java.util.ArrayList;
import java.util.List;

public class County {
    private String name;
    private List<String> zipCodes = new ArrayList<>();

    // constructor that takes county name
    public County(String name) {
        this.name = name;
    }

    public void addZipCode(String code) {
        zipCodes.add(code);
    }

    // ..... more code

If a zip code is a single String, then no need to create a new class for this. If however it is more complex and holds more data than a single String, then create a class for ZipCode, and change the code above to something like

import java.util.ArrayList;
import java.util.List;

public class County {
    private String name;
    private List<ZipCode> zipCodes = new ArrayList<>();

    // constructor that takes county name
    public County(String name) {
        this.name = name;
    }

    public void addZipCode(ZipCode code) {
        zipCodes.add(code);
    }

    // getters, setters, a decent toString method override...

Where ZipCode could contain....

public class ZipCode {
    String code;
    // other fields if needed....

    public ZipCode(String code) {
        this.code = code;
    }

    // ....

then when reading in data, create your County objects and as each Zip code is read in, add it to the appropriate County object using the addZipCode(...) method.

zipCode is an object of Type ZipCode then what are its fields? Think of the reasons for making it an object and not a variable

"Thus the loop may go thru n iterations, and construct n objects of type ZipCode" Unforutnality this is not possible without making the use of Arrays "structure that allows an inputted (scanned) string to be used as the name of an object" Nope can do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM