Results 1 to 15 of 15
  1. #1
    Copaman is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default Really n00bish question...

    So I figure this has to be possible but I don't know how to do it.

    If it makes any difference, I'm coding Java for Android.

    First post here so go easy :)

    ==

    I was assigned to write a program to solve a real world problem in one of my classes. It's due next Monday (one week). The only hangup I have left to solve is possibly the easiest.

    I have one package, com.xxx.android.yyy, and in that package I have private class 1. In that private class, I have a public void, and in that there are a few "if" statements:

    Java Code:
    public void onLocationChanged(Location location) {
        		if(location!=null){
        			if(location.hasSpeed()){
        				mySpeed = location.getSpeed();
        				if(mySpeed>maxSpeed)
        					maxSpeed = mySpeed;
        				tv.setText("\nCurrent speed: " + mySpeed*2.2369 + " mph, Max speed: " + maxSpeed*2.2369 + " mph");
        				if(mySpeed>=limitSpeed){
        					//***Need to call public class from pkg com.xxx.android.zzz***
        				}

    Within that if statement [if(mySpeed>=limitSpeed)], I need to call a public class from package com.xxx.android.zzz

    How do I do that?


    EDIT: I think this might be in the wrong forum. I'm sorry if it is. In the event that I'm right, feel free to move it to the appropriate place :)
    Last edited by Copaman; 06-07-2010 at 11:57 PM.

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Private classes should be only used in nested classes. There is no way to instantiate a private class unless it's nested in a public class.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default

    I need to call a public class from package com.xxx.android.zzz
    Not sure about your question. Is the public class you want to "call" in the package ...zzz
    or is the "calling" code in the ...zzz package?
    By "calling" do you mean use new to create a new instance of the class?
    Or do you mean calling a method in the class?
    Then is the question: How do I get a reference to the class so I can call one of its methods?

  4. #4
    Copaman is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Private classes should be only used in nested classes. There is no way to instantiate a private class unless it's nested in a public class.
    It is. I only posted a snippet of the code.

  5. #5
    Copaman is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Not sure about your question. Is the public class you want to "call" in the package ...zzz
    or is the "calling" code in the ...zzz package?
    By "calling" do you mean use new to create a new instance of the class?
    The class I need to use from the zzz package plays a very important role in the overall program.

    Unfortunately I need it to perform that role in the private class within the yyy package, when mySpeed is greater than limitSpeed.

    here:

    Java Code:
    public class AirplaneChange extends Activity {
    	public void method() {
    		Context context = getApplicationContext();
    		
    		boolean isEnabled = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0)== 1;
    		
    		Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
    I need that to do it's thing when mySpeed > limitSpeed.

  6. #6
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    In that case, the same way you would reference any other class:
    Java Code:
    //Assume private class is called ActionListener.
    //Assume constructor takes no paramaters.
    
    ActionListener listener = new ActionListener();
    
    //Assume there is at least one method that takes no paramaters
    listener.[U]method[/U]();
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  7. #7
    Copaman is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Would that private be from my first package, yyy?

  8. #8
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Only if the public class that has the nested private class is in the package yyy. Example:

    Java Code:
    package yyy;
    
    public class A {
         ...
         private class B {
              ...
         }
    }
    Both classes (A and B) are in the package yyy because A is in the package yyy and B is part of A. I hope that makes sense.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  9. #9
    Copaman is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Yes they are and yes it does.

    From the above example, the one with ActionListener, would I just throw the public void from my public class AirplaneChange in post #5 after the listener.method(); ?

  10. #10
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    if you put listener.method() before the void method, wouldn't it look like this?

    Java Code:
    package yyy;
    public class A {
         ActionListener listener = new ActionListener(); //discouraged. 
         listener.method(); //Pretty sure this will give you java.lang.Error Exception
         public void publicMethod() {
             ...
         }
         private class ActionListener {
             public void method() {
                 ...
             }
         }
    }
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Perhaps you could attach zip file and give us a couple of places to look? I'm not really visualizing your problem at this point. All I know is it has to do with package structure, and that's not really helping me. If for whatever reason you can't attach the code, I'd understand, but it would make things easier :)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default

    I need it to perform that role in the private class within the yyy package
    What does "perform that role" mean?
    The code in a private class should be able to create new instances of public classes.

    Have you tried coding yet? Do you get errors? Given an error, we'd have something to work with. All this theoretical talk is hard to follow. Let's have code.

  13. #13
    Copaman is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Singing Boyo View Post
    Perhaps you could attach zip file and give us a couple of places to look? I'm not really visualizing your problem at this point. All I know is it has to do with package structure, and that's not really helping me. If for whatever reason you can't attach the code, I'd understand, but it would make things easier :)

    I was hesitant to post code but I suppose I can.

    Here you go... this was package yyy in my first post.

    Java Code:
    package com.ryan.android.cellsafe;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.provider.Settings.System;
    import android.widget.TextView;
    import android.content.Context;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    
    public class CellSafe extends Activity {
        
        
        private TextView tv;
        private LocationManager lm;
        private LocationListener ll;
        double mySpeed, maxSpeed, limitSpeed;
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            tv = new TextView(this);
            setContentView(tv);
            
            maxSpeed = mySpeed = 0;
            limitSpeed = 7.8232;
            
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            ll = new SpeedoActionListener();
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
        }
        
        private class SpeedoActionListener implements LocationListener {
            
            @Override
            public void onLocationChanged(Location location) {
                if(location!=null){
                    if(location.hasSpeed()){
                        mySpeed = location.getSpeed();
                        if(mySpeed>maxSpeed)
                            maxSpeed = mySpeed;
                        tv.setText("\nCurrent speed: " + mySpeed*2.2369 + " mph, Max speed: " + maxSpeed*2.2369 + " mph");
                        if(mySpeed>=limitSpeed){
                            
                            //***AREA IN QUESTION***
                            
                        }
                        
                                }
                            
                            }
                        }
                                       
            
            
               @Override
               public void onProviderDisabled(String provider) {
                   //TODO Auto-generated method stub
                   
               }
               
               @Override
               public void onProviderEnabled(String provider) {
                   //TODO Auto-generated method stub
               }
               
               @Override
               public void onStatusChanged(String provider, int status, Bundle extras) {
                   //TODO Auto-generated method stub
                   
            }
    
                       
        }
    }


    And this is package zzz

    Java Code:
    package com.ryan.android.airplanechange;
    
    import android.provider.Settings;
    import android.app.Activity;
    import android.content.Context;
    
    public class AirplaneChange extends Activity {
        public void method() {
            Context context = getApplicationContext();
            
            boolean isEnabled = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
            
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
        }
    
    
    }

    As you can see, I've marked the area in question in the CellSafe package. AirplaneChange switches Airplane Mode on. I need that to happen when mySpeed is greater than or equal to limitSpeed.

  14. #14
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Right. That really does help. I thought it was what you meant, but it would have been just as hard to explain without code as it was to understand it (and I still wasn't sure what you meant originally - code helps)

    At the beginning of the CellSafe method, with the imports, (preferably before or after) add an import for com.ryan.android.airplanechange.AirplaneChange. At the area in question, just use AirplaneChange as you would any other class. In code:

    Java Code:
    //import
    import com.ryan.android.airplanechange.AirplaneChange;
    
    //at area in question
    AirplaneChange airChange = new AirplaneChange();
    //register airChange with whatever you need to
    //I don't know android, but judging from the interface name (Activity)
    //you'd queue it with something to be run ASAP
    Hopefully this helps.

    Singing Boyo

    P.S. Change the package name. com.ryan.android.airplanechange is just unwieldy and long... find an abbreviation, or a different package name based on other contents. If it's the only class in the package, you may want to move it.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  15. #15
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Just one more piece of advice, if you use some package only once, instead of bloating up your imports, you can use it directly:
    Java Code:
    //some method that needs keyboard input
    void someMethod() {
      java.util.Scanner sc = new java.util.Scanner(System.in);
    }
    Functionallity is of course the same as just importing the Scanner class, it's only a matter of style and preference.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Question
    By Java_Fanatic in forum New To Java
    Replies: 10
    Last Post: 10-13-2009, 11:18 PM
  3. question
    By zizou147 in forum New To Java
    Replies: 13
    Last Post: 07-04-2008, 07:05 PM
  4. Question
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 05-21-2008, 02:23 PM
  5. question about rmi
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •