Java problem

1. Nov 26, 2010

blah45

1. The problem statement, all variables and given/known data

Introduction
You are part of a team working on a software system for a mobile phone company. You have been asked to write a class capable of storing the details of a transmitter.
The class will need fields to store the x,y coordinates of the transmitter and the transmitter's signal radius (in metres). The x,y coordinates are integer values. The x range is 800 to 1280 and the y range is 100 to 440. The minimum transmitter radius is 300 metres and the maximum is 500 metres.
A mobile phone user's signal strength is determined by their distance from a transmitter. If the distance is greater than the transmitter radius then the signal strength is zero. Otherwise the signal strength is determined as follows
The distance between a phone user and a transmitter can be calculated using the formula

x1,y1 could be the coordinates of the phone user and x2,y2 the coordinates of the transmitter. Java provides a Math class that includes a sqrt method as well as many other useful mathematical operations.
Create a class called Transmitter that stores the three specified fields (i.e. x, y and radius) and provides the following features
• a constructor that when passed three parameters (i.e. the x and y coordinates and the transmitter radius) creates an instance of the class
• a constructor that when passed two parameters (i.e. the x and y coordinates) sets the radius to a default value (i.e. 300) creates an instance of the class
• an accessor method for all three fields
• a mutator method for the radius field ONLY
Testing the Transmitter Class
To test the transmitter class you should create another class called TransmitterTest. The test class should look like the following
public class TransmitterTest
{
private Transmitter[] network ;

public TransmitterTest(int networkSize)
{
network = new Transmitter[networkSize] ;
//
// to load the network array with
// "networkSize" instances of the Transmitter class.
// The x,y coordinates of the transmitters should be
// randomly generated. You can specify a radius or
// use the Transmitter Class default.
}

// to do the operation(s) specified below

}

• Include a method called bestSignal that has the following header
public int bestSignal(int phoneX, int phoneY)
The method is passed the x,y coordinates of a phone and calculates the distance from the phone to each transmitter in the network. It returns the array index of the network transmitter that provides the best (i.e. highest) signal strength. If none of the transmitters provides a signal level greater than zero the method should return a -1 value.

3. The attempt at a solution

I had a solution but my pc won't open the java file for some reason

If anyone can help thanks

2. Nov 27, 2010

Staff: Mentor

Show us what you've done. The file you created is a text file, so you should be able to open it with any text editor.

3. Nov 27, 2010

blah45

This is the consstructer i made but i don't think it's right.
Once the constructer is right I can move onto the transmitter test class

import java.util.Random ;
public class Transmitter
{

private int X ;
private int Y ;

public Transmitter(int x , int y , int radius)
{
Random Xcord = new Random() ;
int X = Xcord.nextInt(800)+ 481 ;

if((X < 800 ) && (X > 1280))
{
System.out.println("ERROR, The X value you've entered is not in the range 800 to 1280") ;
}

Random Ycord = new Random() ;
int Y = Ycord.nextInt(100)+ 341 ;

if((Y < 100 ) && (Y > 440))
{
System.out.println("ERROR, The Y value you've entered is not in the range 100 to 440") ;
}

Random blahdist = new Random() ;
int blahist = blahdist.nextInt(300) + 201 ;
{
System.out.println("ERROR, The Radius value you've entered is not in the range 300 to 500") ;
}

}

///--Accessors---///
public int getX ()
{
return X ;
}

public int get()
{

return Y ;
}

{

}

///---Mutator---///

{
}
}

4. Nov 27, 2010

Staff: Mentor

You should not be setting the x and y values and radius value with random values in your constructor - they are passed in as parameters. They should be checked, however, to make sure they are in the proper ranges; i.e., 800 <= x <= 1280, 100 <= y <= 400, and 300 <= radius <= 500.

Keep in mind that you will need two constructors - one with three parameters and one with two parameters.

5. Nov 27, 2010

blah45

So if i get rid of the random number generation and just have the x,y and radius with there value ranges that would be one constructer, the othere would be basically the same except has the radius as 300...yeah?

6. Nov 28, 2010

Staff: Mentor

Yes, just like it says in the problem description. Did you read it?

7. Nov 28, 2010

blah45

Yes I did, i've been at it for a whle and it still won't compile. how do i set the x,y and radius in the constructer if they all have minimum and maximum values? I'm getting very tird with it now. I literally do not know what to do anymore.

Last edited: Nov 28, 2010
8. Nov 28, 2010

Staff: Mentor

If it won't compile, you are not using the language correctly. These errors should be easy to fix if you show us what you now have.

In the constructors, check the values of the parameters to see if they are in the proper ranges before using the parameters to set the member variables X, Y, and Radiusdist.

9. Nov 28, 2010

blah45

yeah but how do i set 1 parameter to have 2 values?

10. Nov 28, 2010

Staff: Mentor

You can't. Why would you think you need to?

11. Nov 29, 2010

blah45

cause the x,y and radius have 2 value ranges, but i know now you generate those in the test part.

public class Transmitter

{
public int X ;
public int Y ;
public int Radius = 300 ;

public Transmitter(int X1, int Y1, int Radius1)
{
X = X1 ;

Y = Y1 ;

//Transmitter trans1 = new Transmitter(x1, y1, radius1);
}

public Transmitter(int X1, int Y1)
{
X = X1 ;

Y = Y1 ;

//Transmitter trans1 = new Transmitter(x, y, radius);
}

public int getX()
{
return X ;
}

public int getY()
{
return Y ;
}

{
}

{
}

}

12. Nov 29, 2010

Staff: Mentor

The Transmitter constructors need to validate the input values for x, y, and radius. Use if statements like what you did in the code you showed in post #3.

13. Nov 29, 2010

blah45

I don't understand whats wrong with this one?

14. Nov 29, 2010

Staff: Mentor

It doesn't validate the x and y values or the radius value, that's what's wrong.
I read the requirements to mean that it's the constructors' job to validate input.

15. Nov 29, 2010

blah45

I don't know where to put it in?

16. Nov 29, 2010

Staff: Mentor

In each constructor, before you set the relevant member variable. IOW, before you set X, check that X1 is at least 800 and no larger than 1240. Similar for Y and Radius.

17. Nov 29, 2010

blah45

could you just write out like the X variable to show me i'd find that alot clearer if you wouldn't mind and then i can do the rest?

18. Nov 29, 2010

Staff: Mentor

Just look at what you did in post #3.

19. Nov 29, 2010

blah45

could you just do the x variable to show me and then i can do the rest, it would be much clearer to me then.

20. Nov 29, 2010

blah45

wait i think i have it...

public class Transmitter

{
public int X ;
public int Y ;
public int Radius = 300 ;

public Transmitter(int X1, int Y1, int Radius1)
{
if((X < 800 ) && (X > 1280))
{
System.out.println("ERROR, The X value you've entered is not in the range 800 to 1280") ;
}

X = X1 ;

if((Y < 100 ) && (Y > 440))
{
System.out.println("ERROR, The Y value you've entered is not in the range 100 to 440") ;
}

Y = Y1 ;

{
System.out.println("ERROR, The Radius value you've entered is not in the range 300 to 500") ;
}

//Transmitter trans1 = new Transmitter(x1, y1, radius1);
}

public Transmitter(int X1, int Y1)
{
X = X1 ;

Y = Y1 ;

//Transmitter trans1 = new Transmitter(x, y, radius);
}

///---Accessers---//
public int getX()
{
return X ;
}

public int getY()
{
return Y ;
}

{