Welcome back to the fun of java and to the forum!
There are many helpful tips for the use of this forum in the
Announcements thread, including posting code with tags so it looks like this:
/** FILE: Apple.java */
package testing;
/**
* @author jps
* This class represents an apple...
*/
public class Apple {
/**The approximate diameter of the apple represented in inches*/
public double diameterInInches;
/**True if the apple has a worm, false otherwise*/
public boolean hasWorm;
/**True if the apple has a great taste, false otherwise*/
private boolean greatTaste;
/** Constructs an Apple object:
* @param diameterInInches The diameter of the apple measured in inches.
* @param hasWorm True if the apple has a worm in it, false otherwise.
*/
public Apple(double diameterInInches, boolean hasWorm) {
this.diameterInInches = diameterInInches;
this.hasWorm = hasWorm;
this.greatTaste = true;//of course my perfect apples have great taste!
}
/** @return True if the apple has a great taste, false otherwise. */
public boolean hasGreatTaste() {
return greatTaste;
}
/** @param greatTaste Set the great taste of the apple. */
public void setGreatTaste(boolean greatTaste) {
//this.greatTaste = greatTaste;
this.greatTaste = true;//I am a crooked salesman :P
}
}
I used a class I wrote for another topic as an example here because it demonstrates some things which may help you out.
1) Javadoc comments begin with /** and end with */
-Search keyword "Javadoc"
2) This class is part of the package named testing, merely a folder style hierarchy for organization.
-Search keywords "Java packages"
3) The instance variables diameterInInches, hasWorm, and greatTaste are declared private.
-Search keywords "Java access modifiers"
4) The private variables in this sample class have public getters and setters. (not all are included) The getter is merely a method for returning the value of the variable. The setter is merely a method for setting the value of the variable. While it is possible to say greatTaste = true from within this class, if you create an apple object of this class inside another class, you would not be able to access the variables without the getters and setters. You also maintain control over your variables by limiting access to variables through the methods as was the point of the thread the class was made for. In this case the crooked salesman who wrote the code altered the method used for customer review to force all reviews to say every apple has great taste.
-Search keywords "Java getters and setters"
5) This class has a constructor which requires parameters. This means all apple objects are required to provide values for the size and if there is a worm.
-Search keywords "Java constructor parameters"
6) The basic design of a Java class. Most classes I write basically LOOK the same. There is always a comment on the top of the page stating the name of the saved file. The comment just above the class declaration includes the @author and a description of the purpose and use of the class. Every part of the class has comments, constructors, methods, variables, what ever you find. At first the seemingly endless comments make the code feel cluttered but in time your eyes get used to them and code without them seems naked. As someone starting out, get used to using comments early. Every place who gives me money for code requires comments(Javadoc).... who knew.
-Search keywords "Java conventions"
Your program really lacks depth to be a great example, but there are a couple things to mention. (not an attack on you or your code, just the project itself)
There is no protection against bad input. If I was to run your program, and press cancel when the popup asks "How many stocks have been purchased?", the program would (should) crash with a null pointer exception.
You have some variables you don't necessarily need, for example:
comTotal = paid * comPercent;
formatter.format(comTotal)
will give the same result as:
formatter.format(paid * comPercent)
Having brought that up, let me battle from the other side of this road also... I just told you to use formatter.format(paid * comPercent) in place of the variable comTotal. Now I am going to tell you don't do that.
If I am reading over code and i see a line that says formatter.format(paid * comPercent) I have to figure out what paid is and what comPercent is and why they are being multiplied together before being formatted. On the other hand seeing the line formatter.format(comTotal) I can get a better understanding of what is going on without looking up two variables and deciding what the math operation is doing to them and why.
Why did I tell you to do it, then not to do it?
Many people include far too many unnecessary variables in their code. On the other hand many people try to simplify things (especially in terms of math-in-code) to the point they are no longer simple. There is a happy medium along the way that keeps the code clean and readable but not too crazy toward either extreme.
The program is not bad at all. This will explain a little more about why I said the program lacks depth.
There is no requirement to create a Stock object, or to store the related data, but you should. However, when you are getting used to the use of a variable, one can not require you to have objects with data and methods, and methods with parameters returning values. It is too much at one time. Which is why starter projects lack depth. But for the main method you have, nice work. As you learn to use objects, methods, multiple classes etc.. rewrite this program over and over. Every time you learn a new technique that you can apply to this program, do it. But don't modify the old. Rewrite the program from scratch using your new mind. Keep the old copies and you will have a record to watch the program over time. It is actually great practice to rewrite a program from scratch. First, it is good to go through the development process many times and a short program allows you to go through every step but not bang your head on one step for months. Second the more familiar you become with your program (what better way than from start to finish each time), the easier it is to apply new programming techniques to a program you fully understand than trying to learn a new technique on a program you may or may not understand yet.
Ok too much to read. Good luck