0

I wrote a program to work from the console in Eclipse and the terminal window in Linux. I am now transforming it into an Android app and I have the basic functionality of the Android UI done up until the point where it needs to use the logic from the Java file of the program I wrote. All of my inputs from the Java file are currently from the keyboard (from Scanners).

My question is: how do I transform this to get it work with the user interaction of the app?

The only input would be from the built in NumberPicker. The Java file starts from the main method: public static void main(String[] args) {)

For example:

Sample Android Code:

public class Source1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.source1_layout);
        Intent intent = getIntent();
        int pickerValue = intent.getIntExtra("numEntered", 0);
}

Sample Java Code from program (Test.java):

public class Test {

    public static void main(String[] args) {

    System.out.println("Enter number: ");
    Scanner reader = new Scanner(System.in);
    int num = reader.nextInt();
    ...
    }
}

How do I pass pickerValue into the main method of the Test class (starting the logic of the main method)? I want num = pickerValue; in the main method (replacing the Scanner). How is this done?

Also, will the print statements I have, System.out.println(...);, translate directly into the Android UI and print on the screen or do I have to modify those?

lord_sneed
  • 794
  • 3
  • 12
  • 25

2 Answers2

1

You should try to separate the UI from the logic. Extract the part where you're computing something based on inputs from the part where you actually gather the input. That way, the logic methods (or classes) can be reused with several input-gathering methods.

For example, a Calculator class should not have the following methods:

/**
 * asks for two numbers A and B, reads them from the keyboard, and displays the result 
 * of A^B on the screen
 */
public void power() {...}

/**
 * asks for a number A, reads it from the keyboard, and displays the square root
 * of A on the screen
 */
public void squareRoot() {...}

Instead, it should be separated into two classes:

public class Calculator {
    /**
     * returns a^b
     */
    public double power(double a, double b) {...}

    /**
     * returns the square root of a
     */
    public double squareRoot(double a) {...}
}

public class ConsoleCalculator {
    private Calculator calculator;

    public ConsoleCalculator(Calculator calculator) {
        this.calculator = calculator;
    }

    /**
     * asks for two numbers A and B, reads them from the keyboard, uses
     * the calculator to compute A^B, and displays the result on the screen
     */
    public void power() {...}

    /**
     * asks for a number A, reads it from the keyboard, uses the calculator to
     * compute the square root of A and displays the result on the screen
     */
    public void squareRoot() {...}
}

This way, it's easy to build an AndroidCalculator, because the hard-core math logic is encapsulated in a Calculator class, which doesn't care about where the inputs come from.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Sorry, but I am failing to see where that answers my question. I am looking for how to start my logic code and transfer the user input from Android to it. – lord_sneed Dec 02 '12 at 23:29
  • Re-read my answer. Instead of doing everything in one main method, extract the computation part into its own method, or even its own class. The call the method from your Android code. – JB Nizet Dec 03 '12 at 08:31
  • That is what I am asking if you re-read my question. I have everything set-up. I am asking how to make that specific call to the main method, while passing one integer value from the Android code to the main. I have the Android code and the Java separated, now how to start the Java from the Android? – lord_sneed Dec 03 '12 at 15:01
  • And my point is: your Android code should not call main(). it should call a separate method, which doesn't handle input and output, but only does computation. Putting a whole program into a main method is not acceptable. Split your main method into methods and classes. Look at the javadoc: it's full of reusable classes and methods. It's not composed of a single main() method. Do the same with your code: extract methods for input and output, and other methods for computation. – JB Nizet Dec 03 '12 at 15:20
  • I am really confused. The main method is what starts the Java program though? The main method is from where all of my methods are called from; it is the central core. How am I supposed to have a working program when I just have a bunch of random methods thrown around? My main method has an integer variable. A method is called from the main to do a computation on that number. That finishes and it moves on in the main method. Another method is called to do another computation, etc. So in your explanation, where is the driving force behind the program? What is the central core? – lord_sneed Dec 03 '12 at 15:42
  • Take a look at this question: http://stackoverflow.com/questions/5984321/runing-a-standard-java-code-on-android?lq=1 Should this never be done then? – lord_sneed Dec 03 '12 at 15:44
  • Your main method should do `Inputs i = readInputsFromConsole();Result r = compute(i);writeResultToConsole(r)`; Your android program should do `onButtonClick() {Inputs i = getInputsFromGui();Result r = compute(i);showResultInGui();}` This allows reusing the same `Result compute(Inputs i)` method in both programs. If you put all the code in `main()` directly, you can't reuse anything. – JB Nizet Dec 03 '12 at 17:12
  • I really have no idea what you are talking about and I am not sure you understand what I am trying to do. If I moved everything in `main` to another method called `public static void runProgram() {` then in the `main` method the only thing I had was a call to `runProgram` would I then just create an instance in the Android program to start the program? Again, referencing the link I gave you. – lord_sneed Dec 03 '12 at 18:14
  • For christ sake. Have I told you to move everything from main to another single method? No! I told you to separate the logic you have in main in several parts: one part that reads inputs (and which isn't useful in Android), one part that performs the computation and returns a result (and which can be reused in Android), and one part that prints the output (and which isn't useful in Android). Your main method isn't useful at all in Android. Android applications aren't started from a main method. Read a tutorial on Android, as well as the following question: – JB Nizet Dec 03 '12 at 21:23
  • http://stackoverflow.com/questions/4221467/how-can-android-source-code-not-have-a-main-method-and-still-run – JB Nizet Dec 03 '12 at 21:24
  • Why couldn't you send that before? That link is the sort of answer I was looking for. I am glad you can communicate well. I will upvote and accept this answer because you did eventually answer it even though you were rude at the end. Not everyone has had tons of experience with Android and may have only written some basic apps to understand some of the very basics. Hopefully you will keep that in mind next time you give an answer and if you cannot communicate clearly enough then it is best not to say anything at all. Thank you though for your time and efforts. – lord_sneed Dec 03 '12 at 21:39
  • Sorry if that sounded rude. But you literally said :"I have the basic functionality of the Android UI done up". To me, when you have a UI done up, that means you at least tried to execute it once. Given that, how could I still think that you didn't know Android apps weren't started with a main method: you said you already had the UI application running, and that you just needed to implement the funtionality behind the UI. Didn't you notice you didn't need a main method to run your UI? – JB Nizet Dec 03 '12 at 22:25
  • I do have the basic functionality of the Android UI done (buttons to go to the next activity screen and a simple `NumberPicker` sample where you set the number and hit the "OK" button and it opens a new activity and prints the number picked on the next activity screen). I did notice that there was no `main` method but I thought that it was just unique to the "overlay" or "wrapper" (I guess you call it?), but that when you go to implement the underlying Java logic code that you still required the `main` method to call and run the background logic. – lord_sneed Dec 03 '12 at 22:32
1

You'll have to re-write your Java logic for it to be understood by the Android framework. Depending on the complexity of your app, this may or may not be easy to do.

For example, using the Test class you posted

Sample Java Code from program:

public class Test {

public static void main(String[] args) {

System.out.println("Enter number: ");
Scanner reader = new Scanner(System.in);
int num = reader.nextInt();
...
}
}

This would easily become

public class Source1 extends Activity {

TextView number;
 EditText enterText;
 String result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.source1_layout);

//inflate your components from XML
result = (TextView)findViewById(R.id.resultId);
enterText = (EditText)findViewById(R.id.enterTextId);

//Get the value from the user
//print it to the screen
String userWrites = enterText.getText().toString();
result.setText(userWrites);

}

You seem to be on the right track. Hope this helps

Jade Byfield
  • 4,668
  • 5
  • 30
  • 41
  • The Java code I have in my question is from a completely separate file/class than the Android code where I figured out all of the logic of my program. I am basically trying to run that logic code from the Android activity. The activity will have a `NumberPicker`, which I want to replace the Scanner with. The activity will have a prompt for the user to enter a number. The user enters the number with the `NumberPicker`, hits "OK", then the Java logic code will do its computation. It will continue to do this until the loop breaks in the logic code. – lord_sneed Dec 03 '12 at 02:29
  • I am basically looking for a solution that is similar to this: http://stackoverflow.com/questions/5984321/runing-a-standard-java-code-on-android?rq=1 I am passing an integer though (1 integer) and I want the integer set to `num` in my sample Java code. – lord_sneed Dec 03 '12 at 02:31
  • Do you have any further input as to how to start my program from Android? – lord_sneed Dec 03 '12 at 15:52