Download 14. A few warnings found when trying to compile: Warning: java.util

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
14. A few warnings found when trying to compile:
Warning: java.util.Stack is a raw type. References to generic type java.util.Stack<E> should
be parameterized
Warning: Type safety: The method push(java.lang.Object) belongs to the raw type
java.util.Stack. References to generic type java.util.Stack<E> should be parameterized
File: C:\Users\John\Desktop\IU\CSCI-C212\Deck.java [line: 26]
Warning: java.util.ArrayList is a raw type. References to generic type java.util.ArrayList<E>
should be parameterized
15. There is no switch.
16. import java.util.*;
public class StackDemo{
public static void main(String[] args) {
Stack stack=new Stack();
stack.push(new Integer(10));
stack.push("a");
System.out.println("The contents of Stack is" + stack);
System.out.println("The size of an Stack is" + stack.size());
System.out.println("The number poped out is" + stack.pop());
System.out.println("The number poped out is " + stack.pop());
//System.out.println("The number poped out is" + stack.pop());
System.out.println("The contents of stack is" + stack);
System.out.println("The size of an stack is" + stack.size());
}
}
17.
import java.util.*;
class ArrayListDemo {
public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
System.out.println("Initial size of al: " +
al.size());
// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " +
al.size());
// display the array list
System.out.println("Contents of al: " + al);
// Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " +
al.size());
System.out.println("Contents of al: " + al);
}
}
18. With an ADT, a single declaration allows you to define several different types later to fit your
needs.
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 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()));
}
}
22. No you cannot instantiate class Shape because it is abstract.
23. It serves as a super class, so other classes can extend it and have the same properties.
24.
import java.util.*;
class hw7Test {
public static void main(String[] args) {
Circle c1 = new Circle(2, 5, 3);
Circle c2 = new Circle (3, 3, 7);
Rectangle r1 = new Rectangle (3, 5, 8, 7);
Rectangle r2 = new Rectangle (3, 2, 5, 4);
List<Object> list = new ArrayList<Object>();
list.add(c1);
list.add(c2);
list.add(r1);
list.add(r2);
for(int i = 0; i<list.size(); i++) {
Object ob = list.get(i);
if(ob instanceof Circle) {
Circle cc = (Circle)list.get(i);
System.out.println (cc.radius);
}
}
}
}
25. Instance variables referred by one: 1.
Instance methods referred by one: 2 (set, get).
Instance variables pointed to by reference one: 2.
Instance methods pointed to by reference one: 2 (overrides previous class because same name).
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.(int) length() – This returns the length of this string.
29. (char) charAt(int index) – Returns the char value at specified index.
30. (boolean) endsWith(String, suffix) – Test if this string with the specified suffix, startsWith(String
prefix) – Tests if this string starts with the specified prefix, or startsWith(String prefix, int toffset) –
Tests if the substring of this string beginning at the specified index starts with the specified prefix.
31. (string) substring(int beginIndex, int endIndex) – Returns a new string that is a substring of this
string.
32. (String)toLowerCase() – Converts all of the characters in this String to lower case using the rules
of the default locale, or toLowerCase(Locale locale) – Convers all of the characters in this String to
lower case using the rules of the given Locale.
toUpperCase() – Converts all of the characters in this String to upper case using the rules of the
default locale, or toUpperCase(Locale locale) – Convers all of the characters in this String to upper
case using the rules of the given Locale.
33. (String) trim – Returns a copy of the string, with leading and trailing whitespace omitted.
34. (Static String)









valueOf(boolean b) – Returns the string representation of the boolean argument.
valueOf(char c) - Returns the string representation of the char argument.
valueOf(char[] data) - Returns the string representation of the char array argument.
valueOf(char[] data, int offset, int count) - Returns the string representation of a specific
subarray of the char array argument.
valueOf(double d) - Returns the string representation of the double argument.
valueOf(float f) - Returns the string representation of the float argument.
valueOf(int i) - Returns the string representation of the int argument.
valueOf(long l) - Returns the string representation of the long argument.
valueOf(Object obj) - Returns the string representation of the Object argument.
35. Strings are constant; their values cannot be changed after they are created. But, String buffers
support mutable strings. Because String objects are immutable they can be shared.
36. (StringBuffer)
 insert(int offset, Boolean b) - Inserts the string representation of the boolean argument into
this sequence.
 insert(int offset, char c) - Inserts the string representation of the char argument into this
sequence.
 insert(int offset, char[] str) - Inserts the string representation of the char array argument into
this sequence.
 insert(int index, char[] str, int offset, int len) - Inserts the string representation of a subarray
of the str array argument into this sequence.
 insert(int dstOffset, CharSequence s) - Inserts the specified CharSequence into this sequence.
 insert(int dstOffset, CharSequence s, int start, int end) Inserts a subsequence of the
specified CharSequence into this sequence.
 insert(int offset, double d) - Inserts the string representation of the double argument into this
sequence.
 insert(int offset, float f) - Inserts the string representation of the float argument into this
sequence.
 insert(int offset, int i) - Inserts the string representation of the second int argument into this
sequence.
 insert(int offset, long l) - Inserts the string representation of the long argument into this
sequence.
 insert(int offset, Object obj) - Inserts the string representation of the Object argument into
this character sequence.
 insert(int offset, String str) - Inserts the string into this character sequence.
37. (StringBuffer) delete(int start, int end) – Removes the characters in a substring of this sequence.
38. (StringBuffer)deleteCharAt(int index) – Removes the char at the specified position in this
sequence.
39. (StringBuffer)
 append(boolean b) - Appends the string representation of the boolean argument to the
sequence.
 append(char c) - Appends the string representation of the char argument to this sequence.
 append(char[] str) - Appends the string representation of the char array argument to this
sequence.
 append(char[] str, int offset, int len) - Appends the string representation of a subarray of the
char array argument to this sequence.
 append(CharSequence s) - Appends the specified CharSequence to this sequence.
 append(CharSequence s, int start, int end) - Appends a subsequence of the specified
CharSequence to this sequence.

append(double d) - Appends the string representation of the double argument to this
sequence
 append(float f) - Appends the string representation of the float argument to this sequence.
 append(int i) - Appends the string representation of the int argument to this sequence.
 append(long lng) - Appends the string representation of the long argument to this sequence.
 append(Object obj) - Appends the string representation of the Object argument.
 append(String str) - Appends the specified string to this character sequence.
 append(StringBuffer sb) - Appends the specified StringBuffer to this sequence.
40. ArrayList<Integer> intList = new ArrayList<Integer>(); intList.add(1);
intList.add(2);
41. class ScalableZ {
public static void main(String[] args) {
int size = 5;
boolean z[][] = new boolean[][];
for(i=0; i<size; i++)
for(j=0; j<size; j++)
if ( row == 0 || row == size-1 ||
row+col==size-1 ||
row==size/2 && col >= size/4 && col <= 3 * size / 4 ) {
[z][i][j] = true
} else {
[z][i][j] = false
}
for(i=0; i<size; i++)
for(j=0; j<size; j++)
if([z][i][j]) {
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
}
42.Yes, by using long start = System.currentTimeMillis(); and long end = System.currentTimeMillis()
it times it in milliseconds.
43. import java.util.*;
class sortInts2 {
public static void main(String[] args){
Integer[] b = new Integer[args.length];
long start = System.currentTimeMillis();
for (int i = 0; i < b.length ; i++) {
b[i] = Integer.parseInt(args[i]);
}
String c = Arrays.toString(b);
System.out.println(c);
Arrays.sort(b);
long end = System.currentTimeMillis();
System.out.println("This algorithm took " + (end - start) + " milliseconds");
System.out.println(Arrays.toString(b));
}
}
44. To make an instance of the same class.
45. Every number is the sum of the previous 2 numbers.
46. class MyThread extends Thread {
String like;
int time;
public MyThread (String like, int time) {
this.like = like;
this.time = time;
}
public void print() {
System.out.println("I like" + like);
}
public void run() {
Thread t = Thread.currentThread();
print();
while(t == this) {
try {
Thread.sleep(time*60000);
}
catch(InterruptedException e) { }
}
}
public static void main(String[] args) {
MyThread A=new MyThread (" coffee", 2);
MyThread B=new MyThread (" tea", 3);
MyThread C=new MyThread (" soup", 6);
try { A.run();
B.run();
C.run();
}
catch(Exception ex) {
System.out.println(ex);
}
}
}
47. This allows you to give classes the ability to thread off from the main program yet not be
locked in to inheriting the Thread class.
48. java.util.Vector; java.util.Stack; java.util.LinkedList; java.util.Hashtable
49. Warning: java.util.Hashtable is a raw type. References to generic type java.util.Hashtable<K,V>
should be parameterized
Warning: Type safety: The method put(java.lang.Object, java.lang.Object) belongs to the raw type
java.util.Hashtable. References to generic type java.util.Hashtable<K,V> should be parameterized
50. A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal
for that method to make a field-for-field copy of instances of that class. Attempts to clone instances
that do not implement the Cloneable interface result in the exception CloneNotSupportedException
being thrown.