Question:
PLEASE HELP Java Constructors with instance data???? TEST tomorow?
mr. cheeze
2008-05-29 18:56:18 UTC
Write a constructor for class called Baseball players with the following instance data

private String name, position;
private int numAtBats, numSingles, numDoubles, numTriples, numHomeRuns;
private double battingAverage;

THEN I NEED to write a line of code to instantiate an object of baseball player class.

im not sure how to make this constructor PLEASE HELP
Three answers:
uberl337
2008-05-29 19:33:54 UTC
In order to write a constructor for an object, you must put in the name of the object (BaseballPlayers) like so:



class BaseballPlayers();



This accepts no parameters. If I'm reading the question right, that's acceptable. However, all the private variables must still have information put into them. So, you would have to assign all of them different values. So, for example, if the variables were assigned:



name = "Who";

position = "First Baseman";

numAtBats = 42;

numSingles = 10;

numDoubles = 5;

numTriples = 2;

numHomeRuns = 1;

battingAverage = 0.5;



Then, you would put ALL that information into the class. The only thing left to do is create a baseball player object, so:



class BaseballPlayer()

{

// All the above information here

}



// In a separate section of code...

BaseballPlayer Who = new BaseballPlayer();



I think that's the way to do it. If, however, you must pass in parameters into the class, that would entail different code.
anonymous
2016-04-10 14:55:44 UTC
Thank you for copying and pasting your homework assignment here. But what exactly was your question? Are you asking us to do it for you? I can do that, but I charge $250US / hour for that. Contact me if you are interested. Or, you could do your best at writing the code, and if it doesn't work, you could post that code here, and someone (might even be me, although I rather don't like Java) will probably answer your question for free.
?
2008-05-29 18:58:44 UTC
Java Help Site:



http://forum.java.sun.com/index.jspa


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...