BestJewSinceJC 700 Posting Maven

Seriously - if you can't figure out how to use code tags then read the stickies at the top of the forum that explain in detail how to do so. Or you could just quote one of the other members in here (such as Jocamps) who used code tags in their post, and it would show you exactly how to do it.

BestJewSinceJC 700 Posting Maven

I'm not trying to be rude, but Narue already had a pretty good explanation of at least one of your questions in a similar thread that was created in this forum recently (it should still be on the first page).

(The question):
"I think big-o describes the upper bounds of an algorithm's cost or something? With that, how do you describe and analyze an algorithm?"

As for amortized cost, amortized analysis deals with considering the worst case running time of a sequence of M operations. If you want an example of why this is useful look into splay trees big-oh cost versus their amortized cost.

BestJewSinceJC 700 Posting Maven

And what is your question? You said you can't figure out how to display the header only once. What do you mean by that? Because you said 'header' on the one hand, on the other hand, the only area where you are really displaying stuff is the JTextArea, but that is not a header. . please clarify your question & why you think it isn't working, what you want it to do, etc.

BestJewSinceJC 700 Posting Maven

I agree with you, addict. I found an interesting note on wikipedia as well: "Another notable difference is that static member classes cannot implement an interface, unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, it really has to be a singleton."

BestJewSinceJC 700 Posting Maven

where's ProductDB.inventory declared? you can't just start referencing variables in a function when they were declared in main. You might want to pass it as an argument or declare it somewhere else. :D

ProductDB is a completely different class than ProductApp, and inventory is a variable in ProductDB that is not declared as static. But he is treating the 'inventory' variable as if it was static, which is why the compiler is complaining. The solution is to either declare inventory as a static variable, or to create a ProductDB object (which he already did, in main, as you said), pass it into the method, and use it to access the inventory variable.

BestJewSinceJC 700 Posting Maven

Oh, another thing - you printed the diameter twice, but your project description asks for you to print it once.

OrangeNaa commented: Thank you very much for the help!! Not only did you really help me out, but you came back and made sure you gave me the correct evaluation! It's very appreciated=) Take care!! +0
BestJewSinceJC 700 Posting Maven

But how does that help him learn anything. . ?

BestJewSinceJC 700 Posting Maven

Where object1 is the variable. I tried doing that, but i get an error ")" Expected on that line, i suspect that it is because anything that is expected in that first slot is expected inside of quotes.

Incorrect. Anything in that first argument has to amount to a String, just like the javadoc says for the drawString method. So you could put "I'm a String!" + object.toString() in that slot if you wanted to. Or you could even put "I'm a String!" + object, I think.

BestJewSinceJC 700 Posting Maven

It looks ok to me. Pseudocode doesn't really have a "standard" format (that I know of) - if an experienced programmer were writing that pseudocode, it would look a bit different - but I think you covered most of the steps, which is the important thing. A program wouldn't just "input radius" though. You need to prompt the user first, asking them to input the radius, then you need to read that value into the radius variable.

OrangeNaa commented: Thank you so much for your help! I thought no one would help me! =) +0
BestJewSinceJC 700 Posting Maven

You're going to have to choose a language and begin learning the basics of that language. Regardless of what language you choose, you will have to learn about loops such as for loops, while loops, do while loops, etc. And of course, if statements, if else statements, etc. Once you learn these constructs you won't have so much trouble with this problem.

BestJewSinceJC 700 Posting Maven

It seems like you already figured out how to get the Characters from the ArrayList and compare them to see if they are the same. Now all you need to do is write a for loop that prints from 1 . . n where n is the number of elements in your ArrayList. But once you get to the index where the Characters are that you were supposed to compare, you need an if statement to decide whether to print out the Character itself or whether to print out the index.

P.S. Masijade wasn't being rude, just straightforward. You seem to have the basic idea down, so if you know how to use a for loop and an if statement, you should be able to do this.

BestJewSinceJC 700 Posting Maven

Other things you should be aware of:


1. The two statements inside of your constructor do not do anything. You assigned inputMile equal to itself and then you assigned outputKm equal to itself. I think you meant to set inputMile to inputField and outputKm to outputField.

private Chapter165(JTextField inputField, JTextField outputField) 
    {
    	          this.inputMile = inputMile; 
                  this.outputKm = outputKm;
    }

2. If you used an IDE, such as Eclipse, a lot of your errors would be made apparent to you right off the bat. For example, the only reason I noticed the logical error in your code above is because I pasted your code into Eclipse and it immediately pointed out that your statement didn't do anything.

3. For example, other things that Eclipse pointed out to me about your code: you declared a variable named "text" inside of your if statement, but you never used that variable. Same thing goes for your variable "milesKm", which you declared in two different if statements but never used in either one.

4. You have statements in your code such as
"double input = Double.parseDouble(outputKm.getText());". If you read the javadoc for that method, you will see that if you pass in a String which cannot successfully be parsed as a double, (i.e. I passed it the String "///"), then it will throw an Exception. Currently, this Exception will crash your program, rendering it unusable. So you will need to read about "try catch" so that you can …

george21 commented: Thank you for all of your help. You explained it very well. +1
BestJewSinceJC 700 Posting Maven

You created an inner class that implements ActionListener, and you defined the actionPerformed method for that inner class. But when you make the following statements:

Chapter165 listener = new Chapter165(inputMile, outputKm);
inputMile.addActionListener(listener);
outputKm.addActionListener(listener);

"listener" is an Object of the class type Chapter165. But in the Chapter165 class, you did not implement the actionPerformed method, and so, your code is not working as you wanted. If you get rid of that inner class, and put your actionPerformed method that you currently have in your inner class into your Chapter165 class, I think it will work.

P.S. if you wanted to do it like you have your code right now, you would need to change the code I posted above to this:

Chapter165 listener = new Chapter165(inputMile, outputKm);
inputMile.addActionListener(new ConvertListener());
outputKm.addActionListener(new ConvertListener());

If you don't understand why that is, feel free to ask questions. You can also read about inner classes and I think you should read about Listeners on sun's tutorials a little more.

BestJewSinceJC 700 Posting Maven

Since Ezzaral already answered your question - doesn't your code have syntax errors? You didn't specify a type of Exception in the catch block i.e. catch(Exception e). PS, for things like this, usually the best way to find out is to run a small sample program and see what the output is, if you get errors, etc. Nothing wrong with posting it here, just saying.

BestJewSinceJC 700 Posting Maven

Black box testing would involve trying to break your code by giving it invalid inputs and making sure it handles them appropriately, as well as giving it valid inputs and making sure it comes up with the right result. So I do not think your code counts as black box testing. . however, that isn't to say your test is invalid, it just isn't black box IMO.

BestJewSinceJC 700 Posting Maven

RPS is an extremely simple game. . because of that, it isn't really necessary to do it OP style - what would your objects be? The Rock, Paper, and Scissors need not be Objects because they are better represented as Strings (in my opinion). I suppose you could make the Players themselves Objects and make takeTurn methods and getWinner methods. getWinner could return the name of the player who won or "tie" to keep the game going.

BestJewSinceJC 700 Posting Maven

James is right - you can create the JLabels in the class definition, but nothing further. In the future, you should use an array if you have any type of collection of Objects like you do above. For example, this code will do the same as yours does, but in a few lines:

JLabel[] blocks = new JLabel[100];
for (int i = 0; i < blocks.length; i++){
    blocks[i] = new JLabel();
    blocks[i].setOpaque(true);
    blocks[i].setBackground(MyBlue);
}
BestJewSinceJC 700 Posting Maven

I don't really see anything wrong. You said they are JPanels but they are JLabels. (You can set the background on either, so again, no problem). Do you mind posting your code?

BestJewSinceJC 700 Posting Maven

"What I am not checking for is a character response."

You can look into ASCII character values; in order to make sure that something is an integer (or is not a character), you would compare the character to a range of ASCII values which can be found on the table. This advice is less detailed than a lot of the other responses I see in this thread, so be warned that it isn't a solution but a useful thing to know that can help you build a solution.

BestJewSinceJC 700 Posting Maven

This isn't a website for hand holding. The information is readily available. Look it up. Figure it out.

BestJewSinceJC 700 Posting Maven

Yeah, I'm pretty sure it's because by calling repaint, you are calling paintComponent, which I believe clears the screen for you before drawing whatever it draws. So you'll either have to redraw everything that you want on the screen (which would require your programremembering what was there) or you'll have to edit paintComponent method so that it does not "erase" the screen. I could be slightly off in my advice here, but I will check a previous thread of mine that contains the correct info and I'll get back to you later tonight. (But I think this is right)

BestJewSinceJC 700 Posting Maven

Try to avoid giving people solutions unless it is impossible to help them without pointing out the exact code.

BestJewSinceJC 700 Posting Maven

WorkThread should not be running on the EDT. This indicates that WorkThread is not running in a separate thread - so the new Thread was not set up properly. Look up how to create a separate Thread on google. You should also look into Swing's WorkerThread class.

toucan commented: Good troubleshooting skills. +2
BestJewSinceJC 700 Posting Maven

Btw, the reason I'm having you do that is because if your main code is running on the Event Dispatch Thread, like it should be, and if your "WorkThread" is also, which it should not be, then it could be blocking the thread, preventing any of your mouse clicks from registering. So I'm trying to make sure your Threads are actually set up correctly as separate Threads and that the one is running on the EDT. Thanks for being so patient.

BestJewSinceJC 700 Posting Maven

"I had my event handler print a message every time it detected a mouse event and it only prints once"

Like I said, you should have at least two threads: one would be your WorkThread, and the other would be your main thread, which presumably would run your code on the Event Dispatch Thread. The main thread would be the one responsible for the event handling. You should make sure that your code is running on the Event Dispatch Thread. . you can do so by calling SwingUtilities.isEventDispatchThread() from within the thread. Your "WorkThread" should return false when you call that method, and your main thread should return true when you call that method. So put that line of code into both your main thread code and your WorkThread code and tell me what it returns for each of them.

BestJewSinceJC 700 Posting Maven

All you need to do to draw the circle in its final position is call repaint, set the coordinates of where you want the circle to be drawn, then draw the circle at that position. You'd put the method calls to do that inside of mouseReleased.

BestJewSinceJC 700 Posting Maven

"I can't interact with the GUI and trigger mouse events that should interrupt the WorkThread, even if it goes through multiple iterations of the while loop."

So you're saying that once the WorkThread starts running, your program will not trigger mouse events? Or that your program triggers mouse events, but these mouse events do not interrupt the WorkThread like they're supposed to?

BestJewSinceJC 700 Posting Maven

The best way to "translate the value of the element to the index" is to simply keep track of what index you are looking at. To do a "swap" (switch two values in the array) all you need are three pieces of information: The two indexes you want to swap, and a temporary variable to hold the value of the overwritten index.

So

array[5] = array[4];
array[4] = array[5];

Doesn't work because you just overwrote array[5]. So you'd do this instead:

int temp = array[5];
array[5] = array[4];
array[4] = temp;

That probably doesn't help.... feel free to ask some questions about sorting, we'll all be glad to help.

BestJewSinceJC 700 Posting Maven

I'm not sure that this is the issue, but just so you know, the while loop condition is only going to be checked after the method call that is inside the while loop completes. You probably knew that. Other than that, I'm not sure I understand what problem you're having. What you need to do is have a different thread (probably the main thread) detect the mouse event, then set a condition that lets the other thread know it's time to stop execution. The other thread should periodically check to see if it is time to stop execution.

BestJewSinceJC 700 Posting Maven

If you want to sort your list but do not want to write the sorting method yourself, then you can create an ArrayList, add each of your Integers to the ArrayList, then use Collections.sort(yourArrayList) which will sort it for you. For more information on those concepts, you can look at the Javadocs for each. Here's a small example

//This assumes you already have an array of ints
//called myIntArray set up.
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < myIntArray.length; i++){
    list.add(myIntArray[i]);
}
Collections.sort(list);

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html

However, I would strongly suggest that you learn how to write at least one simple sorting method yourself, this is the easiest way to do it but not the one that will help you learn the most.

BestJewSinceJC 700 Posting Maven

The method you must implement has the method header commandAction(Command, Displayable) whereas the method you wrote has the method header CommandAction(Command, Displayable). These are considered two different methods since Java is case sensitive, which is why you're getting the error. Also, keep in mind that commandAction follows the recommended syntax whereas your method, CommandAction, does not.

BestJewSinceJC 700 Posting Maven

If your inner classes are implemented in a way that does not cause a compiler error, I do not think you can get errors such as memory leaks or anything of that nature. However, when defining inner classes inside an abstract class, I'd be careful about what techniques and syntax are/aren't allowed.

BestJewSinceJC 700 Posting Maven

Good question, I'm actually not sure. I'm not a mod - it just makes it very hard to help people if things aren't in those code tags that color, format, and number the code. But yeah, go ahead and put errors in code tags as well.

BestJewSinceJC 700 Posting Maven

Post the code for your class in the code=java code tags and I'll help you solve this error. I've gotten similar errors before as well. If you don't know how to use code tags then read the sticky at the top of this forum.

BestJewSinceJC 700 Posting Maven

Use the setSize method. The preferred size is just that - preferred. The layout manager is probably ignoring your preference, whereas if you use setSize, you might have better luck. Oh, and set the size of the scroll pane, which contains the other object. If that doesn't work try setting both.

BestJewSinceJC 700 Posting Maven

php is not a programming language it is a scripting language.

BestJewSinceJC 700 Posting Maven

Yeah - you can put \n in any String Object and it will do the same thing. Also, click 'Mark as Solved' once people solve your issues or once you work them out on your own.

@ Sneaker:

No problem, I was just making sure the OP was aware.

BestJewSinceJC 700 Posting Maven

I'm not sure if you want to consider process different from process. or if you want to consider them the same thing. Which is it? If you create a Scanner, read in things one by one, put them into an ArrayList if they are unique words, and increment a counter for that word - then process and process. would be considered different since the Scanner by default would read in the words as "process" and "process." and a search of the ArrayList when you got to "process." would not match "process". If you want them to match, then strip punctuation and any special chars that you don't want from the end of every word.

PS nice help Jenn.

BestJewSinceJC 700 Posting Maven

You didn't explain what happy vs. unhappy is. And you didn't post your code in code tags.

BestJewSinceJC 700 Posting Maven

If you declare the variable as final, it will work. Otherwise you won't be able to access from within an inner class. And I'd be wary of taking sneaker's advice because I'm not sure that it is completely correct. You might want to do more research if you are concerned about the issue.

PS an IDE like Eclipse would have suggested to you that you should make the variable final, and all you would have had to do is click the link on their suggestion and it would have done it for you.

BestJewSinceJC 700 Posting Maven

You compiled it and had a problem? Then post your error including the line number. .

BestJewSinceJC 700 Posting Maven
JButton Reset = new JButton("Reset"); 
    centerpanel2.add(new JButton("Reset"));

Should be:

JButton Reset = new JButton("Reset"); 
    centerpanel2.add(Reset);

Again, follow coding conventions - it should be named "reset" not "Reset" - this would have made it easier to see this mistake. Anyway, simple mistake, your program should work now. You added the actionListener correctly, but you then created a completely different JButton which didn't have the actionListener attached. Anyway, what you should get out of this is to avoid assuming your problem lies in one area, always investigate all possibilities.

MasterGoGo commented: Fixed my problem +1
BestJewSinceJC 700 Posting Maven

Use the modulus operator, %. If you type modulus in google you can learn everything you need to know about it, and you'll see why you need to use it, so I won't explain further.

BestJewSinceJC 700 Posting Maven

MasterGoGo. . is there any reason you're using Panel, Frame, Button, etc instead of JPanel, JFrame, JButton, etc? Reason being because I believe the latter are more up to date. As another note, variable names are by convention lowercase for the first letter, for example names that follow convention: reset, theResetButton, otherButton, etc.

Oh: And it would probably be easy to help you with the NullPointerException, but you need to post in CODE=Java tags, because I don't know what line number your NullPointerException is referring to.

BestJewSinceJC 700 Posting Maven

Making variables global for the sake of easily doing something is usually a bad programming practice. Instead, you'd usually make get methods and set methods to get your data and to modify it.

BestJewSinceJC 700 Posting Maven

Whenever you no longer need to use an Object in Java, if you simply get rid of any references to that Object, Garbage collection will take care of it from there. Set any references to that Object to null.

BestJewSinceJC 700 Posting Maven

I don't know very much C programming but I am pretty good at google. http://www.elook.org/programming/c/strtok.html

jephthah commented: come on, man. :icon_rolleyes: -2
tux4life commented: This doesn't deserve bad rep :) +12
BestJewSinceJC 700 Posting Maven

And also post what error you are getting. If you are successfully reading in the radius, then 2 * radius should give you the diameter without problems as long as the radius is declared as the correct type (i.e. float or double).

BestJewSinceJC 700 Posting Maven

edit, sorry guys. Combined the two posts.

BestJewSinceJC 700 Posting Maven

Yes. Because you have no main method - you need to put any code (other than variable declarations) inside methods. The main method is the method that runs when you first start your program, *edit*


Oops, nevermind - I see that you do have a main method. For statements like String lang, then lang = "abc", you would need to initially just say String lang = "abc"; . . and what I said about only having variable declarations still stands. lang = "abc" is an assignment statement, and thus, isn't allowed unless you do it inside of a method.