![]() |
Jupyter at Bryn Mawr College |
|
|
Public notebooks: /services/public/dblank / CS206 Data Structures / 2016-Spring / Notebooks |
[Your name in next cell as a level-2 header. Delete this line when done.]
Goals:
LinkedList
Rubric:
Write a Java function letterGrade
that takes a double score
and returns the letter grade based on the following scale:
Put your function definition by itself in the following cell:
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";
}
letterGrade(100);
letterGrade(95);
letterGrade(89.9);
letterGrade(59.99);
letterGrade(65);
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:
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:
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);
}
}
Robot robot = new Robot(0, 0);
robot.moveDown(1);
robot.moveDown(1);
robot.y
Robot robot = new Robot(0, 0);
robot.moveRight(5);
robot.moveRight(6);
robot.moveLeft(4);
robot.x
Robot robot = new Robot(0, 0);
robot.moveDown(2.5);
robot.moveUp(5.75);
robot.moveDown(1.2);
robot.y
Robot robot = new Robot(0, 0);
robot.moveRight(5);
robot.moveDown(5);
robot.y + robot.x
Write Java class LinkedList
and Node
.
Node:
value
)next
that is of type Node
append
that takes an node and either:LinkedList:
list
that is of type Node
length()
that returns how many nodes are in listappend()
that takes a node and adds it to the end of the listDefine your class Node
by itself in the following cell:
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);
}
}
}
Node node = new Node(42);
node.value
Node node = new Node(-1);
node.next
Node node = new Node(0);
node.append(new Node(1));
node.next.value
Node node = new Node(0);
node.append(new Node(1));
node.append(new Node(2));
node.next.next.value
Define your class LinkedList
in the following cell:
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);
}
}
}
LinkedList ll = new LinkedList();
ll.length()
LinkedList ll = new LinkedList();
ll.append(new Node(100))
ll.length()
LinkedList ll = new LinkedList();
ll.append(new Node(100))
ll.append(new Node(67))
ll.length()
LinkedList ll = new LinkedList();
for (int i = 0; i < 100; i++) {
ll.append(new Node(i));
}
ll.length()