需要帮助创建学生信息代码

时间:2014-12-09 05:30:40

标签: java

我需要帮助创建一个程序 必须包括: 1.学生 一个。一定有 一世。学生姓名(第一个和最后一个) II。生日 III。 ID # IV。年级(新生,大二等) v。课程表(一个数组?) 六。您选择的3个构造函数(必须包含默认值) 七。所有实例变量的Getter和Setter方法 1.这意味着每个实例变量至少有2个方法,以便能够获取和设置这些变量。 2.这些方法是公开的,因此它们可以在类外使用,而实例变量将是私有的,因此它们不会被类的用户破坏。 八。适用于所有方法的文档

这是我到目前为止所拥有的

public class StudentTester
{

public static void main(String[] args)
{
    Student someGuy = new Student();    //This creates a new student using the defalt constructor
    Student someGal = new Student("Amanda","Soh");  //This creates a new student and automatically sets the firt and last name variables

    String justAHolder = someGuy.getFullName();
    String justAnotherHolder = someGal.getFullName();

    System.out.print(someGal.getFullName());
    someGuy.setMonth(11);
    someGal.setMonth(14);
    //If you uncomment the next line and try and compile the program, you will be able to see how it generates an error
    //someGal.grade=12;       //NO, NO, No, grade is supposed to be private!!!!!
}

/**
 * Constructor for objects of class Student
 */
public Student()
{
    // initialise instance variables
    fName = "John";
    lName = "Doe";


}

/**
 * This is my second constructor example that students name
 * 
 * @param   fName   This is the input for the first name
 * @param   lName   This is the input for the last name
 */
public Student(String fName, String lName)
{
    this();
    this.fName = fName;
    this.lName = lName;

}

//Here are the Getter Methods
/**
 * This is the getter method to get the full name
 * 
 * @return  This returns the first and last name as a single String
 */
public String getFullName()
{
    return fName+" "+lName;
}

//Here are the Setter Methods
/**
 * This is the method to set the month of the students birthday
 * 
 * @param   newMonth    The new month to set the birthdate with
 */
public void setMonth(int newMonth)
{
    if(newMonth>0 && newMonth<13)
    {
        bDayMonth = newMonth;
    }else
    {
        System.out.println("No one is born in Octovember!");
    }
}

}

1 个答案:

答案 0 :(得分:0)

您的学生班:

package com.test;

public class Student {

    public Student() {
        firstName = "John";
        lastName = "Doe";
        birthDate = "05/11/1990";
    }

    public Student(String fName, String lName, String dob, String sGrade,
            String[] classSchedule) {
        firstName = fName;
        lastName = lName;
        birthDate = dob;
        grade = sGrade;
        schedule = classSchedule;
    }

    private String firstName;

    private String lastName;

    private String birthDate;

    private String grade;

    private String[] schedule;

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName
     *            the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName
     *            the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the birthDate
     */
    public String getBirthDate() {
        return birthDate;
    }

    /**
     * @param birthDate
     *            the birthDate to set
     */
    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }

    /**
     * @return the grade
     */
    public String getGrade() {
        return grade;
    }

    /**
     * @param grade
     *            the grade to set
     */
    public void setGrade(String grade) {
        this.grade = grade;
    }

    /**
     * @return the schedule
     */
    public String[] getSchedule() {
        return schedule;
    }

    /**
     * @param schedule
     *            the schedule to set
     */
    public void setSchedule(String[] schedule) {
        this.schedule = schedule;
    }

    public String getFullName() {
        return firstName + " " + lastName;
    }
}

然后是测试员班:

package com.test;

public class StudentTester {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Student st1 = new Student(); // This creates a new student using the
                                        // defalt constructor
        String[] sch = { "Math 10.30", "Physics 14.00" };
        Student st2 = new Student("Amanda", "Soh", "10/12/1990", "Freshman",
                sch);// This creates a new student and automatically sets all
                        // attributes
        System.out.println("First Student is: " + st1.getFullName());
        System.out.println("Details of 2nd student: " + st2.getFullName() + " "
                + st2.getBirthDate() + " " + st2.getGrade() + " "
                + st2.getSchedule()[0] + " " + st2.getSchedule()[1]);
    }

}