Sign in with
Sign up | Sign in
Your question

Very Confused - JAVA

Last response: in Applications
Share
May 3, 2011 12:41:43 AM

Hey I am so confused on my last homework for my java class. I am not getting what he wanted me to do. Help would be awesome, so stressed from all the work I have to do before the semester ends. I will put the code that I have so far and also the assignment specs:

I am confused on the whole main method part, I just cant figure out what he wants. Also I think some of my coding is wrong =/

THANK YOU for looking!!!!!




Assignment Specs:


1. Create an interface called IName. This interface should contain the following two methods:
String GetName() – The implementation of this method should return the name of the object.
void SetName(String name) – The implementation of this method should set the name of the object.

2. Create an interface called ISerializable. This interface should contain the following method:
void Save(PrintStream ps) – The implementation of this method should save the class member variable data to the given PrintStream instance.

3. Create an abstract class called Player. Here are the specifications for the Player class:
a. Interfaces:
i. Implement the IName interface.
ii. Implement the ISerializable interface
b. Member Variables:
Name, TeamName, GamesPlayed
Note: These should be the ONLY member variables. Use appropriate access modifiers.
c. Normal Member Methods:
i. Write appropriate Get/Set methods.
ii. Default constructor. This should set the member variables to appropriate default values.
d. Abstract Member Method:
i. void ShowStats().
ii. Methods on the ISerializable interface.

4. Create a BaseballPlayer class that is derived from Player. BaseballPlayer should contain the following:
a. Member Variables: AB (stands for “at bats”), hits, HR
Note: These should be the ONLY member variables. Use appropriate access modifiers.
b. Member Methods:
i. Write appropriate Get/Set methods.
ii. Default constructor. This should set the member variables to appropriate default values.
iii. double Avg() – Returns the batter’s average. A batter’s average is calculated by dividing the hits by the at bats. For example, if a player has 30 hits and 100 AB then their average is .300.
iv. Override ShowStats(). This method should print the following:
Name, Team, Games, AB, hits, Avg, HR
v. Override Save(PrintStream ps). This method should save the member variable data to the given PrintStream.

5. Create a FootballPlayer class that is derived from Player. FootballPlayer should contain the following:
a. Member Variables: TD (stands for “touch down”), Carries, Yards
Note: These should be the ONLY member variables. Use appropriate access modifiers.
b. Member Methods:
i. Write appropriate Get/Set methods
ii. Default constructor. This should set the member variables to appropriate default values.
iii. double YardsPerCarry() – Returns the player’s average yards per carry. Yards per carry is calculated by dividing the yards by the carries. For example, if a player has 100 yards and 20 carries then their yards per carry is 5.
iv. Override ShowStats(). This method should print the following:
Name, Team, Games, TD, Carries, Yards, Yards Per Carr
v. Override Save(PrintStream ps). This method should save the member variable data to the given PrintStream.

6. Main method.
a. Create an array of the base type (Player). The array should contain at least five elements. Populate the array with instances of BOTH derived types. You should fill each element with different values for the member variables.
b. Write a loop to iterate through the elements of the array. Inside of the loop you should demonstrate polymorphic behavior. Hint: Call the method that will run code depending on the type of the underlying object.
c. Open an output file. Use a PrintStream object for as the output file.
d. Declare an ISerializable instance.
e. Write a loop that will iterate through the elements of the players array again. For this loop, during each iteration you will do two things. First, copy the current element of the array into the ISerializable pointer. Second, call the Save method through the ISerializable pointer. Make sure you pass the PrintStream object that you previously opened into the Save method.



BaseballPlayer Class
  1. /**
  2. * @(#)BaseballPlayer.java
  3. *
  4. * BaseballPlayer application
  5. *
  6. * @author
  7. * @version 1.00 2011/5/2
  8. */
  9.  
  10. import java.util.Scanner;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.FileReader;
  14. import java.io.PrintStream;
  15. import java.io.*;
  16. import java.text.*;
  17.  
  18. public class BaseballPlayer extends Player{
  19.  
  20. private int AB, hits, HR;
  21. private Player[] players = new Player[5];
  22.  
  23. public void setPlayers(int i, Player newPlayers){
  24. players[i] = newPlayers;
  25.  
  26. }
  27.  
  28. public BaseballPlayer(){
  29. AB = 0;
  30. hits = 0;
  31. HR = 0;
  32. }
  33.  
  34. public int GetAB(){
  35. return AB;
  36. }
  37.  
  38. public void SetAB(int newAB){
  39. newAB = AB;
  40. }
  41.  
  42. public int GetHits(){
  43. return hits;
  44. }
  45.  
  46. public void SetHits(int newHits){
  47. newHits = hits;
  48. }
  49.  
  50. public int GetHR(){
  51. return HR;
  52. }
  53.  
  54. public void SetHR(int newHR){
  55. newHR = HR;
  56. }
  57.  
  58. public double Avg(){
  59. double avgBat;
  60. avgBat = hits/AB;
  61. return avgBat;
  62. }
  63.  
  64. public void ShowStats(){
  65. double avg = Avg();
  66. System.out.printf("%s %s %d %d %d %f %d", name, TeamName, GamesPlayed, AB, hits, avg, HR);
  67.  
  68. }
  69.  
  70. public void Save(PrintStream ps){
  71. double avg = Avg();
  72. ps.printf("%s %s %d %d %d %f %d", name, TeamName, GamesPlayed, AB, hits, avg, HR);
  73. }
  74.  
  75.  
  76. public static void main(String[] args) {
  77.  
  78. Player baseball = new Player();
  79. Player football = new Player();
  80. Scanner input = new Scanner(System.in);
  81.  
  82.  
  83.  
  84. for (int x = 0; x < 6; x++){
  85.  
  86. baseball = new Player();
  87.  
  88. System.out.printf("Please enter baseball players name: ");
  89. String a = input.nextLine();
  90. baseball.SetName(a);
  91. System.out.printf("Please enter baseball players team: ");
  92. String b = input.nextLine();
  93. baseball.SetTeamName(b);
  94. System.out.printf("Please enter baseball players games played: ");
  95. int c = input.nextInt();
  96. baseball.SetPlayed(c);
  97. System.out.printf("Please enter baseball players AB: ");
  98. int d = input.nextInt();
  99. baseball.SetAB(d);
  100. System.out.printf("Please enter baseball players hits: ");
  101. int e = input.nextInt();
  102. baseball.SetHits(e);
  103. System.out.printf("Please enter baseball players HR: ");
  104. int f = input.nextInt();
  105. baseball.SetHR(f);
  106. setPlayers(x,baseball);
  107.  
  108. }
  109.  
  110. for (int x = 0; x < 6; x++){
  111. football = new Player();
  112.  
  113. System.out.printf("Please enter football players name: ");
  114. String a = input.nextLine();
  115. football.SetName(a);
  116. System.out.printf("Please enter football players team: ");
  117. String b = input.nextLine();
  118. football.SetTeamName(b);
  119. System.out.printf("Please enter football players games played: ");
  120. int c = input.nextInt();
  121. football.SetPlayed(c);
  122. System.out.printf("Please enter football players TD: ");
  123. int d = input.nextInt();
  124. football.SetTD(d);
  125. System.out.printf("Please enter football players Carries: ");
  126. int e = input.nextInt();
  127. football.SetCarries(e);
  128. System.out.printf("Please enter football players Yards: ");
  129. int f = input.nextInt();
  130. football.SetYards(f);
  131. setPlayers(x,football);
  132.  
  133. }
  134.  
  135. }
  136. }


FootballPlayer Class

  1. import java.util.Scanner;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.FileReader;
  5. import java.io.PrintStream;
  6. import java.io.*;
  7. import java.text.*;
  8.  
  9. public class FootballPlayer extends Player {
  10.  
  11. protected int TD;
  12. protected int Carries;
  13. protected int Yards;
  14.  
  15. public FootballPlayer(){
  16. TD = 0;
  17. Carries =0;
  18. Yards = 0;
  19. }
  20.  
  21. public int GetTD(){
  22. return TD;
  23. }
  24.  
  25. public void SetTD(int newTD){
  26. newTD = TD;
  27. }
  28.  
  29. public int GetCarries(){
  30. return Carries;
  31. }
  32.  
  33. public void SetCarries(int newCarries){
  34. newCarries = Carries;
  35. }
  36.  
  37. public int GetYards(){
  38. return Yards;
  39. }
  40.  
  41. public void SetYards(int newYards){
  42. newYards = Yards;
  43. }
  44.  
  45. public double YardsPerCarry(){
  46. double avgYards;
  47. avgYards = Yards / Carries;
  48. return avgYards;
  49.  
  50. }
  51.  
  52. public void ShowStats(){
  53. double avg = YardsPerCarry();
  54. System.out.printf("%s %s %d %d %d %d %f", name, TeamName, GamesPlayed, TD, Carries, Yards, avg);
  55.  
  56. }
  57.  
  58. public void Save(PrintStream ps){
  59. double avg = YardsPerCarry();
  60. System.out.printf("%s %s %d %d %d %d %f", name, TeamName, GamesPlayed, TD, Carries, Yards, avg);
  61.  
  62. }
  63.  
  64. }


Player Class
  1. import java.util.Scanner;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.FileReader;
  5. import java.io.PrintStream;
  6. import java.io.*;
  7. import java.text.*;
  8.  
  9. public class Player implements IName, ISerializable {
  10.  
  11. protected String name;
  12. protected String TeamName;
  13. protected int GamesPlayed;
  14.  
  15. public Player(){
  16. name = "";
  17. TeamName = "";
  18. GamesPlayed = 0;
  19. }
  20.  
  21. public String GetName(){
  22. return name;
  23. }
  24.  
  25. public void SetName(String NewName){
  26. NewName = name;
  27. }
  28.  
  29. public String GetTeamName(){
  30. return TeamName;
  31. }
  32.  
  33. public void SetTeamName(String newTeamName){
  34. newTeamName = name;
  35. }
  36.  
  37. public int GetPlayed(){
  38. return GamesPlayed;
  39. }
  40.  
  41. public void SetPlayed(int played){
  42. played = GamesPlayed;
  43. }
  44.  
  45. abstract void ShowStats();
  46.  
  47. public void Save(PrintStream ps);
  48. }


IName interface
  1. import java.util.Scanner;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.FileReader;
  5. import java.io.PrintStream;
  6. import java.io.*;
  7. import java.text.*;
  8.  
  9. public interface IName {
  10.  
  11. public String GetName();
  12. public void SetName(String name);
  13. }


ISerializable interface
  1. import java.util.Scanner;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.FileReader;
  5. import java.io.PrintStream;
  6. import java.io.*;
  7. import java.text.*;
  8.  
  9. public interface ISerializable {
  10.  
  11. public void Save(PrintStream ps);
  12. }




ERRORS
<--BaseballPlayer class-->
cannot find symbol method SetAB(int) -Line 99
cannot find symbol method SetHits(int) -Line 102
cannot find symbol method SetHR(int) -Line 105
non-static method setPlayers(int,Player) cannot be referenced from a static context -Line 106
cannot find symbol method SetTD(int) -Line 124
cannot find symbol method SetCarries(int) -Line 127
cannot find symbol method SetYards(int) -Line 130
non-static method setPlayers(int,Player) cannot be referenced from a static context -Line 131

<--Player class-->
Player is not abstract and does not override abstract method ShowStats() in Player - Line 9
missing method body, or declare abstract - Line 47

More about : confused java

a b L Programming
May 3, 2011 8:59:35 AM

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.
December 7, 2011 2:00:03 AM

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



a b L Programming
December 7, 2011 7:35:41 AM

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()
!