20111029_0410PM (1483640)/homework seven

advertisement
Jacob Senne jhsenne
Homework 7
14. First warning you get is an unchecked call with the program.
15. After you fix with the indicated switch it gives you an error saying that the constructer Card
cannot be applied given the types and the argument lists differ in length.
16. Stacks allows you to add an object to a list and sets it at the end it also allows you to remove
a certain name or object in the list by calling its name.
17. List is an ordered collection that has precise control over where in the list the elements go
and you have complete control over the list and are allowed to search and change elements in the
list.
18. An abstract data type is a mathematical model for a certain class of data structures that have a
similar behavior. It works only in the constraints given by the program.
19. class Point {
double x, y;
Point(double x, double y) { this.x = x; this.y = y; }
Point() { this(0, 0); }
static Point origin = new Point();
double distanceTo(Point other) {
double dx = this.x - other.x;
double dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
20. class Line {
Point a, b;
double length() {
return a.distanceTo(b);
}
Line(Point u1, Point u2) {
this.a = u1;
this.b = u2;
}
}
21. class Triangle {
Line a, b, c;
Triangle (Point a, Point
this.a = new Line(a,
this.b = new Line(a,
this.c = new Line(b,
}
b, Point c) {
b);
c);
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()));
}
}
22. Yes you can instantiate this class but only creating the objects Circle and Rectangle.
23. You can and the purpose is to allow you to put more shapes in the class so you can add more
like a triangle.
24. class Eighteen {
public static void main(String[] args) throws Exception {
Scanner a = new Scanner(new File(args[0]));
ArrayList<Object> c = new ArrayList<Object>();
while (a.hasNextLine()) {
String line = a.nextLine();
if (line.trim().equals("")) { } else {
Scanner b = new Scanner(line);
String what = b.next();
if (what.equals("Circle")) {
System.out.print(b.radius);
}
}
}
25.This has 2 instance variables inside of it and it has 2 instance methods in this program. Im not
quite sure what this next part is asking but, If it is asking the same questions it would be the same
answers because it is giving you two objects and has only 1 object method because there is only
one way shown to give you the answer.
26. The URL for java.lang.object gives you many useful class fields and methods. Among them
are provided by the system class are standard input and standard output and error output streams.
Also to input it, it is called by java.io
27.This is also called by java.io. In this string is just called upon and used regularly like you
would otherwise. It allows you to call it and change it in different ways.
28. string.length(), the length method takes a string that you define and returns the length of the
string.
29. charAt( int), charAt takes an integer, and returns the character at that index.
30. endsWith and startsWith have the same signature, filename.endsWith(string) it checks to see
if the file ends with the given string.
31. substring(int, int), substring takes two integers a beginning index and ending index, and
returns a substring from the two indicies.
32. Once again both have the same signature. String.toLowerCase(). The method takes a string
and passes it through the method, it returns the string with all letters lower cased.
33. string.trim(). Trim will take a string and return the string with all whitespace deleted from
the string.
34. valueOf(<primitive>), valueOf takes any primitive type that is not a string, and convets it
into a string.
35. Stringbuffer unlike string, is a mutable object meaning that its contents can be modified
directly.
36. stringBuffer.insert(int, string), insert will take a string Buffer and will place the defined
string at the index defined as the integer.
37. stringBuffer.delete(int, int), the delete method takes two integers, a start and an end, and
returns the string with all values between the two indicies deleted.
38. stringBuffer.deleteCharAt(int), deleteCharAt takes a string buffer and returns the string with
the character at the index defined as integer deleted.
39. http://download.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html
String1.append.string2, append takes two strings, and concacanates them together.
40. ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
for (int i : intList)
System.out.println(i);
41. ArrayList<Boolean> booleanList = new ArrayList<Boolean>();
for (i = 0 ; i < size.booleanList ; i++){
if (get.booleanList(int i) = true ){
booleanList.add(int i, “* “);}
else {
booleanList.add(int i, “ “);
}
}
42. Yes it does calculate the time it takes. It does this by using the System.currentTimeMillis. It
get the current time when the program starts and the stop is when it ends, then subtract the end
time from the start.
43. It is faster to use the built in method or Arrays.sort. I set them up by using the
System.currenTimeMillis around each of the method and ran them with the same arrays used. I
did this for both type of sort and it gave me the times.
44. The cloneable method is a public interface that you can implement in a class if you want to
override the Object clone method. It makes it so a clone of an object can make a copy of
instances of the class.
45. This program starts by creating spots for the previous, current and a temp. The first values
are created and are 1 and 1. Then it stores the current into the temp and makes the current
previous + current and then it finally makes the previous the temp. It does this continually until
the user stops the program.
46. Thread Coffee = new Thread (“I like Coffee!”);
Thread Tea = new Thread (“I like Tea!”);
Thread Soup = new Thread (“I like Soup!”);
Coffee.start();
Tea.start();
Soup.start();
Coffee.sleep(2000, 0);
Tea.sleep(3000, 0);
Soup.sleep(6000, 0);
47. The runnable interface should be implemented by any class whose instances are intended to
be executed by a thread. This interface is designed to provide a common protocol for objects that
wish to execute code while they are active. This is needed so that we can execute code onto
threads that are still running and haven’t been stopped yet.
48. Vector = java.util.Arrays (ArrayList)
Stack = ArrayList.add
Linked List = ArrayList.add and ArrayList.get
Hash table = java.util.Hashtable
49. The compiler doesn’t know what type of objects to expect when trying to compile this code.
imageTable = new Hashtable<String, String> (n);
private Hashtable<String, String> imageTable;
I added the String part to make it work.
50. http://www.cs.indiana.edu/classes/a348/t540/spr2002/lectures/HashTest.html
I couldn’t get the site to get set up on dreamweaver
Download