简体   繁体   中英

How to assign array elements to private variable and use them in another class?

/*
c - csis
a - acct
b - busn
p - phys

*/
class CourseInfo {

  private String courseTitle;
  private double coursePrice;
  private int courseSeatsAvail;
  private String courseDesc;
  private char courseType;

  private static String empPassword;

  CourseInfo(String crseTitle, double crsePrice, int crseSeats, String crseDesc, char type) {

    courseTitle = crseTitle;
    coursePrice = crsePrice;
    courseSeatsAvail = crseSeats;
    courseDesc = crseDesc;
    courseType = type;
  }

  /*
   * Finish the class with the set/get methods
   */
}

///////////////////////////////

class CourseList {

  CourseInfo[] courseList;

  public void createList() {

    courseList = new CourseInfo[11];

    courseList[0] = new CourseInfo("acct110", 375.49, 35, "this course teaches \nbasic accounting practice", 'a');
    courseList[1] = new CourseInfo("busn110", 375.49, 35, "this course teaches \nbasic business pratice", 'b');
    courseList[2] = new CourseInfo("busn240", 375.49, 35, "this course teaches \nadvance business pratice", 'b');
    courseList[3] = new CourseInfo("csis110", 375.49, 2, "this course teaches \nbasic computing pratice", 'c');
    courseList[4] = new CourseInfo("csis220", 375.49, 35, "this course teaches \nR language", 'c');
    courseList[5] = new CourseInfo("csis290", 375.49, 25, "this course teaches \nbasic hardware pratice", 'c');
    courseList[6] = new CourseInfo("csis340", 375.49, 35, "this course teaches \nadvance CPU tech", 'c');
    courseList[7] = new CourseInfo("csis420", 375.49, 17, "this course teaches \nbasic computer graphics", 'c');
    courseList[8] = new CourseInfo("csis491", 375.49, 3, "this course teaches \nbasic game programming", 'c');
    courseList[9] = new CourseInfo("phys120", 499.19, 30, "this course teaches \nbasic physics theory", 'p');
    courseList[10] = new CourseInfo("phys240", 399.99, 35, "this course teaches \nbasic quantum mechanics", 'p');

  }
}

I'm trying to access the data in CreateList from a different .java file but my instructor wants me to do it in this specific way with private variables and sets and gets and I have no idea how to proceed. I know how to create basic sets and gets but I don't understand how they're meant to work with the CourseInfo constructor.

'Getters' and 'setters' are used to encapsulate variables / objects in Java. This means (as your instructor said), you should be making the variables private and the getters and setters public .

For example:

private String courseTitle;

This variable is only accessible within the class, so to be able to get this variable from outside the class (ie from wherever you are using the CourseList from), you need to create a getter (to get the variable) and a setter (to change the value of the variable). As follows:

// Getter
public String getCourseTitle() {
    return this.courseTitle;
}

// Setter
public void setCourseTitle(String newCourseTitle) {
    this.courseTitle = newCourseTitle;
}

You can read more about encapsulation here .

Now, to retrieve the values, use the getter , as follows (assuming courseInfo is a CourseInfo object):

String data = courseInfo.getCourseTitle();

Try to do this for each variables create his set and get method like: private String courseTitle;

 public String getCourseTitle() {
    return courseTitle;
}

public void setCourseTitle(String courseTitle) {
    this.courseTitle = courseTitle;
}

And in CourseList class try to set the value of courseTitle like this:

setCourseTitle("value of courseTitle");

Than you can get it like this:

String var = getCourseTitle();

And use the value where do you want for example add it in array courseList .

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