1

I have 2 major troubles (that I'm aware of) with this program. Firstly, I don't know how to get FinalGrade and LetterGrade into the array. Secondly, I don't know how to use last name and first name to search for a student in the array. Let me know if you find other problems with my program. Thanks

This is the 1st class

package student;

public class Person {

    protected String FirstName, LastName;

    //Constructor
    public Person(String FirstName, String LastName) {
        this.FirstName = FirstName;
        this.LastName = LastName;
    }

    //Getters
 public String getFirstName() {
        return FirstName;
    }

    public String getLastName() {
        return LastName;
    }
    //Setters
    public void setFirstName(String FirstName) {
        this.FirstName = FirstName;
    }

     public void setLastName(String LastName) {
        this.LastName = LastName;
    }


}

This is the 2nd class:

package student;

import java.util.ArrayList;
import java.util.Scanner;



public class Student extends Person{
    private int HomeworkAve, QuizAve, ProjectAve, TestAve; 
    private double FinalGrade;
    private String LetterGrade;

//Constructor for the averages
    public Student(int HomeworkAve, int QuizAve, int ProjectAve, int TestAve, String FirstName, String LastName) 
    {
        super(FirstName, LastName);
        this.HomeworkAve = HomeworkAve;
        this.QuizAve = QuizAve;
        this.ProjectAve = ProjectAve;
        this.TestAve = TestAve;

    }

    //Method to calculate final grade and letter grade
    //Final grade calculation
    public double CalcGrade (int HomeworkAve, int QuizAve, int ProjectAve, int TestAve)
    {
FinalGrade = (double)(0.15*HomeworkAve + 0.05*QuizAve + 0.4 * ProjectAve + 0.4*TestAve);
return FinalGrade;
    }

    //Letter grade calculation
    public String CalcGrade ( double FinalGrade)
    {
        if ( FinalGrade >= 90.00)
        LetterGrade="A";
    else if(FinalGrade >= 80.00)
        LetterGrade="B";
    else if(FinalGrade>=70.00)
        LetterGrade="C";
    else if(FinalGrade>=60.00)
        LetterGrade="D";
    else LetterGrade="F";

        return LetterGrade;
    }

    public String getFullName (String FirstName,String LastName)
    {
        String str1 = FirstName;
        String str2 = LastName;
        String FullName = str1+","+str2;
        return FullName;
    }

    public Student(int HomeworkAve, int QuizAve, int ProjectAve, int TestAve, double FinalGrade, String LetterGrade, String FirstName, String LastName) {
        super(FirstName, LastName);
        this.HomeworkAve = HomeworkAve;
        this.QuizAve = QuizAve;
        this.ProjectAve = ProjectAve;
        this.TestAve = TestAve;
        this.FinalGrade = FinalGrade;
        this.LetterGrade = LetterGrade;

    }


    //Setters for this student class
    public void setHomeworkAve(int HomeworkAve) {
        this.HomeworkAve = HomeworkAve;
    }

    public void setQuizAve(int QuizAve) {
        this.QuizAve = QuizAve;
    }

    public void setProjectAve(int ProjectAve) {
        this.ProjectAve = ProjectAve;
    }

    public void setTestAve(int TestAve) {
        this.TestAve = TestAve;
    }

    public void setFinalGrade(int FinalGrade) {
        this.FinalGrade = FinalGrade;
    }

    public void setLetterGrade(String LetterGrade) {
        this.LetterGrade = LetterGrade;
    }


    //Getters for this student class
    public int getHomeworkAve() {
        return HomeworkAve;
    }

    public int getQuizAve() {
        return QuizAve;
    }

    public int getProjectAve() {
        return ProjectAve;
    }

    public int getTestAve() {
        return TestAve;
    }

    public double getFinalGrade() {
        return FinalGrade;
    }

    public String getLetterGrade() {
        return LetterGrade;
    }

   public void DisplayGrade (){
       System.out.println(FirstName+" "+LastName+"/nFinal Grade: "+FinalGrade+"/nLetter Grade: "+ LetterGrade);
   }




   public static void main(String[] args){
    Scanner oScan = new Scanner(System.in);
    Scanner iScan = new Scanner(System.in);

    boolean bContinue = true;
    int iChoice;

   ArrayList<Student> students = new ArrayList<>();


   while (bContinue == true)
        {
   //The menu
       System.out.println("1.       New Class List");
       System.out.println("2.       Search for a Student");
       System.out.println("3.       Exit");
       System.out.println("Choose an item");

       iChoice = iScan.nextInt();


   //The 1st case: when the user wants to enter the new list   
   if (iChoice == 1){

   System.out.println("Enter the number of students");

   int numberOfStudents = iScan.nextInt();

    for(int iCount = 0;iCount < numberOfStudents;){

    System.out.println("Enter the name for Student " + ++iCount);
    System.out.println("Enter First Name");
    String FirstName = oScan.nextLine();
    System.out.println();

    System.out.println("Enter Last Name");
    String LastName = oScan.nextLine();
     System.out.println();

    System.out.println("Enter Homework Average");
    int HomeworkAve = iScan.nextInt();
    System.out.println();

    System.out.println("Enter Quiz Average");
    int QuizAve = iScan.nextInt();
    System.out.println();

    System.out.println("Enter Project Average");
    int ProjectAve = iScan.nextInt();
    System.out.println();

    System.out.println("Enter Test Average");
    int TestAve = iScan.nextInt();
    System.out.println();

How to get FinalGrade and LetterGrade??

 Student hobbit = new Student(HomeworkAve,QuizAve, ProjectAve,TestAve,FirstName, LastName);
    students.add(hobbit);
  }
}




 //The 2nd case: when the user wants to search for a student        
    else if (iChoice == 2)
    {
     System.out.println("Enter First Name");
        String FirstName = oScan.nextLine();
        System.out.println();

        System.out.println("Enter Last Name");
        String LastName = oScan.nextLine();
        System.out.println();

Here is my revised code to find the student but I don't know how to print the array after I found it

int i = 0;
for (Student student : students) {

    if (FirstName != null & LastName != null);{
    if (student.getFirstName().equals(FirstName) && student.getLastName().equals(LastName)) {

   System.out.println(students.get(i)); //this doesn't work

        break;
    }
    i++;

}
}   

 //The 3r case: when the user wants to exit       
 else if (iChoice == 3)
            {
                bContinue = false;
            }
}
}

}

1 Answer 1

2

You have an ArrayList of type Student and you are doing indexOf on a variable of type String. You will have to traverse the entire ArrayList and then do a comparison to find out if the student name mathces. Sample code:

int i = 0;
for (Student student : students) {
    // Add null checks if first/last name can be null
    if (student.getFirstName().equals(firstName) && student.getLastName().equals(lastName)) {
        System.out.println("Found student at " + i);
        break;
    }
    i++;
}

FinalGrade and LetterGrade are variables in each object, I think you are mistaken about the concept of an Array/ArrayList. The ArrayList only holds each Student object, not the variable in each object. You can go through each student and get their finalGrade and letterGrade with the get* functions. Before doing this, you should make a call to CalcGrade methods so the grades are calculated.

Sign up to request clarification or add additional context in comments.

2 Comments

Probably better off using a map if you need to find the student by a common key, such as lastName,firstName. If you put the students into a Map<String,Student> where there key is lastName + "," + firstName, then you can find your student a lot easier.
True, but this is a more general solution if you need to find elements by comparing variables of an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.