我有一个方法文件名学生,他们是一堆方法。我需要这些方法来完成实验13.我还有一个学生对象的arraylist。在我的实验室13代码我收到一个错误,我无法将字符串转换为学生。
我已经尝试了所有我可以做的事情我试图通过将学生放在代码前面示例student.jessica.addfriend(“emily”)来转换它。但它仍然无法正常工作。请帮我解决我的问题。
这是实验室13的代码
import java.util.ArrayList;
public class Lab13
{
public static void main(String[] args)
{
Student jessica = new Student("Jessica", 23,
Student.SocialYear.SENIOR);
Student henry = new Student("Henry", 16, Student.SocialYear.FRESHMAN);
String n = jessica.getName();
System.out.println("One student is " + n + ".");
System.out.println("This student is " + jessica.getAge() + " years old.");
System.out.print(henry.getName() + " used to be " + henry.getAge() + " years old.");
henry.getAge();
System.out.println(" He just had a birthday and is now " + henry.getAge() + " years old.");
System.out.println(jessica.getName() + " is a " + jessica.getSocialYear() + ".");
printVote(jessica);
printVote(henry);
// HELP! Modify me to use the updated Student class.
//What type input parameter should be passed into the method?
jessica.addFriend("Erin");
jessica.addFriend("Bert");
henry.addFriend("Isaac");
henry.addFriend("Kaley");
// HELP! Modify me to use the updated Student class.
// What type of objects does the ArrayList of friends have?
printFriend(jessica, "Bert");
printFriend(henry, "Sam");
// HELP! Create more instances of the Student class for testing.
// HELP! Add friends to the ArrayLists of the instances.
// HELP! Print out the favorite friend of one of the students.
// HELP! Change the favorite friend of one of the students.
// The new favorite should already be a friend of the student.
// Print out this new favorite friend.
// HELP! Print out all the friends of one of the students.
// HELP! Check to see if a student is friends with another student.
// This test should be true (the student should have the friend).
// HELP! Check to see if a student is friends with another student.
// This test should be false (the student should NOT have the friend).
// HELP! A friend transfers and a student loses track.
// The student should unfriend the friend.
// Print out the updated list of friends.
}
/**
* This method prints whether a student can vote based on age.
*
* @param s A Student object
*/
public static void printVote(Student s)
{
if (s.canVote()) {
System.out.println(s.getName() + " can vote.");
} else {
System.out.println(s.getName() + " is not old enough to vote.");
}
}
/**
* This method prints whether a student knows the friend.
*
* @param s A Student object
* @param f A String representing the friend
*/
public static void printFriend(Student s, String f)
{
if (s.hasFriend(f)) {
System.out.println(s.getName() + " is friends with " + f + ".");
} else {
System.out.println(s.getName() + " does not know " + f + ".");
}
}
}
这是我的学生方法
import java.util.ArrayList;
import java.util.Collections;
public class Student
{
public enum SocialYear { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR }
private String name;
private ArrayList<Student> friends;
private int age;
private SocialYear year;
/**
* Constructor for objects of class Student
*/
public Student(String n, int a, SocialYear sy)
{
this.name = n;
this.age = a;
this.year = sy;
this.friends = new ArrayList<Student>();
}
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public void setAge()
{
this.age++;
}
public SocialYear getSocialYear()
{
return this.year;
}
public boolean canVote()
{
return this.age >= 18;
}
public void addFriend(Student friend)
{
friends.add(friend);
}
public boolean hasFriend(String friend)
{
return friends.contains(friend);
}
public void unFriend(Student friend)
{
friends.remove(friend);
}
public void getFriends()
{
for(Student s : friends)
{
System.out.println(s + ",");
}
}
public Student getFavFriendName()
{
return friends.get(0);
}
public void myNewFavFriend(Student friend)
{
friends.remove(friend);
friends.add(0, friend);
}
}
方法addFriend
以Student
为参数
public void addFriend(Student friend)
您的代码提供了一个String。
jessica.addFriend("Erin");
这会奏效
jessica.addFriend(henry);
因为你已经创建了一个名为亨利的Student
。
Student henry = new Student("Henry", 16, Student.SocialYear.FRESHMAN);