Jump to content

albertlee

Senior Members
  • Posts

    1259
  • Joined

  • Last visited

Everything posted by albertlee

  1. Question 4: for formatting, is the format() method equivalent to printf()?
  2. Question 6: Given the fully-qualified class names: com.foo.bar.Dog com.foo.bar.blatz.Book com.bar.Car com.bar.blatz.Sun Which graph represents the correct directory structure for a JAR file from which those classes can be used by the compiler and JVM? What does the question mean??????? please help
  3. Question 3: public class Test{ public static void main(String[] ag) { for(int x=1; x<ag.length; x++){ System.out.print(ag[x]+" "); } } } As simple as the above code... first thing, what does it mean by "seperate command line invokations"? well, this is the question: two seperate line invokations: java Test java Test 1234 what is the result? well, I expect the first one to give no result, and the second one to give the result "1234"... but the real answer is no result and "234".....why??? I tried compile and run this code and my answer appears correct.. So, I think I might read the question wrongly? please help thx
  4. thanks for the explanation for question 1 Now, question 2: package geometry; public class Hypoteneuse{ [b]public[/b] InnerTriangle it = new InnerTriangle(); class InnerTriangle{ public int base;} } why cant I create an object of a class outside the package geometry that can reference to the variable base? although the class InnerTriangle is declared as default, but there is an object member of that type already created in the class and declared as public... Why can't an object of ANY class reference to the base variable then? please help thanks
  5. thanks Aeternus for your explanation.... But regarding your explanation on question 2: I still dont understand why this works. I can possibly parameterize List<? super Integer> with ArrayList<Number>, and then try to add elements of Object type. Clearly, this should fail, and why not the compiler show an error while the extends keyword, if used, does show an error? For example, this wont work as well, List<? extends Object> l = new ArrayList<Integer>(); l.add(new Number()); (1) l.add(new Integer()); (2) and as expected, both cases will fail... but with super keyword, only case (1) fails but case (2) will be OK.... this is what I mean.... please help thx
  6. again, question 1 class a{ void meth() throws Exception{throw new Exception();} //void meth() {System.out.println("hello from a");} } class b extends a{ void meth() {System.out.println("hello from b");} } public class Test{ public static void main(String[] ag) { [b]a[/b] o = new b(); o.meth(); } } I get a compilation error saying that the method's exception is not caught even though it has been overriden........ hope this shows what I mean clearer... plz help thx
  7. thanks... I finally get it.. end of Some Java Questions thread
  8. I also dont get question 2.... ok... I dont understand intrisincally why this is the case. this is just similar to extends keyword. I mean, you can still get the same error where extends keyword can yield. For example, with super, you define a collection: List<? super Integer> l = new ArrayList<Number>(); and you are trying to add an element of type Object... well, you should get a conversion error during runtime I suppose because the instance only allows elements of type Number or its subtypes, not Object, which is none of these...... do you get what I mean? please help thanks
  9. Secondly, for question 1.... class a{ [b]//void meth() throws Exception{throw new Exception();}[/b] void meth() {System.out.println("hello from a");} } class b extends a{ void meth() {System.out.println("hello from b");} } public class Test{ public static void main(String[] ag){ a o = new b(); [b]o.meth();[/b] } } how come the bold method, when uncomented, will be typecasted, while the other one underneath, wont?? This is confusing.... :-? plz help
  10. For question 2, Aeternus, I honestly have to say, you dont quite understand the concept of generics in Java fully. Well, simply, List<Object> l = new ArrayList<Object>(); l.add(new String("test"); wont compile because in Java's generics, a generic collection can basically only accepts 1 type of elements. And clearly, String class is not Object class, even though String is a subclass of Object. Try and compile it and you will see it wont work..... any way, really thanks for your help on this, but since you are majoring computer science, I think you can really give more information on question 2 again... please help thanks
  11. I still have problem with question 1... ok, I understand that there is the typecasting for the data member, therefore, the actual "i" being accessed is the one from Foo. However, can you really tell me, the reason why this is designed this way? I mean, why data member are typcasted while methods are overriden instead of typecasted. Why not make this issue consistent? , since we can access both of them with super keyword any way please help thx
  12. Aeternus, regarding the code sample you provided, I tried it, and it did not throw IllegalMonitorStateException at runtime..... yes, the program simply stops working because the current thread, main, which is the only reachable thread becomes not runnable as it cannot regain the object lock, but there is NO IllegalMonitorStateException.... please illustrate thanks
  13. Aeternus, thanks for your post regarding question 1. however it's still confusing. because the type of an object only indicates what data members and methods SHOULD it have, but the actual implementation/content of the data/methods should depend on the actual INSTANCE of the object. please help thanks
  14. 5) import java.util.*; class km{ public int i; public km(int i){this.i=i;} public boolean equals(Object o){return i==((km)o).i;} public int hashCode(){return i;} } public class MapIt{ public static void main(String[] rafre){ Set<km> set = new HashSet<km>(); km k1 = new km(1); km k2 = new km(2); set.add(k1);set.add(k1); set.add(k2);set.add(k2); System.out.print(set.size() +":"); k2.i=1; System.out.print(set.size() +":"); set.remove(k1); System.out.print(set.size() +":"); set.remove(k2); System.out.print(set.size()); } } the result shows : 2:2:1:1 my expectation is 2:1:0:0, because first, there are 2 unique elements in the set. Second, the i value in k2 has been changed to 1, and since according to the km's equal method, now k2 and k1 are now same object, so there should be 1 element left. Third and Forth, the element has been removed, so 0 element left....This is my reasoning. please help thanks
  15. oh...Aeternus, sorry for my mistake again.... the code has been changed again....
  16. 4) public static void main(String[] ag){ try{ File f = new File("f"); File g = new File(f, "g"); if(!f.exists()){ fw.createNewFile(); } if(!g.exists()){ g.createNewFile();} } catch(IOException ioe){System.out.println(ioe);} } I dont get it, why an IOException is fired at runtime from the createNewFile method? the exception said that the system cannot find the path specified... well, shouldn't that be the point of the createNewFile method? please help
  17. 3) import java.util.*; public class Test{ static public void main(String[] eargqe){ PriorityQueue<String> pq = new PriorityQueue<String>(); pq.add("a");pq.add("b");pq.add("c"); System.out.println(pq.poll()+" : "+pq.peek()); } } why isn't the output "a : b" but "b : c"? first, the poll method retrieves a and then removes it, then peek method retrieves b, simple as that, but why is this right? please help
  18. sorry, Aeternus, I have edited the post....the rest of my question remains constant please help thanks
  19. another Java question: 2) import java.util.*; public class Test{ static void d(List<? [b]super[/b] String> d){d.add("bla"); d.add("dsfew");} public static void main(String[] ag){ List l = new ArrayList(); d(l); Iterator i = l.iterator(); System.out.println(i.next()); System.out.println(i.next()); } } As simple as this, why does the bold part, if "super", produces no compilation error, while "extends" prodcues such error at 'add' method? to my expectation, both should yield compilation error because of the question mark, which indicates anything, so canot add any element to the list..... please help thanks
  20. As opposed to my "trivial java questions" thread, I will post all my in the near future Java questions which I think concern more to my understanding of Java programming language and computer science rather than just facts. So, I think it's better to seperate them. This is the head start question: 1) public class Foo{ public int a; public Foo(){a = 3;} public void add(){a+=5;} } public class Bar extends Foo{ public int a; public Bar(){a = 8;} public vid add(){a+=5;} } why does this code: Foo foo = new Bar(); foo.add(); [b]System.out.print(foo.a);[/b] show the result 3 instead of the expected 13? thanks
  21. I guess I will be asking java some Java questions, trivial ones, these few days... so in order to be consistent, I will post all my trivial Java questions here... starting with the first one 1) acocrding to this image Why doesn't the yield method put the thread into not runnable? clearly, the thread stops for a while and allows other threads to come in and execute.....
  22. thanks for ur response, Aeternus... Well, only 1 problem left... for the second question, the real answer is IllegalStateException.... can any one of you give me some examples when EXACTLY an IllegalStateException is fired? I searched through the Java offical site, and it only briefly defines it as an exception for an object or thread having an illegal state..... please help here thanks
  23. oh... the code tag of this forum doesn't have coloration thanks for the response any way, but... Aeternus, I think you really like to type or write, because, for example, your post can be written with 2 lines: the result is shown as what it's shown because the method "somethingsomething" is commented. Any way, I am not trying to be picky, but just a mutually benefitial opinion for both of us to save my time on reading and your time on typing. You are great after all, most of my java threads are answered by you.
  24. import java.io.*; class Food {Food(){System.out.print("1");}} class Fruit extends Food implements Serializable{ Fruit(){System.out.print("2");} } public class Banana2 extends Fruit { int size = 42; public static void main(String[] af){ Banana2 b = new Banana2(); try{ ObjectOutputStream oob = new ObjectOutputStream(new FileOutputStream("bla.txt")); oob.writeObject(b); oob.close(); ObjectInputStream oib = new ObjectInputStream(new FileInputStream("bla.txt")); Banana2 b1 = (Banana2) oib.readObject(); oib.close(); System.out.println(" restored "+b.size+" ");} catch(Exception e){System.out.println(e);} } } 1, why does the above code give me "121 restored 42" as the result instead of "12 restored 42" which is what I expected? Given: void wfs(){ Object o = new Object(); synchronized (Thread.currentThread()){ o.wait(); o.notify();; } } 2, what exception maybe thrown from the above code? this is actually a Java exam question. The possible answers are: a) IllegalStateException b) TimeoutException c) InterruptedException 3, what does the extra ";" symbol after the o.notify() method stand for? please help thanks
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.