hasCompletedAllRequiredClasses

hasCompletedAllRequiredClasses

public class CSStudent
{
String[] requiredGeneralClasses = {"biology", "calculus", "chemistry", "english", "physics", "psychology", "economics"};
String[] requiredInMajorClasses = {"algorithms", "data structures", "numerical analysis", "graphics", "databases"};
String[] completedClasses;
int[] gradeForCompletedClasses;
String[] inCompleteClasses;
int count = 0;

public CSStudent()
{
    completedClasses = new String[1000];
    gradeForCompletedClasses = new int[1000];
    inCompleteClasses = new String[1000];
}

public void completeClass(String classs /*class*/, int gradeOnHundredPointScale)
{
    completedClasses[count] = classs;

    if(gradeOnHundredPointScale <= 100)
    {
        gradeForCompletedClasses[count] = gradeOnHundredPointScale;
    }
    count++;
}

public double getInMajorGPA()
{
    double inMajorGPA = 0;
    double numberOfInMajorClassesCompleted = 0;
    double gradeForCompletedInMajorClasses = 0;
    for(int i = 0; i < count; i++)
    {
        if(completedClasses[i].equals(requiredInMajorClasses[0]) || completedClasses[i].equals(requiredInMajorClasses[1]) || completedClasses[i].equals(requiredInMajorClasses[2]) || completedClasses[i].equals(requiredInMajorClasses[3]) || completedClasses[i].equals(requiredInMajorClasses[4]))
        {
            numberOfInMajorClassesCompleted++;
            if(gradeForCompletedClasses[i] > 93.50 && gradeForCompletedClasses[i] <= 100)
            {
                gradeForCompletedInMajorClasses +=  4.0;
            }
            else if(gradeForCompletedClasses[i] > 90 && gradeForCompletedClasses[i] < 93.50)
            {
                gradeForCompletedInMajorClasses +=  3.7;
            }
            else if(gradeForCompletedClasses[i] > 86.5 && gradeForCompletedClasses[i] < 90)
            {
                gradeForCompletedInMajorClasses +=  3.3;
            }
            else if(gradeForCompletedClasses[i] > 83.5 && gradeForCompletedClasses[i] < 86.5)
            {
                gradeForCompletedInMajorClasses +=  3.0;
            }
            else if(gradeForCompletedClasses[i] > 80 && gradeForCompletedClasses[i] < 83.5)
            {
                gradeForCompletedInMajorClasses +=  2.7;
            }
            else if(gradeForCompletedClasses[i] > 76.5 && gradeForCompletedClasses[i] < 80)
            {
                gradeForCompletedInMajorClasses +=  2.3;
            }
            else if(gradeForCompletedClasses[i] > 73.5 && gradeForCompletedClasses[i] < 76.5)
            {
                gradeForCompletedInMajorClasses +=  2.0;
            }
            else if(gradeForCompletedClasses[i] > 70 && gradeForCompletedClasses[i] < 73.5)
            {
                gradeForCompletedInMajorClasses +=  1.7;
            }
            else if(gradeForCompletedClasses[i] > 66.5 && gradeForCompletedClasses[i] < 70)
            {
                gradeForCompletedInMajorClasses +=  1.3;
            }
            else if(gradeForCompletedClasses[i] > 63.5 && gradeForCompletedClasses[i] < 66.5)
            {
                gradeForCompletedInMajorClasses +=  1.0;
            }
            else if(gradeForCompletedClasses[i] > 60 && gradeForCompletedClasses[i] < 63.5)
            {
                gradeForCompletedInMajorClasses +=  0.70;
            }
            else if(gradeForCompletedClasses[i] < 60)
            {
                gradeForCompletedInMajorClasses +=  0;
            }
        }
    }
    inMajorGPA = gradeForCompletedInMajorClasses/numberOfInMajorClassesCompleted;
    return inMajorGPA;
}

public double getOverallGPA()
{
    double overallGPA = 0;
    double gradeForAllCompletedClasses = 0;
    for(int i = 0; i < count; i++)
    {
        if(gradeForCompletedClasses[i] > 93.50 && gradeForCompletedClasses[i] <= 100)
        {
            gradeForAllCompletedClasses +=  4.0;
        }
        else if(gradeForCompletedClasses[i] > 90 && gradeForCompletedClasses[i] < 93.50)
        {
            gradeForAllCompletedClasses +=  3.7;
        }
        else if(gradeForCompletedClasses[i] > 86.5 && gradeForCompletedClasses[i] < 90)
        {
            gradeForAllCompletedClasses +=  3.3;
        }
        else if(gradeForCompletedClasses[i] > 83.5 && gradeForCompletedClasses[i] < 86.5)
        {
            gradeForAllCompletedClasses +=  3.0;
        }
        else if(gradeForCompletedClasses[i] > 80 && gradeForCompletedClasses[i] < 83.5)
        {
            gradeForAllCompletedClasses +=  2.7;
        }
        else if(gradeForCompletedClasses[i] > 76.5 && gradeForCompletedClasses[i] < 80)
        {
            gradeForAllCompletedClasses +=  2.3;
        }
        else if(gradeForCompletedClasses[i] > 73.5 && gradeForCompletedClasses[i] < 76.5)
        {
            gradeForAllCompletedClasses +=  2.0;
        }
        else if(gradeForCompletedClasses[i] > 70 && gradeForCompletedClasses[i] < 73.5)
        {
            gradeForAllCompletedClasses +=  1.7;
        }
        else if(gradeForCompletedClasses[i] > 66.5 && gradeForCompletedClasses[i] < 70)
        {
            gradeForAllCompletedClasses +=  1.3;
        }
        else if(gradeForCompletedClasses[i] > 63.5 && gradeForCompletedClasses[i] < 66.5)
        {
            gradeForAllCompletedClasses +=  1.0;
        }
        else if(gradeForCompletedClasses[i] > 60 && gradeForCompletedClasses[i] < 63.5)
        {
            gradeForAllCompletedClasses +=  0.70;
        }
        else if(gradeForCompletedClasses[i] < 60)
        {
            gradeForAllCompletedClasses +=  0;
        }
    }
    overallGPA = gradeForAllCompletedClasses/count;
    return overallGPA;
}

public String[] getAllIncompleteRequiredClasses()
{
    int counter = 0;
    for(int i = 0; i < count; i++)
    {
        if(!requiredGeneralClasses[0].equals(completedClasses[i]))
        {
            inCompleteClasses[counter] = requiredGeneralClasses[0];
            counter++;
        }
    }
    return inCompleteClasses;
}

public String[] getAllIncompleteInMajorClasses()
{

}

public boolean hasCompletedAllRequiredClasses()
{
    boolean hasCompletedAllRequiredClasses = false;
    for(int i = 0; i < count; i++)
    {
        if(requiredGeneralClasses[0].equals(completedClasses[i]) && requiredGeneralClasses[1].equals(completedClasses[i]))
        {
            hasCompletedAllRequiredClasses = true;
        }
    }
    return hasCompletedAllRequiredClasses;
}


测试仪

public class CSStudentTester
{
public static void main(String[] args)
{
    CSStudent student = new CSStudent();
    student.completeClass("biology", 75);
    student.completeClass("algorithms", 95);
    student.completeClass("physics", 89);
    student.completeClass("data structures", 85);
    System.out.println("InMajor GPA: " + student.getInMajorGPA());
    System.out.println("Overall GPA: " + student.getOverallGPA());

    for(int i = 0; i < student.count; i++)
    {
    System.out.println(student.getAllIncompleteRequiredClasses()[i]);
    }

    System.out.println(student.hasCompletedAllRequiredClasses());
}
}


我有这个课,我有2种方法的问题:


对于getAllIncompleteRequiredClasses方法,我应该打印所有未完成的必需类。我使用打印语句只是为了看看是否正确。但是我不希望它打印dkl,因为在tester类中已经调用了requiredGeneralClasses [4](即物理学)(请参见上文)。我做错了什么事?
对于public boolean hasCompletedAllRequiredClasses()方法,当我调用以下方法时,它应该返回true,因为所有必需的类都在那里。但是当我尝试它时,它返回false。为什么打印错误?


码:

 student.completeClass("calculus", 75);
student.completeClass("chemistry", 95);
student.completeClass("english", 89);
student.completeClass("psychology", 75);
student.completeClass("economics", 95);

最佳答案

您可以通过以下方式修改函数getAllIncompleteRequiredClasses。我假设您要打印所有不完整的必需类(常规和专业)

public String[] getAllIncompleteRequiredClasses()
{
    int counter = 0;
    boolean match =false;
    for(int k = 0; k < requiredGeneralClasses.length; k++)
    {
        for(int i = 0; i < count; i++)
        {

            //System.out.println(requiredGeneralClasses[k] + " "+ completedClasses[i]);
            if(requiredGeneralClasses[k].equalsIgnoreCase(completedClasses[i]))
            {
                match=true;
                break;
            }
        }
        if(!match)
        {
            //inCompleteClasses[counter] = new String();
            inCompleteClasses[counter] = requiredGeneralClasses[k];
            counter++;
        }
        match = false;
    }

    for(int k = 0; k < requiredInMajorClasses.length; k++)
    {
        for(int i = 0; i < count; i++)
        {

            //System.out.println(requiredGeneralClasses[k] + " "+ completedClasses[i]);
            if(requiredInMajorClasses[k].equalsIgnoreCase(completedClasses[i]))
            {
                match=true;
                break;
            }
        }
        if(!match)
        {
            inCompleteClasses[counter] = requiredInMajorClasses[k];
            counter++;
        }
        match = false;
    }
    return inCompleteClasses;
}


和您的功能hasCompletedAllRequiredClasses

public boolean hasCompletedAllRequiredClasses()
{
     for(int i = 0; i < inCompleteClasses.length; i++)
        {
            if(inCompleteClasses[i]!=null)
                return false;
        }
     return true;
}


当然,您可以以更好的方式优化代码。

09-11 18:13