• No se han encontrado resultados

Procedimiento para la realización de la soldadura junta con proceso GMAW

2.2. Análisis de soldabilidad

2.2.2. Procedimiento para la realización de la soldadura junta con proceso GMAW

With our current knowledge of Java, we are not yet able to complete our math game

but we can make a significant start. We will look at how we can ask the player a

question and offer them some multiple choice answers (one correct and two incorrect). At this stage, we know enough of Java to declare and initialize some variables that will hold the parts of our question. For example, if we want to ask the times tables question 2 x 2, we could have the following variable initializations to hold the values for each part of the question:

int partA = 2; int partB = 2;

The preceding code declares and initializes two variables of the int type, each to the value of 2. We use int because we will not be dealing with any decimal fractions. Remember that the variable names are arbitrary and were just chosen because they seemed appropriate. Clearly, any math game worth downloading is going to need to ask more varied and advanced questions than 2 x 2, but it is a start.

Now we know that our math game will offer multiple choices as answers. So, we need a variable for the correct answer and two variables for two incorrect answers. Take a look at these combined declarations and initializations:

int correctAnswer = partA * partB; int wrongAnswer1 = correctAnswer - 1; int wrongAnswer2 = correctAnswer + 1;

Note that the initialization of the variables for the wrong answers depends on the value of the correct answer, and the variables for the wrong answers are initialized after initializing the correctAnswer variable.

Now we need to put these values, held in our variables, into the appropriate elements on our UI. The question variables (partA and partB) need to be displayed in our UI elements, textPartA and textPartB, and the answer variables (correctAnswer, wrongAnswer1, and wrongAnswer2) need to be displayed in our UI elements with the following IDs: buttonChoice1, buttonChoice2, and buttonChoice3. We will see how we do this in the next step-by-step tutorial. We will also implement the variable declaration and initialization code that we discussed a moment ago:

1. First, open GameActivity.java in the editor window. Remember that you can do this by double-clicking on GameActivity in our java folder or clicking on its tab above the editor window if GameActivity.java is already open.

2. All of our code will go into the onCreate method. It will go after the setContentView(R.layout.activity_game); line but before the closing curly brace } of the onCreate method. Perhaps, it's a good idea to leave a blank line for clarity and a nice explanatory comment as shown in the following code. We can see the entire onCreate method as it stands after the latest amendments. The parts in bold are what you need to add. Feel free to add helpful comments like mine if you wish:

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

//The next line loads our UI design to the screen setContentView(R.layout.activity_game);

//Here we initialize all our variables int partA = 9;

int partB = 9;

int correctAnswer = partA * partB; int wrongAnswer1 = correctAnswer - 1; int wrongAnswer2 = correctAnswer + 1; }//onCreate ends here

3. Now we need to add the values contained within the variables to the TextView and Button of our UI. But first, we need to get access to the UI elements we created. We do that by creating a variable of the appropriate class and linking it via the ID property of the appropriate UI element. We already know the class of our UI elements: TextView and Button. Here is the code that creates our special class variables for each of the necessary

UI elements. Take a close look at the code, but don't worry if you don't

understand all of it now. We will dissect the code in detail once everything is working. Enter the code immediately after the code entered in the

previous step. You can leave a blank line for clarity if you wish. Just before you proceed, note that at two points while typing in this code, you will be prompted to import another class. Go ahead and do so on both occasions:

/*Here we get a working object based on either the button or TextView class and base as well as link our new objects directly to the appropriate UI elements that we created previously*/ TextView textObjectPartA = (TextView)findViewById(R.id.textPartA); TextView textObjectPartB = (TextView)findViewById(R.id.textPartB); Button buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1); Button buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2); Button buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);

In the preceding code, if you read the multiline comment, you will see that I used the term object. When we create a variable type based on a class, we call it an object. Once we have an object of a class, we can do anything that that class was designed to do. This is very powerful and is explored thoroughly in Chapter 6,

4. Now we have five new objects linked to the elements of our UI that we need to manipulate. What precisely are we going to do with them? We need to

display the values of our variables in the text of the UI elements. We can use the objects we just created combined with a method provided by the class, and use our variables as values for that text. As usual, we will dissect this code further at the end of this tutorial. Here is the code to enter directly after the code in the previous step. Try and work out what is going on before we look at it together:

//Now we use the setText method of the class on our objects //to show our variable values on the UI elements.

//Just like when we output to the console in the exercise - //Expressions in Java, only now we use setText method //to put the values in our variables onto the actual UI. textObjectPartA.setText("" + partA);

textObjectPartB.setText("" + partB);

//which button receives which answer, at this stage is arbitrary. buttonObjectChoice1.setText("" + correctAnswer);

buttonObjectChoice2.setText("" + wrongAnswer1); buttonObjectChoice3.setText("" + wrongAnswer2);

5. Save your work.

If you play with the assignment values for partA and partB, you can make them whatever you like and the game adjusts the answers accordingly. Obviously, we

shouldn't need to reprogram our game each time we want a new question and we

will solve that problem soon. All we need to do now is link the game section we have just made to the start screen menu. We will do that in the next tutorial.

Now let's explore the trickier and newer parts of our code in more detail.

In step 2, we declared and initialized the variables required so far:

//Here we initialize all our variables int partA = 2;

int partB = 2;

int correctAnswer = partA * partB; int wrongAnswer1 = correctAnswer - 1; int wrongAnswer2 = correctAnswer + 1;

Then in step 3, we got a reference to our UI design through our Java code. For the TextViews, it was done like this:

For each of the buttons, a reference to our UI design was obtained like this:

Button buttonObjectChoice1 =

Button)findViewById(R.id.buttonChoice1);

In step 4, we did something new. We used a the setText method to show the values of our variables on our UI elements (TextView and Button) to the player.

Let's break down one line completely to see how it works. Here is the code that

shows the correctAnswer variable being displayed on buttonObjectChoice1. buttonObjectChoice1.setText("" + correctAnswer);

By typing buttonObjectChoice1 and adding a period, as shown in the following line of code, we have access to all the preprogrammed methods of that object's class

type that are provided by Android:

buttonObjectChoice1.

The power of Button and the Android API

There are actually lots of methods that we can perform on an object of the Button type. If you are feeling brave, try this to get a feeling of just how much functionality there is in Android.

Type the following code:

buttonObjectChoice1.

Be sure to type the period on the end. Android Studio will pop up a list of possible methods to use on this object. Scroll through the list and get a feel of the number and variety of options:

If a mere button can do all of this, think of the possibilities for our games once we have mastered all the classes contained in Android. A collection of classes designed to be used by others is collectively known as an Application Programmers Interface (API). Welcome to the Android API!

In this case, we just want to set the button's text. So, we use setText and concatenate the value stored in our correctAnswer variable to the end of an empty string, like this:

setText("" + correctAnswer);

We do this for each of the UI elements we require to show our variables.

Playing with autocomplete

If you tried the previous tip, The power of Button and the Android API, and explored the methods available for objects of the Button type, you will already have some insight into autocomplete. Note that as you type, Android Studio is constantly making suggestions for what you might like to type next. If you pay attention to this, you can save a lot of time. Simply select the correct code completion statement that is suggested and press Enter. You can even see how much time you saved by selecting

Help | Productivity Guide from the menu bar. Here you will see statistics for every aspect of code completion and more. Here are a few entries from mine:

As you can see, if you get used to using shortcuts early on, you can save a lot of time in the long run.

Documento similar