...

View Full Version : refering class help



StealthRT
02-08-2008, 04:37 PM
Hey all i am new at java and im having problems with trying to get data from a class file to another one.

Lets say i have a class file called Occupant:


public class Occupant
{
private int adults;
private int children;
private int nights;

public int getAdults()
{
return adults;
}

public int getChildren()
{
return children;
}

public int getNights()
{
return nights;
}

public int getAdultCharge()
{
int adultcharge = adults * .10 * nights;
return adultcharge;
}

public Occupant(int a, int c, int n)
{
adults = a;
children = c;
nights = n;
}
}

Now how can i reference that using another class file?


public class Test
{
public static void main(String[] args)
{
System.out.println("# of adults: " + Occupant.getAdults());
System.exit(0);
}
}

But it throws an error. Am i doing it correctly?

David

Aradon
02-08-2008, 04:44 PM
Well if it throws an error of course not! heh.

So, whenever you create a class, have a class, see a class, usually there is a constructor within.

Every class comes with a constructor, even if it's not specified. Lucky for us, there is one specified already!



public Occupant(int a, int c, int n)
{
adults = a;
children = c;
nights = n;
}


You'll noticed that a constructor does not have a return type. Not only that but it also share the name of the class as the method name!

So in order to use an "Occupant" object, we must first create it!




Occupant apt101 = new Occupant(1, 0, 0);


We now have an occupant with one adult, not children and 0 nights. We can then get stuff from this object by doing a print!



System.out.println(apt101.getAdults());


And there you go, object initialization! Let us know if you have any issues.

StealthRT
02-08-2008, 04:50 PM
Aradon: But doesnt that just get the data from the (1, 0, 0) and not the other class file?

David

Aradon
02-08-2008, 06:23 PM
Of course. In order to get the data from this "other class file" you have to have some instantiation of that class. What other class file are you talking about?

StealthRT
02-08-2008, 10:07 PM
Ok i have 4 files..

Occupant.java
Occupant.class
Test.java
Test.class

When i run test.class i want it to pull the values from Occupant.class but it doesnt...


import java.text.DecimalFormat;
public class Test
{
public static void main(String[] args)
{
DecimalFormat number = new DecimalFormat("$###.00");
Occupant occ = new Occupant(2, 2, 2);
System.out.println("# of adults: " + occ.getAdults());
System.exit(0);
}
}

Its says "# of adults: 2" which is correct for the static values i have in there but i need to get the values from the Occupant.class file.

I figured it went something like this:


Occupant occ = new Occupant(Occupant.getAdults(), Occupant.getChildren(), Occupant.getNights());

David

Aradon
02-09-2008, 12:30 AM
Those "static values" are the values in that particular Occupant variable. I'm not sure what you mean by other values. What are you expecting to happen?

Gox
02-10-2008, 02:17 AM
Hi StealthRT,

I think we're a little confused on what you're trying to do here. The way that you've stated things it appears that you want to get information from an instance of the Occupant Class. If this is the case then you should follow Aradon's advice as a method for getting that information.


public static void main(String[] args)
{
DecimalFormat number = new DecimalFormat("$###.00");
Occupant occ = new Occupant(2, 2, 2);
System.out.println("# of adults: " + occ.getAdults());
System.out.println("# of children: " + occ.getChildren());
System.out.println("# of nights: " + occ.getNights());
System.exit(0);
}


Its says "# of adults: 2" which is correct for the static values i have in there but i need to get the values from the Occupant.class file.
The Occupant class doesn't have any values until you give it some, when you make an instance of that class. If you look at your code you'll see no numbers in you Occupant class, thus how could it return a number if you haven't given it one in the constructor? It can't. They way your code is you have to do things in the above example; Make an instance of an Occupant and give it some values, then you can use this information later if needed.

StealthRT
02-10-2008, 05:34 AM
Ok yeah sorry Aradon & Goz for my noobness...

I did this:


public int getAdults()
{
adults = 100;
return adults;
}

And it displayed 100 for the getAdults within my Test class. :) I figured if you put (2,2,2) that it would take that OVER the values in the other class no matter what.

Thanks again!
David



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum