Click to See Complete Forum and Search --> : BlueJ project help


joeboy
April 22nd, 2009, 01:47 PM
A
d

dlorde
April 22nd, 2009, 05:24 PM
here is my code for Person class:Looking at your code, the comments are quite confusing - in your Person class all the comments refer to a player instead of a person - and as I understand it, a player is a person, but a person isn't necessarily a player. IOW, making 'appropriate use of inheritance', Player inherits from Person.

My problem comes now as I'm not sure how to develop the Player class so that the players stored in Person class can be given their record for the season. I think I need an Array and a for loop but my code doesn't do what I want it to.. bit vague I know. The requirement is simple: "The information stored for each player consists of the number of games played, runs scored and the number of times the player was dismissed". Why would you need an array or a loop?

The first step towards wisdom is calling things by their right names...
Chinese Proverb

joeboy
April 22nd, 2009, 06:44 PM
Dlorde thanks.. have changed all the comments and simplified the code. I now understand that a person doesn't have to be a player. I just don't understand whether I need to make the two classes relate to each other? so that an object created in person can become a player? or do I need to look more closely at inheritance?

dlorde
April 23rd, 2009, 06:26 AM
I just don't understand whether I need to make the two classes relate to each other? so that an object created in person can become a player? or do I need to look more closely at inheritance?The instructions say "You should make appropriate use of inheritance in your solution", and it's clear that in this context a Player is a type of Person - i.e. a Player has all the attributes of a Person, plus the additional attributes of a Player. Also, in my last post, I said that Player inherits from Person - I don't really know how I could make it clearer...

Inheritance is generally about going from the general to the specific, e.g. Vehicle to Car, Person to Player, Player to Bowler. As a rule of thumb, if you can say that A is a specific type of B, then you can have an inheritance relationship where A inherits from and extends B. When working these things out, you should spend a little time thinking about the relationship between the types you are considering. Once you are clear about what each does and what its relationship is to the other types, you can think about how best to represent that relationship (inheritance, aggregation, composition, association, etc).

n practice, you do need to be careful with inheritance, because it's very easy to end up with deeply nested inheritance trees which can become unwieldy and inflexible - e.g. suppose you need Person who can be both a Player and a Student... in this sort of situation you might rework the design so that instead of using inheritance, a Person has Roles, which can include a PlayerRole and a StudentRole. However, just for the purposes of learning about inheritance, I wouldn't worry about this yet[/COLOR]].

[I]Teachers open the door, but you must enter by yourself...
Chinese Proverb

ProgramThis
April 23rd, 2009, 08:34 AM
That's because the constructor Person() (with no parameters) does not exist. Since your parent class only has a constructor that takes parameters you have to pass them in super(param1, param2...).

ProgramThis
April 23rd, 2009, 12:45 PM
right. Think I've sorted out Person and Player classes:

public class Person
{

public Person( String FirstName, String SecondName, int pAge, int pHeight, int pWeight)
{
...
}



public class Player extends Person
{
public Player(String stname, String ndname, int age, int height, int weight, int matchesPlayed, int runsScored, int timesDismissed)
{
super(stname, ndname, age, height, weight);
...
}

from here I thought it would be simple to make WicketKeeper and Bowler subclasses of player.. tried with bowler only to get "cannot find symbol player(int, int, int)" error..
can somebody spot what I'm doing wrong?

public class Bowler extends Player
{
public Bowler(int matches, int runs, int dismissals, int ballsBowled, int wicketsTaken, int runsConceded)
{
super(matches, runs, dismissals);
...
}

Look at your constructor for the Player class. Now look at how you call the super constructor from the Bowler class. Do you see a constructor in the Player class that takes three parameters? I see one that takes 8.

joeboy
April 23rd, 2009, 01:01 PM
public Bowler(String stname, String ndname, int age, int height, int weight, int matches, int runs, int dismissals, int ballsBowled, int wicketsTaken, int runsConceded)
{
super(matches, runs, dismissals, stname, ndname, age, height, weight);
balls = ballsBowled;
wickets = wicketsTaken;
runsAg= runsConceded;
}

is this what you mean? I still get the same error.

ProgramThis
April 23rd, 2009, 01:34 PM
You really need to read up on passing parameters (http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html). Parameters have to be passed to a method / constructor in the same order that they are expected.The first parameter expected in the Player constructor is of type String. You are trying to pass it an int as the first parameter.

joeboy
April 28th, 2009, 01:52 PM
Just need one more bit of help with this. I need to order the players by surname using the comparable interface.. i am not sure A. which class should implement and B. how to go about doing this without affecting my Superclasses.

dlorde
April 28th, 2009, 06:04 PM
Just need one more bit of help with this. I need to order the players by surname using the comparable interface.. i am not sure A. which class should implement...The Comparable interface does what it says on the tin - makes a class comparable. So it will be the class you want to compare that implements it.

B. how to go about doing this without affecting my superclasses.What do you mean by 'affecting'? You can implement it directly on the class in question, but 'surname' actually comes from a superclass, so you might want to think about whether that superclass should implement it so all the subclasses can be ordered by surname... That would affect the superclasses by giving them and every subclass the extra functionality - but it's up to you whether you think this is worth doing.

Note that there is another interface for comparisons, Comparator. This is used to implement separate comparison objects that will compare two objects of a class. Comparators can be passed to sort methods to be used in sorting lists and arrays, so that you can have a variety of Comparators to sort the same list or array in different ways. This is more flexible than having your class implement the Comparable interface, but does need the extra Comparator objects. So implementing Comparable is really providing the default or 'natural' sort order of your class, and separate Comparators can provide alternative sorts if necessary.

Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it...
A. Aho and J. Ullman