In class we did the following examples:
- Print out to the console from info entered in a text field.
- Ask a user for a number and give the user that number doubled.
- Ask a user for a number and multiply it by a second number entered by the user.
To accomplish the second and third task we needed to first save the data (accessed on the text field with the .getText() method) and then parse the data (think casting!) as an integer.
Here's the code we used in class:
public void doubleNumber() {
numToMultiply = jTextField1.getText();
numToMultiplyBy = jTextField2.getText();
//Getting the info from the text field
numberToMultiply = Integer.parseInt(numToMultiply);
numberToMultiplyBy = Integer.parseInt(numToMultiplyBy);
finalNumber = numberToMultiplyBy * numberToMultiply;
//Double the number
// numToDouble = Integer.toString(numberToDouble);
//Convert the doubled number to a String
newNum.setText(Integer.toString(finalNumber));
// Set the label "newNum" with the doubled number!
}
private String numToMultiply, numToMultiplyBy;
private int numberToMultiply, numberToMultiplyBy, finalNumber;
The in-class assignment was to create a LoveTester that asks the user for 3-5 pieces of information about himself and 3-5 pieces of information about his lover. The complicated LoveTester Algorithm (patent pending) is going to reveal the level of attraction, lust, and love for the two people!
No comments:
Post a Comment