Java将对象调用到另一个类的arraylist中
好的,在任何事情之前,我想说的是,这是为学校,所以请不要写任何代码来修复我的代码,因为这不会教我任何东西。相反,如果我使用了不正确的术语,我需要的是参考资料、解释和适当的术语 所以我这里有一些问题。这是我需要做的 *在学生课堂上包括以下方法: A.第B1部分中每个实例变量的访问器(即getter) BB1部分中每个实例变量的一个变数(即setter) 注意:对Student类实例变量的所有访问和更改都应该通过访问器和mutator方法进行 c。使用所有输入参数的构造函数 Dprint()使用访问器(即getter)打印特定的学生数据(例如,学生ID、名字、姓氏) 使用包含所有ArrayList方法调用的以下方法创建学生花名册类: A.public static void remove(字符串studentID),用于按学生ID从名册中删除学生 注意:如果学生ID不存在,该方法应打印一条错误消息,指示找不到该ID b。public static void print_all(),它使用访问器方法打印完整的以制表符分隔的学生数据列表 注意:选项卡的格式可以是:1[tab]名字:John[tab]姓氏:Smith[tab]年龄:20[tab]年级:{88,79,59}。print_all()方法应该循环遍历student数组列表中的所有学生,并为每个学生调用print()方法 c。public static void print_average_grade(字符串studentID),用于按学生ID正确打印学生的平均成绩 Dpublic static void print_invalid_emails(),用于验证学生电子邮件地址并向用户显示所有无效电子邮件地址* 上面是我遇到麻烦的地方,arraylist、构造函数和调用花名册类中的Student类 这是我的密码 学生班Java将对象调用到另一个类的arraylist中,java,arrays,arraylist,constructor,encapsulation,Java,Arrays,Arraylist,Constructor,Encapsulation,好的,在任何事情之前,我想说的是,这是为学校,所以请不要写任何代码来修复我的代码,因为这不会教我任何东西。相反,如果我使用了不正确的术语,我需要的是参考资料、解释和适当的术语 所以我这里有一些问题。这是我需要做的 *在学生课堂上包括以下方法: A.第B1部分中每个实例变量的访问器(即getter) BB1部分中每个实例变量的一个变数(即setter) 注意:对Student类实例变量的所有访问和更改都应该通过访问器和mutator方法进行 c。使用所有输入参数的构造函数 Dprint()使用访问
/**
* 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
}
}
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
}
}
这个花名册类要求我在调用stu=new Student()时添加参数;但我不明白要加入什么样的论点?
我也不太明白我将如何合并我的arraylist,以便从学生类中添加我的方法?(抱歉,如果我使用了错误的术语,请在需要时更正)
排班班
/**
* 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
}
}
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
}
}
import java.util.ArrayList;
/**
*在这里写下班级花名册的描述。
*
*@作者(Richard Talcik)
*@版本(v1)
*/
公共班级名册{
//实例变量-将下面的示例替换为您自己的
/**
*类的对象的构造函数
*/
公共静态void main(字符串参数[]){
学生stu=新学生(“约翰”、“史密斯”John1989@gmail.com“、20、1、等级[1]);
ArrayList MyFloster=新的ArrayList();
我的花名册。添加(新学生(“苏姗”、“埃里克森”、“埃里克森”)_1990@gmailcom", 19, 2, [91,72,85]));
}
公共静态无效删除(字符串studentID)
{
//把你的代码放在这里
}
公共静态无效打印_all()
{
//做点什么
}
公共静态无效打印平均分数(字符串studentID)
{
//做点什么
}
公共静态无效打印\u无效\u电子邮件()
{
//做点什么
}
}
请注意,任何有助于解释这一点的教程都是值得的
另外,我上第三班,我的学校完全在线。在我醒着的时候,我的导师不在,或者导师不在,电子邮件也不是最好的沟通方式,因为回复需要几天时间。您编写了一个带参数的构造函数的学生类。所以,在Roaster类中,您必须调用Student类的匹配构造函数。我猜你不太明白我的答案。
我建议您在oracle网站上查找java教程,如果您能获得一本名为java编程语言的书,那就更好了。首先,对于初学者来说,不错:) 但是,在java中使用ArrayList对象类型时,列表的类型必须是要放入ArrayList中的对象的类型,也就是说,如果要保留学生列表,则必须将其作为
AssrayList<Student> = new ArrayList<Student>();
所以你的构造器应该在
Student(String name, String surname) {
setName(name)
setSurname(surname)
}
希望这有帮助:)
学生stu=newstudent()代码>=>您需要根据您的CTOR公共学生提供参数数据(字符串sFirstName、字符串sLastName、字符串sEmail、int iAge、int iStudentID、int[]iaGrades)
。您可以为演示目的硬编码它们,或者从文件中读取它们,或者要求输入它们。。。但对于初学者,我会硬编码它们(编一些)。ArrayList
您需要一个ArrayList
并添加其中的一些。然后你可以迭代它。或者get
它的一项(然后是一个学生,你可以调用它的方法)。顺便说一句:(a)前缀为“cs”和“ci”称为。不需要使用现代工具和语言,因为今天的解释器、编译器和调试器能够很好地理解类型和作用域,并且能够清楚地与您交流类型和作用域。把这种苦工留给机器干吧csEmail
->email
(B)这些是实例(对象)变量,而不是您所说的(非面向对象的)。其中的关键词是“非常旧”。匈牙利符号与和一起出现,旨在解决我们不再有的问题。myfloster.Add(newstudent(“A”,“Student”…);添加(新学生(“B”,“学生”…)所以我读了书中关于继承和实例化的部分,但我还是不明白。这可能是因为我不知道如何调用另一个类的构造函数。谢谢你的回复,所以我想如果我使用访问器和获取器的返回变量,我就能在构造函数中关联它们了?让它做我想做的事?你是说这是不正确的?不,一点也不正确