-2

So for part of an assignment i have I have to tell if a student is elgiable for a scholarship if their gpa is above a 3.0 but I'm confused as to how I would do it? Here's my array in main

Student[] student = new Student[3]; //create the array of students
    student[0] = new Student("Tom", "Cooper", 3.5, "EE", "Junior"); 
    student[1] = new Student("Annie", "Todd", 2.3, "CS", "Sophomore");
    student[3] = new Student("Luis", "Rodriguez", 3.8, "INFO", "Sophomore");

and then I have a student class file that contains the constructor

public Student(String firstName, String lastName, double gpa, String major, String year) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.gpa = gpa;
    this.major = major;
    this.year = year;
  }
 public void setgpa(double gpa) {
    this.gpa = gpa;
  }

  public double getgpa() {
    return gpa;

  }

So for my output I want just those who have above a 3.0, I'm assuming I would need a for loop but not exactly sure how it would specifically check their gpa for eligibility or if I do it within the main or the class?

Inez
  • 9
  • 2
  • 3
    Do you know how to iterate through an array? Do you know how to compare a number with another? Do you know how to call the method of an object (i.e: do you know how to call "getgpa()" of student[0])? – MyUsername112358 Apr 13 '16 at 03:45
  • 1
    Not a homework factory here. Stack Overflow is for narrowly focused technical questions. Many similar Questions could be explored such as [this](http://stackoverflow.com/q/26766273/642706), [this](http://stackoverflow.com/q/10202700/642706), and [this](http://stackoverflow.com/q/19507832/642706). – Basil Bourque Apr 13 '16 at 03:47
  • Yes two the first two! Calling the method of an object was my first instinct but I wasn't too sure how to do it? I tried doing it for loop with an if getgpa >= 3.0 but was getting an error so to that I am lost on – Inez Apr 13 '16 at 03:48
  • I bet you didn't even searched how do this. – Johnny Willer Apr 13 '16 at 03:55
  • @JohnnyWiller Yikes, my first go to is definitely not asking on here. I have been searching through this site and many other sites also in my textbook and notes. This is my last I'm pulling my hair out resort since i've been lucky to receive a lot of help here nor am I asking for my entire assignment to be done for me. This is simply a small part I'm not understanding and have been trying for the last couple of hours but thanks for your comment. – Inez Apr 13 '16 at 04:01
  • In order for us to believe you actually tried something, we need to actually see your relevant code. Show us how you iterated through the array, called the method of each students, then compared the result. If your code can't compile, tell us what errors it gives you. We could easily do it for you, but that's not the point of this website. Show us you actually want to get helped and that you tried stuff on your own too. – MyUsername112358 Apr 13 '16 at 04:45

3 Answers3

0
for(int i=0; i < 3; i++) {
    if(student[i].getGPA() > 3) {
        System.out.println(student[i].getName());
    }
}
VHS
  • 9,534
  • 3
  • 19
  • 43
  • This is how I did it before asking it here but my getGPA is in my Student class not my main so it creates an error that I need to create getGPA() in my main – Inez Apr 13 '16 at 03:55
  • Yes, the getGPA method will be in the Student class because GPA is an attribute for a Student object. As long as you are instantiating the Student class in the Main method, you should be able to access all the Student methods including the getGPA. You shouldn't have received any errors. If you still got an error, it could be because of something else. Post your full code for us to help you further. – VHS Apr 13 '16 at 03:59
  • Your code is wrong. Java is a case sensitive language. getGPA() is not the same as getgpa(). Also, you giving him the answer for his homework like that is of no help at all. Stackoverflow is NOT an homework factory. – MyUsername112358 Apr 13 '16 at 13:08
  • @MyUsername112358. I know that Java is case sensitive. When I provided the OP with a quick solution to his problem, my intention was not to provide an exact working code.. Rather I was intending to give him the method. i didn't check whether his method is getGPA or getGpa. – VHS Apr 13 '16 at 14:27
0

you can do like this. Iterate through all and add the eligible students to a list.

public List<Student> getEligibleStudents(Student[] students){
    List<Student> eligibleStudents=new Arraylist<>();
    for(int i=0;i<students.length;i++){
        if(student[i].gpa>3.0){
            eligibleStudents.add(student[i]);
        }
    }
    return eligiblStudents;
}
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
0
for (Student stud : student) {
    if (stud.getGPA >= MINIMUMGPA) {
        DO YOUR OUTPUT HERE;
    }
}

This would be implemented in the same scope in which you initialised your Studennt[] student indicated above.

Note that this implementation will work for any number of array elements, without needing to be modified to a specific number which you have included in your array

Adam S
  • 76
  • 1
  • 6