관리 메뉴

데이터 과학

배열에서 출력 문제 (StudentTest) 본문

AP/AP Computer Science A

배열에서 출력 문제 (StudentTest)

티에스윤 2024. 8. 27. 15:33

public class StudentTest {

public static void computeAllGrades(Student[] studentList) {
for (Student s : studentList)
   if (s != null)
     s.computeGrade();
}



public static void main(String args[]){

Student[] stu = new Student[5];

stu[0] = new Student("Brian Lorenzen", new int[] {90,94,99},"none");
stu[1] = new UnderGrad("Tim Broder", new int[] {90,90,100},"none");
stu[2] = new GradStudent("Kevin Cristella",new int[] {85,70,90}, "none", 1234);

computeAllGrades(stu);

 }
}

class Student
{
//data members
public final static int NUM_TESTS = 3;
private String name;
private int[] tests;
private String grade;
//constructor
public Student()
{
name = "";
tests = new int[NUM_TESTS];
grade = "";
}
//constructor
public Student(String studName, int[] studTests, String studGrade)
{
name = studName;
tests = studTests;
grade = studGrade;
}
public String getName()
{ return name; }
public String getGrade()
{ return grade; }
public void setGrade(String newGrade)
{ grade = newGrade; }
public void computeGrade()
{
if (name.equals(""))
grade = "No grade";
else if (getTestAverage() >= 65)
grade = "Pass";
else
grade = "Fail";
System.out.println(grade);
}

public double getTestAverage()
{
double total = 0;
for (int score : tests)
total += score;
return total/NUM_TESTS;
}
}


 
class UnderGrad extends Student
{
public UnderGrad() //default constructor
{ super(); }
//constructor
public UnderGrad(String studName, int[] studTests, String studGrade)
{ super(studName, studTests, studGrade); }
public void computeGrade()
{
if (getTestAverage() >= 70)
setGrade("Pass");
else
setGrade("Fail");
}
}

class GradStudent extends Student
{
private int gradID;
public GradStudent() //default constructor
{
super();
gradID = 0;
}
//constructor
public GradStudent(String studName, int[] studTests,
String studGrade, int gradStudID)
{
super(studName, studTests, studGrade);
gradID = gradStudID;
}
public int getID()
{ return gradID; }
public void computeGrade()
{
//invokes computeGrade in Student superclass
super.computeGrade();
if (getTestAverage() >= 90)
setGrade("Pass with distinction");
}
}

 

 

결과:

Pass

Pass

'AP > AP Computer Science A' 카테고리의 다른 글

indexOf, substring 결과  (0) 2025.10.20
추상화 예제  (0) 2024.10.29
ArrayList - add(), get(), set(), remove()  (0) 2024.06.04
ArrayList 예제 -2  (0) 2024.06.04
추상화와 super 예제  (0) 2024.06.04