Tag Archives: Java

An example that pleased me: The difference between an abstract class and an interface, in Java:


Here’s the punch line:

In Java, Prussia can extend (“be a”) one of the super-classes, Holy, Roman or  Empire, but only one. Prussia can implement the other two as interfaces, but only with methods and fields uniquely its own. If Prussia is to be Holy, be Roman and be an Empire, the strictly hierarchical relationship of those three super-classes has to be worked out separately and in detail, in advance. I can only imagine Herr von Bismark would approve.

 

And the whole magilla:
1) What is the difference between an interface and an abstract class?

An abstract class defines data (fields) and member functions but may not, itself, be instantiated. Usually, some of the methods of an abstract class are abstract and expected to be supplied by a sub-class, but some of the methods are defined.  Unless they are final, they can be overridden, and they can always be overloaded. Private parts of an abstract super class, for example, data, are not available to a subclass, so access methods (public or protected) must be used by the subclass. An abstract superclass is “extended” by a subclass. A given subclass may only extend one super-class, but a super-class may extend another super-class, in a hierarchy. (This avoids the complexities/difficulties of multiple inheritance in C++)

An interface is a proper subset of an abstract class, but has a different scope and use. An interface has ONLY abstract member functions and static, final, fields, aka constants. Any subclass has to provide all the variable fields and code which implements an interface. The implementing class cannot override the interface’s member signatures – the signatures are what the interface *is*. It is possible to overload an interface’s signatures, adding or subtracting variables, changing return or variable types, but the overloads do not satisfy the requirements of the interface. The implementing class(s) must contain actual member functions to satisfy all of the signatures in the interface, because there is no default, no code in the interface.  As used above, a given class ‘implements’ an interface, it does not ‘extend’ it. These limitations to an interface allow a given class to implement more than one, which retains most of the utility of multiple inheritance without, as it were, opening Plethora’s bag. (grin)

For example: In Java, Prussia can extend (“be a”) one of the super-classes, Holy, Roman or  Empire, but only one. Prussia can implement the other two as interfaces with methods and fields uniquely its own. If Prussia is to be Holy, be Roman and be an Empire, the strictly hierarchical relationship of those three super-classes has to be worked out separately and in detail, in advance. I can only imagine Herr von Bismark would approve.

Java run-time Profiling. NetBeans seems to run, Eclipse TPTP won’t, (!) on a Mac…


Here’s some hard-won wisdom that I’ll gladly share with others

1) If you’ve got a Java library that forms an API to some other thing, and you want to test it, JMeter may not be the best vehicle. This is because JMeter wants to get between the client and server… at which point, how you test the client becomes a bit of a mystery to me! I’ll revisit this when I’ve got a bit more experience, but I have a running program, no UI but works creat from a command line or inside Eclipse. JMeter wants me to add stuff  before it will profile it. Hmm. Next.

2) TPTP sure looked good when I was reading the description and I got to be a bit more mindful in downloading and adding it- copied its jars, etc, from where it unzipped its …/features and ../plugins directories. (NOTE: cp -rp : you need this to be recursive !) into …/eclipse/features and …/eclipse.plugins. I did NOT copy any file with the same name and time stamp as was already present. All looked good, there was the test and profile icon in the menu bar and everything. But there’s no Agent for Mac OS X, just Windows and *nix. There’s an open bug with 70+ responses to it in the Eclipse bug tool, nobody has come forward with a complete, working, version and the project management (this is open source freeware…) says they’d like to deliver the Mac OS X version but hasn’t got the resources… I can’t figure whether to laugh or cry! Maybe I should put my old shoulder to this wheel, when I am competent enough. Crazy making. Its like flying into 1995 and being told that professional programmers don’t use Macs. Malarkey, but there were planty of people saying it. If doing Java development, compile once, run on all platforms, blah blah, can’t be made to work on a Mac at this point in history, there’s something very odd. How the heck to developers using Eclipse work on their shiny new Macs? Without profiling or testing? Hard to believe.

3) SO, now I’m running NetBeans and I have to say it downloads and installs pretty quick, and appears to have a testing tool-kit built in, not a secondary download, which is nice. Here’s what I’ve learned about it:

NetBeans has an Anagram example program. Of course, you have to build it, and when you bring up the sources, you are offered a panel layout tool where you could pick additional events or add stuff or mess up the demo 100 different ways in 5 minutes or less. Don’t! Don’t add anything.  Just save and build. It runs and now you’ve got a NEW problem- you’ve created an anagram game with complex words and you aren’t thinking anagrams and haven’t got a clue what the plain text for

“batsartcoin”,
“maibuguos”,
“ratimhteci”,

should be.  Or maybe you do. Think about Java the language and the conceptual space it exists in.  I won’t spoil it by giving the plain text here or any complete solution. You can exercise your creativity, just like you should, do a search for “batsartcoin” and follow the hints you find.

Of Course you should learn how to find things like constant strings with the NetBeans IDE, and you should probably learn how to do it from a command prompt (or cygwin on Windows) too. And you can exercise your clever muscles looking on the web, where you can find the plain text, as a literal, or as a result from the anagram breaking tool(s) that exist out there.

BUT let me just say, after you’ve cheated for an answer or two, or racked your brain and gotten an answer or two just take a look at the anagrams and plain texts.

Bill

Now that’s odd… Java really IS different


My friend Harrison sent me a  Java file that was giving him trouble. He wanted to read in an Integer from in a command-line program. He found the method readInt() in the class Console. But it wouldn’t compile:

for ( i = 0; i < 5; i++)
System.out.println(“Array[“+ i +”] = “+ array[i]);
String name = null;
int age = 0;
age = Console.readInt(“Enter Age:”);
}
}

PersonTest.java:74: cannot find symbol
symbol  : method readInt(java.lang.String)
location: class java.io.Console
age = console.readInt(“Int Enter Age:”);
^
1 errors

I did a little searching and found suggestions that he was going in the right direction:

   char letter = Console.readChar("Enter a letter: ");
   double d = Console.readDouble("Enter a real number: ");
   int i = Console.readInt("Enter an integer: ");
   String name = Console.readLine("Enter your full name: ");

http://faculty.cs.wwu.edu/martin/Software%20Packages/BreezyGUI/breezyguijavadoc/breezygui/console.html

hahaha! Turns out breezygui.console isn’t the same as console… the base language doesn’t actually have the readInt method, or readChar or readDouble. But apparently C# and or J++ and/or some dialect/library for C++ DO have a Console class with a readInt, readChar and readDouble members…

SO… you have to be VERY careful what the on-line document you’re counting on really says. It might have the method you want but only in a library you don’t have, or in another language completely. Ooof! is this fun or what???

Of course I showed Harrison how to get integer, double and char values he wanted. That’s what friends are for.

So be careful what you read and believe. Including this!

Completed Java 1, wrote a tidy bit of code:


If I’d turned in one last homework it would have been an “A”. Mia Culpa.  I nailed all the homework I did turn in- 12.5/12.5 for 1-6, but only got #7 ready to compile by the deadline.

Here’s I neat piece of code I wrote and used in a couple of places- give it a string containing a number and it will give you back the number- the base language doesn’t keep this in any obvious place, but perhaps I should check the utility library before claiming to have set them right. :^)
/* Here’s an object that packages the messy job of converting Strings that represent numbers into a double.  Int/float/double/long don’t appear to even offer a polymorphic solution. That I’ve found anyway.
Instantiate with a string,
get back a double

anything else I might want

Ok wise guy, how do you get the base classes text value??
Ah ha! String is final- no extending it! This class can HAVE one but can’t BE one
*/

public class NumericString{  // can’t “extends String” its final

String input;
Double value;
// Constructor

public void NumericString( String inputValue ) {

input = inputValue;

// later we can filter out all but the signed numeric nugget we care about…

if (-1 == input.indexOf(‘.’)) {   //only numerals, no decimal, its an integer.
value = (double) Integer.parseInt( input )
;
} else {   // there must have been a decimal, its a float/double
value = ((Double.valueOf(input).doubleValue()));
} // if -1 … else…
}

public Double num() {
return value;
}

} // class NumericString