Sample Programs

Here are some sample programs for you to try out and play around with

Program 1

void main() {
   String message;
   message = "Hello, World!";
   printLine(message);   
}

Program 2
void main() {
   int product;
   product = 3 * 4;
   printLine(product);
}

Program 3
void main() {
   int distanceFromLeft;
   int distanceFromTop;
   int width;
   int height;

   setBackgroundColor(green);
   setColor(white);

   printLine("How far from the left edge do you want to draw a white oval?");
   distanceFromLeft = readInt();

   printLine("How far from the top edge do you want to draw a white oval?");
   distanceFromTop = readInt();

   printLine("How wide do you want the oval to be?");
   width = readInt();

   printLine("How high do you want the oval to be?");
   height = readInt();

   printLine("Here is your oval...");
   fillOval(distanceFromLeft, distanceFromTop, width, height);
}

Program 4
void main () {
   int grade1;
   int grade2;
   int grade3;
   int grade4;
   int sum;
   double average;

   grade1 = 95;
   grade2 = 74;
   grade3 = 86;
   grade4 = 91;

   sum = grade1 + grade2 + grade3 + grade4;
   average = sum / 4.0;

   printLine("Grade 1 is " + grade1);
   printLine("Grade 2 is " + grade2);
   printLine("Grade 3 is " + grade3);
   printLine("Grade 4 is " + grade4);
   printLine("Average Grade is " + average);
}
Program 5
void main () {
   double sum;
   double number;
   sum = 0;

   while (sum <= 1000) {
      printLine("Enter a number.");
      sum = sum + readDouble();

      printLine("The total is " + sum);
      print("I will keep asking you until the total ");
      printLine("of what you enter adds up to more than 1000.");
   }
   printLine("The total " + sum + " is more than 1000.");
}

Back to Loops     Forward to Ten Things to Remember