Joel McNary

Bartender
+ Follow
since Aug 20, 2001
Joel likes ...
Eclipse IDE Java Ruby
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).
Philadelphia Metropolitan Area, Pennsylvania, USA
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
7
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
(keep public parts private until JForum day)
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt
Moderation Tools

Recent posts by Joel McNary

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.
1 year 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.
1 year 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.
1 year 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?
1 year 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.
1 year ago
True; I forget about that, because I am stuck in 1.6 land. (And still need to support 1.5).
1 year 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.
1 year 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.
1 year 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.)
1 year 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
1 year 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?"
1 year 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.
1 year 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.
1 year ago
Welcome to the ranch, Dwijesh!

Without seeing the source code for the Calculate class, it is difficult to tell you exactly what it does, but it does seem to be the method that performs the actual volume calculations. It is probably an overloaded method (meaning there is actually more than one method with that name, each taking a different number of parameters). Based on the number of parameters, it is determining what volume calculation to use. (There is another option, that the "main" method uses variable length arguments, but I would argue that a series of overloaded methods would be a better design choice in this case. But that is an argument beyond the realm of the beginner, so I won't bore you with it. Learning the basics of a language can be overwhelming enough.)

Normally the "main" method is one reserved for starting the application (as in line 4 of your code), but there is nothing prohibiting other code from calling it. In this case, however, I think it is just poorly-named ("runCalculation" might be better). Having a better name would make the code "self-documenting," which basically means easier to understand when reading it over.

As for why the get_Vol() method is being called on the Calculate class instead of the cal instance, that is because the get_Vol() method is static; you do not need an instance of the class to call it. In this case, I would imagine that there are other methods (like get_Area()) that can also be used to perform calculations, and since these calculations are mathematic functions independent of data, you don't actually need an instance of the Calculate method to invoke them.
1 year ago

Michael Matola wrote:Every time I pass through Eighty Four (on 519) (about twice a year), I think, "Wasn't there some guy on JavaRanch from Eighty Four?" (That's you, right?)



Yes, that's me, although that hasn't been my home address for nigh upon 22 years now -- you've probably passed through it more than I have during that time.

Anyways, in keeping with my ability to resurrect ancient posts (and since I decided to stop in and peek around the ranch), here's my current stats. Why? Because I can. I haven't quite been about the ranch for 15 years, but it's getting close.

Registered: Monday, August 20, 2001
Today: Monday, February 22, 2016
Days Elapsed: 5299
Number of Posts: 1826
Posts per day: 0.3445933194942442
Days per post: 2.9019715224534504
1 year ago