Two Laptop Bag*
The moose likes Applets and the fly likes Splitting an applet up into 2 classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Splitting an applet up into 2 classes" Watch "Splitting an applet up into 2 classes" New topic
Author

Splitting an applet up into 2 classes

Randy Helene
Greenhorn

Joined: Apr 16, 2002
Posts: 21
I have a calculator applet for a homework assignment, and I need to split it up into 2 classes. Here is the code.

When the instructor was talking about this, the whole class was confused. He wants us to begin the new class with the actionPerformed logic and we have to pass the values of the text fields from the CalcMini class, to the next class which he called class Logic. The way he was passing the values had something to do with using the this keyword. I am not sure how to do this. Can someone please help.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I'd guess your instructor is trying to get you to seperate the user interface from the data and the operations on the data a bit. In my experience, this is a good first step for new object oriented programmers to take to begin to understand concepts such as patterns and good object oriented design.
So, as you were saying, your main program class will have the text fields and buttons (or whatever) and it will handle the display, while the other class will have methods to work on data (and maybe a field or two to know what data to work on - or maybe the data to work on will be passed as parameters to the methods that work on that data).
The this keyword is not complicated. It just references the current object in use. You probably won't really need to use it on this assignment except when registering the components that generate action events.
A simple example to consider:


Of course, your program will have to handle more operations and decide which operation to perform based on the origin of the action event.
Making sense?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Randy Helene
Greenhorn

Joined: Apr 16, 2002
Posts: 21
Yes, that helps a lot. Hopefully that is what he had in mind. I do have a couple of questions about your code though.
1. Your import statements are different than mine, more specific. I am curious whether using more specific import statements, for example, import java.awt.event.ActionListener, rather than import java.awt.event.* makes performance better.
2. When I tried to compile your program with both classes in the same file, I got the error,

C:\java\MiniCalc.java:26: class Logic is public, should be declared in a file named Logic.java
public class Logic
So I tried taking the public off the public class logic statement. Why does that matter?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Regarding the import statements:
There is no performance difference. The compiler actually fully qualifies all class references when it makes the bytecode files. The import statements are to lessen the programmer's typing burden. In my experience as a beginner and working with beginners, new programmers learn more about where different classes are and why they might be there (which helps when looking for similar classes in the future) when they just import one class at a time. Most new programmers seem to prefer to just import entire packages and not worry about what is where and why.
Regarding class descriptions in a file or files:
You can only have one top level public class per source file.
The Logic class doesn't need to be public.
Either do what you did, or do what Paul says 90% of programmers do and put each class description in its own file (excepting inner classes of course).
Good Luck.
Randy Helene
Greenhorn

Joined: Apr 16, 2002
Posts: 21
Thank you for your help and comments. I learn something new each time I post something here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Splitting an applet up into 2 classes
 
Similar Threads
Float to String
Wrapper
ERROR IN CODE
Need help with applet
linux file access