0

Hello all so I was given this Driver class and was told to make classes to get the driver class working properly. For the most part I think im on the right track but then again im not completely sure because I am new to java. I am getting 2 compiling errors because I have not added a add(temp) method. to be honest im not sure what class to put the method into. for now I have it in the Team class but im getting a compiling error. if anyone can give me some insight it will be well appreciated.

public class Driver{
 public static void main(String args[]) throws Exception{
    //All information is stored in input.txt and is being
    //read in below.
    Scanner input = new Scanner(new File("input.txt"));
    //Creates a new League and passes in a String signifying
    //which league it is.
    League american = new League("AL");
    League national = new League("NL");
    for(int i=0; i<15; i++){
        //Creates a new team and adds the current team
        //to the american league.
        //You can assume there are exactly 15 teams in each league.
        Team temp = new Team(input.next());
        american.add(temp);   // compile error
    }
    for(int i=0; i<15; i++){
        //Creates a new team and adds the current team
        //to the national league.
        //You can assume there are exactly 15 teams in each league.
        Team temp = new Team(input.next());
        national.add(temp);   // compile error 
    }
    }

my League class

public class League{
private String league;

public League(String League){
     league = League;
}

public void setLeagueAmerican(String League){
    this.league = League;
}

public String getLeagueAmerican(){
    return league;
}

public void setLeagueNational(String national){
    this.league = national;
}

public String getLeagueNational(){
    return league;
}


public void League( String League){

     league = League;

}
}

my Team Class

public class Team
{
// instance variables - replace the example below with your own
private String team;

/**
 * Constructor for objects of class Team
 */
public Team(String Team)
{
    team = Team;
}

public void setTeam(String Team){
    this.team = Team;
}

public String getTeam(){
    return team;
}



public String add(League y)
{

    return y;       //compiling error 
}
}
5
  • 3
    Always post the compiling errors Commented Dec 13, 2017 at 2:05
  • That add() method should be in the League class: public void add(Team team) { /* ... */ }. Also, tag your question with the java tag. Commented Dec 13, 2017 at 2:08
  • i will add them in just a second Commented Dec 13, 2017 at 2:08
  • in the league class was i correct by making a getLeagueAmerican and getLeagueNational and a set for both of them or should i only have a getLeague and setLeague? Commented Dec 13, 2017 at 2:12
  • Robbie what would put in the body of the add method? Commented Dec 13, 2017 at 2:17

1 Answer 1

1
public String add(League y)
{
    return y;       //compiling error 
}

This function returns a String. You are passing the parameter y, which is a League. You then try to return the very same League as if it was a String, which produces the error.

Either change the return type to be a League, or don't return y but a meaningful String (or even y.toString())

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

Comments

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.