• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble linking static methods into main  RSS feed

 
Adam Bush
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,
First time programmer here. Im taking the intro to java 142 (CSC142) at my local community college. I was asked to write a program that takes in two GPS coordinates (DDD MM SS Direction) and display the distance between those two points in miles.

The problem Im having is trying to link the 3 static methods: input, output, processing into the main method of my program. So when I run main, input executes, the input from user gets placed into a parameter sent to processing, processing then does the calculations and returns the answers to main, where output takes those answers and displays the correct distance in miles.

This code isnt 100% and doesnt compile, Im still trying to fix everything.

 
Rajith Pemabandu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose your code should be modified as below.




Hope this helps.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajith Pemabandu wrote: . . . Hope this helps.
I think that code needs more drastic things than that.
OP: Have they not taught you how to create a class to encapsulate the lat/long data? Have they not shown you how to write a distanceFrom method?Now all you have to do is do the calculations method remembering you can write other.lat and other.long. in the Note that you must keep the same formula for ever if you describe it in the documentation comments like that. If you ever want to change it, you will have to change that part of the comment.Java® is spposed to be an object‑oriented language and there seem to be many people around who don't understand objects.
 
Adam Bush
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply's guys. Were still in the first 4 chapters of the book, so we cant implement classes or use array lists. The only thing we can do is implement static methods.

Chapter 1 - Intro to java programming
Chapter 2 - primitive data and definite loops
Chapter 3 - Intro to parameters and objects
Chapter 4 - Conditional execution
......
......
......
......
Chapter 7 - Arrays
Chapter 8 - Classes

I did get my code working.... I was missing 1 crucial return in the input static method. Where I needed to return processing. And in my  main method I needed to equal " double processing = input(); ".



Everything is working but my mileage return is off about 30 miles.... Any ideas to fix this?
Im getting 1764.0 miles where I need to get 1794.3 miles.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Bush wrote:Everything is working but my mileage return is off about 30 miles.... Any ideas to fix this?
Im getting 1764.0 miles where I need to get 1794.3 miles.


First of all go through the calculations with pencil and paper and work out how that value is derived from the inputs. Write down everything which goes into the calculation and the intermediate stages. And of course "pencil and paper" means you don't look at your code or execute it at all.

Next go through what your code does and have it output everything which it puts into the calculation and its intermediate stages. Compare that to what your pencil and paper says the answer should be.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 39 in the original post looks wrong. If direction is not a Character, the equals call with 'N' which is a char can be relied upon to return false. You would have to pass "N" as a String. Similarly for 'S'.
Try a switch statement with those strings as the cases; it has been possible to use Strings since Java7 (2009).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!