Comp Sci Creating a Transmitter Class for Mobile Phones

AI Thread Summary
The discussion focuses on creating a Transmitter class for a mobile phone application, which requires storing x, y coordinates and a signal radius. The x and y coordinates must fall within specific ranges, and the radius must be between 300 and 500 meters. Participants emphasize the need for constructors to validate input values before assigning them to class fields. There is also a mention of a testing class, TransmitterTest, which will help ensure the Transmitter class functions correctly. The conversation highlights common coding errors and the importance of proper parameter handling in constructors.
blah45
Messages
65
Reaction score
0

Homework Statement



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
(1 - (distance/radius)) * radius
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.
The Task
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] ;
//
// PUT YOUR CODE HERE
// 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.
}

// PUT YOUR CODE HERE
// 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.


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
 
Physics news on Phys.org
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.
 
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 ;
private int Radiusdist ;

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 ;
blahist = radius ;
if((radius < 300 ) && (radius > 500))
{
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 ;
}

public int getradius()
{

return Radiusdist ;
}

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

public void setradius(int radius)
{
Radiusdist = radius ;
}
}
 
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.
 
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?
 
Yes, just like it says in the problem description. Did you read it?
 
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:
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.
 
yeah but how do i set 1 parameter to have 2 values?
 
  • #10
You can't. Why would you think you need to?
 
  • #11
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 ;

Radius = Radius1 ;

//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 ;
}

public int getRadius()
{
return Radius ;
}

public void setRadius(int rad)
{
Radius = rad;
}

}
 
  • #12
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
I don't understand what's wrong with this one?
 
  • #14
It doesn't validate the x and y values or the radius value, that's what's wrong.
Cathal Cronin said:
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.

I read the requirements to mean that it's the constructors' job to validate input.
 
  • #15
I don't know where to put it in?
 
  • #16
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
could you just write out like the X variable to show me i'd find that a lot clearer if you wouldn't mind and then i can do the rest?
 
  • #18
Just look at what you did in post #3.
 
  • #19
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
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 ;

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

Radius = Radius1 ;

//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 ;
}

public int getRadius()
{
return Radius ;
}

///---Mutators---///
public void setRadius(int rad)
{
Radius = rad;
}

}
 
Last edited:
  • #21
I get an error msg if i uncomment the "Transmitter trans1 = new Transmitter(x, y, radius);" bit but i don't know why, do i even need it
 
  • #22
You can't have the constructor calling itself, which is what you have with that line of code.

Not only do you not need it, it shouldn't be there in either constructor.

You're getting warmer on your constructor code, but think what happens when the constructor is called like this:
Code:
Transmitter t = new Transmitter(500, 500, 600);
Would the three member variables be set to these values?
 
  • #23
This has to be it public class Transmitter

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

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

X = X1 ;

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

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

Y = Y1 ;

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

Radius = Radius1 ;

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

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

X = X1 ;

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

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

Y = Y1 ;

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

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

public int getY()
{
return Y ;
}

public int getRadius()
{
return Radius ;
}

///---Mutators---///
public void setRadius(int rad)
{
Radius = rad;
}

}
 
  • #24
A little warmer, but no cigar just yet.
I combined your if and if else statement, and put your code in [ code] and [ /code] tags] (w/o the extra spaces), and indented it.


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

   X = X1 ;

There is still a problem. What happens if the first parameter in a call to the Transmitter constructor is, say, 500? Does X get set?
 
  • #25
what do the 2 upright bars mean? " if(X1 < 800 || X1 > 1280)" , x would print an error cause it's less than the range set. I don't know what else to do after this
 
  • #26
|| is the logical OR operator. If either expression is true (or both of them) the overall expression evaluates to true.

What happens after the error message is displayed?
 
  • #27
i've only started to learn to program and never come across it before so i'll put that in, it will create an instance of the class even though the values are wrong, but i don't know how to make it stop creating the class if the wrong values are entered.
 
  • #28
How is it that you know the logical AND operator (&&) but not the logical OR operator (||)?

As far as bad input values are concerned, if the user of the constructor puts in bad values, you could display an error message, but set X and Y to some default values, say 800 and 100. If you do this, you should tell the user what the constructor is doing.

The basic algorithm is:
Code:
if (user gives bad value for X)
{
   display error message about bad input, and that default value is being used
   set X to 800
}
else
{
   set X to X1
}
Do the same for Y and Radius.
 
  • #29
Our lecturer never mentioned it, Is this it?

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 = 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 = 100 ; //Minimum value ( deafult range)//
}else
{
Y = Y1 ;
}

if(Radius1 < 300 || Radius1 > 500)
{
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");

Radius1 = 300 ; //Minimum value ( deafult range)//) ;
}else
{
Radius = Radius1;
}
}

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 = 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 = 100 ; //Minimum value ( deafult range)//
}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;
}

}
 
  • #30
I think this is wrong still, because it's not setting the X1 to the default value.
 
  • #31
I know I'm not finished with the first part but this is what I've done for the TransmitterTest Part

import java.util.Random ;

public class TransmitterTest
{
public int X ;
public int Y ;
public int X2 ;
public int Y2 ;
public int Radius = 300 ;

private Transmitter[] network ;

public TransmitterTest(int networkSize)
{
network = new Transmitter[networkSize] ;
int i = 0 ;
int countX = 0 ;
int countY = 0 ;
int countRad = 0 ;

while (i < networkSize)
{

Random randnumb = new Random() ;
X = randnumb.nextInt(800) + 480 ;
System.out.println(X + " ") ;
countX++ ;

Random randnumb2 = new Random() ;
Y = randnumb.nextInt(100) + 340 ;
System.out.println(Y + " ") ;
countY++ ;

Radius = Radius ;
System.out.println(Radius + " ") ;
countRad++ ;

i++ ;
}

}

///---Accessars---///

public int getX()
{
return X ;
}

public int getY()
{
return Y ;
}

public int getradius()
{
return Radius ;
}

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)) ;

return (1 - ((distance/Radius)) * (Radius)) ;
}
}
 
Last edited:
  • #32
To preserve your indentation, put [ code] and [ /code] tags (without the extra space) around your code. I've don't that below.
Cathal Cronin said:
Our lecturer never mentioned it, Is this it?
Code:
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 = 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 = 100 ; //Minimum value ( deafult range)//
        }else
        {
            Y = Y1 ;
        }

        if(Radius1 < 300 || Radius1 > 500)
        {
            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"); 

            Radius1 = 300 ; //Minimum value ( deafult range)//) ;
        }else
        {
            Radius = Radius1;
        }
    }

    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 = 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 = 100 ; //Minimum value ( deafult range)//
        }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;
    }

}
This is pretty close. Here are the remaining problems that I see.
1. In the first constructor the error messages for the radius are those for Y, not Radius. They should be changed.
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.
 
  • #33
Cathal Cronin said:
I think this is wrong still, because it's not setting the X1 to the default value.
See post #32.
 
  • #34
Cathal Cronin said:
I know I'm not finished with the first part but this is what I've done for the TransmitterTest Part

import java.util.Random ;

public class TransmitterTest
{
public int X ;
public int Y ;
public int X2 ;
public int Y2 ;
public int Radius = 300 ;

private Transmitter[] network ;

public TransmitterTest(int networkSize)
{
network = new Transmitter[networkSize] ;
int i = 0 ;
int countX = 0 ;
int countY = 0 ;
int countRad = 0 ;

while (i < networkSize)
{

Random randnumb = new Random() ;
X = randnumb.nextInt(800) + 480 ;
System.out.println(X + " ") ;
countX++ ;

Random randnumb2 = new Random() ;
Y = randnumb.nextInt(100) + 340 ;
System.out.println(Y + " ") ;
countY++ ;

Radius = Radius ;
System.out.println(Radius + " ") ;
countRad++ ;

i++ ;
}

}

///---Accessars---///

public int getX()
{
return X ;
}

public int getY()
{
return Y ;
}

public int getradius()
{
return Radius ;
}

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)) ;

return (1 - ((distance/Radius)) * (Radius)) ;
}
}

You have a long way to go on this part. Here are just a few questions that come to mind.
1. Shouldn't there be a Main method in this class?
2. Why does this class have the same accessors as the Transmitter class?
3. What is bestSignal supposed to do? See the description in post #1.
4. You aren't using your Transmitter constructors correctly. You need to create each element of the network array one at a time using the Transmitter() constructor.
 
Last edited:
  • #35
Code:
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 = 800 ; //Minimum value ( deafult range)//

        }
        else 
        {
            X1 = X ;
        }

       
        
        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 = 100 ; //Minimum value ( deafult range)//
        }
        else
        {
            Y1 = Y ;
        }

        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 = 300 ; //Minimum value ( deafult range)//) ;
        }
        else
        {
            Radius1 = Radius ;
        }
    }

    public Transmitter(int X1, int Y1)
    {
        if(X1 < 800 || X1 > 1280)
        {
            X1 = 800 ; //Minimum value ( deafult range)//
            
            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");

            
        }
        else 
        {
            X1 = X ; 
        }

        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 = 100 ; //Minimum value ( deafult range)//
        }
        else
        {
            Y1 = Y ;
        }
        
        
    }

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

    public int getY()
    {
        return Y  ;
    }

    public int getRadius()
    {
        return Radius ;
    }

    ///---Mutators---///
    public void setRadius(int rad)
    {
        Radius = rad;
    }

}
 
Last edited:
  • #36
Please read my comments in post #32.
 
  • #37
I did i thought that's 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.
 
  • #38
Cathal Cronin said:
I did i thought that's 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.
Cathal Cronin said:
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( ... );)
 
  • #39
This is definately 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.

Code:
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;
    }

}
 
Last edited:
  • #40
That will work, but has some extra cruft in it that should be removed.

Code:
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.
 
  • #41
okay done can u help with the test part I'm very confused with it
 
  • #42
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
(1 - (distance/radius)) * radius
.
.
.
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] ;
//
// PUT YOUR CODE HERE
// 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.
}

// PUT YOUR CODE HERE
// 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.
------------------------------------------------------------------------------------
You're going to need a Main method that sets a value for networkSize and creates the network array with that many Transmitter objects in it. You'll need a loop that executes networkSize times, once for each Transmitter object in the network array.

For each Transmitter object generate random numbers for the x and y coordinates and the radius.

The bestSignal method should go through the network array and calculate the distance from the specified phone (the one with coordinates phoneX and phoneY), and return the index of the Transmitter array with the highest signal strength. In your earlier code you had bestSignal returning the distance, which isn't what it's supposed to do.
 
  • #43
i basically have that don't i. all i need to do is the best signal part isn't it?
 
  • #44
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
Code:
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
Code:
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.
Code:
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?
 
  • #45
oh okay, right i don't 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
 
  • #46
Cathal Cronin said:
oh okay, right i don't 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.
Cathal Cronin said:
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.
Cathal Cronin said:
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.
 
  • #47
yeah your right I'm trying to do it now i'll out up what I've done in a few mins
 
  • #48
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
 
  • #49
it's not fully finished but is the random generation right?

Code:
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 ;
    }
   
}
 
  • #50
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??
 

Similar threads

Replies
2
Views
2K
Replies
12
Views
2K
Replies
7
Views
3K
Replies
15
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
5
Views
3K
Replies
2
Views
4K
Back
Top