9 Replies - 365 Views - Last Post: 08 April 2012 - 11:07 AM Rate Topic: -----

#1 javanewbie99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-April 12

Calling objects from another class in ArrayLists

Posted 08 April 2012 - 09:07 AM

I am trying to call the objects from my Room class in my Hotel class. It compiles, but only returns default values. Can someone explain why?

Here is my Room class:

import java.lang.String;

public class Room
{	
	private int getRoomNum;
	private String getBedType;
	private double getRoomRate;
	private char getSmoking;
	private String getOccupant;
	private String notOccupied;
	public String q;
	public String k;
	private String occupantName;

		
	public Room(int roomNum, String bedType, double rate, char smoking, String occupantName)
	{
		roomNum = 0;
		bedType = "";
		rate = 0;
		smoking = ' ';
		occupantName = "";
		
	}
	
	public String getBedType(String bedType)
	{
		
		if(bedType == "q")
		{
			bedType = "Queen";
		}
		else if (bedType == "k")
		{
			bedType = "King";
		}
		else 
		{
			bedType = "Twin";
		}
		return bedType;
	}
	
	public char getSmoking(char smoking)
	{
		
		if(smoking == 's')
		{
			smoking = 's';
		}
		else
		{
			smoking = 'n';
		}
		return smoking;
		
	}
	
	public int getRoomNum(int roomNum)
	{
		return roomNum;	
	}
	
	public double getRoomRate(double rate)
	{
		return rate;
	}
	
	public String getOccupant(String occupantName)
	{
		return occupantName;
	}
	
	public String setOccupied(boolean isOccupied)
	{
		notOccupied = "Not Occupied";
		if(isOccupied)
		{
			return occupantName;
		}
		else
		{
			return notOccupied;
		}
		
	}
	
	public String setOccupant(String occupantName)
	{
		this.occupantName = occupantName;
		return occupantName;
	}

	public boolean isOccupied(boolean occupied)
	{	
	

		if(occupantName == "Not Occupied")
		{
			return false;
		}
		else
		{
			return true;
		}		
	}
	
	public String toString()
	{
		String showRoom = "Room Number: " + getRoomNum + "\n" + "Occupant Name: " + getOccupant + "\n" + "Smoking Room: " + getSmoking + "\n" + "Bed Type: " + getBedType + "\n" + "Rate: " + getRoomRate;
		return showRoom; 
	}
}



and Hotel class:

import java.util.ArrayList;

public class Hotel
{
	
	ArrayList<Room> theRooms = new ArrayList<Room>();
	
	public void addRoom()
	{
		theRooms.add(new Room(101, "q", 99.99, 's', "Sally Fields")); 
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Calling objects from another class in ArrayLists

#2 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1974
  • View blog
  • Posts: 4,816
  • Joined: 10-September 10

Re: Calling objects from another class in ArrayLists

Posted 08 April 2012 - 09:11 AM

And how are you driving what you've shown us? Do you have a class with a main() method somewhere?
Was This Post Helpful? 1
  • +
  • -

#3 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: Calling objects from another class in ArrayLists

Posted 08 April 2012 - 09:22 AM

	public ArrayList geValues(){
	 return this.theRooms;
	}

Was This Post Helpful? 1
  • +
  • -

#4 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 201
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Calling objects from another class in ArrayLists

Posted 08 April 2012 - 09:28 AM


public class Hotel
{
	
	ArrayList<Room> theRooms = new ArrayList<Room>();
	
	public void addRoom()
	{
		theRooms.add(new Room(101, "q", 99.99, 's', "Sally Fields")); 
	}
}



This is a default method, entering default values for the room object when no arguments are used for addRoom. Do you also have another the overloaded method, which takes arguments for the room object, e.g.

//Method to add an user defined room
public void addRoom(int roomNum, String bedType, double rate, char smoking, String occupantName){
    //Add the room to the array list
     theRooms.add(new Room(roomNum, bedType, rate, smoking, occupantName))
}



There is a high possibility that this is the problem. If you already have such a method, post your public static void main(|String[] args) from your test class and we will try to help you further.

Edit: the subject of this topic looks awfully familiar with one of the topics I've seen earlier these days.

This post has been edited by karabasf: 08 April 2012 - 09:32 AM

Was This Post Helpful? 1
  • +
  • -

#5 codeMonkey_1066  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 71
  • Joined: 22-March 12

Re: Calling objects from another class in ArrayLists

Posted 08 April 2012 - 09:29 AM

Look at your constructor.

View Postjavanewbie99, on 08 April 2012 - 09:07 AM, said:

	public Room(int roomNum, String bedType, double rate, char smoking, String occupantName)
	{
		roomNum = 0;
		bedType = "";
		rate = 0;
		smoking = ' ';
		occupantName = "";
		
	}


you don't assign any of the parameter values to those of the objects fields.
What you want is code of the form
public class Room
{
 private int roomNumuber = 0 ; // sets the default value.

 public Room(int roomNumber)
 {
  // set the room number of THIS new Room object to the value of the variable that is passed in.
  this.roomNumber = roomNumber ;
 }
}


This post has been edited by codeMonkey_1066: 08 April 2012 - 09:30 AM

Was This Post Helpful? 1
  • +
  • -

#6 javanewbie99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-April 12

Re: Calling objects from another class in ArrayLists

Posted 08 April 2012 - 09:40 AM

I apologize. I forgot to add the test class that I was using.

public class RoomTest
{
	private String q;
	
	public static void main(String[]args)
	{
		
		
		Room r1 = new Room(101, "q", 99.99, 's', "Sally Fields");
		System.out.println(r1.toString());
	
		
		
	}
}



I could not determine how to test it directly from the Hotel class, so I copied and pasted the code directly into a RoomTest class. This is the first time that I've ever tried to use code from one class in another class and then test it in a third class, so I'm totally confused.

Thanks for your response.
Was This Post Helpful? 0
  • +
  • -

#7 codeMonkey_1066  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 71
  • Joined: 22-March 12

Re: Calling objects from another class in ArrayLists

Posted 08 April 2012 - 09:46 AM

A couple of extra comments.

As a room can be smoking or non smoking ( only two options ) your far better off using a boolean to record it.

Also having too many parameters to your constructor ( or any method for that method ) is bad practice.
Try to limit the parameters to only those that are different for every instance of the class and create set methods for the other fields.
For example
 private int roomNumber = 0 ;
 private double rate = 0.0;
 private boolean smoking = false ;

 public Room(int roomNumer)
 {
  this.roomNumber = roomNumber ;
 }

 public void setRate(double rate)
 {
  this.rate = rate ;
 }

 ...


 Room room = new Room(101);
 room.setRate(10.20);
 room.setSmoking(true);
 ...
 
 theRooms.add(room);
 



For simple classes it may seem like a lot of work but it's a habit that will stand you in good stead when you start working on more complicated code.
Was This Post Helpful? 1
  • +
  • -

#8 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1974
  • View blog
  • Posts: 4,816
  • Joined: 10-September 10

Re: Calling objects from another class in ArrayLists

Posted 08 April 2012 - 09:51 AM

As I think has been suggested, your Room constructor:
public Room(int roomNum, String bedType, double rate, char smoking, String occupantName)
    {
        roomNum = 0;
        bedType = "";
        rate = 0;
        smoking = ' ';
        occupantName = "";
        
    }

needs to incorporate the values being passed by the new Room() statement in your RoomTest class. The beginning of the updated Room class and constructor would look like:
public class Room
{   
    private int roomNum;
    private String bedType;
    private double rate;
    private char smoking;
    private String occupantName;
    private String notOccupied;
    public String q;
    public String k;
        
    public Room(int roomNum, String bedType, double rate, char smoking, String occupantName)
    {
        this.roomNum = roomNum;
        this.bedType = bedType;
        this.rate = rate;
        this.smoking = smoking;
        this.occupantName = occupantName;
        
    }
. . . and on

Then your toString() method output needs to be changed:
        String showRoom = "Room Number: " + roomNum + "\n" + "Occupant Name: " 
                    + occupantName + "\n" + "Smoking Room: " + 
                smoking + "\n" + "Bed Type: " + bedType + "\n" + "Rate: " 
                    + rate;


That will give you the results you're hoping for.
Was This Post Helpful? 1
  • +
  • -

#9 javanewbie99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-April 12

Re: Calling objects from another class in ArrayLists

Posted 08 April 2012 - 10:52 AM

Thank you so much! I tried this.roomNum and changing the toString() method. I just didn't try them both at the same time. I truly appreciate it!
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1974
  • View blog
  • Posts: 4,816
  • Joined: 10-September 10

Re: Calling objects from another class in ArrayLists

Posted 08 April 2012 - 11:07 AM

Now that you've seen how to do it, I want to comment on what you were trying to do. From the original names you'd given your Room class instance variables, it appeared that you had a concept of the right idea, but you applied it in the wrong place.

In another class (like RoomTest), to access the variables of an instance of the class Room called room, you'd do something like:

int roomNumber = room.getRoomNum();
String bedType = room.getBedType();

etc. . .

That's why you wrote those 'getters' in the class Room.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1