简体   繁体   English

Java将对象调用到另一个类的数组列表中

[英]Java calling an object into an arraylist of another class

Okay, before anything, I would like to mention that this is for school so please DO NOT write any code that fixes my code as that will not teach me anything. 好的,首先,我想提到的是,这是供学校使用的,因此请不要编写任何可修复我的代码的代码,因为它不会教给我任何东西。 Instead what I am looking for is references, explanations and proper terminologies if I use incorrect terminology. 相反,如果我使用了错误的术语,我正在寻找的是参考,解释和适当的术语。

So I am having a few issues here. 所以我在这里有几个问题。 Here is what I need to do, 这是我需要做的

*Include the following methods in the Student class: a. *在Student类中包括以下方法: an accessor (ie, getter) for each instance variable from part B1 b. B1b部分中每个实例变量的访问器(即getter)。 a mutator (ie, setter) for each instance variable from part B1 B1部分中每个实例变量的修改器(即设置器)

Note: All access and change to the instance variables of the Student class should be through accessor and mutator methods. 注意:对Student类的实例变量的所有访问和更改都应通过accessor和mutator方法进行。

c. C。 constructor using all of the input parameters d. 构造函数使用所有输入参数d。 print() to print specific student data (eg, student ID, first name, last name) using accessors (ie, getters) print()使用访问器(即吸气剂)打印特定的学生数据(例如,学生证,名字,姓氏)

Create a student Roster class with the following methods that contain all ArrayList method calls: a. 使用包含所有ArrayList方法调用的以下方法创建一个学生名册类: public static void remove(String studentID) that removes students from the roster by student ID public static void remove(String studentID)通过学生ID将学生从名册中删除

Note: If the student ID doesn't exist, the method should print an error message indicating that it is not found. 注意:如果学生证不存在,该方法应打印一条错误消息,指出找不到该学生证。

b. public static void print_all() that prints a complete tab-separated list of student data using accessor methods public static void print_all()使用访问器方法打印完整的制表符分隔的学生数据列表

Note: Tabs can be formatted as such: 1 [tab] First Name: John [tab] Last Name: Smith [tab] Age: 20 [tab] Grades: {88, 79, 59}. 注意:选项卡的格式可以如下:1 [选项卡]名:John [选项卡]姓氏:史密斯[选项卡]年龄:20 [选项卡]成绩:{88,79,59}。 The print_all() method should loop through all the students in the student array list and call the print() method for each student. print_all()方法应遍历学生数组列表中的所有学生,并为每个学生调用print()方法。

c. C。 public static void print_average_grade(String studentID) that correctly prints a student's average grade by student ID d. public static void print_average_grade(String studentID),它按学生ID d正确打印学生的平均成绩。 public static void print_invalid_emails() that verifies student e-mail addresses and displays all invalid e-mail addresses to the use* 公共静态无效print_invalid_emails(),用于验证学生的电子邮件地址并显示所有无效的电子邮件地址以供使用*

That above is where I am having trouble, The arraylist, the constructor and calling the Student class in the Roster class. 上面是我遇到麻烦的地方,即数组列表,构造函数并在Roster类中调用Student类。

here is my code. 这是我的代码。

Student Class 学生班

 /**
  * Write a description of class Student here.
  * 
  * @author (Richard Talcik) 
  * @version (v1 3/5/17)
  */
 public class Student {
  // initialize instance variables
  private String csFirstName;  //I am using cs infront of the name because this is a class variable and a string
  private String csLastName;
  private String csEmail;
  private int ciAge;
  private int ciStudentID;
  private int[] ciaGrades;   //cia for class, integer, array;

  public Student(String sFirstName, String sLastName, String sEmail, int iAge, int iStudentID, String[] grades) {

    //doing the consturctor
    this.setFirstName(sFirstName);
    this.setLastName(sLastName); 
    this.setEmail(sEmail); 
    this.setAge(iAge);
    this.setStudentID(iStudentID);
    this.setGrades(grades);


} 
  /**he methods to get and then followed by set
 */
public String getFirstName()
{
    // put your code here
    return csFirstName; //returning the value of the first name when called.

}
 public String getLastName()
{
    // put your code here
    return csLastName;

}
 public String getEmail()
{
    // put your code here
    return csEmail;

}
 public int getStudentID()
{
    // put your code here
    return ciStudentID;

}
 public int getAge()
{
    // put your code here
    return ciAge;

}
 public int[] getGrades()
{
    // put your code here
    return ciaGrades;

}

// calling the sets from here on out
 public void setFirstName(String newFirstName)
{
    // put your code here
    csFirstName = newFirstName;
    //setting it

}
public void setLastName(String newLastName)
{
    // put your code here
    csLastName = newLastName;
    //setting it

}
public void setEmail(String newEmail)
{
    // put your code here
    csEmail = newEmail;
    //setting it

}
public void setAge(int newAge)
{
    // put your code here
    ciAge = newAge;
    //setting it

}
public void setStudentID(int newStudentID)
{
    // put your code here
    ciStudentID = newStudentID;
    //setting it

}
public void setGrades(int[] newGrades)
{
    // put your code here
    ciaGrades = newGrades;
    //setting it

 }
}

This roster class is asking me to add arguments when I call Student stu = new Student(); 这个花名册类要求我在调用Student stu = new Student()时添加参数。 But I don't understand what arguments to add into there? 但是我不明白要在其中添加哪些参数? I also dont really understand how I am going to incorporate my arraylist to add my methods in there from the student class? 我也不太了解如何将我的数组列表并入学生类中的方法中? ( sorry if i used wrong terminology, please correct me if needed ) 对不起,如果我使用了错误的术语,请根据需要对我进行更正

Roster Class 名册类

import java.util.ArrayList;

 /**
  * Write a description of class Roster here.
  * 
  * @author (Richard Talcik) 
  * @version (v1)
  */
 public class Roster {




// instance variables - replace the example below with your own


/**
 * Constructor for objects of class Roster
 */
 public static void main(String args[]) {
    Student stu = new Student("John", "Smith", "John1989@gmail.com", 20, 1, Grades[1]);
    ArrayList<Student> myRoster = new ArrayList<Student>();
    myRoster.add(new Student("Suzan", "Erickson","Erickson_1990@gmailcom", 19, 2, [91,72,85]));

 }
 public static void remove(String studentID)
{
    // put your code here

}

public static void print_all()
{
    //do something
}
public static void print_average_grade(String studentID)
{
    //do something
}

public static void print_invalid_emails()
{
    //do something
}


}

please any tutorials that will help explain this will be well worth it. 请任何有助于说明这一点的教程都是值得的。

Also, I work third shift and my school is fully online. 另外,我从事第三班工作,我的学校完全在线。 My mentor isn't or tutor isn't available during the hours that I am awake and emails are not really best method of communication as it takes days for a response. 我醒着的时间里没有我的导师或没有老师,电子邮件也不是最好的沟通方式,因为回复需要几天的时间。

You have written Student class with a constructor that takes parameters. 您已经使用带有参数的构造函数编写了Student类。 So, in Roaster class you have to call matching constructor of Student class. 因此,在Roaster类中,您必须调用Student类的匹配构造函数。 My guess is that you did not quite understand my answer. 我的猜测是您不太了解我的答案。 I would suggest look for a java tutorial on oracle site and even better if you can get a copy of this book called The java programming language 我建议在oracle网站上寻找Java教程,如果可以得到本书的副本《 Java编程语言》,那就更好了。

Firstly for a beginner, not bad :) However when using the ArrayList object type in java, the type of the list must be of the object you whish to put inside the ArrayList, ie if you want to keep a list of students, then you have to write it as 首先,对于初学者来说,还不错:)但是,在Java中使用ArrayList对象类型时,列表的类型必须是您希望放入ArrayList中的对象的类型,即,如果要保留学生列表,则您可以必须写成

AssrayList<Student> = new ArrayList<Student>();

you can also specify the size of the list as a param, but remember, ArraList grows dynamically as things are added and removed from the list. 您也可以将列表的大小指定为参数,但是请记住,随着列表中内容的添加和删除,ArraList会动态增长。

As far as you Constructor goes, you have to add the variables you assign values to on the inside as the params of the constructor. 就构造函数而言,您必须在内部添加分配值的变量作为构造函数的参数。 also the question states that you have to access all class objects with the use of their accessor and mutator methods, having that the current way you have your constructor at the moment is incorrect as you are directly assigning values to the class onjects of Student, you can change that to be similar to the following: 还有一个问题指出,您必须使用其访问器和mutator方法访问所有类对象,由于您直接将值直接分配给Student的类对象,因此当前使用构造函数的当前方式不正确,可以将其更改为类似于以下内容:

constructor (paramType param) {
  mutatorMethod(param)
}

so your constructor should be in the lines of 因此您的构造函数应位于

Student(String name, String surname) {
  setName(name)
  setSurname(surname)
}

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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