Joel McNary

+ Follow
since Aug 20, 2001
Joel likes ...
Eclipse IDE Ruby Java
Merit badge: grant badges
Joel is here and there and back again.  Flies kites, takes pictures (sometimes while flying kits), writes code, camps and hikes, looks at stars, writes more code.  Does not like java (the drink).  Does like Java (the language).
For More
Philadelphia Metropolitan Area, Pennsylvania, USA
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Joel McNary

With the question of who's been here 20 years showing up in another thread (and Randall Twede making an appearance), I figured I'd surface, too.  Not quite 21 years here for me - in just over a week, my JavaRanch persona will be old enough to drink in the USA!

Registered:      Monday, August 20, 2001
Today:           Thursday, August 11, 2022
Days Elapsed:    7661
Number of Posts: 1843
Posts per day:   0.24056911630335465
Days per post:   4.156809549647314
1 year ago
You are correct in that you have changed the Name of the existing object in the map.  You have not added a second object in the map; when you print out the Map, there is still only one object in there.  What happens if you put in another object with the key "emp2"?  Try that and see what happens when you print out the map; you should see one object associated with emp1 and one object associated with emp2.

Perhaps you are confused by the fact that when you get the object from the map, it does not create a copy of the object.  You have a reference to the object locally (through the emp1 variable) and one in the map.  Since both references refer to the same object, any change to the object is visible by all the references, so when you print out the map it is printing out the changed value.
3 years ago
I would suggest using Integer as the type for your list. Integers in Java are 32 bits, and you need to handle m <= 16, so Integer will be more than enough. If you want, you could use Short (which has a length of 16 bits), but if you start debugging you could see some unexpected display results and get confused. Short is a type really only used in memory-limited applications, though, and so isn't commonly used in classroom assignments. Other than that, your next steps look correct to me.
8 years ago
Hi, Anthony, and welcome to JavaRanch! We're friendly to new Java developers here, so don't be afraid to ask questions. I do have a bit to say about this code though before I get to the meat of your question.

You do have a basic foundation here, but it is pretty rough (understandable considering a teacher who goes off on tangents -- I've had a few of those). First off, this code is pretty hard to read - there are standard Java conventions that you are not following here -- not a big deal in this short assignment, but you should probably learn them for larger-scale projects where you are working in a team. (Typically, method names and variable names start lower-case in Java, so "elmCity" and "public double distance(Point iCity)"). There are others, too, and they do make the code easier to read.

Second, you seem to have defined several classes inside of your main method. I will be honest -- I have never seen this before and was surprised that this would compile! There are several disadvantages to doing this, though. First, because this is very uncommon, it was difficult to follow what was going on (see "Coding conventions," above). Second, and more importantly, these classes only have scope within your main method. This means that you cannot create another method that takes, for example, a Vehicle object as a parameter -- the compiler will complain about that, because Vehicle objects are only valid within your main method.

Your Vehicle class is confusing. While you are defining points individually (elmCity, rokfordTown, etc.) you seem to be defining one vechicle with arrays of data, and then creating an array to hold 10 Vehicle objects. Consider defining the vehicles individually like you do the cities.

Now, to the meat of your question: how to take the price of fuel into account. I'm not going to answer the question directly (we don't do homework here, you understand, but we will help you understand how to do it, which is much more valuable in the long run). Instead, how would you do this if you were trying to calculate it on a piece of paper? If you can calculate the cost of fuel from elmCity to rokfordTown of someone driving your first SUV (MPG of 13) with fuel costing $2.80 a gallon, and then describe to the computer the steps you took to do it, you have solved that problem. (By the way, the answer is approx. $41.55. The answer is not important -- its how you get there that is.)

I think that's enough to think about to start with. See what you can do with that information.
8 years ago

First of all, I'm sorry but I'mt not speaking very well English (rather so bad), so I could be wrong to interpret and/or answer.

You are doing quite well. At the very least, we are misunderstanding each other in the same way

Ok, so you're saying that it's better not to use a byte as a key because it would create problems when then I have to change the value of m (m = 1, m = 2, etc.)?

Yes. Especially for m > 8.

So I would need a Java function that, taken one byte (which I see as a decimal), decompose the bytes in portions long m with m variable.
Right? And how can I do that?

Yes, you are understanding what the function needs to do. In order to do that, look at bitwise comparators (the single-character '|' and '&' operators). You might also find the bit shift operators useful ( '<<', '>>', and '>>>'). This took me a little bit of thought to come up with an algorithm, so don't get too discouraged it it takes you a while as well.

Question: is it right to throw the two most significant bit or the least significant?
Is correct to not consider the "excess" bits?

That I do not know; file entropy is usually expressed with m = 8, so I don't know what would be correct for m = 3. If I, personally, were to discard anything, however, it would be the least significant bits.
8 years ago
You are headed along the correct path.

Why are 2 or 3-bit numbers (32 65 97 98 99 100 101...)? It should not be sequences of 8-bit type 00000000, 00000001, 00000010, etc?
I was wrong to use the method?

This is just the decimal representation of the byte (they are *not* 2 or 3-bit numbers, by the way!). By default, it will print that instead of the bit-string. So, 32 is 00100000, 114 is 01110010, etc. This means that 32 is a 6-bit number (it takes a minimum of 6 bits to represent it in base 2), and 114 is by the same reasoning a 7-bit number. Since bytes are 8-bit, these numbers can be represented by a byte. 597, however, cannot (it takes more than 8 bits to represent that value).

I must definitely use another method. I read that in Java you can read the files only to the maximum until the bytes, for example not as a bit sequence...

You are right, but I wouldn't change how you read the file. Instead, see point 2 below.

1). I would not use Byte as the keys in my maps, because that will not handle your larger m-values (Bytes handle a maximum of m = 8, and even then you might see some unexpected results if you print out the Map, since bytes in Java are signed!)
2). You will need another method to transform your byte[] input to an array (or List) of something else. For example, an input of byte[] {114} and m = 2 should give you back an array of two-bit numbers ({1, 3, 0, 2}). I leave it as an exercise to you to figure out why 114 would become those four numbers, but I gave you the answer already in this post. What should an input of byte[]{114} and m = 4 return? If you can figure that out, you are most of the way to writing the transformation function.
3). Do you have to handle cases like m = 3? That can be interesting, because there is no guarantee that a file will have an even multiple of 3 bits. What should be done in those cases? (What would byte[]{114} and m=3 return?(

But before you start those changes, can you get it working front-to-back for m = 8? What entropy value does it give you for that text input?
8 years ago
Your question is not entirely clear, but I think I might begin to understand what you are asking -- certainly, when you are confused about an assignment, it can be difficult to ask clear questions!

First, your general steps are correct, insofar as step 1 is create a program that reads text and steps 2-3 are apply an algorithm to that text to calculate the entropy. The big question is, what algorithm should you use? Here's where I start to have some questions:

1. You are expressing m in terms of bits, but your step 2 is talking about letters and numbers, which are expressed in bytes.

2. Have you been given any sort of direction on this in class (I am assuming this is a class assignment)? Are you supposed to develop the algorithm yourself, or just write a Java program to implement an algorithm? I could point you to an algorithm to use (the one I am thinking of works for m=8, but we can help you figure out how to adjust it for other values of m), but if that's not the assignment, I don't want to start down that path.

Perhaps if you were to post the actual text of the assignment, we might be able to provide a little clearer direction.
8 years ago
True; I forget about that, because I am stuck in 1.6 land. (And still need to support 1.5).
8 years ago
You are correct, but when initializing the map, the formal types of the arguments must match -- you can't use subtypes. The class itself can be a subtype, though.

Translation: You are correct to use HashMap, but it should be List<String>, not ArrayList<String>. That's because the declaration allows for all types of Lists, and you can't alter the declaration.
8 years ago
Hi Kris, and welcome to JavaRanch!

It looks like you are doing well with the code (there's a few stylistic things I would comment on, but they're beside the point); it's the algorithm that giving you a little bit of trouble. Here's the problem: you are not doing something in your second while loop that you are doing in your first one. Take a closer look at the code and see if you can spot it (hint: take a look around line 54). If you can't find it, let me know and I will point it out, but you will learn better if you find the answers yourself!

Once you get that working, I would recommend trying to split your algorithm for calculating pi to a separate method. That way, when you call the method, you are certain it is running the same code and you don't suffer from copy-and-paste errors.
8 years ago
And just an additional thought on the super keyword: super is generally used when a subclass wants to so something *in addition* to what the superclass does. Back to the animals!

A Marsupial is a Mammal that raises its young in a pouch. Kangaroos, Opossums, Koalas -- these are all Marsupials. The baby is born, like any other mammal, but then it moves to the pouch and continues development there. Let's model that in a class:

There: the Marsupial reproduces just like any other Mammal (we ensure that by calling super.reproduce(), which call the version of the method defined in the Mammal class), but then it adds the bit about making sure the baby moves to the pouch. Yes, we could have just overridden the method like this:

and it would have the same effect. For now. If the definition of reproduce in the Mammal class changes, this will still do things the old way, so it doesn't stay up-to-date. Also, if the definition of reproduce in the Mammal class were 100 lines long, we would have to copy the 100 lines here. Using the super keyword allows for code reuse (instead of code copying), and it ensures that the subclass will stay in sync with the parent class. So don't use the second example, because while it achieves the same effect, it is an inferior solution. Use super instead.

Incidentally, you are only forced to call super first in constructors. In methods, it can be anywhere in the method, although it typically is first there, as well (like in our example above). When it is not first, it is last -- meaning that you want to do something custom *before* calling the super method. It is very rare to find it in the middle of a method, though, just because most process flows don't need it there. (And it can make the code hard-to-read! Code that is easy to read is also easy to maintain, so that is an important consideration.)
8 years ago

Nikki Smith wrote:
Last thing, is I'm supposed to use an example of polymorphism, but I can't even understand what it is, muchless what it looks like. Could someone please help? :3

Polymorphism is simply the concept that when you call a method on an object, you know that something will be done, but there's no guarantee *how* it will be done. Sound confusing? It isn't, really. Let's consider a concrete example from nature.

Mammals reproduce by giving birth to live young. So, if we were to describe that in a class, you might have something like this:

Note that I didn't bother to define the superclass Animal of the method giveBirthToLiveYoung; this example is not intended to be code complete!

Now, for a fun fact about nature: not all mammals give birth to live young. (You may or may not already know this). There is a group of mammals called monotremes that actually lay eggs. (They are still mammals because the mothers produce milk for the young once they have hatched.) The Platypus and the Echidna are the members of this group. We may describe this behaviour in a class this way:

Now, we have a subclass of Mammal that lays eggs instead of giving birth when asked to reproduce. It can inherit other methods of Mammal (perhaps "produceMilk()"), but when it comes time to reproduce, it lays eggs.

So this method:

takes a Mammal as a parameter and tells it to reproduce. You know that it it going to reproduce *somehow*, but not exactly how (unless, of course, you know the exact class of the parameter-- but you don't, and in most cases where polymorphism comes into play, you really shouldn't care). That Mammal could be a live-birth mammal (like a Cat or a Dog) or it could be a Monotreme -- it could lays eggs.

And that's polymorphism in a nutshell. For a more detailed example (again, using animals), check out our campfire story How My Dog Learned Polymorphism
8 years ago
A layout manager would be better; GridBagLayout is my favorite and would work well in your case here, but it takes a bit of patience to understand all of its nuances. But that is beside the point of your question.

To check the result, just call System.out.println(userHook) and see what you get. Or add a breakpoint in eclipse and check the value of the variable. The big question is, "Is it null?"
8 years ago
There's nothing in particular wrong with doing that way -- I just got stuck thinking along one line of thought. I can probably come up with theoretical arguments for and against both designs, but if it works and is easy to understand and maintain, go for it.
8 years ago
In this case, your test and work classes should call into the common speech-recognizer class. That way, they receive notifications the same way they did before, but you can handle all the similar code in the common class. This is called the "Delegation Pattern," and you can use it like this:

And the "Work" class would be implemented the same way as the "Test" class.
8 years ago