Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Java Code

 

For Comparison with Blocks, etc.

Mystery Code

 

 

For Comparison with Blocks, etc

 

public class Demonstration

{

  //main method - the starting point - statements preceded by // are comments, compare with the text explanatory blocks in NXT       

  public static void main(String args[])

  {

              int age = 17;       //declare a variable

              printAge(age);     //call a method named printAge and pass the variable age to the method (NXT uses data wires to do this)

   }

  public static void printAge(int age)  //the method called - compare to a block in NXT, braces enclose the contents fo a method (block in NXT)

  {

              if (age > 10)      //logic statement, NXT also uses

              {

                          System.out.println("Age is greater than 10");    //Printing to the screen, compare to the Display block in NXT

              }

              if (age < 10)     //compare these 2 if statements to the switch statement in NXT

              {

                          System.out.println("Age is less than 10");

              }

  }

}

Additional Commentary

The programming platform used by NXT is LabView. This is compared to the compilers that are used by progamming languages.

There are numerous compilers for all languages.

In general, commands in Java a executed sequentially - compare with the sequence beam in NXT

A Java program execution begins with the main method. An NXT program begins with the first block on the sequence beam

Java applets have a "look alike" for the simultaneous processing option offered by NXT
 

 

 

Mystery Code

 

 

import java.util.*;

public class Students

{

            public static void main (String[]args)

            {

                String group[]= new String[17];

                   //current team 1

                group[0] = "Alan"; group[1] = "Jake"; group[2] = "Andy"; group[3] = "Blaise"; group[4] = "Alex";

                                    //current team 2

                group[5] = "JohnC"; group[6] = "JohnL"; group[7] = "Jonathan"; group[8] = "Alex"; group[9] = "Tyler";

                                  //current team 3

                group[10] = "Danny"; group[11] = "Tommy"; group[12] = "Beau"; group[13] = "Andrew";

                                    //current team 4

                group[14] = "Armin"; group[15] = "Logan"; group[16] = "Scott";

                Random randomizer = new Random();

                int high = 16; int low = 0;

                for (int i = 0; i<17; i++)

                {

                      int position = randomizer.nextInt(high-low + 1) + low; 

                      String temp = group[i];

                      group[i] = group[position];

                      group[position] = temp;

                 }

                       int m = 0;

                       System.out.println("The two groups of 4");

                       while (m < 8)

                        {

                               System.out.print(group[m]+ " ");

                               m = m + 1;

                        }

                                System.out.println();

                       while (m < 17)

                        {

                                 System.out.print(group[m] + " ");

                                 m = m + 1;

                       }

            }

}