使用多态性填充数组

时间:2015-04-28 00:55:37

标签: java arrays polymorphism

这是我的代码。我做了一点工作。我不应该只做一个申请人,我应该做很多,我应该把申请人填入两个不同的阵列,一个用于毕业生,一个用于本科生。我需要帮助搞清楚如何制作一个空阵列并填写那些申请人。一个开始会有帮助

import java.util.Scanner;

public class CollegeApplicant
{
String applicantName;
String collegeName;
public String getApplicantName()
{
    return applicantName;

}
public void setApplicantName(String applicantName)
{
    this.applicantName = applicantName;

}
public String getCollegeName()
{
    return collegeName;

}
public void setCollegeName(String collegeName)
{
    this.collegeName = collegeName;

}
public void checkCollege()
{
    int choice;
    Graduate g = new Graduate();
    Undergraduate ug = new Undergraduate();
    Scanner input = new Scanner(System.in);
    System.out.println("1. Undergraduate");
    System.out.println("2. Graduate");
    System.out.println("enter your choice ");
    choice = input.nextInt();
    if (choice == 1)
    {
        System.out.println("Enter SAT marks :");
        ug.SAT = input.nextDouble();
        System.out.println("Enter GPA marks :");
        ug.GPA = input.nextDouble();

    }
    else
    {
        System.out.println("Enter the origin of college:");
        g.collegeOrigin = input.next();
        System.out.println("Status :" + g.checkCollege());

    }

}
public class Undergraduate
{
    double SAT;
    double GPA;

}
public class Graduate
{
    String collegeOrigin;
    String checkCollege()
    {
        String information = null;
        if (collegeName.equals(collegeName)) information = "Applicant is applying from inside";
        else information = "Applicant is applying from outside";
        return information;

    }

}

}

客户

import java.util.Scanner;

public class CollegeApplicantClient extends CollegeApplicant
{
public static void main(String[] args)
{
    TestCollege tc = new TestCollege();
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the name of applicant ");
    tc.setApplicantName(input.next());
    System.out.println("enter the college name of applicant ");
    tc.setCollegeName(input.next());
    tc.checkCollege();

  }

 }

以下是可能输出的示例

Name: Joe

College Applied to: Harvard

SAT:    5

GPA:    2

Name: Tom

College Applied to: Yale

College of Origin: NYU – from outside

我需要能够将毕业生放在数组中,将本科生放在数组中。然后在完成后显示

0 个答案:

没有答案