簡體   English   中英

Java將對象調用到另一個類的數組列表中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM