20111029_0831PM (1484975)/HomeWork7

advertisement

14. Compile the code. What warning do you get?

When trying to compile CardTest.java, the following message is returned to the command line:

Note: ./Deck.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

Apparently, something must be fixed in Deck.java

15. Compile with the indicated switch. What seems to be the problem?

One of the problems seems to be that Stacks haven’t been specified. We don’t know what “kind” of Stack this is. We would need Stack<Data Type> x = new Stack<Data

Type>(); in order to specify.

16. Find the minimal comprehensive example with Stacks in the online tutorial .

I couldn’t find an online tutorial with stacks on that link. So I used oracle’s online documentation instead: http://download.oracle.com/javase/6/docs/api/java/util/Stack.html

Deque<Integer> stack = new ArrayDeque<Integer>();

17. Find the minimal comprehensive example with Lists in the online tutorial .

I couldn’t find an online tutorial with stacks on that link. So I used oracle’s online documentation instead: http://download.oracle.com/javase/tutorial/collections/interfaces/list.html

public interface List<E> extends Collection<E> {

// Positional access

E get(int index);

E set(int index, E element); //optional

boolean add(E element); //optional

void add(int index, E element); //optional

E remove(int index); //optional

boolean addAll(int index,

Collection<? extends E> c); //optional

// Search

int indexOf(Object o);

int lastIndexOf(Object o);

// Iteration

ListIterator<E> listIterator();

ListIterator<E> listIterator(int index);

// Range-view

List<E> subList(int from, int to);

}

18. What is an Abstract Data Type (ADT)? Is there anything about it in the online tutorial .

Once again, I couldn’t find ADT in tutorial.

( http://www.answers.com/topic/abstract-data-type )

An abstract data type is an object that contains a “carrier set” and collection of operations that manipulate them. The simplest example that I know of is the Integer class.

19. A Point is a pair of two integer coordinates. Define class Point. class Point {

int x, y;

Point(int x, int y) {

this.x = x;

this.y = y;

}

}

20. A Line is a Point (think of it as origin) with a target (another Point). Define class Line. class Line {

Point a, b;

Line(Point a, Point b) {

this.a = new Point(a.x, a.y);

this.b = new Point(b.x, b.y);

}

}

21. A Triangle is a Line with an extra Point not on it. Define Triangle class Point {

Line a;

Point b;

Triangle(Line c, Point d) {

if (! ((c.a.x == d.x) || (c.a.y == d.y) || (c.b.x == d.x) || (c.b.y == d.y))) {

this.c = new Line(Point c.a, Point c.b);

this.b = new Point(d.x, d.y);

}

}

}

22. Consider the following code: Can you instantiate class Shape ?

No.

23. If not, what is the purpose of its constructor?

It allows for its subclasses, Circle and Rectangle to call its super class to construct

Circle and Rectangle.

24. Write a method that

 receives an ArrayList<Shape> goes through it and identifies the Circle objects for each one it prints the size of its radius instance variable

static void circleSize(ArrayList<Shape> a) {

for (int i = 0; i < a.size(); i = i + 1) {

if (a.get(i) instanceof Circle) {

System.out.println(((Circle)a.get(i)).radius);

} //end if

} //end for loop

} //end circleSize method

25. Finally consider the following code:

How many different instance variables does the object referred by one have inside it?

Just one.

How many different instance methods does the object referred to by one have inside it?

Two.

Same questions for the object pointed to by reference one . Support your answers with running code.

Exactly the same. I have modified the code to show that the two objects perform exactly the same functions. As a matter of fact, even if I remove the “extends” portion, they do the exact same thing. class One {

int x;

void set(int x) {

this.x = x;

}

int get() {

return this.x;

}

} class Two extends One {

int x;

void set(int x) {

this.x = x;

}

int get() {

return this.x;

}

} class Three {

public static void main(String[] args) {

Two two = new Two();

One one = new One();

one.set(5);

two.set(3);

System.out.println( two.get() );

System.out.println( one.get() );

}

}

26. What's the URL for the java.lang.Object

class in the Java 1.6 API documentation? http://download.oracle.com/javase/6/docs/api/java/lang/Object.html

27. What's the URL for the java.lang.String

class in the Java 1.6 API documentation? http://download.oracle.com/javase/6/docs/api/java/lang/String.html

Identify and explain in the above docs the signatures for the following methods:

28. The length

instance method of class java.lang.String

Method Signature: public int length().

Returns the length of the this string.

29. The charAt

instance method of class java.lang.String

Method Signature: public charAt(int index)

Returns the char value at the specified index.

30. The endsWith

and startsWith

instance methods of class java.lang.String

Method Signature: public boolean endsWith (String suffix)

Method Signature: public boolean startsWith(Stirng suffix)

Tests if this string ends or starts with the specified suffix, respectively.

31. The substring

instance method of class java.lang.String

Method Signature: public String substring(int beginIndex) or,

Public String substring(int beginIndex, int endIndex) if only one argument: returns a new string that is a substring of this string.

If two arguments: Returns a new string that is a substring of this string.

32. The toLowerCase

and toUpperCase

instance methods of class java.lang.String

Method Signature: public String toLowerCase(Locale locale)

Method Signature: public String toUpperCase(Locale locale)

With no argument: Converts all of the characters in this

String

to lower case using the rules of the default locale.

With argument: Converts all of the characters in this

String

to lower case using the rules of the given

Locale

.

33. The trim

instance method of class java.lang.String

Method Signature: public String trim()

Returns a copy of the string, with leading and trailing whitespace omitted.

34. The valueOf

instance method(s) of class java.lang.String

public static <Data Type> valueof(Data Type) (i.e. Object obj, char[] data)

Depending on the input, it returns the string representation of the argument.

For example, if an int is provided, then the method returns a string representation of the int.

35. What is the main difference between a

String

and a

StringBuffer

?

(

StringBuffer

is defined in the same package java.lang

as

String

).

String objects are immutable, whereas StringBuffer objects are.

Identify and explain the signature of the following methods of class

StringBuffer

:

36. insert

Method Signature: public StringBuffer insert(int offset, boolean b)

Inserts the string representation of the data type of the argument into the sequence.

37. delete

Method Signature: public StringBuffer delete(int start, int end)

Removes the characters in a substring of this sequence. A substring begins at the specified “start” and extends to the character index “end”.

38. deleteCharAt

Method Signature: public StringBuffer deleteCharAt(int index)

Removes the char at the specified position in this sequence. This sequence is shortened by one char.

39. append

Method Signature: public StringBuffer append(data type a)

Appends the string representation of the argument of some data type to the sequence.

40. How do you store values of type int

in an ArrayList? Write code for your answer.

You do so by specifying the type to the ArrayList – you use the Integer Object.

Import java.util.*; class One {

public static void main(String[] args) {

ArrayList<Integer> a = new ArrayList<Integer>();

a.add(5);

a.add(3);

System.out.println( a );

}

}

41. Suppose you want to store a scalable Z pattern into a 2D array of boolean

values.

(You know, each value stored is true

if an

*

is needed at that location, false

otherwise).

Suppose further that you want to use

ArrayList s for that. How would you do it? Please write some code. import java.util.*; class One { public static void main(String[] args) { int size = Integer.parseInt(args[ 0 ]);

//for loop to account for rows for ( int j = 0 ; j < size; j = j + 1 ) {

ArrayList<Boolean> a = new ArrayList<Boolean>();

//for loop to account for columns for ( int i = 0 ; i < size; i = i + 1 ) {

//Top and Bottom Row of Pattern if ((j == 0 ) || (j == size - 1 )) {

a.add( true );

System.out.print( " " + a.get(i) + " " );

} //end if

//Diagonal to draw Z else if ((j != 0 ) && (j != size - 1 ) && (i == size - j - 1 )) {

a.add( true );

System.out.print( " " + a.get(i) + " " );

} //end else if

//All else print false else {

a.add( false );

System.out.print( a.get(i) +

} //end else

} //end for loop

System.out.println();

" " );

} //end for loop

} //end main

} //end One

42. Run the

Sieve.java

program indexed here .

Does it calculate how long it takes to complete the task it's written for?

How does it do it?

Yes, it does so by using the currentTimeMillis() method.

43. Implement a sort method that relies on the sorted method you wrote for the previous homework.

Determine how fast it works on average using the tools from the previous question.

Then do the same for the

Arrays.sort(...)

method we used in the past.

Which one performs better on average? How did you set things up?

What did you measure and how?

(Your analysis need not be exhaustive, just illustrative of how one would in fact proceed).

44. In a few words describe what the java.lang.Cloneable

interface is and how it's to be used.

45. In a few sentences explain accurately how this program is designed to work.

46. Write the simplest program that creates three threads that print three different things.

For example while one thread says "I like coffee!" every 2 seconds, another might say "I like tea." every 3 seconds.

A third thread might state "I like soup!" every six seconds. Can a thread choose a random time to wait?

47. What is the purpose of the java.lang.Runnable

interface? Why do we need it?

48. What are today's equivalents for the four types listed above?

49. There's an example in the book of using class

Hashtable

.

Compile the code and explain the warning.

Without fixing anything upload the code to your Tomcat server.

Make the applet available on a page and indicate the URL here.

50. Change the code to maintain functionality while losing the compilation warning(s).

Download