Very Confused - JAVA

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
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
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
 

Rusting In Peace

Distinguished
Jul 2, 2009
312
0
19,060
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.
 

Jeremiah_20

Distinguished
Dec 6, 2011
1
0
18,510
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



 

Rusting In Peace

Distinguished
Jul 2, 2009
312
0
19,060
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()