Hello,

I'm in a software design class and we are using java; I've taken other programming classes and I'm familiar with a few other languages but that's the problem. I feel like I'm missing the point of java. I don't even know the term for it...but the java style I geuss. How can you program only with classes? I more or less understand the idea of OOP, I've written classes in C++, but I think I mostly just programmed in a C style and used classes to make a new data type and then I'd use it. Java doesn't work like that.

The specific problem I'm working on is making a class for a GUI panel that takes in some user information. We're using regular expressions for validation but the professor wants that stuff in a separate file. In other languages I could just import the file and use it like it were one long file. It doesn't seem to work like that in java.

I'm being a little vague on purpose and I'm not posting code because it's not about this one problem it's about java in general, how do I program like a java programmer, instead of trying to trick the syntax into behaving like a C program. I don't expect this to be a short answer, I'm prepared to read, but I don't even know the correct term for the thing I'm thinking of so I haven't been able to google it yet. I'm sure there's books and essays and websites and probably a few threads on this site that discuss this, so if someone could just point me in the right direction I would be very grateful.

Thank you.

Recommended Answers

All 3 Replies

A proper answer could take one or more complete books, so this is just a snippet:

in a C style and used classes to make a new data type

In java you use classes to provide services - data is usually hidden inside the class, and the public interface consists of methods you can call to get things done.
When you're building the class you concentrate on how you will get those things done. You then test it by hard-coding some calls to the public methods.
Once it's working you can just include that class in any application that needs those services, and you don't worry about how they are provided, you just use the public interface.
Eg (semi-pseudo code)
You have some user info and it needs to be validated.

public class UserInfo {
    public UserInfo(...) { set up a new user object}
    public boolean isValid(some data) {
      if (somedata .... logical tests) return true; // valid
      else return false;
   }
}

then in the gui:

UserInfo info = new UserInfo(...);
String input = someUIcomponent.getText();
if (info.isValid(input) ...

Thanks!

A proper answer could take one or more complete books

Do you know any good ones?

The tutorials I have found are too basic and start from the very bottom of programming, way too far advanced, or they're just a list of differences between java and C++. These are helpful but I want to know how to program the java way. The jave philosophy. It's not clicking right now and I can't understand a how a class calls other classes.

Some more specific questions:
What is an appropriate level of variables to pass to another function or to another method in another class? When does it start slowing down the program?

I have one method that accepts user input and I need the method that checks it to know the string that the user entered, the validation string, JtextField to display the cleaned up version of the entry (toUpper and such), and a specific error message that says why a string was rejected.

Should I be passing all of this information to another method? Should I do all of this in the original method? Should I make an array in the method to store the possible error messages and validation strings?

What is the java way of doing all of this and more importantly, when I have other issues of this type, what book/essay/website/tutorial should I be looking in for the answers?

Thank you.

I have one method that accepts user input and I need the method that checks it to know the string that the user entered, the validation string, JtextField to display the cleaned up version of the entry (toUpper and such), and a specific error message that says why a string was rejected.

Should I be passing all of this information to another method? Should I do all of this in the original method? Should I make an array in the method to store the possible error messages and validation strings?

Try to separate the things that are intrinsic to the application from those that happen to belong to a particular UI. It's always a good test to consider what you could re-use and how if you had to produce a version of the same app where the UI was done via web pages.
So,validation take social security number as an eg. The valid formats for a SSN are independent of how the user happened to enter them. JTextField is definitely NOT relevant. Error messages are controversial, real apps often have to work in >1 local language. I vere towards returning an error number, then providing a lookup of texts which can be localised. It looks like:

// DataModel...
public int validateSSN(String ssn) {
   // returns 0 if valid, error number if not
   ...
}

// UI...
String s = myJLabel.getText();
int errorCode =  myDataModel.validateSSN(s);
if (errorCode != 0) {
  // display error message using String     DataModel.getErrorText(errorCode);

Check out MVC (model - view - controller) on the web.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.