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);
   
   3 because integer arithmetic results in truncation of information after the decimal

B. System.out.println(4 * 3.5);
   
   14.0 because floating point numbers always include the decimal and floating point times integer --> floating point
   
C. String b = "caribou";
   System.out.println(b.charAt(0));
   
   c In Java, indexing starts at 0
   
D. String s = "albacore good";
System.out.println(s.substring(0, 2) + s.substring(8, 11) + s.substring(6, 8)); al gore Note that spaces are characters, so s.substring(8,11) returns " go" E. double x = 5.75; System.out.println(Math.round(x) * ((int) x)); 30 5.75 rounds up to 6 and casting 5.75 as an int truncates to 5.

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 double sideLength; public Square(double sideLength) { this.sideLength = sideLength; } public double getArea() { return sideLength * sideLength; } public double getPerimeter() { return 4 * sideLength; } }

 

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 int nextInt() {

        int temp = first;

        first = second;

        second = second + temp;

        return first;
    }
}

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.)
public double distance(Point p) {
    return Math.sqrt((getX()-p.getX())*(getX()-p.getX()) + (getY()-p.getY())*(getY()-p.getY()));
}