Rounding (#3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Round { | |
public static void main(String[] args) { | |
System.out.println(round(3.124, 2)); | |
} | |
static double round(double x, int n) { | |
double y = x * tenToN(n); | |
y = Math.round(y); | |
return y / tenToN(n); | |
} | |
static double tenToN(int n) { | |
if (n > 0) { | |
return 10 * tenToN(n-1); | |
} | |
else { | |
return 1; | |
} | |
} | |
} |
Reversing a string with recursion (#4)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Back { | |
public static void main(String[] args) { | |
System.out.println(backRec("cs-111")); | |
} | |
static String back(String x) { | |
String y = ""; | |
for (int i=0; i<x.length(); i++) { | |
y = y + x.charAt(x.length()-1-i); | |
} | |
return y; | |
} | |
static String backRec(String x) { | |
if (x.length() > 0) { | |
return x.charAt(x.length()-1) + backRec(x.substring(0, x.length()-1)); | |
} | |
else { | |
return ""; | |
} | |
} | |
} | |
Spiral (#5)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Spiral { | |
public static void main(String[] args) { | |
Drawing d = new Drawing(); | |
spiral(d, 4); | |
} | |
static void spiral(Drawing d, int n) { | |
// Base case | |
if (n > 0) { | |
// Make input smaller | |
spiral(d, n-1); | |
// Combine smaller step with current step | |
d.drawLine(100 + -20*n+20, 100 + -20*n, 100 + 20*n, 100 + -20*n); | |
d.drawLine(100 + 20*n, 100 + -20*n, 100 + 20*n, 100 + 20*n); | |
d.drawLine(100 + 20*n, 100 + 20*n, 100 + -20*n, 100 + 20*n); | |
d.drawLine(100 + -20*n, 100 + 20*n, 100 + -20*n, 100 + -20*n-20); | |
} | |
else { | |
d.drawLine(100, 100, 100, 80); | |
} | |
} | |
} | |
The HotelRoom class (#12)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Hotel { | |
public static void main(String[] args) { | |
HotelRoom room = new HotelRoom(); | |
room.setGuestName("Joe"); | |
HotelRoom room2 = new HotelRoom(); | |
System.out.println(room.getGuestName()); | |
room.setGuestName("Melissa"); | |
System.out.println(room.getGuestName()); | |
} | |
} | |
class HotelRoom { | |
private String guestname; | |
HotelRoom() { | |
} | |
String getGuestName() { | |
return this.guestname; | |
} | |
void setGuestName(String x) { | |
this.guestname = x; | |
} | |
HotelRoom foo() { | |
return this; | |
} | |
} | |
Polygons (#13)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Polygon | |
{ | |
// define fields here | |
private int numvertices; | |
Point[] vertices; | |
public Polygon(int numvertices) | |
{ | |
this.numvertices = numvertices; | |
this.vertices = new Point[numvertices]; | |
} | |
public boolean setVertex(int vertexnumber, Point p) | |
{ | |
System.out.println(numvertices); | |
if (vertexnumber >= numvertices) { | |
return false; | |
} | |
else if (vertexnumber < 0) { | |
return false; | |
} | |
else { | |
vertices[vertexnumber] = p; | |
System.out.println(java.util.Arrays.toString(vertices)); | |
return true; | |
} | |
} | |
public double getPerimeter() | |
{ | |
double total = 0.0; | |
for (int i=0; i<vertices.length; i++) { | |
int j = i + 1; | |
if (j == vertices.length) { | |
j = 0; | |
} | |
total = total + vertices[i].distanceTo(vertices[j]); | |
} | |
return total; | |
} | |
public double getArea() | |
{ | |
return 0.0; // replace this line with your code | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PolygonTest { | |
public static void main(String[] args) { | |
Polygon foo = new Polygon(3); | |
System.out.println(foo.getPerimeter()); | |
foo.setVertex(0, new Point(3, 1)); | |
foo.setVertex(1, new Point(3, 4)); | |
foo.setVertex(2, new Point(5, 1)); | |
} | |
} | |
For these you need Andrew's Point
class, which is in the zip
file on Sakai. (Resources -> Practice Problems).
Good luck everyone!
No comments:
Post a Comment