Creating a Transmitter Class for Mobile Phones

In summary, the conversation discusses the task of creating a Transmitter class for a software system for a mobile phone company. The class needs to have fields for storing x, y coordinates and the transmitter's signal radius, with specific ranges for these values. The class also needs to have a constructor, accessor and mutator methods, and a method for calculating the best signal strength for a given phone's coordinates. The conversation also mentions a test class, TransmitterTest, and provides some guidelines for its implementation. Finally, the individual shares their attempted solution for the constructor but mentions difficulty opening the java file.
  • #1
blah45
65
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
  • #2
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
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 ;
}
}
 
  • #4
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
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
Yes, just like it says in the problem description. Did you read it?
 
  • #7
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:
  • #8
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
yeah but how do i set 1 parameter to have 2 values?
 
  • #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?
 
  • #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:

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Introductory Physics Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top