Xojo Programing

Order Description

Week 2 Assignments: Loops, If-Then-Else, Case statement and more In chapter 3 and 4 of Xojo intro to programming we saw learned a lot about various kinds of loops and select case statements. Because this is arguably the TWO MOST DIFFICULT chapters in this book, we’re going to dive in quite a bit more. First on page 46 you should have tried this: Dim myAge As Integer myAge = 16 If myAge > 17 Then MsgBox "You are old enough to vote" Else MsgBox "You are not yet old enough to vote" End If But let’s enhance this by creating an app with a text field and a corresponding label that ask the user to input their age. Then by having a push button that executes the code we’ll get our answer. Well, not quite, we have to get age from the user, and not have it hard coded as shown above. So we’ll create an app that looks something like this:

The text box is named age and so to get the text of that box we ask for age.text as shown below. Recall from the discussion on page 26, cdbl is a built-in function that converts strings to integers. Dim myAge As Integer myage = cdbl(age.text) 'cdbl converts string to Integer

If myAge > 17 Then MsgBox "You are old enough to vote" Else MsgBox "You are not yet old enough to vote" End If Also, pay particular attention to the greater than sign above “>”. In this case we’re looking for someone older than (greater than) 17, meaning they would have to be 18 or more. Sometimes this type of programming can throw off subsequent programmers who may be maintaining code you wrote, so maybe it’s better to write it, if myAge >=18, meaning if the person is equal to or greater than 18. Go ahead and try this in your own program (you may copy/paste the code above into your own button). In Java it’s a bit different…. you have to use the Scanner class for console input as we did in radius program last week. In addition, Java’s syntax is similar, but also quite different as shown below: public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter age System.out.print("Enter an age "); double myage = input.nextDouble();

if (myage > 17) { System.out.println("You are old enough to vote"); } else { System.out.println("You are not yet old enough to vote" + myage); } } } Note the required bracket in Java. As we have seen, these are not required in Xojo, so this does take a bit of getting used to. Now, Suppose that you need to display a string (e.g., Welcome to Java!) a hundred times. It would be tedious to have to write the following statement a hundred times:

How do we solve this? With a While loop. int count = 0; while (count < 100) { System.out.println("Welcome to Java!"); count++; } How do we solve this? With a For loop. for(int count = 0, count < 100, count++){ System.out.println("Welcome to Java!"); }

How do we solve this? With a Do while loop. count = 0; do{ System.out.println("Welcome to Java!"); count++ } while (count < 100); We read about the While loop in Xojo and this is very similar. Again, in Java we have brackets that we must use, and note that in Xojo we would increment count by saying count = count +1, but in Java it’s done by saying count++. The ++ means add one to count. Let’s fancy this up a bit and output the count on each line: public class WelcomeToJava {

public static void main(String[] args) { int count = 1; while (count <= 100) { System.out.println("Welcome to Java! " + count); count++ } } } Assignments:

1.Re-write the “Welcome to Java” program that displays count after “Welcome to Java” in your own Xojo program. Hint, you will want to display this in a large text box. If you put it in a message box you’ll have to hit “OK 100 times. Better yet, let’s just make it 10 times instead of 100. Also note that you’ll need to set the multiline property of the text box to “On” to get it to display multiple lines. Don’t forget to use the EndofLine statement as we did on page 54 to get a new line after each count.

Open a Word document and place your code for this program as well as a screen shot of the output into the Word document.

2.On page 50 of Intro to programming we learned about the select case statement. Imagine if we didn’t have this and instead had to use an If then else statement to accomplish the job. It would look something like this:

If today.Month = 1 then MsgBox(“It’s January”) elseif today.Month = 2 then MsgBox(“It’s February”) elseif today.Month = 3 then MsgBox(“It’s March”)

. .’comment, more statements going all the way to December . endif

While both methods are similar the select case statement is more efficient. In Java, we don’t have a select case statement but we do have something quite similar, the switch statement. Here’s an example:

class Day {   public static void main(String[] args) {

     int week = 7;      String day;               switch (week) {         case 1:           day = "Sunday";           break;         case 2:           day = "Monday";    break;         case 3:           day = "Tuesday";           break;         case 4:           day = "Wednesday";           break;         case 5:           day = "Thursday";           break;         case 6:           day = "Friday";           break;         case 7:           day = "Saturday";           break;         default:           day = "Invalid day";           break;      }      System.out.println(day);

Assignment 2 deliverable: In Java create a program that does just what the program on page 50 of Xojo does. In the Word document that you created in assignment one, add the code for this program as well as a screen shot of the output.

Assignment 3 optional(Extra Credit 25 points): In Java create a program that does just what the program on page 55 of Xojo does. In the Word document that you created in assignment one and two, add the code for this program as well as a screen shot of the output. Dim x As Integer While x < 100 x = x + 1 Wend MsgBox Str(x) Note*Use the WelcomeToJava class example to format this assignment, and use the Scanner class to ask the user for the x value.

Submit the Word document to the week 2 assignment dropbox.

Methods In Java the methods declaration must have the following: 1.Modifiers—such as public, private, and others you will learn about later. 2.The return type—the data type of the value returned by the method, or void if the method does not return a value. 3.The method name—the rules for field names apply to method names as well, but the convention is a little different. 4.The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses. 5.An exception list—to be discussed later. 6.The method body, enclosed between braces—the method's code, including the declaration of local variables, goes her Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized Reference - https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html Here is an example:

Here we have a Car class that contains a drive car method. The method is public with a String return value that takes a boolean as a parameter (true or false).

In the main method we create a new instance of the Car class using the new keyword. Once we have created a new Car object we will have access to the driveCar method. Since the driveCar method return a String, we need to create a new String variable that will contain the result of the method. In this example we called it “message”. As we recall, the driveCar method used a boolean as a parameter (true or false). The we print it to the console. Although is good practice, you do not have to create the “message” String variable. The method call can just get passed into the System.out.println like so:

If we ran the application we get the result:

If we change the boolean to false:

Scope In Java the scope is controlled by the location of the variables and by the Access Level Modifiers.

In this Car class we see that there are two class variables at the top of the class (color and year). These two variables are available to any method in the class only. The variable located in the driveCar method called “message”, is only accessible in that method.

Reference - https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html As we can see in this Access Level Modifiers table, all the public methods can be accessed anywhere in the project. The private modifiers only allow to be accessed within the class. This modifiers help provide the correct restrictions on what can and can’t be accessed.