Very Confused - JAVA

3 answers Last reply
More about very confused java
  1. Ok firstly all method names should start with a lowercase character. Always.

    The BaseballPlayer class should not contain the main method. Have another class to contain the main method. Right now every single BaseballPlayer instance has an array of the 5 players. So like I suggested last time just have a class called Runner and put your main method in that and have a variable, not property of the Runner class, that is the array of the 5 players.

    All these missing symbol method messages is because you are using the player class instead of the relevant class. Do this instead:

    baseball = new BaseballPlayer();

    Either make baseball an instance of BaseballPlayer or when you are calling the method do a cast to the correct class:

    ((BaseballPlayer)baseball).setAB(10);

    The method you are getting about the player class is because you are trying to instanciate an object that has not been fully implemented. Make the player class abstract. Then create instances of BaseballPlayer and FootballPlayer in the main method when you need to.
  2. I'm having trouble with the same problem. I stumbled on this and tried to build off of it. I tried the changes you suggested but there's still problems.

    import java.util.Scanner;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.PrintStream;
    import java.io.*;
    import java.text.*;

    public class run {

    player[] players = new player[5];



    public static void main(String[] args)
    {

    public void setPlayers(int i, player newPlayers)
    {
    players = newPlayers;
    }



    player baseball = new Baseballplayer();
    player football = new Footballplayer();
    Scanner input = new Scanner(System.in);

    for (int x=0; x < 6; x++)
    {
    baseball = new Baseballplayer();

    System.out.printf("Please enter baseball players name: " );
    String a = input.nextLine();
    baseball.SetName(a);
    System.out.printf("Please enter baseball players team: " );
    String b = input.nextLine();
    baseball.SetTeamName(b);
    System.out.printf("Please enter baseball players games played: " );
    int c = input.nextInt();
    baseball.SetPlayed(c);
    System.out.printf("Please enter baseball players AB: " );
    int d = input.nextInt();
    ((Baseballplayer)baseball).SetAB(d);
    System.out.printf("Please enter baseball players hits: " );
    int e = input.nextInt();
    ((Baseballplayer)baseball).SetHits(e);
    System.out.printf("Please enter baseball players HR: " );
    int f = input.nextInt();
    ((Baseballplayer)baseball).SetHR(f);
    Baseballplayer.ShowStats();
    setPlayers(x,baseball);
    }

    for (int x = 0; x <6; x++)
    {
    football = new Footballplayer();

    System.out.printf("Please enter football players name: " );
    String a = input.nextLine();
    football.SetName(a);
    System.out.printf("Please enter football players team: " );
    String b = input.nextLine();
    football.SetTeamName(b);
    System.out.printf("Please enter football players games played: " );
    int c = input.nextInt();
    football.SetPlayed(c);
    System.out.printf("Please enter football players TD: " );
    int d = input.nextInt();
    ((Footballplayer)football).SetTD(d);
    System.out.printf("Please enter football players Carries: " );
    int e = input.nextInt();
    ((Footballplayer)football).SetCarries(e);
    System.out.printf("Please enter football players Yards: " );
    int f = input.nextInt();
    ((Footballplayer)football).SetYards(f);
    Baseballplayer.ShowStats();
    setPlayers(x,football);
    }

    }
    }
    ----------------------------------
    public abstract class player implements IName, ISerializable {

    protected String name;
    protected String TeamName;
    protected int GamesPlayed;

    public player()
    {
    name = "name";
    TeamName = "Team";
    GamesPlayed = 0;
    }

    public String GetName()
    {
    return name;
    }
    public void SetName(String NewName)
    {
    NewName = name;
    }

    public String GetTeamName()
    {
    return TeamName;
    }
    public void SetTeamName(String newTeamName)
    {
    newTeamName = TeamName;
    }

    public int GetPlayed(){
    return GamesPlayed;
    }
    public void SetPlayed(int played)
    {
    played = GamesPlayed;
    }


    abstract void ShowStats();
    public void Save(PrintStream ps);

    }
    ---------------------
    public class Baseballplayer extends player
    {
    private int AB, hits, HR;

    public Baseballplayer()
    {
    AB = 0;
    hits =0;
    HR = 0;
    }





    public int GetAB(){
    return AB;
    }
    public void SetAB(int newAB){
    newAB = AB;
    }
    public int GetHits(){
    return hits;
    }
    public void SetHits(int newHits){
    newHits = hits;
    }
    public int GetHR(){
    return HR;
    }
    public void SetHR(int newHR){
    newHR = HR;
    }

    public double Avg(){
    double avgBat;
    avgBat = hits/AB;
    return avgBat;
    }
    public void ShowStats(){
    double avg = Avg();
    System.out.printf("%s %s %d %d %d %f %d", name, TeamName, GamesPlayed, AB, hits, avg, HR);
    }
    public void Save(PrintStream ps){
    double avg = Avg();
    ps.printf("%s %s %d %d %d %f %d", name, TeamName, GamesPlayed, AB, hits, avg, HR);
    }
    }

    -----------------------------------
    import java.util.Scanner;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.PrintStream;
    import java.io.*;
    import java.text.*;

    public class Footballplayer extends player
    {
    protected int TD, carries, yards;

    public Footballplayer()
    {
    TD = 0;
    carries = 0;
    yards = 0;
    }




    public int GetTD(){
    return TD;
    }
    public void SetTD(int newTD){
    newTD = TD;
    }
    public int GetCarries(){
    return carries;
    }
    public void SetCarries(int newCarries){
    newCarries = carries;
    }
    public int GetYards(){
    return yards;
    }
    public void SetYards(int newYards){
    newYards = yards;
    }




    public double YardsPerCarry(){
    double avgyards;
    avgyards = yards/carries;
    return avgyards;
    }
    public void ShowStats(){
    double avg = YardsPerCarry();
    System.out.printf("%s %s %d %d %d %d %f", name, TeamName, GamesPlayed, TD, carries, yards, avg);
    }
    public void Save(PrintStream ps){
    double avg = YardsPerCarry();
    System.out.printf("%s %s %d %d %d %d %f", name, TeamName, GamesPlayed, TD, carries, yards, avg);
    }

    }

    Alot of errors coming from the public void setplayers, Baseballplayer.ShowStats, and setPlayers(x,baseball)


    any help is appreciated
  3. You should have probably created another thread for this.

    When you say errors do you mean this doesn't compile?

    You're using a development environment (IDE) like Eclipse right? That will give you error details and how to fix them.

    Firstly all class names should start with an uppercase letter so players should be Players. The Footballplayer class should call super() in it's constructor.

    The setplayers method should be outside of the main method; you cannot have method declarations inside other methods. To call it from a static method you'll need to also make it static -> public static void setPlayers(int i, player newPlayers)

    Baseballplayer.ShowStats() would be a static method call on the Baseballplayer class. You want to call showStats on the Baseballplayer object. What you are looking to do is baseball.ShowStats()
Ask a new question

Read More

Programming Java Apps