Douglas Wolfinger

Ranch Hand
+ Follow
since Aug 28, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Douglas Wolfinger

I am using a class ListArrayBased to define the add() and get() methods. I would like to know how to retrieve the first item. The items are added from the list position of 1, and get() throws an exception if I specify get(0).
The purpose of the class is to sum the items in the list.

[ September 18, 2002: Message edited by: Douglas Wolfinger ]
22 years ago
There are substantially more c/c++ jobs than java jobs on dice.com for San Francisco.
23 years ago
My program that was calling an exception wasn't working. But it was because I forgot to put try { catch() {} } around the method call in main(). So when I put the try-catch in there and declared the method with the superclass exception and threw a subclass, it worked. I think throws is more confusing than meets the eye initially. Thanks again!
Question 9 in the Sample Test asks 'which method declarations would be suitable to the compiler?'. These methods are throwing an exception. BadTasteException is abstract, and extends Exception. BitterException and SourException both extend BadTasteException.
Here is choice a.
a) public void eatMeth() throws BadTasteException
The answer indicates that choice 'a' is one of the correct answers. But if BadTasteException is abstract it can't be instantiated in the 'throw new' statement. Is it possible to throw an abstract exception? Thanks!
yep, you beat me to it. I was coming back in to say that I put diagnostic prints in the try blocks, and of course they printed. Originally I thought the long value I put in the wait(5000) would slow it down, but it didn't, so I thought the waits() weren't being executed.
Thank you, much appreciated! I haven't had much time to study your code, which is considerably different than my Consumer4.java. (It's interesting that you're assigning a String the value of the method retrieveMessage().) However, wait() never gets called in either Mailbox method, same as in my version. And that whole section of ch. 7 was about wait() and notify().
They explain the concepts well, but it seems strange that they would include fragmented code that doesn't actually run, if that is the case. I'm sure I have come across a program with wait() and notify() that runs, so I'll look for such a program and play with it.
The version of Mailbox I have posted below is the version the RHE authors say is the complete Mailbox class. Since it is a monitor class, I want to execute the synchronized methods with threads. But Thread.class doesn't have these methods so I can't just say
Thread t = new Thread();
Thread t2 = new Thread();
t.retrieveMessage();
t2.storeMessage("a message");
So, as shown in my previous message, I've altered the version of Mailbox the authors say is complete, and made it a Runnable and put in a run() method. In Consumer, I pass the Runnable object into 2 thread instances, and use those to start run() in Mailbox. But it still doesn't work like a monitor should because wait() isn't being executed in either method. I would like to know how to execute the Mailbox code below with threads. Thanks again!
Thanks so much for the reply.
This is the closest I have come to getting it to do what I want. It goes back and forth a couple times between the three methods in Mailbox3, but wait() never gets called in either of them. I also made a slightly altered version where the message is passed into another method, but control never escapes from the wait() in meth2().
I think the intent by the authors was to have both meth1() and meth2() be called by threads. And if my run() method is in Consumer4, I don't know how to call Mailbox3's methods without simply calling them with an instance of Mailbox3. In that case, they aren't threads, they're just ordinary method calls.
I'm not whining, just explaining myself.
23 years ago
<b>Why don't I move this to the Thread Forum where they discuss stuff like this all the time. </b>
You mean like in my other topic in that forum entitled 'Question on thread in RHE', to which nobody has responded since Friday? It would be nice to atleast be informed on what is wrong with the question...
23 years ago
Why don't I move this to the Thread Forum where they discuss stuff like this all the time;
You mean like in my other topic 'Question on thread in RHE', where I'm asking the same question, with no responses? If I could just get an idea of what is wrong with the question, I'll alter it in whatever way necessary. Thanks.
[ February 18, 2002: Message edited by: Douglas Wolfinger ]
Hi,
Page 214 in RHE has the code for Consumer3, but the code in that version of the program isn't intended to call properly formed thread methods. Below, is my version of Consumer3 calling the methods in Mailbox3.
How am I going to call the synchronized methods in Mailbox3? I can't call them with an instance of Thread because Thread doesn't have those methods. It's no use to make Mailbox3 a Runnable because it doesn't use the run() method. And I can't use the run() method because run() won't accept anything passed into it.
Thanks!
import java.lang.*;
public class Consumer3 extends Thread {
Mailbox3 m3 = new Mailbox3();
Thread t = new Thread(m3);
Thread t2 = new Thread(m3);
public void run() {
t.meth2();
t2.meth1("hey everybody");
}
public static void main(String args[]) {
Consumer3 c3 = new Consumer3();
c3.start();
}
}
import java.lang.*;
class Mailbox3 implements Runnable {
private boolean request;
private String message;
public void run();
public synchronized void meth1(String message) {
while(request == true) {
try {
wait();
} catch (InterruptedException ie) {
}
}
request = true;
this.message = message;
notify();
}
public synchronized String meth2() {
System.out.println("I'm in meth2()");
while(request == false) {
System.out.println("request = this in meth2() " + request);
try {
wait();
} catch (InterruptedException e) { }
}
request = false;
notify();
return message;
}
}
Hi,
I just finished a Roundup session. Question #223 asks:
True or False. In a switch statement, the argument to the case label can be a var which can fit within an int.
Answer: False. The case argument must be either an int literal, or an int-compatible var which is a constant (i.e., static final).
I'm confused. The byte in this program is not a constant, but can fit within an int, and it works.
class Switch2 {
byte x = 0;
Switch2() {
switch(x) { }
}
public static void main(String args[]) {
new Switch2();

}
}
I posted that same question in Beginners, but I can't find it. I understand that Swing has a really neat way of dealing with this.
23 years ago
Thanks, I was casting the value made by Math.random() to an int which, as you pointed out, always leads to a value of 0! The problem I was having that each occurence after the one passed to the method was 0. So I moved the (int) cast outside the whole calculation. And I'll make it more organized with a calculation method and display method, as you suggested.
23 years ago