Intro to Java Test #1

NAME_________________________ PERIOD ___________

INSTRUCTIONS

1. (5 points; 1 point each) What is the output? BE PRECISE.

A. System.out.println(14 / 4);
   


B. System.out.println(4 * 3.5);
   
   
   
C. String b = "caribou";
   System.out.println(b.charAt(0));
   
   
   
D. String s = "albacore good";
System.out.println(s.substring(0, 2) + s.substring(8, 11) + s.substring(6, 8)); E. double x = 5.75; System.out.println(Math.round(x) * ((int) x));

2. (5 points) 2. (5 points) The class Square will have a constructor that takes a side length in the form of a floating point number as its input. It will have two methods, getArea() and getPerimeter() that return the area and perimeter of the square.

Here is an example, written in a class (such as a Main class) that can see the Square class:

Square sq = new Square(5.0);             // sideLength = 5.0
System.out.println(sq.getArea());        // prints 25.0
System.out.println(sq.getPerimeter);     // prints 20.0

Fill in the blanks to complete the class.

public class Square {
private ____________ sideLength; public ____________(____________ sideLength) { ________________________________________; } public __________ getArea() { return ________________________________________; } public __________ getPerimeter() { return ________________________________________; } }

 

3. (5 points) The Fibonacci numbers are the sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, ... where each number in the sequence is the sum of the previous two numbers.

The class FibonacciGenerator will have a constructor that takes no input. It will have one method, nextInt(). Each time nextInt() is called, it returns the next number in the Fibonacci sequence. Here is an example, written in a class (such as a Main class) that can see the FibonacciGenerator class:

FibonacciGenerator fg = new FibonacciGenerator();
System.out.println(fg.nextInt());    // prints 1
System.out.println(fg.nextInt());    // prints 1
System.out.println(fg.nextInt());    // prints 2
System.out.println(fg.nextInt());    // prints 3
System.out.println(fg.nextInt());    // prints 5
System.out.println(fg.nextInt());    // prints 8

Fill in the blanks to write the FibonacciGenerator class.

public class FibonacciGenerator {
    private int first;
    private int second;

    public FibonacciGenerator() {
        first = 0;
        second = 1;
    }

    public ____________ nextInt() {

        int temp = ____________;

        ____________ = ____________;

        ____________ = ____________ + ____________;

        return ____________;
    }
}

4. (5 points) Assume that the following class has been defined for a point with (x, y) coordinates:

public class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() { return x; }
    public double getY() { return y; }
.
    public double distance (Point q) {
        // Your code here
    }
}

The formula for the distance between two points is:
  _______________
√(x1-x2)2 + (y1-y2)2

The following code should print 5.0 because that is the distance between the two points (3, 4) and (0, 0):

Point p1 = new Point(3, 4);
Point p2 = new Point(0, 0);
System.out.println(p1.distance(p2));
Write the distance method for the Point class. (Math.sqrt can be used to computer a square root of a number. It returns a double.)