12 Replies - 1496 Views - Last Post: 11 December 2010 - 11:10 PM Rate Topic: -----

#1 Guest_Nagasami*


Reputation:

Passing an object to anther class?

Posted 11 December 2010 - 03:57 PM

Hello!
I need help on one of the program im doing.

Basically what i want to know is how to pass an object from one class to another?

let say:

Object obj = new Object(0);
Number num = new Number(obj);

this seem to be illegal. So how can i pass that value from the obj reference to the Number class?

Thank you
Is This A Good Question/Topic? 0

Replies To: Passing an object to anther class?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,091
  • Joined: 27-December 08

Re: Passing an object to anther class?

Posted 11 December 2010 - 04:07 PM

The Number class is abstract, and therefore uninstantiable. Also, the Objet constructor does not accept any parameters. What you could do though is create an Object variable to point to an Integer, then down-cast to Number.
Object o = new Integer(0);
Number n = (Number)o;



Since Integer is-a Number, you can downcast o to a Number or an Integer.
Was This Post Helpful? 1
  • +
  • -

#3 Guest_Nagasami*


Reputation:

Re: Passing an object to anther class?

Posted 11 December 2010 - 04:28 PM

The Object and Number classes are not abtract. Basically in the Object class, the variable 0 is pass into and from there it does the calculation. In the Number class it will take the variable 0 of Object class and do it own calculation. But i dont know how to pass it.
Was This Post Helpful? -2

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,091
  • Joined: 27-December 08

Re: Passing an object to anther class?

Posted 11 December 2010 - 04:30 PM

Number is an abstract class. Take a look at the Documentation if you don't believe me. As for Object, I never said it was abstract.

Are you using standard java.lang.Number and java.lang.Object, or are you using your own custom classes? If you are using your own custom classes, then post them. Otherwise, the example I posted is the only way to accomplish this.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Nagasami*


Reputation:

Re: Passing an object to anther class?

Posted 11 December 2010 - 04:44 PM

oh..those classes i just made them up xD

the program im talking about is

Quote

The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are as follows:

• To know the car’s current amount of fuel, in gallons.
• To report the car’s current amount of fuel, in gallons.
• To be able to increment the amount of fuel by 1 gallon. This simulates putting fuel in the car. ( The car can hold a maximum of 15 gallons.)
• To be able to decrement the amount of fuel by 1 gallon, if the amount of fuel is greater than 0 gallons. This simulates burning fuel as the car runs.

The Odometer Class: This class will simulate the car’s odometer. Its responsibilities are as follows:

• To know the car’s current mileage.
• To report the car’s current mileage.
• To be able to increment the current mileage by 1 mile. The maximum mileage the odometer can store is 999,999 miles. When this amount is exceeded, the odometer resets the current mileage to 0.
• To be able to work with a FuelGauge object. It should decrease the FuelGauge object’s current amount of fuel by 1 gallon for every 24 miles traveled. ( The car’s fuel economy is 24 miles per gallon.)

Demonstrate the classes by creating instances of each. Simulate filling the car up with fuel, and then run a loop that increments the odometer until the car runs out of fuel. During each loop iteration, print the car’s current mileage and amount of fuel.



the part that trouble me is the

Quote

• To be able to work with a FuelGauge object. It should decrease the FuelGauge object’s current amount of fuel by 1 gallon for every 24 miles traveled. ( The car’s fuel economy is 24 miles per gallon.)


in which have to pass FuelGauge object which is the value of gallons so that Odometer can do the calculation
Was This Post Helpful? 0

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Passing an object to anther class?

Posted 11 December 2010 - 04:50 PM

Well let us see your whole code then so you can get some feedback and suggestions.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Nagasami*


Reputation:

Re: Passing an object to anther class?

Posted 11 December 2010 - 04:58 PM

public class Odometer
{

	private double mileage;


	public Odometer(double mile)
	{
		mileage=mile;
	}

	public void incrementMile()
	{
		if(mileage>999999)
		{
			mileage=0;
			mileage++;
		}
	}
	
	public void decrementMile()
	{
		if(mileage==24)
		{
			mileage=0

	public double getMileage()
	{
		return mileage;
	}
}



public class FuelGauge
{
	private double total;

	public FuelGauge(double gallon)
	{
		total=gallon;

	}

	public void incrementFuel()
	{
		for(total=0;total>-1;total++)
		{
			if(total>15)
				total--;
		}

	}

	public void decrementFuel()
	{
		if(total>0)
			total--;
	}
	
	public double getFuel()
		{
			return total;
		}
}



the main method code im trying to write to make these two work together but as of right now its just these two codes that i have. I dont think they are correct as of right now.
Was This Post Helpful? 0

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Passing an object to anther class?

Posted 11 December 2010 - 07:17 PM

View PostNagasami, on 11 December 2010 - 05:28 PM, said:

The Object and Number classes are not abtract. Basically in the Object class, the variable 0 is pass into and from there it does the calculation. In the Number class it will take the variable 0 of Object class and do it own calculation. But i dont know how to pass it.

Yes Number is abstract -1
Was This Post Helpful? 1
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Passing an object to anther class?

Posted 11 December 2010 - 07:36 PM

View PostNagasami, on 11 December 2010 - 05:44 PM, said:

oh..those classes i just made them up xD


Yah, now we're getting to it. The java.lang.Number class is abstract, but you made it yourself. IF you mention that in the first place, it will definitely save you some headaches around here.
Was This Post Helpful? 1
  • +
  • -

#10 Guest_Nagasami*


Reputation:

Re: Passing an object to anther class?

Posted 11 December 2010 - 08:44 PM

sorry about that. the main question for me is how do i pass that double variable from FuelGauge to Odometer?
Was This Post Helpful? 0

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Passing an object to anther class?

Posted 11 December 2010 - 10:22 PM

Set up some get/set methods so that whatever controller class is operating those two can do this form of access
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Nagasami*


Reputation:

Re: Passing an object to anther class?

Posted 11 December 2010 - 10:46 PM

sorry i can you explain more? like making set/get methods to get the object from FuelGauge? i dont know how to pass the object into those methods in Odometer. How could i make it possible to accept the object into the method?
Was This Post Helpful? 0

#13 cmh0114  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 143
  • Joined: 03-January 10

Re: Passing an object to anther class?

Posted 11 December 2010 - 11:10 PM

I'm not sure exactly what you're doing with the Mileage class. It looks like you're using the variable 'mileage' to represent both the mileage of the car (in mpg) and the distance traveled by the car. My advice would be to add another variable -- 'miles'. When you call IncrementMile(), the resulting value of 'mileage' will either be the same as it was before, or it will equal 1. As defined, it will check to see whether 'mileage' is greater than 999,999. If it's less than 999,999, it does nothing. If greater, then it will set mileage=0, then add one, so 'mileage' ends up equaling one. I think you want to say
 public void IncrementMile()
{
miles++;
if(miles > 999999)
{
miles = 0;
}
}


I think that you're referring to the actual amount of miles the car has traveled (assuming you can't just increase the mileage of the car at will).


What is the method DecrementMile() supposed to do? You're missing a bracket at the end of that, by the way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1