[英]Java calling an object into an arraylist of another class
好的,首先,我想提到的是,这是供学校使用的,因此请不要编写任何可修复我的代码的代码,因为它不会教给我任何东西。 相反,如果我使用了错误的术语,我正在寻找的是参考,解释和适当的术语。
所以我在这里有几个问题。 这是我需要做的
*在Student类中包括以下方法: B1b部分中每个实例变量的访问器(即getter)。 B1部分中每个实例变量的修改器(即设置器)
注意:对Student类的实例变量的所有访问和更改都应通过accessor和mutator方法进行。
C。 构造函数使用所有输入参数d。 print()使用访问器(即吸气剂)打印特定的学生数据(例如,学生证,名字,姓氏)
使用包含所有ArrayList方法调用的以下方法创建一个学生名册类: public static void remove(String studentID)通过学生ID将学生从名册中删除
注意:如果学生证不存在,该方法应打印一条错误消息,指出找不到该学生证。
湾 public static void print_all()使用访问器方法打印完整的制表符分隔的学生数据列表
注意:选项卡的格式可以如下:1 [选项卡]名:John [选项卡]姓氏:史密斯[选项卡]年龄:20 [选项卡]成绩:{88,79,59}。 print_all()方法应遍历学生数组列表中的所有学生,并为每个学生调用print()方法。
C。 public static void print_average_grade(String studentID),它按学生ID d正确打印学生的平均成绩。 公共静态无效print_invalid_emails(),用于验证学生的电子邮件地址并显示所有无效的电子邮件地址以供使用*
上面是我遇到麻烦的地方,即数组列表,构造函数并在Roster类中调用Student类。
这是我的代码。
学生班
/**
* 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
}
}
这个花名册类要求我在调用Student stu = new Student()时添加参数。 但是我不明白要在其中添加哪些参数? 我也不太了解如何将我的数组列表并入学生类中的方法中? ( 对不起,如果我使用了错误的术语,请根据需要对我进行更正 )
名册类
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
}
}
请任何有助于说明这一点的教程都是值得的。
另外,我从事第三班工作,我的学校完全在线。 我醒着的时间里没有我的导师或没有老师,电子邮件也不是最好的沟通方式,因为回复需要几天的时间。
您已经使用带有参数的构造函数编写了Student类。 因此,在Roaster类中,您必须调用Student类的匹配构造函数。 我的猜测是您不太了解我的答案。 我建议在oracle网站上寻找Java教程,如果可以得到本书的副本《 Java编程语言》,那就更好了。
首先,对于初学者来说,还不错:)但是,在Java中使用ArrayList对象类型时,列表的类型必须是您希望放入ArrayList中的对象的类型,即,如果要保留学生列表,则您可以必须写成
AssrayList<Student> = new ArrayList<Student>();
您也可以将列表的大小指定为参数,但是请记住,随着列表中内容的添加和删除,ArraList会动态增长。
就构造函数而言,您必须在内部添加分配值的变量作为构造函数的参数。 还有一个问题指出,您必须使用其访问器和mutator方法访问所有类对象,由于您直接将值直接分配给Student的类对象,因此当前使用构造函数的当前方式不正确,可以将其更改为类似于以下内容:
constructor (paramType param) {
mutatorMethod(param)
}
因此您的构造函数应位于
Student(String name, String surname) {
setName(name)
setSurname(surname)
}
希望这可以帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.