JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Ona completely separate point - you seem to be using a jaax.swing.Timer to run your game loop. Your architecture for this (separating the game loop from the screen painting) is exactly right, but that's not the best Timer for this purpose. The swing Timer will run your game loop on the swing event dispatch thread, where it will compete with the screen painting. If you use a java.util.Timer your game loop will run in its own thread, simultaneously if you have a dual processor. That wil allow you to achieve higher frame rates and smoother animation for no extra effort.
The syntax for java.util.Timer is slightly different, but it should only take a minute or two to change the code over.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

on or This looks difficult! I looked at the source code for readPassword and it uses a native method to turn off echoing, plus the Console class is final, ie you can't extend it. I'd move on to something else if I were you.

tux4life commented: Yeah, done that before and my conclusion was the same back then ;) +13
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A class diagram does not document a process; it's a static view of the class structure of a module or application. Processes like yours are documented in Use Case diagrams.

The very shortest "how to" goes like this: Look through your process description. Underline the important nouns - these are candidates for classes, underline the important verbs - these are candidates for public methods. Eg
"eye doctor who will give the patient a prescription"
possible classes: EyeDoctor, Patient, Prescription
possible method in EyeDoctor: issuePrescription
Now look at how some of these relate to each other - a Patient has zero or more Prescriptions, a Prescription is issued by an EyeDoctor etc

Make a first draft like that, then work through the use cases to see how well it hangs together. Refine the design, add details, delete stuff that isn't really needed. Repeat until satisfied.

And, of course, there's a vast amout of naterial on the web that's all just one short Google away.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I know naff-all about javax.amil, but you seem to be trying to override a method with your

new javax.mail.Authenticator() {
   protected javax.mail.PasswordAuthentication getPasswordAuthentication(URLName name) {...

but the API doc fpr javax.mail.Authenticator (Java EE 6) only has a

protected PasswordAuthentication getPasswordAuthentication()

method (no parameters), so you are not overriding anything with your code.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You are taking on a very steep learning curve, but...
Oracle have a GUI builder/IDE called NetBeans that goes with Java. Here's a very simple tutorial that will give you a feel for how it looks, at least on a simple example. Learning object-oriented programming is a whole other thing...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

new ImageIcon(...) is famous for not throwing any exceptions when it fails to find the file; it's just empty. JLabel is happy to accept null for its image icon, so the final result is no image and no error messages. We see that quite often in DaniWeb "help!" posts.
Use the exists() method of the File class with the path/name of the file to confirm that your program can find the file. Alternatively, check the width/height of the image to confirm they are >0

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sounds like a really interesting project! Some ideas:
You could use an ArrayList to hold a sequence of U/D/L/R/S(traight on)/B(ack) etc Characters as the genetic code - easy to process and easy to make a new one by mixing from 2 "parents" and making some random additions or substitutions ("mutations").
Maybe you could measure "fitness" by how far they move? Maybe make them die without reproducing if they get stuck in a dead end?

"The code snippet in your post is formatted incorrectly. Please use the Code button in the editor toolbar when posting whitespace-sensitive text or curly braces. "
The daniweb editor assumes indentation means code, not plain text, so your indetation from line 4 onwards has confused it.

Sacrificer_911 commented: Ooohhh... So that's what it means. Thank you :D +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Since nobody seems to know exactly where this is going, I would aim for clear, open, and extendable, rather than "just good enough". My instinct here is for

class Line { 
   private ArrayList<Point> points // 3 or more points defining this line
   public addPoint(Point p) etc
   public compareTo(Line l2) // equal if have same Points, even if order is different
   etc etc
}
...
ArrayList<Line> foundLines ....
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you specify a type for an ArrayList you should use <>, not (), as in ArrayList<String>

else without if means you've probably messed up matching the { and } barackets. If you indent your code properly it will probably become obvious where the error is (but fix all ocurrences of mistake 1 below first)

Now, for a new user bonus, here are two errors tha the compiler won't tell you about...
21: if(str1 == str2);
1. the ; terminates the whole if block ie if(str1 == str2) // do nothing
it's a simple mistake and the ; shouldn't be there.
2. == compares two strings to see if they are exactly the same object, which they are not (one is a constant in your program, the other has been read in from the user). To see if two strings contain the same sequence of characters you use String's equals method (see the String API doc for details)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What you ask is not difficult, and learning about it will be very valuable anyway, so here goes:
You need a window with a simple JPanel in it. You override the JPanel's paintComponent method to draw your points. The first two steps of the Oracle tutorial will get you that far (ignore step 3), and you can just copy/paste their code...
http://docs.oracle.com/javase/tutorial/uiswing/painting/step1.html

In your paintComponent method, to draw the points you can just start with a little circle for each point in the ArrayList, eg

for (Point2D point : myListOfPoints) {
   g.fillOval(point.x, point.y, 3, 3); // 3 pixel diameter filled circle at x, y
   // assumes the points fit in the pixel sixe of the window, eg 0 < x,y < 200
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Comparators are simpler than you probably think. When the Collections class is sorting data it needs to know what order to put them in. Some data types (eg integers) have "natural ordering", but objects in general do not. A Comparator just takes two objects and tells you which comes first in the sort order.
All the sort algorithms reduce to comparing pairs of objects then moving them around based on which object goes first. The sort algorith selects a pair of objects the asks the Comparator which comes first. The it picks anothe pair etc etc.
The stuff about "one point" doesn't quite make sense. A Comparator<Point2D> just compares any two Point2D instances that get passed to it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In that case, it's time to stop and read the instructions properly:

Instruction: NOTFOUND is set to -1;
Your code: NOTFOUND = grades.get(-1);

Instruction: findGrade - private method given a parameter representing the grade's date
Your code: findGrade(Grade aGrade)

Instruction: returns the ArrayList index of a grade
Your code: Grade g .... return g;

So that's three examples of not following the instructions just from that tiny excerpt.

For your own sake I would start again with a clean sheet of paper and re-read the instructions until you understand every single word in them. Until then your code is doomed to go off in random directions.
Please don't think I'm being negative or critical; I'm just giving the best advice I can, even if it's uncomfortable.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So far you posted getGrade (no parameter list) and getGrade() (empty parameter list), yet the method signature requires a parameter of type Score, so neither of those is right. And what's the difference between a Score and a Grade? Line 6 implies that a Score can be equal to a Grade, but without the class definitions who knows?

Grade NOTFOUND = grades.get(-1); will never assign a value to NOTFOUND, and none of the rest of findGrade will ever be executed because it will always throw an exception first (see the JavaDoc for ArrayList's get(int) method). Similarly getGrade will also stop executing and throw the exception as soon as it calls findGrade. (And why call findGrade anyway when you just ignore the value it returns). From which I guess you haven't been able to execute any of this code yet.

It seems to me that you are trying to code this in pieces that are too big. You will have fewer problems if you write one method at a time, write some trivial discardable code to test that method, and get the method working before starting on the next method. If you try to write too much in one go then you get buried in errors when you finally try to compuile and run, and its really hard to see where to start fixing things.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The Java compiler is your friend. Don't be afraid to try and compile your code. There's no point trying to get it right before compiling. Just chuck it into the compiler and see what errors it throws up. That costs you nothing.
At first you;ll get a lot of errors, but start with the first, fix them one at a time, re-compiling after each one.

But first...
Go back to your very first Java program (Hello World?). Remenber the method called main? Every program needs one. That's where you can: create new grades list, add some data to it, call your getLowest method, and print the result.

Have a look at that, if/when you get stuck come back here and post the whole of your code - it's hard to help you if we have to guess what's in the bits you didn't post!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

this may help
http://www.roseindia.net/java/java-conversion/BinaryToDecima.shtml

... or not. That's a really tortuous and horrible way to see if a string just contains '0' and '1' (if you can work it out at all, given the lousy coding standards). There may be some good stuff on RoseIndia, but most of it,, like this one, seems to be rubbish. I certainly would not use it as a reference source.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's just that you do a bit too much in one statement...

seatAssigned = seatingPlan.assignSeat(Integer.parseInt( action ));

just split it up to the keep track of the class...

int classRequested = Integer.parseInt( action );
seatAssigned = seatingPlan.assignSeat(classRequested);

now you have all the info you need, and also the code is easier to understand

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't agree with sleep here. The "application" can't sleep, only a thread can sleep. After the start() method everything in this code runs on the Swing EDT, and sleeping that thread will kill the animation (and any other swing activity).
Three solutions:
1. When you set the text, set a counter to 100. In your timer's actionPerformed dcrement the counter. When it gets to zero reset the text.
2. When you set the text get the system time, and 1 sec to it and save it in a variable. In the actionPeformed check the current system time against that variable to reset the text.
3. When you set the text start a new Timer with its own actionPerformed and a delay of 1 sec. In that actionPeformed reset the text and stop the timer.

Option 2 is probably the easiest/safest for you to code, but 3. is how Swing does stuff like popping up hints when you hover over a control for a second.

stultuske commented: agreed +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you execute a return statement your method finishes at that point, and it does not execute any following statements (1)

(1) Unless you have a try/finally structure

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can also convert integer to double by using the below statements..

Not in Java you can't!

k99rs
Welcome to DaniWeb.
Next time you post you may want to check any code you post, because otherwize you may end up looking foolish.The code you posted does not even compile ("Cannot cast from Integer to Double")

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It looks like maybe you start a new timer when you restart, but the old timer is still running, so now your actionPerformed gets called twice every 10 mSec, and every time you restart that gets worse. Make sure that when you restart either you just keep using the old timer, or you stop the old timer before starting a new one.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

One observation:
A game either can or cannot end in a tie. That's a boolean condition, and the best way to implement it would be a boolean variable called soemthing like "canTie".
An int variable called "tieNum" will work, but anyone reading the code will have no idea what it is or how it works unless you all a load of comments. The well-named boolean is completely obvious.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Unless you are runnning a very old verison of Java you can convert an Integer to a Double trivially using auto boxing/unboxing:

 Integer i = 3;
 Double d = new Double(i);

Your array index exception is at line 42 of Poly.IntegerPolynomial.getCoeff, where you are trying to access element number 3 of an array that doesn't have 4 elements (see the exception message), but since you didn't post that code it's hard to say any more.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I love building things, making things, designing things. And I love teaching. As CEO of a software house I wasted years in meetings with bankers, lawyers, shareholders etc, while the people working for me were doing all the things I wanted to do. On the other hand, the money was nice. In the end I went back to consulting in software design because being happy is more important than being rich.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK guys, that's enough now. Please restrict yourselves to answering bobit's orignal post. Thanks.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If this simple exercise is causing problems its probably because the idiot who designed it doesn't understand inheritance. Inheritance is a is-a-kind-of relationship, and a cylinder is NOT a kind of circle. What happens if I add some perfectly sensible methods to Circle like boolean containsPoint(int x, int y) ? That makes no sense for a cylinder because a cylinder is three dimensional.
class Cylinder extends Circle is the same mistake as class Car extends Gearbox
The correct model is has-a, not is-a, composition not inheritance. A cylinder consists of a circle that's extended into 3d along a line

class Cylinder {
   Circle xSection; 
   int length;
   ...

BUT - you have to do what the teacher asks, so just follow the instructions without thinking too hard about it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
if (rollTotal == 10) {
   rollHist[8] += 1;
} (etc)

There's a pattern- the array index is always two less than the rollTotal, so all those ifs can eb replaced with a single line of code something like rollHist[rollTotal-2] += 1;

stultuske commented: missed your post, but yup, that's the main point +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Whenever I need to reduce stress I find a post that was obviously intended as a joke and take it very very seriously </humour>

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

thisPeg is var from the Panel, yes? So it's not available in the Model.
The best way is to make toString nicely general so you can use it whenever/wherever you want to see what's happening in the model, not just in mouseClicked. So I would show everything (pseudocode):

String temp = "Abacus Model:\n";
for each element i in peg_array
    append "peg " + i + " = " + peg_array[i] + "\n"  to temp
return temp

Your lines 6,7,8 are not needed, all you need is the toString method then you can simply print myAbacus from within mouseClicked or anywhere else that you are working with myAbacus

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You are heading in the wrong direction! Just take a step back and look at the problem. You want to see the current state of your abacus. That info is all in the AbacusModel class. Java has a standard way of doing that, which uses the public String toString() method that every class has. In your AbacusModel class you override toString so it returns all the info you want to see. Now you can just print an instance of AbacusModel and see all the info.
eg suppose you have an ordinary Person class

class Person {
   private String familyName, givenName;
   private Date dateOfBirth;
   ... etc etc

in that class you override toString...

 public String toString() {
    return "Person: " + familyName + " " + givenName + "\nD.O.B. " + dateOfBirth;
 }

Now anywhere you have a Person, you can just print it, eg

 Person me = new Person(etc
 System.out.println(me);  // prints:  Person: Cherill James\nD.O.B. etc

Do the same thing for your AbacusModel, returning numbers of counters or anything else interesting, and then you can print out all its info from anywhere inAbacusPanel by just calling

System.out.println(myAbacus);
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Java is trying to print your Pair instances, which it does by calling their toString() method. All classes inherit one from Object, which just returns the name of the class and it's hash code, eg Example$Pair@1fc4bec
To print something more useful, override the public String toString() method to return something useful, eg x + ", " + y

tux4life commented: Correct and to the point. +13
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you println(something) the implementation actually calls something.toString() to convert the object to a printable string. Every class inherits a toString method from Object, so it always works, but usually isn't very helpful.
When you create your own class its a very good idea to override the toString method so when you print an instance of that class you get a meaningful output. In your case you may want to do something like:

@Override
public String toString() {
   String temp = "";
   // for each of the rows on this abacus append the number of counters to temp
   return temp;
}

Then you can call System.out.println(myAbacus); and get a useful ouput.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Making stuff static is not the answer. When you say AbacusModel.addCounter(thisCol); AbacusModel is a class, not an instance, so you cannot use that to call an instance method.
However, in your constructor for AbacusPanel you do (correctly) create an instance of AbacusModel
myAbacus = new AbacusModel(numCols,numRows);
now you can use that instance to call the instance emethods of the AbacusModel class, eg
myAbacus.addCounter(thisCol);

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

One small suggestion: To help you read that data back in again you may want to write the number of columns and number of rows at the beginning of the file. Otherwize you won't know how many column names to read before starting to read tha data, or how to divide the data in rows &cols.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I vote for the second version. The extra lines and indentation in version 1 create a lot of clutter and will push the following code off the screen - just imagine it inside a loop inside a method definition...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Now my question is, how would I go about making an array that will hold the attributes of the class circle in the geometry class?

Answer: you don't. The individual Circle objects hold their own attributes, all you need is an array of Circle objects

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Are those methods defined in the AbacusModel class? In that case you created an instance of that class on line 23 as part of your constructor, so you can use myAbacus to call those methods. Without seeing the method definitions it's impossible to comment on what parameter(s) to pass.
I have to disagree with joshfizzle - just defining everything as static reduces Java to the level of beginner's Basic. In this case you create an instance with values for the number of rows and columns - presumably instance variables, which would not be accessible if you made the methods static.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why are you overriding those methods? What's wrong with just inheriting them? Your overides look just like the originals anyway.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

can we create java file having two classes and both have main methods?

Based on some trials, and a quick scan through the JLS, I believe this is the answer:

Yes, and you can use either as the entry point, subject to...
Both classes must be top-level classes (not inner classes).
Only one can be public, and the .java file must have the same name as the public class.
There is no equivalent question for .class files because a .class file only contains one class.

EDIT:
It turns out you can use a main method from an inner (static) class as well. You just need to use the fully-qualified class name, eg
Main-Class: MulipleMains$MulipleMains2

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's more to this than it seems. Here's a simple single source file

public class MulipleMains {
   public static void main(String[] args) {
      System.out.println("One");
   }
}

class MulipleMains2 { // can't be public
   public static void main(String[] args) {
      System.out.println("Two");
   }
}

Eclipse will let me run that single file from either main method.
I also created two runnable jars with the same .class file, but different manifests, and can thus run wither main method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I can't say anything about the code because your class extends other classes that I don't have the source code for. If you think the problem is somewhere in that try block, just insert a whole load of System.out.println statements to print the key variables at various points in the block, so you can see what's being executed and what values are being used.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your catch block is passed an Exception to tell you exactly what went wrong. You should always start by printing all that info:

catch (Exception e) {
   e.printStackTrace();
   ...
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you are sending screen captures for a remote monitoring kind of app then you may find this thread interesting. A few of us tried all kinds of ideas, and found the best performance by keeping a connection open, and sending only the differences between each screen and te previous one using a simple protocol that we hacked together in a few lines of code.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There may be some advantage in using java.util.Timer rather than sleep (fewer threads hanging around, easier to clean up resources between events). All you can do about memory handling is to ensure you don't keep any references to objects you no longer need. Try running a profiler to see if your memory usage is flat or increasing over time?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I would prefer myMap.values().iterator().next().

Yes, I like that better as well, especially because it eliminates that horrible cast. Somehow I never thought of using an iterator without a loop! Thank you very much.
OK GUYS - BGUILD HAS THROWN DOWN THE CHALLENGE!

Hey James, I was shocked to see you asking the question

Why? Do you think anyone ever gets to know the best way to do everything in Java? Is anyone so smart they can't learn from someone else? I'm flattered by your shock, but it's completely unjustified :)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here's a simple thing that I can't find a simple solution for - so any suggestions will be very welcome...

I have a Map<String, MyClass> and if it has more than 1 entry I let the user pick a MyClass instance by displaying the keys. No problem there.

If it has exactly one entry I just want to use that single MyClass instance.
The best code I have come up with so far is the horribly tacky
(MyClass) myMap.values().toArray()[0];

Surely there's a better way? Someone...?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't mean to be rude, but if you don't know how to declare, set and use a variable in the correct scope then there's no way you should be trying to write a multi-threaded sound program. Maybe you are doing this all in the wrong sequence? It's a mistake to try to go too far too fast. IMHO you should set this to one side, spend a bit more time on the basics, then come back to this when you have the requisite skills. If you don't like this advice then please just ignore it; I'm only trying to say what's best.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If your method declares a return type (boolean, int etc) then every possible execution of the method MUST return a suitable value. You have if/else tests that, if they are true, do not return a value. You must add return statements so that every possible path through yopur methods returns a suitable value.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't feel confortable using enum, for some reasons I don't like them, so if I can do without I feel happier.

I do hope you will reconsider that. Enums were added to the language in Java 1.5 because Java needed them, and so do you. They are not difficult to use, but they do eliminate many kinds of difficult bugs, ie they make it much harder to write buggy code. Your code will be so much better if you use them, and you will be a much better coder.