7 Replies - 827 Views - Last Post: 10 February 2010 - 06:29 PM Rate Topic: -----

Topic Sponsor:

#1 jazzrockcompile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 09-February 10

Class Design Problem

Posted 10 February 2010 - 02:31 PM

Hey D.I.C.

Im having a few problems creating basic classes and was wondering if you could give me some ideas, basically i need...

Room class

+Data
name of room
can contain up to 10 animal objects // use an Array of objects? how to declare

+Constructor
room name

+public void addAnimal(Animal a) method //add animals into the array

+toString() that calls Animals toString() //returns name of room as well as the names of animals inside the room

and an...

Animal Class

+Data
name of animal

+Constructor
intitalize animal name

+room reference // Animal objects need to know what Room they are in

+toString() - returns names of all animals in the room

so far i have ...

public class Room {

// instance variables
    String name;

// constructor
    Room(String name) {
        this.name = name;
    }

// array that limits the amount of animals in a Room object
    roomArray = new String[9]

// methods


    
}



public class Animal {

    String name;

    Animal(String name){
        this.name = name;
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Class Design Problem

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 997
  • View blog
  • Posts: 3,023
  • Joined: 20-August 09

Re: Class Design Problem

Posted 10 February 2010 - 02:54 PM

first, i would like to suggest some guidelines if i may.
when declaring variables, methods or constructors in the class, it would be wise to assign them Access Levels.
public\private etc.
when you don't assign them with access level, the compiler set them with default access level.
for small projects and assignments, you will not feel to much different, but when working on bigger projects, part of the design would be determining the access levels of the members of the class.

now, a Room Object should be able to hold 10 Animal Objects. so the array should be an array of Animal Objects.
as:
Animal[] roomArray = new Animal[10];


now look at my code, and look at yours:
roomArray = new String[9] 


first mistake, what is roomArray? what kind of data type it contains? you should first declare the data type.
second, when declaring an array of SIZE values, declare it with the SIZE. the array's indexes will be int values between 0 to SIZE -1. But the declaration must be of the SIZE. The way you declare your array, you will have 9 values instead of 10.

also, usually, when declaring a non constant variable, better initiate it in the constructor.
so, your Room class should look like:
public class Room {

private String roomName;
private Animal[] roomArray;

public Room(String name){
this.roomName = name;
this.roomArray = new Animal[10];
}

public void addAnimal(Animal a){
//here is a "tricky" part. do you have place in the array? 
//or is the array full? what index should you add to?
//if you are allowed to use List implementation, use it instead of an array.




i hope i could help, as for the Animal class, try to apply my suggestion on it too. good luck :^:
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7492
  • View blog
  • Posts: 28,831
  • Joined: 27-December 08

Re: Class Design Problem

Posted 10 February 2010 - 02:57 PM

For your roomArray, don't forget to declare the type before initializing and the semi-colon at the end of the statement, like so: String[] roomArray = new String[9]; from roomArray = new String[9]. And actually, you would probably want to declare it as an Animal[], not a String[], so you can store Animal objects in it. Also, the length of the array should be 10 if you want to store 10 elements, not 9. Remember, 9 will be the last index in the array.

As for the room each Animal is stored in, it should contain a Room variable in the Animal class like so:
class Animal{
   Room animalRoom;
   ..code..
}


Was This Post Helpful? 1
  • +
  • -

#4 jazzrockcompile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 09-February 10

Re: Class Design Problem

Posted 10 February 2010 - 03:14 PM

Thanks! i wasn't sure if i should declare the array as an array of string variables or an array objects


awesome advice thanks guys!

now i made an Animal Class

public class Animal {

private Room animalRoom;
private String animalName;

public Animal(String name) {
	this.animalName = name;
	this.animalRoom = roomName;
	
	
	
	
	
}


}



Now im afraid that the animalName will refer to the name variable in the Room class instead of the one in the Animal class... should i make the Animal object private?

thanks for the help

This post has been edited by jazzrockcompile: 10 February 2010 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 jazzrockcompile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 09-February 10

Re: Class Design Problem

Posted 10 February 2010 - 05:43 PM

uh bump...
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7492
  • View blog
  • Posts: 28,831
  • Joined: 27-December 08

Re: Class Design Problem

Posted 10 February 2010 - 05:54 PM

Quote

Now im afraid that the animalName will refer to the name variable in the Room class instead of the one in the Animal class... should i make the Animal object private?

thanks for the help


In your Animal class, you will get an error upon compilation b/c the varibale roomName is undefined. You probably want to include that as a param in the constructor. Also, animalName will not refer to the name variable in the Room class until you tell it to. The param in the constructor String name is simply a local variable called a parameter designed to allow a method to accept certain values or references to objects in order to perform its function.
public class Animal {

private Room animalRoom;
private String animalName;

public Animal(String name) {
	this.animalName = name;
	this.animalRoom = roomName;	
}

}


This post has been edited by macosxnerd101: 10 February 2010 - 05:54 PM

Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 997
  • View blog
  • Posts: 3,023
  • Joined: 20-August 09

Re: Class Design Problem

Posted 10 February 2010 - 06:11 PM

macosxnerd101 was correct, however i believe he had accidently pasted just a copy of your code, instead of his correction.

the Room object doesnt have to be initiated in the constructor.
only when an Animal object is added to the a certain room.
use a setRoom(Room room) method to do it.
public void setRoom(Room room){
this.animalRoom = room;
}


and leave the constructor to handle only the Animal's name:
public class Animal { 
 
private Room animalRoom; 
private String animalName; 
 
public Animal(String name) { 
        this.animalName = name;      
} 
public void setRoom(Room room){
this.animalRoom = room;
}
}



make sure to call setRoom whenever an Animal is added to a Room Object.
this should be done inside the addAnimal method of Room:
public void addAnimal(Animal animal){
animal.setRoom(this);
//handle adding animal to this room
}



**EDITED curly brackets added

This post has been edited by japanir: 10 February 2010 - 06:12 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7492
  • View blog
  • Posts: 28,831
  • Joined: 27-December 08

Re: Class Design Problem

Posted 10 February 2010 - 06:29 PM

View Postjapanir, on 10 February 2010 - 09:11 PM, said:

macosxnerd101 was correct, however i believe he had accidently pasted just a copy of your code, instead of his correction.


Sorry for any confusion. I was referring to the code while I was posting, and I just left it there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1