Java

TOPIC 05 – YOUR FIRST JAVA PROGRAM
AP EXTRA

 

AP LESSON WORK

 

 

QUESTION 1


The code below is indented using the Java K&R style that the AP exam will use.  Relocate the curly brackets to change the indenting style to Allman style (Mr. Campeau's favourite style).

 

Note that this is an extremely easy exercise just to make sure that you can work with both styles.

 

a)

public class Tester {

   public static void main(String[] args) {

      Person p = new Person();

      p.first = "Patrick";

      p.last = "Campeau";

      p.birthYear = 1977;

      System.out.println(p.first + " " + p.last);

   }

}

b)

public class OurMath {

   public static double square(double n) {

      double nSquared = n * n;

      return nSquared;

   }

 

   public static double cube(double n) {

      double nCubed = n * n * n;

      return nCubed;

   }

}

c)

public class OddStuff {

    public static void main(String[] args) {

         for(int x=1; x<50; x++) {

             if (x%2==0) {

                 System.out.println(x + " is even.");

             }

             else {

                 System.out.println(x + " is odd.");

             }

         }

    }

}