20111029_0853PM (1485095)/Homework 7

advertisement
Rohan Warriar
14. 8 errors, all have to do with a method that references a generic type that should be parameterized.
15.
16.
// create a new Stack object
Stack s = new Stack();
// throw some values on the stack
s.push(new Integer(5));
s.push(new Character('X'));
s.push(new Float(5.5));
s.push(new Integer(10));
s.push(new Double(Math.PI));
s.push(new Boolean(true));
// unwind the stack, printing each value along the way
while(!s.empty())
{
System.out.println(s.pop());
}
17.
18. Abstract data types (ADT) are models for certain classes of data structures that have similar behavior
19. class Point {
int x, y;
Point() {
this(0, 0);
}
Point (int x, int y) {
this.x = x;
this.y = y;
}
double distanceTo(Point other) {
double dx = this.x - other.x;
double dy = this.y - other.y;
return Math.sqrt( dx * dx + dy * dy );
}
static public void main(String[] args){
Point a = new Point(0, 3);
Point b = new Point(4, 0);
System.out.println(a.distanceTo(b));
}
}
20. class Line {
Point a, b;
Line(Point a, Point b) {
this.a = a;
this.b = b;
}
String report() {
Rohan Warriar
return "Line between " + this.a.report() + " and " + this.b.report();
}
double length() {
return this.a.distanceTo(this.b);
}
}
21. class Triangle {
Line a, b, c;
Triangle (Point a, Point b, Point c) {
this.a = new Line(a, b);
this.b = new Line(a, c);
this.c = new Line(b, c);
}
double area() {
double s = (this.a.length() + this.b.length() + this.c.length()) / 2;
return Math.sqrt(s * (s - a.length()) *
(s - b.length()) *
(s - c.length()));
}
public static void main(String[] args) {
Triangle a = new Triangle(new Point(0, 0),
new Point(0, 3),
new Point(4, 0));
System.out.println(a.area()); // prints 3 * 4 / 2 (that is: 6 (six))
}
}
22. No
23. it creates a class that has two variables that are common to both rectangle and circle. That is, both
rectangle and circle use a point that can be defined in shape so that it does not have to be defined each
time in circle and rectangle.
24. import java.util.*;
import java.text.*;
abstract class Shape implements Sortable<Shape> {
int x, y;
Shape(int x, int y) {
this.x = x;
this.y = y;
}
public boolean comesAfter(Shape other) {
if (this.area() > other.area()) return true;
return false;
}
abstract double area();
Rohan Warriar
}
class Circle extends Shape {
int r;
Circle(int x, int y, int r) {
super(x, y);
this.r = r;
}
double radius() {
return r;
}
public String toString() {
NumberFormat formatter = new DecimalFormat("#0.00");
return "Circle:" + formatter.format(radius());
}
}
class Rectangle extends Shape {
int width, height;
Rectangle(int x, int y, int width, int height) {
super(x, y);
this.width = width;
this.height = height;
}
}
class Two {
public static void main(String[] args) {
ArrayList<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Circle(0, 0, 1));
shapes.add(new Circle(0, 0, 2));
shapes.add(new Circle(0, 0, 3));
shapes.add(new Rectangle(0, 0, 3, 12));
shapes.add(new Rectangle(0, 0, 9, 3));
shapes.add(new Rectangle(0, 0, 6, 2));
System.out.println(shapes);
}
}
25. one instance variable and method
26. http://download.oracle.com/javase/6/docs/api/java/lang/Object.html
27. http://download.oracle.com/javase/6/docs/api/java/lang/String.html
28. returns the length of the string
29. returns the character at the given index of the string
30. endsWith tests if the string ends with the specified index
startsWith tests if the string begins with the specified prefix
31. substring returns a new string that is a substring of the given string
Rohan Warriar
32. toLowerCase converts all of the characters in the String to lower case.
toUpperCase converts all of the characters in the String to upper case.
33. returns a copy of the string with leading and trailing whitespace omitted
34. returns the string representation of the Boolean argument
35. StringBuffer and String are the same except StringBuffer can be modified
36. insert method adds the characters at a specified point of the string
37. delete removes the characters in a substring of the sequence
38. deleteChar removes the character at the specified position in the sequence; the sequence is
shortened by one char
39. append method adds characters to the end of the buffer
http://download.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
40. int[] anArray = new int[i] where i is the number of indexes wanted in the array.
41.
class Z {
public static void main(String[] args) {
int size = Integer.parseInt(args[0]);
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if ( row == 0 || row == size-1 || row+col==size-1 || row==size/2 && col >= size/4 && col <= 3 * size
/4){
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
42. Yes, it uses the current time before the loop starts and then the time after the loop ends and finds
the difference between the two times.
43.
class Arrays {
public static void main(String[] args) {
int[] numbers = new int[5];
numbers [0] = 1;
numbers [1] = 3;
numbers [2] = 2;
numbers [3] = 4;
numbers [4] = 1;
long start = System.currentTimeMillis();
Arrays.sort(numbers);
for (int i = 0; i < numbers.length - 1; i++){
System.out.println(numbers[i]);
}
long end = System.currentTimeMillis();
Rohan Warriar
System.out.println((end - start) + " milliseconds");
}
public static void sort(int[] n) {
boolean sorted = false;
while (! sorted) {
sorted = true;
for (int i = 0; i < n.length - 1; i++) {
if (n[i] > n[i+1]) {
int a = n[i];
n[i] = n[i+1];
n[i+1] = a;
sorted = false;
}
}
}
}
}
import java.util.Arrays;
class Sort{
public static void main(String[] args){
int[] lengths = {1, 3, 4, 2, 1};
long start = System.currentTimeMillis();
Arrays.sort(lengths);
for (int i = 0; i < lengths.length - 1; i++){
System.out.println(lengths[i]);
}
long end = System.currentTimeMillis();
System.out.println(Arrays.toString(lengths));
System.out.println((end - start) + " milliseconds");
}
}
At first, I did not inclue the for loop, and both read at 0 milliseconds. After I included the for loop, both
outputted 52 milliseconds. Both take the same amount of time.
44. cloneable is a public interface used to override the Object clone method. It contains no methods.
45. the program prints three numbers for each sequence. The first and second numbers are added to
produce the third. The program keeps going until it is forced to stop. Running it multiple times, it seems
it doesn’t matter what order it is printed, it starts at B sometimes and C other times, but the Fibonacci
sequence if evident in all of them, proving it runs correctly.
47. the Runnable Interface gives classes the ability to thread off from the main program yet not be
locked in to the inheriting class.
48. java.util.Vector = java.util.Arrays
Rohan Warriar
java.util.Stack = java.util.AbstractList
java.util.LinkedList = java.util.ArrayList
49, 50. I have no idea how to upload the code to the Tomcat server.
Download