CS206: Lab 2

Bryn Mawr College, CS206: Data Structure
Douglas Blank, Spring 2016

[Your name in next cell as a level-2 header. Delete this line when done.]

by STUDENT NAME

Goals:

  1. Review of Java language
  2. Review of Java Classes
  3. First Data Structre: LinkedList

Rubric:

  1. Not only must you have the correct answer, but it must also be formatted with proper indentation.
  2. Points are assigned to each test

Problem 1

Write a Java function letterGrade that takes a double score and returns the letter grade based on the following scale:

  • score > 97, returns "A+"
  • score > 94, returns "A"
  • score > 90, returns "A-"
  • score > 87, returns "B+"
  • score > 84, returns "B"
  • score > 80, returns "B-"
  • score > 77, returns "C+"
  • score > 74, returns "C"
  • score > 70, returns "C-"
  • score > 67, returns "D+"
  • score > 64, returns "D"
  • score > 60, returns "D-"
  • otherwise, returns "F"

Put your function definition by itself in the following cell:

In [2]:
String letterGrade(double score) {
    if (score > 97) 
        return "A+";
    else if (score > 94)
        return "A";
    else if (score > 90)
        return "A-";
    else if (score > 87)
        return "B+";
    else if (score > 84)
        return "B";
    else if (score > 80)
        return "B-";
    else if (score > 77)
        return "C+";
    else if (score > 74)
        return "C";
    else if (score > 70)
        return "C-";
    else if (score > 67)
        return "D+";
    else if (score > 64)
        return "D";
    else if (score > 60)
        return "D-";
    else 
        return "F";
}
|  Added method letterGrade(double)

In [11]:
letterGrade(100);
|  Expression value is: "A+"
|    assigned to temporary variable $10 of type String

Out[11]:
A+
In [12]:
letterGrade(95);
|  Expression value is: "A"
|    assigned to temporary variable $11 of type String

Out[12]:
A
In [13]:
letterGrade(89.9);
|  Expression value is: "B+"
|    assigned to temporary variable $12 of type String

Out[13]:
B+
In [14]:
letterGrade(59.99);
|  Expression value is: "F"
|    assigned to temporary variable $13 of type String

Out[14]:
F
In [4]:
letterGrade(65);
|  Expression value is: "D"
|    assigned to temporary variable $3 of type String

Out[4]:
D

Problem 2

Write a Java class Robot that takes an double x and y in the constructor. The robot needs to keep track of where it is in the world.

Methods:

  • moveUp(double distance)
  • moveDown(double distance)
  • moveLeft(double distance)
  • moveRight(double distance)

Make it so that the robot can't move beyond (0,0) and (10,10). If it tries, it will be right at the boundary value. For example, if it were at (1, 5) and tried to move left 2, then it would be at (0, 5).

Define your class by itself in the following cell:

In [21]:
class Robot {
    double x;
    double y;
    Robot(double x, double y) {
        this.x = x;
        this.y = y;
    }

    void move(double dx, double dy) {
        this.x += dx;
        this.y += dy;
        
        this.x = Math.min(Math.max(this.x, 0), 10);
        this.y = Math.min(Math.max(this.y, 0), 10);
    }

    void moveUp(double distance) {
        move(0, -distance);
    }
    void moveDown(double distance) {
        move(0, distance);    
    }
    void moveLeft(double distance) {
        move(-distance, 0);
    }
    void moveRight(double distance) {
        move(distance, 0);
    }
}
|  Modified class Robot
|    Update overwrote class Robot

In [22]:
Robot robot = new Robot(0, 0);
robot.moveDown(1);
robot.moveDown(1);
robot.y
|  Modified variable robot of type Robot with initial value Robot@13b6d03



|  Expression value is: 2.0
|    assigned to temporary variable $28 of type double

Out[22]:
2.0
In [23]:
Robot robot = new Robot(0, 0);
robot.moveRight(5);
robot.moveRight(6);
robot.moveLeft(4);
robot.x
|  Modified variable robot of type Robot with initial value Robot@3ecf72fd




|  Expression value is: 6.0
|    assigned to temporary variable $33 of type double

Out[23]:
6.0
In [25]:
Robot robot = new Robot(0, 0);
robot.moveDown(2.5);
robot.moveUp(5.75);
robot.moveDown(1.2);
robot.y
|  Modified variable robot of type Robot with initial value Robot@5474c6c




|  Expression value is: 1.2
|    assigned to temporary variable $42 of type double

Out[25]:
1.2
In [26]:
Robot robot = new Robot(0, 0);
robot.moveRight(5);
robot.moveDown(5);
robot.y + robot.x
|  Modified variable robot of type Robot with initial value Robot@61443d8f



|  Expression value is: 10.0
|    assigned to temporary variable $46 of type double

Out[26]:
10.0

Problem 3

Write Java class LinkedList and Node.

Node:

  • constructor takes an integer (called value)
  • class has a property called next that is of type Node
  • a method called append that takes an node and either:
    • if next is null, puts it there
    • else, tells next to append it

LinkedList:

  • has a propery called list that is of type Node
  • has a method called length() that returns how many nodes are in list
  • has a method called append() that takes a node and adds it to the end of the list

Define your class Node by itself in the following cell:

In [1]:
class Node {
    int value;
    Node next;
    
    Node(int value) {
        this.value = value;
    }
    
    void append(Node node) {
        if (next == null) {
            next = node;
        } else {
            next.append(node);
        }
    }
}
|  Added class Node

In [7]:
Node node = new Node(42);
node.value
|  Added variable node of type Node with initial value Node@6956de9

|  Expression value is: 42
|    assigned to temporary variable $6 of type int

Out[7]:
42
In [8]:
Node node = new Node(-1);
node.next
|  Modified variable node of type Node with initial value Node@12bc6874

|  Expression value is: null
|    assigned to temporary variable $8 of type Node

Out[8]:
null
In [10]:
Node node = new Node(0);
node.append(new Node(1));
node.next.value
|  Modified variable node of type Node with initial value Node@2471cca7


|  Expression value is: 1
|    assigned to temporary variable $12 of type int

Out[10]:
1
In [11]:
Node node = new Node(0);
node.append(new Node(1));
node.append(new Node(2));
node.next.next.value
|  Modified variable node of type Node with initial value Node@763d9750



|  Expression value is: 2
|    assigned to temporary variable $16 of type int

Out[11]:
2

Define your class LinkedList in the following cell:

In [2]:
class LinkedList {
    Node list;
    
    int length() {
        Node current = list;
        int count = 0;
        while (current != null) {
            current = current.next;
            count++;
        }
        return count;
    }
    
    void append(Node node) {
        if (list == null) {
            list = node;
        } else {
            list.append(node);
        }
    }
}
|  Added class LinkedList

In [3]:
LinkedList ll = new LinkedList();
ll.length()
|  Added variable ll of type LinkedList with initial value LinkedList@4ca8195f

|  Expression value is: 0
|    assigned to temporary variable $4 of type int

Out[3]:
0
In [5]:
LinkedList ll = new LinkedList();
ll.append(new Node(100))
ll.length()
|  Modified variable ll of type LinkedList with initial value LinkedList@12bc6874


|  Expression value is: 1
|    assigned to temporary variable $8 of type int

Out[5]:
1
In [6]:
LinkedList ll = new LinkedList();
ll.append(new Node(100))
ll.append(new Node(67))
ll.length()
|  Modified variable ll of type LinkedList with initial value LinkedList@1ef7fe8e



|  Expression value is: 2
|    assigned to temporary variable $12 of type int

Out[6]:
2
In [8]:
LinkedList ll = new LinkedList();

for (int i = 0; i < 100; i++) {
    ll.append(new Node(i));
}

ll.length()
|  Modified variable ll of type LinkedList with initial value LinkedList@6979e8cb




|  Expression value is: 100
|    assigned to temporary variable $17 of type int

Out[8]:
100