Mark Channer

Greenhorn
+ Follow
since May 09, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mark Channer

Thank you everyone for your replies.

The getDistance() method is indeed in the same class as the distanceTo() method. Now that you mention it Knute, I think I did read somewhere about not passing Class variables as parameters. If you had any references to where that is mentioned, I would like to check it out again. I'm sure it's in lots of books though. I guess the reason I did so was because I wanted the getDistance method to be able to simply take two parameters and then return the result. That way I can use the same method to get the distance between the x's and y's of both Point objects. I'm wondering if there's a better solution where I could still avoid filling out the distanceTo() method with lots of code?

Regarding your question Knute about being able to calculate the length between the two points on one line, I do know how to do that but, as Junilu pointed out it was for the sake of clarity. I read a really nice book called 'Clean Code' by Robert C. Martin where the author tells of visiting the inventor of Extreme Programming, Kent Beck, and being struck by how small the functions in his code were. He goes on to say that "every function in this program was just two, or three, or four lines long. Each was transparently obvious. Each told a story. And each led you to the next in a compelling order. That’s how short your functions should be!".

On the other hand, I read yesterday on p.189 of Joshua Bloch's widely respected 'Effective Java' that you should avoid going overboard on convenience methods. So I'm wondering at what point you cross the line of 'going overboard'. I'd be interested to hear views on this.

Thank you also for all your advice Junilu. I had no idea that my attempt was actually a pattern. I also agree that I could have better variable names. I recently did a little mortgage calculator exercise and found looking at real mortgage application forms to be really useful for coming up with better names so will take a look at relevant links for this too.

Thanks!

9 years ago
Hi all,

I've just been having a go at an exercise where I have to create and use a class called Point, with two fields of type double. I have to create some methods for it, one of which is a distanceTo(Point) method, that calculates the distance to another point. I've tried to keep the distanceTo(Point) method short so have created some other methods to use within the method. My question is about the getDistance() method that I've made. As you can see below, I've given it two parameters, which are references to values within two Point objects (this.x and otherPoint.x). Basically, I'm wondering if doing this kind of thing is a good idea or not. The method seems to do its job, but I would like to avoid getting into any bad habits. Any advice welcome.


9 years ago
Finally submitted OOP-3 (SortNames)
10 years ago
Finally got it sorted!

There were 2 problems:

1. I needed to import java.io.*; (thanks for the heads up Margaret)
2. I needed to add 'throws Exception' to the end of my main() method.

Thanks for your help guys!
10 years ago
That sounds hopeful Margaret, what is it?

Thanks for your persistence K, here's the details:
1. Classpath: C:\JavaExercises;C:\JavaExercises\jr.jar
Path: C:\Program Files\Java\jdk1.7.0_21\bin

2. To get to my working directory, I type 'cd\JavaExercises'. Then if I type 'dir' from there the files I get are:
DaysOld.class
DaysOld.java
GeekWatch.class
GeekWatch.java
Grains.class
Grains.java
jr.jar
Leap.class
Leap.java
names.txt
SortNames.java

As you can see, SortNames.java, jr.jar, and names.txt are all in here.

3. I cannot compile the program. To compile, I first type 'cd\JavaExercises' and then 'javac SortNames.java'. This is when I get the message:
'unreported exception FileNotFoundException; must be caught or declared to be thrown'

4. Unfortunately I've not made it past compiling yet.

Thanks both of you! I really appreciate the help.


10 years ago
Just tried that, but still the same exception.
I've tried reinstalling the jr.jar file also, but that doesn't do the trick. I've also tried putting this stupid txt file in different folders but for some reason it's not getting recognized.
Gggrrrrrr.
10 years ago
Thanks for your reply K,

Yes my txt file is in the working directory, which is basically a file called JavaExercises located straight off my C-drive, ie: C:\JavaExercises. My other classes, java files, as well as the java ranch jr file are located there also. I'm sure these are all working and set-up fine though, because I can still use other classes such a JDate and GDate from the java ranch file.

Just to confirm, my txt file is called names.txt and is basically a notepad document with a list of 8 names on it, nothing else. That's all I need, right?

My classpath is all set up fine I think. As mentioned above I can use other classes in the java ranch file without any issues. My classpath is: C:\JavaExercises;C:\JavaExercises\jr.jar (jr.jar is the Java Ranch file)

I don't get what I'm missing here, and it's driving me bat-sh*t crazy!!



10 years ago
Hi all,

I've just started having a go at the SortNames assignment and I seem to be stumbling at the first hurdle. I'm trying to set up the TextFileIn class to read the names that I've saved in names.txt, but when I run the code below that's given in the com.javaranch.common package I get a filenotfoundexception.


I've tried the suggestions in this thread, such as specifying the full path name like:
TextFileIn in = new TextFileIn( "C:\\JavaExercises\\names.txt" ) ; but I still get the filenotfoundexception.
This is most probably a daft question, but can I save a txt file using anything? I basically opened up notepad and copied in that list of eight names. I've made sure that it's saved in my JavaExercises folder, where my jr.jar file is.

Help!
10 years ago
Thanks Greg.

I've just signed up and also ordered the textbook. I'll have a crack at the first assignment this weekend.

Cheers!

10 years ago
Hello,

I'm a beginner Java programmer and love the sound of Cattle Drive and hope to be able to learn good clean code and Object Orientation principles if I sign up.
Can I just ask though, how far can the assignments take me and if I were to work hard how long would it be before there were no more assignments / material left.

Thanks!
10 years ago
If anyone has any bright ideas regarding the above, I'd appreciate whatever you have to say. It's driving me round the bend!
10 years ago

It's not bad. The biggest problem here is the code duplication between firstClassSeat() and economySeat(). If you find yourself copy-and-pasting, you're doing something wrong. You should combine these into one `bookSeat` method, taking a parameter to indicate class. You can use an `int` for the class of seat, but in real life it would be better to use an enumeration.


You could use a boolean flag, something like `seatAssigned` that starts as false, and gets set to true when you assign a seat. If it's still false at the end of the loop, you know the section was full. You can also use this flag in the for-loop condition, which allows you to avoid `break`


Thanks to Luigi's and everyone's suggestions I've managed to make some changes so far:
- Used an enumeration for the class of seat (haven't used enum's before so thanks for getting me to do that).
- Combined the firstClassSeat() and economySeat() methods into one bookSeat() method. (This is mostly based on Luigi's code, although I added the line 'seating[i] = true' (line 49) otherwise the method keeps assigning the same seat every time.
- Added the boolean flag 'seatAssigned' to break out of the loop (rather than using a break statement).

I've run into a problem though: I can't figure out how to not return seatNumber if the seats are full. In other words, the method still returns a value even if it's not been possible to assign a seat. Here's the revised code. Thank you for all your help.



10 years ago
Okay, I'll have another crack at it over the weekend. Thanks!
10 years ago

Luigi Plinge wrote:It's not bad. The biggest problem here is the code duplication between firstClassSeat() and economySeat(). If you find yourself copy-and-pasting, you're doing something wrong. You should combine these into one `bookSeat` method, taking a parameter to indicate class. You can use an `int` for the class of seat, but in real life it would be better to use an enumeration.

Your system for checking if each section is fully booked doesn't seem very watertight (although it might be OK in this toy example). You know it's fully booked if you've been through and not found a seat, not by checking if one particular seat is full. You could use a boolean flag, something like `seatAssigned` that starts as false, and gets set to true when you assign a seat. If it's still false at the end of the loop, you know the section was full. You can also use this flag in the for-loop condition, which allows you to avoid `break` (which is bad style, because it provides multiple exit points making code harder to follow).



I've been having a go at making a bookSeat method using a parameter and also using a boolean flag to indicate when the section on the plane was full, but I'm finding myself getting nowhere fast. Do you think I would be better off starting from scratch as I'm finding in quite difficult to combine the above suggestions into the code that I've already written? Thanks.
10 years ago
Thanks Winston,
I've shortened the comments that were carrying over to the next line now. Is that what you were referring to?
Apologies, I've only started posting today so am confidently clueless.
10 years ago