Я новичок в кодировании, и у меня есть задание по кодированию, которое помогает нам практиковаться с конструкторами и классами. Я чувствую, что хорошо разбираюсь в своем уровне, поэтому я пришел сюда, чтобы опубликовать это, потому что был в тупике. Я получаю сообщение об ошибке в Main при попытке сделать print getColleges(20))
, метод внутри CollegeGroup. CollegeGroup должен быть массивом объектов College. Колледжи 20, как предполагается, сортируют колледжи за обучение 20 000 $. Однако я получаю ошибку
не может найти символ
символ: метод getColleges (int)
расположение: класс Main
Что я могу изменить, чтобы я мог правильно печатать getColleges(20))
? Спасибо за вашу помощь
College.java
package colleges;
public class College {
private int tuition;
private String name;
private String region;
private double minGPA;
public College(int aTuition, String aName, String aRegion, double minScore) {
tuition = aTuition;
name = aName;
region = aRegion;
minGPA = minScore;
}
public double getMinimumGPA() {
return minGPA;
}
public String getName() {
return name;
}
public int getTuition() {
return tuition;
}
public String getRegion() {
return region;
}
public void setTuition(int aTuition) {
tuition = aTuition;
}
public String toString() {
return ("College: " + name + "\tTuition: " + tuition
+ "\tRegion: " + region);
}
CollegeGroup.java
package colleges;
import java.util.List;
import java.util.ArrayList;
public class CollegeGroup {
private List<College> colleges = new ArrayList<College>();
public void addCollege(College aCollege) {
colleges.add(aCollege);
}
public void removeCollege(String collegeName) {
for (int i = 0; i < colleges.size(); i++) {
if (colleges.get(i).getName().equalsIgnoreCase(collegeName)) {
colleges.remove(i);
}
}
}
public void updateTuition(String collegeName, int newTuition) {
for (int i = 0; i < colleges.size(); i++) {
if (colleges.get(i).getName().equalsIgnoreCase(collegeName)) {
colleges.get(i).setTuition(newTuition);
}
}
}
public List<College> getColleges() {
return colleges;
}
public List<College> getColleges(String region) {
//return a list of colleges in the given region
List<College> regionCollege = new ArrayList<College>();
for (int i = 0; i < colleges.size(); i++) {
if (colleges.get(i).getRegion().equalsIgnoreCase(region)) {
regionCollege.add(colleges.get(i));
}
}
return regionCollege;
}
public List<College> getColleges(int maxTuition) {
//return a list of colleges with tuition below maxTuition
List<College> mTut = new ArrayList<College>();
for (int i = 0; i < colleges.size(); i++) {
if (colleges.get(i).getTuition() < maxTuition) {
mTut.add(colleges.get(i));
}
}
return mTut;
}
public List<College> getColleges(double myGPA, int maxTuition) {
//return a list of colleges with minimum GPA requirements below
//myGPA, and tuitions below maxTuition
List<College> perfCollege = new ArrayList<College>();
for (int i = 0; i < colleges.size(); i++) {
if (colleges.get(i).getTuition() < maxTuition && colleges.get(i).getMinimumGPA() <= myGPA) {
perfCollege.add(colleges.get(i));
}
}
return perfCollege;
}
public void sortCollegesByRegion() {
//sort in the following order:
//Northeast, Southeast, MidWest, West
List<College> Northeast = new ArrayList<College>();
List<College> Southeast = new ArrayList<College>();
List<College> MidWest = new ArrayList<College>();
List<College> West = new ArrayList<College>();
for (int i = 0; i < colleges.size(); i++) {
if (colleges.get(i).getRegion().equalsIgnoreCase("Northeast")) {
Northeast.add(colleges.get(i));
} else if (colleges.get(i).getRegion().equalsIgnoreCase("Southeast")) {
Southeast.add(colleges.get(i));
} else if (colleges.get(i).getRegion().equalsIgnoreCase("MidWest")) {
MidWest.add(colleges.get(i));
} else if (colleges.get(i).getRegion().equalsIgnoreCase("West")) {
West.add(colleges.get(i));
}
}
}
public void sortCollegesByMinimumGPA() {
//code goes here, sort from low to high.
int smallest = Integer.MAX_VALUE;
List<College> sorted = new ArrayList<College>();
while (colleges.size() != 0) {
for (int i = 0; i < colleges.size(); i++) {
if (colleges.get(i).getMinimumGPA() < smallest) {
sorted.add(colleges.get(i));
colleges.remove(i);
}
}
}
sorted = colleges;
}
public void sortCollegesByTuition() {
//code goes here, sort from low to high.
int smallest = Integer.MAX_VALUE;
List<College> sorted = new ArrayList<College>();
while (colleges.size() != 0) {
for (int i = 0; i < colleges.size(); i++) {
if (colleges.get(i).getTuition() < smallest) {
sorted.add(colleges.get(i));
colleges.remove(i);
}
}
}
sorted = colleges;
}
public String toString() {
String out = "";
for (int i = 0; i < colleges.size(); i++) {
out += colleges.get(i).toString(); //test whether toString is necessary on colleges.get()
out += "\n";
}
return out.substring(0, out.length() - 1);
}
Main.java
package colleges;
public class Main {
public static void main(String[] args) {
CollegeGroup collegeList = new CollegeGroup();
College colUni = new College(17025,"College University", "Northeast", 2.3);
College westCol = new College(28450,"Westcoast College", "West", 2.9);
College budgCol = new College(700,"Budget College", "Southeast", 1.0);
College goodUni = new College(95000,"Reallygood University", "Northeast", 3.9);
College partyUni = new College(22150,"Partyschool U ", "West", 2.1);
College hogwarts = new College(12000,"Hogwarts", "Northeast", 3.5);
collegeList.addCollege(colUni);
collegeList.addCollege(westCol);
collegeList.addCollege(budgCol);
collegeList.addCollege(goodUni);
collegeList.addCollege(partyUni);
collegeList.addCollege(hogwarts);
System.out.println(collegeList.getColleges());
System.out.println(colUni.toString());
budgCol.setTuition(8);
System.out.println(getColleges(20));
}