how to pass vars from file to file
hello
i have three classes which i will post below.....i want to get my point variables from the point class to the FinalJava class and then to the LineDraw Class although every time i try what i am doing....passing them to final java from point and calling it from there...it will not save them for later use......is there another way that this will work?
import java.*;
import javax.swing.*;
import java.awt.*;
publicclass FinalJavaextends Object{
String choicestring,twodchoicestring, intersectionchoicestring;
String linexonestring, lineyonestring, linextwostring, lineytwostring;
int choice,twodchoice,intersectionchoice;
int linexone,lineyone,linextwo,lineytwo;
int x1,y1;
publicstaticvoid main(String [] args)
{
FinalJava g =new FinalJava();
g.Choice();
}
publicvoid Choice()
{
choicestring = JOptionPane.showInputDialog("Enter 0 to chooose 2D shapes to draw\nEnter 1 to plot some intersections\nEnter 2 to draw some 3D shapes");
choice = Integer.parseInt(choicestring);
if(choice == 0)
{
FinalJava fj =new FinalJava();
fj.twoDChoice();
}
elseif (choice == 1)
{
this.PlotIntersectionsChoice();
}
elseif (choice == 2)
{
//ThreeDShapesChoice(); not made yet
}
}//end Choice() void
publicvoid twoDChoice()
{
twodchoice = (Integer.parseInt(JOptionPane.showInputDialog(null,"enter 1 to define point vars\nenter two to make a line\nenter 3 to make a rect")));
if (twodchoice==1)
{
Point p =new Point();
p.enterVariables();
}
elseif (twodchoice==2)
{
LineDraw ld =new LineDraw();
ld.Choice();
}
elseif (twodchoice==3)
{
//DrawRect dr = new DrawRect();
Point p =new Point();
x1 = p.x1;
y1 = p.y1;
JOptionPane.showMessageDialog(null, x1+y1+"");
//
}
}
publicvoid PlotIntersectionsChoice()
{
//see waht type of intersection the user wants
intersectionchoicestring = JOptionPane.showInputDialog(null,"Enter one to intersect between line and line\n Enter two for line and circle\nEnter three for circle and circle");
intersectionchoice = Integer.parseInt(intersectionchoicestring);
if (intersectionchoice == 1)
{
LineIntersect li =new LineIntersect();//go to my line-line intersect class
li.VariablesEnter();
}
elseif (intersectionchoice == 2)
{
//LineCircleIntersect lci = new LineCircleIntersect();//go to my line circle intersect class
//lci.EnterVariables();
}
elseif (intersectionchoice == 3)
{
CircleIntersect ci =new CircleIntersect();//go to my circle-circle intersect calss
ci.CircleDefinition();
}
}
}
and now the point class
import java.*;
import javax.swing.*;
import java.awt.*;
publicclass Point//extends FinalJava
{
int x1,y1;
publicstaticvoid main(String [] args)
{
Point p =new Point();
p.enterVariables();
}
publicvoid enterVariables()
{
x1 = (Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter x1")));
y1 = (Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter y1")));
JOptionPane.showInputDialog(null,"Your variables have been created and saved....");
FinalJava fj =new FinalJava();
fj.Choice();
}
}
and now the linedraw
import javax.swing.*;
import java.*;
import java.awt.*;
publicclass LineDrawextends Point{
int oldx1, oldy1;
String newx1, newy1, newx2, newy2;
int newx1int, newy1int, newx2int, newy2int;
publicstaticvoid main(String[] args){
LineDraw ld =new LineDraw();
ld.Choice();
}
publicvoid Choice()
{
intchoice = (Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter 1 if you would like to use the variables defined in point\nPress two if you would like to define new ones")));
if (choice == 1)
{
LineDraw ld =new LineDraw();
ld.displayResultsOld();
}
elseif(choice == 2)
{
LineDraw ld =new LineDraw();
ld.displayResultsNew();
}
}
publicvoid displayResultsOld()
{
FinalJava p =new FinalJava();
oldx1 = p.x1;
oldy1 = p.y1;
newx2 = JOptionPane.showInputDialog(null,"Please enter another x");
newy2 = JOptionPane.showInputDialog(null,"Please enter another y");
JOptionPane.showMessageDialog(null,"your line goes from"+oldx1+","+oldy1+"\n to \n"+newx2+","+newy2+"\n using the old points");
}
publicvoid displayResultsNew()
{
newx1 =JOptionPane.showInputDialog(null,"Please enter another x");
newy1 = JOptionPane.showInputDialog(null,"Please enter another y");
newx2 = JOptionPane.showInputDialog(null,"Please enter another x");
newy2 = JOptionPane.showInputDialog(null,"Please enter another y");
newx1int = Integer.parseInt(newx1);
JOptionPane.showMessageDialog(null,"your line goes from"+newx1int+","+newy1+"\n to \n"+newx2+","+newy2+"\n using the new points");
}
}
[10148 byte] By [
hwrdrpknsa] at [2007-11-26 12:40:22]

I don't see where you're passing any values to the LineDraw class at all.
The way to pass a value from one object to another is by calling methods on that object, or passing it in the constructor, as in:
class SomeClass {
int someValue = 3;
public void someMethod() {
SomeOtherClass soc = new SomeOtherClass(someValue);
// or
soc.setValue(someValue());
}
}
In FinalJava you in one case create a LineDraw object and then throw it away. So of course any instance variables in that instance will be thrown away as well.
Rather than showing us all your code, expecting us to reverse engineer it, and then guess what you were intending, you might want to either (1) write a very small example that expresses what you're trying and what's causing you problems (and nothing else), or (2) just going into more detail about what you're trying to achieve.
i don't understand your code abovewhen your setting a value...what exactly are you setting and where are you setting it...are you setting it in the subclass (ex point) or are you setting it in the superclass(ex FinalJava)?
Neither. I'm passing it from the caller (shown) to an instance of SomeOtherClass. It gets set there in the argument list, but that's a local variable that will go away. If SomeOtherClass wants to preserve it, it has to set it internally in a field. And if it sets it internally in an instance field (which is generally the right way) then the whole thing will go away when the instance goes away.
Are you asking about setting variables or passing variables? Do you understand the difference?
Do you understand the difference between instances and classes?
Do you understand what is meant by the lifetime or life cycle of an object?
i was asking about passing variables....
i do understnad the difference
and i do understnad the difference between lifetime and cycle
however
i just finished what i was trying to do......however now i have a java.lang.StackOverflowError so i gotta fix that
cheers
hwrd
error fixed using System.gc(); method
Message was edited by:
hwrdrpkns
Please refrain from crossposting. http://forum.java.sun.com/thread.jspa?threadID=5116040
acutally i had thought i fixed it because i put the System.gc() method in my for loop but it only ran twice before it went out of memory.
Which thread are you going to post in?Please choose one.
lets choose this one, ya?
by the way i researched the overflow error and it says
"Thrown when a stack overflow occurs because an application recurses too deeply. "
ummm how do i fix this.....
is it a computer problem or is my code messed up...
import java.*;
import javax.swing.*;
import java.text.*;
import java.lang.*;
public class CircleInheritance extends FinalJava
{
public static void main(String args[])
{
// set flating pint number format
DecimalFormat twoDigits = new DecimalFormat("0.00");
//create Point and extending objects
Point point = new Point( 2 , 3);
Circle circle = new Circle ( 10,22,14);
Cylinder cylinder = new Cylinder ( 20,30,3.3,10.75);
Cone cone = new Cone( 2,54,2,3);
Sphere sphere = new Sphere(2,6,4,5);
String output = point.getName() + ":" + point + "\n"+ circle.getName()+":"+circle+"\n"+cylinder.getName() + ":" + cylinder+"\n";
Shape arrayOfShapes[] = new Shape[5]; // creates the shape array
arrayOfShapes [0]= point ;
arrayOfShapes [1]= circle ;
arrayOfShapes [2]= cylinder;
arrayOfShapes [3]= cone ;
arrayOfShapes [4]= sphere;
for (int i=0; i <= arrayOfShapes.length; i++)
{
output += "\n\n" + arrayOfShapes[i].getName()+ ":" + arrayOfShapes[i].toString()+"\nArea"+twoDigits.format(arrayOfShapes[i].getArea())+"\nVolume="+twoDigits.format(arrayOfShapes[i].getVolume());
}
JOptionPane.showMessageDialog(null, output);
System.exit(0);
}
}
You have code that is recursing forever.Like thismethodA calls methodB.methodB calls methodAmethodA calls methodBetc.So find out where that's happening and fix it.
> ummm how do i fix this.....> is it a computer problem or is my code messed up...> Your code is messed up. See my previous post.
It's something wrong with your code.You have a recursive constructor or a recursive method without proper end conditions.Do you understand passing values now? If not, what part of my example didn't you get?
oh no thats exactly what i have
i have a point class
tree
point
circle extends point
cylinder extends circle
cone extends circle
sphere extends circle
and then i have the class you see above this post displaying all that stuff out....
is that what you mean cotton?
@ paul
yes i do understnad the conept of passing and setting now...thank you very much for your help with that...
Message was edited by:
hwrdrpkns
> > is that what you mean cotton?> > No.Please note againmethodA calls methodBmethodB calls methodAThat's the problem. If you havemethodA calls methodBmethodB calls methodCthat is NOT a problem.
i see but i don't have that anywhere in my code....i don't have two of the exact same method nor do i have one method calling another.
> i see but i don't have that anywhere in my codeYes you do.
> are you absolutely sure?Yes.
ok i'm not seeing this at all....i'm really not calling anything except in the last program........
but i know the error happens between these two clases because the error happens after i get after circle and before cylinder.
public class Circle extends Point {
private double radius;
public Circle()
{
}
//constrctor
public Circle( int x, int y, double radiusValue)
{
super (x,y); // all the point constructor
setRadius(radiusValue);
}
// set radisu
public void setRadius (double radiusValue)
{
radius = (radiusValue < 0.0 ? 0.0:radiusValue);
}
//return radius
public double getRadius()
{
return radius;
}
public double getDiameter()
{
return Math.PI * getDiameter();
}
public double getCircumference()
{
return Math.PI * getDiameter();
}
//calculate and return diameter
public double getArea()
{
return Math.PI * getRadius() * getRadius();
}
public String getName()
{
return "Circle";
}
//override to stirng ot return representation of circle
public String toString()
{
return "center =" + super.toString()+ "; Radius ="+ getRadius();
}
}// end calss circle
and this one
public class Cylinder extends Circle
{
private double bla; // new variagble height
public Cylinder()
{
}
public Cylinder(int x , int y, double radius, double blaValue)
{
super(x, y, radius);
setBla(blaValue);
}
public void setBla ( double blaValue)
{
bla = (blaValue < 0.0 ? 0.0 : blaValue);
}
public double getBla()
{
return bla;
}
public double getArea()
{
return 2* super.getArea() * getCircumference() * getBla();
}
public double getVolume()
{
return super.getArea() * getBla();
}
public String getName()
{
return "Cylinder";
}
public String toString()
{
return super.toString()+";Height = " + getBla();
}
}// end class cylinder
now look at taht and tell me i'm calling method A to from Method B and then calling Method A from Method B....
and if you find it then you don't have to solve it....tell me its there and i'll find it eventually...
however i don't see anything wrong with that code
Another possibility is that you have the same thing, only with constructors:
Constructor of class A creates object of class B.
Constructor of class B creates object of class A.
Or:
Constructor (of any class) calls this(...) constructor, with identical arguments, i.e., it constructs itself over again.
Add some debugging code to see what's going on.
like debugging code how?to see where its going wrong or what?
Yeah, like:
public void someMethod() {
System.err.println("starting someMethod");
// body of method
System.err.println("ending someMethod");
}
You can do roughly the same thing with constructors, although there may be problems when the constructors start with a this().
In that case, using the profiler or other debugging switches to the JVM may help. Actually you can do that stuff right now without changing your code, although I'm not certain whether it'll actually help.
Message was edited by:
paulcw
see but the problem with that is
i don't have a main method in all of my classes
i just have this
Point point = new Point( 2 , 3);
Circle circle = new Circle ( 10,22,14);
Cylinder cylinder = new Cylinder ( 20,30,3.3,10.75);
Cone cone = new Cone(2,54,2,3);
Sphere sphere = new Sphere(2,6,4,5);
String output = point.getName() + ":" + point + "\n"+ circle.getName()+":"+circle+"\n"+cylinder.getName() + ":" + cylinder+"\n";
Shape arrayOfShapes[] = new Shape[5]; // creates the shape array
arrayOfShapes [0]= point ;
arrayOfShapes [1]= circle ;
arrayOfShapes [2]= cylinder;
arrayOfShapes [3]= cone ;
arrayOfShapes [4]= sphere;
which calls and executes methods from other classes based on the values that I inputed....so i have no main method in any of my other classes then how can i add debugging code in them?
hwrd
Eh? Just add it. You don't need a main() method to call System.err.println().
zomg thank you so much everyone
it turns out i had this
public double getDiameter()
{System.out.println("start method4");
return 2 * getDiameter();
}
so i was multiplying by 2 and getDiameter() instead of 2* getRadius() and so it kept looping
your debugging tactic worked excellent!!! I just numbered it by method and i saw which method it was getting stuck one!!! thats awesome!!
thanks again
hwrd