Java将对象调用到另一个类

时间:2017-03-14 11:49:34

标签: java arrays arraylist constructor encapsulation

好的,在此之前,我想提一下,这是针对学校的,所以请不要写任何修复我的代码的代码,因为这不会教我任何东西。相反,如果我使用不正确的术语,我正在寻找的是参考,解释和适当的术语。

所以我在这里遇到了一些问题。这是我需要做的,

*在Student类中包含以下方法: 一个。来自部分B1的每个实例变量的访问器(即,getter) 湾来自B1部分的每个实例变量的mutator(即setter)

注意:所有访问和更改Student类的实例变量都应该通过访问器和mutator方法。

℃。构造函数使用所有输入参数 d。 print()使用访问者(即,获取者)打印特定学生数据(例如,学生ID,名字,姓氏)

使用包含所有ArrayList方法调用的以下方法创建学生Roster类: 一个。 public static void remove(String studentID),用于通过学生ID从学校名单中删除学生

注意:如果学生ID不存在,该方法应该打印一条错误消息,表明找不到。

湾public static void print_all()使用访问器方法打印完整的以制表符分隔的学生数据列表

注意:标签可以这样格式化:1 [tab]名字:John [tab]姓氏:Smith [tab]年龄:20 [tab]成绩:{88,79,59}。 print_all()方法应遍历学生数组列表中的所有学生,并为每个学生调用print()方法。

℃。 public static void print_average_grade(String studentID),可以按学生ID正确打印学生的平均成绩 d。 public static void print_invalid_emails(),用于验证学生的电子邮件地址并显示所有无效的电子邮件地址*

上面是我遇到麻烦的地方,arraylist,构造函数和在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

 }
}

当我打电话给学生stu = new Student()时,这个名册课要求我添加参数;但我不明白在那里添加什么参数? 我也真的不明白我将如何整合我的arraylist来从学生班中添加我的方法? (对不起,如果我使用了错误的术语,请在需要时纠正我

名册等级

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
}


}

请任何有助于解释这一点的教程非常值得。

另外,我工作第三班,我的学校完全上线。在我醒来的时间里,我的导师不是或导师是不可用的,而且电子邮件并不是最好的沟通方式,因为回复需要几天时间。

2 个答案:

答案 0 :(得分:0)

你已经用带有参数的构造函数编写了Student类。因此,在Roaster类中,您必须调用Student类的匹配构造函数。我的猜测是你不太明白我的答案。 我建议在oracle站点上寻找一个java教程,如果你能得到一本名为The java programming language的书的副本,那就更好了

答案 1 :(得分:0)

首先是初学者,不错:) 但是,当在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)
}

希望这会有所帮助:)