April 30th

Useful links

Project Euler

Loops

Methods

Objects/Classes

Video on Objects/Classes

Introduction to Robot Programming

Lesson Plan

Review Homework

The challenge homework from last week was fairly complicated and required a lot of mental problem solving to come up with a solution. Because of this, it isn’t expected that you solved the problem, but you should have at least tried. Here is my solution to the problem:

public static int maxValue = 1000000;
public static void main(String[] args)  {
     int startingPrimeForLongestList = 2;
     int lengthOfLongestList = 0;
     for (int i = 2; i < maxValue; i++) { if (isPrime(i)) { int lengthFori = lengthOfConsecutivePrimes(i); if (lengthFori > lengthOfLongestList) {
                    lengthOfLongestList = lengthFori;
                    startingPrimeForLongestList = i;
                }
        }
    }
		
     System.out.println("Sum of primes: " + totalOfConsecutivePrimes(startingPrimeForLongestList));
     System.out.println("Starting prime: " + startingPrimeForLongestList);
     System.out.println("Length of prime list: " + lengthOfLongestList);
		
}

public static int lengthOfConsecutivePrimes(int startingPrime) {
    int total = 0;
    int primesInList = 0;		
    int lengthOfLongestWorkingPrimeList = 0;

    for (int i = startingPrime; i <= maxValue && total < maxValue; i++) {
        if (isPrime(i)) {
            primesInList = primesInList + 1;
            total = total + i;
            if (total < maxValue && isPrime(total)) {
                lengthOfLongestWorkingPrimeList = primesInList;
            }
        }
    }
    return lengthOfLongestWorkingPrimeList;
}
public static int totalOfConsecutivePrimes(int startingPrime) {
    int subTotal = 0;
    int primesInList = 0;		
    int total = 0;

    for (int i = startingPrime; i <= maxValue && subTotal < maxValue; i++) {
        if (isPrime(i)) {
            primesInList = primesInList + 1;
            subTotal = subTotal + i;
            if (subTotal < maxValue && isPrime(subTotal)) {
                total = subTotal;
            }
        }
    }
    return total;
}

public static boolean isPrime(int number) {
    for (int i = 2; i <= Math.sqrt(number); i++) {
        if (number % i == 0) {
            return false;
        }
    }
    return true;
}

Review of Methods

Before continuing on, make sure you have an understanding of how methods work and how they are constructed. If you are unsure on how methods are created and used, check out this page here: Methods

Introduction of Objects and Classes

For the introduction to objects, go over the page here: Objects/Classes

That page contains all the information needed for understanding how objects and classes work in Java. If you still are confused conceptually with what classes/objects are, this video may help clear things up: Video on Objects/Classes. You should watch this video only after reading the page above on Objects and Classes.

Practice Problems

  1. Create a Vector3D class that can store the x, y, and z coordinates of a vector as decimal values (doubles). This class should have the following aspects:
    1. private double values for each coordinate axis (x, y, and z)
    2. a constructor that takes in x,y, and z coordinates and apply them to the local x,y,and z variables
    3. a method (function) that will multiply the size of the vector (3 coordinates) by a double value that is accepted as an input
    4. a method that will take in another Vector3D object and add the coordinates of that vector to the vector the method was called from
    5. a method that will use Pythagorean theorem to return the length of the vector Hint: length = Math.sqrt(x*x+y*y+z*z)
  2. Create 2 classes. The first being a class called Coordinate and the second being called Rectangle. The two classes should have the following aspects:
    1. Coordinate class: contain private double values for x and y coordinates.
    2. Coordinate class: have a constructor that takes in an x and y coordinate and assigns the values to the local x and y variables in the class.
    3. Rectangle class: contain two private Coordinate objects (bottomLeftCorner and topRightCorner)
    4. Rectangle class: have a constructor that takes in two Coordinate objects and assigns them to the local variables in the Rectangle class
    5. Rectangle class: For this class, the bottomLeftCorner Coordinate is the x and y position of the bottom left corner of the rectangle and the topRightCorner Coordinate is the x and y position of the top right corner of the rectangle. create two methods that will return the width or height of the rectangle.
    6. Rectangle class: create a method that uses the methods created in requirement 5 to calculate and return the area of the rectangle
    7. Rectangle class: create a method that will take in a Coordinate object and return a value of

Homework

  • Go to the links below to review objects and classes:

Objects/Classes

Video on Objects/Classes (If you are confused)

If after the video, you are having trouble understanding what’s going on, please let me know and I’ll help explain. Objects and classes are hard to grasp for people programming for the first time so it is expected that you will have some difficulty.

  • Go through the links on this guide which will introduce you to writing programs for the robot.