Register to reply

Java problem

by blah45
Tags: java
Share this thread:
blah45
#37
Nov30-10, 01:01 PM
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.
Mark44
#38
Nov30-10, 01:13 PM
Mentor
P: 21,216
Quote Quote by Cathal Cronin View Post
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 Quote by Cathal Cronin View Post
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( ... );)
blah45
#39
Nov30-10, 04:21 PM
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;
    }

}
Mark44
#40
Nov30-10, 05:45 PM
Mentor
P: 21,216
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.
blah45
#41
Nov30-10, 05:49 PM
P: 65
okay done can u help with the test part i'm very confused with it
Mark44
#42
Nov30-10, 06:20 PM
Mentor
P: 21,216
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.
blah45
#43
Nov30-10, 06:42 PM
P: 65
i basically have that don't i. all i need to do is the best signal part isn't it?
Mark44
#44
Nov30-10, 06:59 PM
Mentor
P: 21,216
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?
blah45
#45
Nov30-10, 07:08 PM
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
Mark44
#46
Nov30-10, 07:16 PM
Mentor
P: 21,216
Quote Quote by Cathal Cronin View Post
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 Quote by Cathal Cronin View Post

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 Quote by Cathal Cronin View Post

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.
blah45
#47
Nov30-10, 07:19 PM
P: 65
yeah your right i'm trying to do it now i'll out up what i've done in a few mins
blah45
#48
Nov30-10, 07:24 PM
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
blah45
#49
Nov30-10, 07:33 PM
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 ;
    }
   
}
blah45
#50
Nov30-10, 07:54 PM
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??
Mark44
#51
Nov30-10, 08:18 PM
Mentor
P: 21,216
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.
blah45
#52
Dec1-10, 06:39 AM
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++ ;
        }

        
    }
Mark44
#53
Dec1-10, 09:25 AM
Mentor
P: 21,216
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.
blah45
#54
Dec1-10, 09:31 AM
P: 65
i don't understand the main() can u show me


Register to reply

Related Discussions
Homework problem in Java Engineering, Comp Sci, & Technology Homework 9
Problem w/ java calculation Engineering, Comp Sci, & Technology Homework 0
Help me with a Java code problem Engineering, Comp Sci, & Technology Homework 2
Java problem Computing & Technology 4
Java Problem Introductory Physics Homework 0