# Java problem

by blah45
Tags: java
 P: 65 I did i thought thats what you menat to change. for post #34 I put in the accesors just for me iwanted to see the values that were being returned. the main method is the transmitter test.
Mentor
P: 19,710
 Quote by Cathal Cronin I did i thought thats what you menat to change.
2. In both constructors, if the values for x and y are not in the required range, you set X1 and Y1, not the member variables X and Y. Same problem for the first constructor, where Radius1, not Radius, gets set.

The member variables for your class are X, Y, and Radius. X1, Y1, and Radius1 are parameters in the constructors. You should NOT be setting X1, Y1, and Radius1.
 Quote by Cathal Cronin for post #34 I put in the accesors just for me iwanted to see the values that were being returned. the main method is the transmitter test.
No, there should be, I'm pretty sure, a method named Main. If you want to see the values, put in some output statements (System.out.println( ... );)
 P: 65 This is definitly it :) i have a System.out.println() method that prints out all the x,y cords but it uses the last set to calculate the signal strength. For the next part i don't really understand what i've done wrong, it genrates the random corodinates for x,y for however many transmitters the user puts in. I know i'm probably wrong but like before i'm confused where to go with it,it seems right to me.  public class Transmitter { public int X ; public int Y ; public int Radius = 300 ; public Transmitter(int X1, int Y1, int Radius1) { if(X1 < 800 || X1 > 1280) { System.out.println("ERROR, The X value you've entered is incorrect.") ; System.out.println("Range MUST be : 800 to 128") ; System.out.println("A default value will be set to 800"); X1 = X ; //Minimum value ( deafult range)// X = 800 ; } else { X = X1; } if(Y1 < 100 || Y1 > 440) { System.out.println("ERROR!, The Y value you've entered is incorrect.") ; System.out.println("Range MUST be : 100 to 440") ; System.out.println("A default value will be set to 100"); Y1 = Y ; //Minimum value ( deafult range)// Y = 100 ; } else { Y = Y1 ; } if(Radius1 < 300 || Radius1 > 500) { System.out.println("ERROR!, The Radius value you've entered is incorrect.") ; System.out.println("Range MUST be : 300 to 500") ; System.out.println("A default value will be set to 300"); Radius1 = Radius ; //Minimum value ( deafult range)//) ; Radius = 300 ; } else { Radius1 = Radius ; } } public Transmitter(int X1, int Y1) { if(X1 < 800 || X1 > 1280) { System.out.println("ERROR, The X value you've entered is incorrect.") ; System.out.println("Range MUST be : 800 to 1280") ; System.out.println("A default value will be set to 800"); X1 = X ; X = 800 ; //Minimum value ( deafult range)// } else { X = X1 ; } if(Y1 < 100 || Y1 > 440) { System.out.println("ERROR!, The Y value you've entered is incorrect.") ; System.out.println("Range MUST be : 100 to 440") ; System.out.println("A default value will be set to 100"); Y1 = Y ; //Minimum value ( deafult range)// Y = 100 ; } else { Y = Y1; } } ///---Accessers---// public int getX() { return X ; } public int getY() { return Y ; } public int getRadius() { return Radius ; } ///---Mutators---/// public void setRadius(int rad) { Radius = rad; } }
 Mentor P: 19,710 That will work, but has some extra cruft in it that should be removed. if(X1 < 800 || X1 > 1280) { System.out.println("ERROR, The X value you've entered is incorrect.") ; System.out.println("Range MUST be : 800 to 128") ; System.out.println("A default value will be set to 800"); X1 = X ; //<-- Take out this line X = 800 ; } There are similar lines in the following code where you set Y1 = Y and Radius1 = Radius. They should be removed. In the second constructor there are two lines where you set X1 = X and Y1 = Y. They should be removed, too.
 P: 65 okay done can u help with the test part i'm very confused with it
 P: 65 i basically have that don't i. all i need to do is the best signal part isn't it?
 Mentor P: 19,710 No, you don't "basically have that." 1. You don't have Main(). 2. You aren't creating any Transmitter objects in your network array. 3. The values you are generating for X, Y, and Radius aren't correct. For example, in your code you have X = randnumb.nextInt(800) + 480 ; This will give you a value for X in the range [480, 1280]. What you want is a value in the range [800, 1280] There's a similar problem for Y, where you have Y = randnumb.nextInt(100) + 340 ; This will give you a value in the range [340, 440], not [100, 440] as you're supposed to have. 4. You're doing something very silly with Radius. Radius = Radius ; There's NEVER a good reason to to this. It doesn't do any harm, but it's silly to set a variable to the value it already has. 5. The Transmitter objects should have random values for Radius, but your code isn't doing this - it uses the value 300 all the time. 6. What is the purpose of countX, countY, and countRad? By the time you get to i++ in your loop, i will have the same value as these other three variables, so what's the use of having them?
 P: 65 oh okay, right i dont understand the main() part. how do i make it create a transmitter instance inside another class? The count parts we're so that when it had 1 value it stored that then generated another one depending on how many transmitters you wantted the radius is being set to the default radius in the transmitter that just has X1,Y1 as paprameters, i thought it would be easier if i just used the x and y instead of using all the variables
Mentor
P: 19,710
 Quote by Cathal Cronin oh okay, right i dont understand the main() part. how do i make it create a transmitter instance inside another class?
By using either of the two constructors you wrote for the Transmitter class.
 Quote by Cathal Cronin The count parts we're so that when it had 1 value it stored that then generated another one depending on how many transmitters you wantted
You really should get rid of the countX, countY, and countRadius variables. I guarantee that they won't do anything except confuse you further.
 Quote by Cathal Cronin the radius is being set to the default radius in the transmitter that just has X1,Y1 as paprameters, i thought it would be easier if i just used the x and y instead of using all the variables
Your instructor is going to wonder why all of your Transmitter objects (when you actually get to the point where you create some) have the same radius. He/she will probably deduct points if you do this.
 P: 65 yeah your right i'm trying to do it now i'll out up what i've done in a few mins
 P: 65 How do i use the transmitters i made do i rewrite the code in or is there a way of calling a class within a class? do i apply the distance formula for each instance of the transmitters that i call
 P: 65 it's not fully finished but is the random generation right? import java.util.Random ; public class TransmitterTest { public int X ; public int Y ; public int X2 ; public int Y2 ; public int RadiusDefalut = 300 ; public int Radius ; private Transmitter[] network ; public TransmitterTest(int networkSize) { network = new Transmitter[networkSize] ; int i = 0 ; while (i < networkSize) { Random randX = new Random() ; X = randX.nextInt(480) + 800 ; System.out.println(X) ; Random randY = new Random() ; Y = randY.nextInt(340) + 100 ; System.out.println(Y + " ") ; Random randRadius = new Random() ; Radius = randRadius.nextInt(200) + 300 ; System.out.println(Radius); i++ ; } } public int bestSignal(int phoneX, int phoneY) { int bestSignal ; int X1 = X ; int Y1 = Y ; int X2 = phoneX ; int Y2 = phoneY ; int distance = (int)Math.sqrt( (X2 - X1)*(X2 - X1) + (Y2 - Y1)*(Y2 - Y1)) ; bestSignal = (1 - ((distance/Radius)) * (Radius)) ; return bestSignal ; } }
 P: 65 i'm so confused i don't know how to input each x,y and radius into each transmitter that gets created in the array, does it go in the loop or what??
 Mentor P: 19,710 Yes, your random numbers look fine. They're now in the right intervals. For your other question, the x, y, and radius values don't come from user input - they are generated as random numbers. And yes, each iteration of the loop needs to generate an x, a y, and a radius, then call a Transmitter constructor, passing those values of x, y, and radius in the constructor call. You're going to have a statement like this inside the loop: // generate random x, y, and radius values network[i] = new Transmitter(x, y, radius); So, network[i] will be a Transmitter object. network[2].getX() will return the X value for the Transmitter object at index 2 in the array. network[m].getY() will return the Y value for the Transmitter object at index m in the array. I think I remember that you have a getRadius() method. It would work the same way.
 P: 65 is this part right now import java.util.Random ; public class TransmitterTest { public int X ; public int Y ; public int X2 ; public int Y2 ; public int RadiusDefalut = 300 ; public int Radius ; private Transmitter[] network ; public TransmitterTest(int networkSize) { network = new Transmitter[networkSize] ; int i = 0 ; int p = 0 ; while (i < networkSize) { network[i] = new Transmitter(X,Y,Radius) ; Random randX = new Random() ; X = randX.nextInt(480) + 800 ; System.out.println(X) ; network[i].getX() ; Random randY = new Random() ; Y = randY.nextInt(340) + 100 ; System.out.println(Y) ; network[i].getY(); Random randRadius = new Random() ; Radius = randRadius.nextInt(200) + 300 ; System.out.println(Radius); network[i].getRadius() ; i++ ; } }
 Mentor P: 19,710 No. 1. You are calling the Transmitter constructor before you generate the values for X, Y, and Radius. 2. You're still missing a Main() function, which should be a member function of the TransmitterTest class. 3. Your calls to the getX, getY, and getRadius accessors aren't doing anything useful. Each of them evaluates to something, but since the values aren't stored anywhere or displayed, these calls have no useful effect.
 P: 65 i don't understand the main() can u show me

 Related Discussions Engineering, Comp Sci, & Technology Homework 9 Engineering, Comp Sci, & Technology Homework 0 Engineering, Comp Sci, & Technology Homework 2 Computing & Technology 4 Introductory Physics Homework 0