Creating a Transmitter Class for Mobile Phones

Click For Summary

Discussion Overview

The discussion revolves around creating a Transmitter class for a mobile phone application, focusing on the implementation of constructors, validation of input parameters, and the overall structure of the class. Participants are addressing a homework problem that involves object-oriented programming in Java.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant outlines the requirements for the Transmitter class, including fields for x, y coordinates and signal radius, along with specific ranges for these values.
  • Another participant suggests that the constructor should not generate random values but should accept parameters and validate them against the specified ranges.
  • There is a discussion about the need for two constructors: one that accepts all three parameters and another that defaults the radius to 300 when only x and y are provided.
  • Some participants express confusion about how to implement input validation within the constructors, particularly regarding the range checks for x, y, and radius.
  • Participants emphasize the importance of validating input values before assigning them to member variables in the constructors.
  • There are requests for clarification on how to structure the code to include validation checks, with one participant asking for a specific example of how to validate the x variable.

Areas of Agreement / Disagreement

Participants generally agree on the need for input validation in the constructors, but there is some confusion regarding the implementation details and the correct handling of parameters.

Contextual Notes

Participants have noted that the input validation must occur before setting the member variables in the constructors, but there is uncertainty about the exact implementation steps and the handling of parameter ranges.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 23 ·
Replies
23
Views
6K