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
FootballPlayer Class
Player Class
IName interface
ISerializable interface
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
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
Code:
/**
* @(#)BaseballPlayer.java
*
* BaseballPlayer application
*
* @author
* @version 1.00 2011/5/2
*/
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 BaseballPlayer extends Player{
private int AB, hits, HR;
private Player[] players = new Player[5];
public void setPlayers(int i, Player newPlayers){
players[i] = newPlayers;
}
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);
}
public static void main(String[] args) {
Player baseball = new Player();
Player football = new Player();
Scanner input = new Scanner(System.in);
for (int x = 0; x < 6; x++){
baseball = new Player();
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();
baseball.SetAB(d);
System.out.printf("Please enter baseball players hits: ");
int e = input.nextInt();
baseball.SetHits(e);
System.out.printf("Please enter baseball players HR: ");
int f = input.nextInt();
baseball.SetHR(f);
setPlayers(x,baseball);
}
for (int x = 0; x < 6; x++){
football = new Player();
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();
football.SetTD(d);
System.out.printf("Please enter football players Carries: ");
int e = input.nextInt();
football.SetCarries(e);
System.out.printf("Please enter football players Yards: ");
int f = input.nextInt();
football.SetYards(f);
setPlayers(x,football);
}
}
}
FootballPlayer Class
Code:
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;
protected int Carries;
protected int 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);
}
}
Player Class
Code:
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 Player implements IName, ISerializable {
protected String name;
protected String TeamName;
protected int GamesPlayed;
public Player(){
name = "";
TeamName = "";
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 = name;
}
public int GetPlayed(){
return GamesPlayed;
}
public void SetPlayed(int played){
played = GamesPlayed;
}
abstract void ShowStats();
public void Save(PrintStream ps);
}
IName interface
Code:
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 interface IName {
public String GetName();
public void SetName(String name);
}
ISerializable interface
Code:
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 interface ISerializable {
public void Save(PrintStream ps);
}
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