Calculating math in GPS format - java

I'm making a Android Application to calculate Math in GPS Format.
Example:
Given
N 48°44.(30x4) E 019°08.[(13x31)+16]
the App calculates it, and result is:
N 48°44.120 E 019°08.419
Is it possible to do this?
I searched for plugins and solutions, but it's all just for math strings like as "14 + 6".

I am assuming you are working in Java as it is tagged in your question.
You could create a new public class for your GPS coordinates, and store the actual value of the coordinate in the lowest division, which according to your example appears to be minutes or seconds. This allows you to store the value as an int or a double with whatever precision you wish. You could then create a set of private and public methods to complete your mathematical operations and others to display your values in the appropriate fashion:
public class GPSCoordinate {
private double verticalcoord;
private double horizontalcoord;
//Constructors
GPSCoordinate(){
setVertical(0);
setHorizontal(0);
}
GPSCoordinate(double vert, double horiz){
setVertical(vert);
setHorizontal(horiz);
}
//Display methods
public String verticalString(){
return ((int)verticalcoord / 60) + "°" + (verticalcoord - ((int)verticalcoord / 60) *60);
}
public String horizontalString(){
return ((int)horizontalcoord / 60) + "°" + (horizontalcoord - ((int)horizontalcoord / 60) *60);
}
//Setting Methods
public void setVertical(double x){
this.verticalcoord = x;
}
public void setHorizontal(double x){
this.horizontalcoord = x;
}
//Math Methods
public void addMinutesVertical(double x){
this.verticalcoord += x;
}
}
This will allow you to initiate an instance in your main code with a given GPS coordinate, and then you can call your math functions on it.
GPSCoordinate coord1 = new GPSCoordinate(567.23, 245);
coord1.addMinutesVertical(50);
coord1.otherMathFunction(50 * 30);
You will, of course, need to refine the above to make it fit your project. If this isn't helpful, please provide more specifics and I'll see if I can think of anything else that might fit what your looking for.

Can't you just substring the whole thing and search for the expression in the brackets? Then it's just a matter of simple calculation. If I understood the question correctly. The gps data doesn't look like an ordinary expression, so you can't appy math() directly.

Related

Can someone correct my code over here? Also, can someone tell me the proper way to define a function in Java?

This code does not run when I try to compile it, I am sure it is because I
defined my function/method incorrectly, so it would be much appreciated if
someone can correct my code and also tell me what is wrong with it.
I know C++ so I tried to define the function like how I would define it
normally in Cpp but with a few tweaks. I really don't know what I am doing
right now.
class Calculator {
public static void main(String[] arguments) {
float Celcius;
float Farenheit = 32;
final float k = 5 / 9;
System.out.println("This is the temperature in degrees celsius: " +
Converter(Farenheit));
public float Converter(float Farenheit) {
return 5 / 9 * (Farenheit - 32);
}
}
}
So the comments noted the key issues. The method cannot be within main. 5/9=0 in Java. Here is a little program I just checked on jdoodle.com. It does what you said in what might be typical Java style (though for sure there are possible improvements and things with which to quibble). For learning Java (which is not the same as for experienced users for development), bluej is an interesting IDE with which to start (specifically because it doesn't do all the work for you). But StackOverflow does not want judgment questions like that, so ignore if you wish.
public class Calculator {
public double converter(double Farenheit) {// convention converter lower case because not a class name
return 5.0 / 9 * (Farenheit - 32); //note 5.0 ensures real number arithmetic, not integer
}
public static void main(String[] arguments) {
Calculator calculator = new Calculator();// make a calculator object, alternative would be to declare converter static
double Farenheit = 32;
System.out.println("This is the temperature in degrees celsius: " +
calculator.converter(Farenheit));
}
}
Your method public float Converter(float Farenheit) is written inside main method . This is not allowed in JAVA . You can however write a anonymous class inside a method and calls its methods .
The correct code is :
class Calculator {
private static final float k = 5.0f / 9;
public static void main(String[] arguments) {
float Celcius;
float Farenheit = 32;
System.out.println("This is the temperature in degrees celsius: " +
Converter(Farenheit));
}
public static float Converter(float Farenheit) {
return k * (Farenheit - 32);
}
}
Please note I have modified Converter method to a static one . We cannot call non static methods from static context in JAVA(as main is static here) . If Converter would have been non static then we would have to create an object of the Calculator class.
Calculator c = new Calculator();
c.Converter(Fahrenheit);
You can declare k as class level variable if it is to be used across multiple methods and has a constant value .
private static final float k = 5.0f / 9;

Java: calculating velocity of a skydiver

In Java, I am trying to implement the following equation for calculating the current velocity of a skydiver not neglecting air resistance.
v(t) = v(t-∆t) + (g - [(drag x crossArea x airDensity) / (2*mass)] *
v[(t-∆t)^2] ) * (∆t)
My problem is that I am not sure how to translate "v(t - ∆t)" into a code. Right now I have this method below, where as you can see I am using the method within itself to find the previous velocity. This has continued to result in a stack overflow error message, understandably.
(timeStep = ∆t)
public double calculateVelocity(double time){
double velocity;
velocity = calculateVelocity(time - timeStep)
+ (acceleration - ((drag * crossArea * airDensity)
/ (2 * massOfPerson))
* (calculateVelocity(time - timeStep)*(time * timeStep)))
* timeStep;
}
return velocity;
}
I am calling the above method in the method below. Assuming that the ending time = an int, will be the user input but written this way to be dynamic.
public void assignVelocitytoArrays(){
double currentTime = 0;
while(currentTime <= endingTime){
this.vFinal = calculateVelocity(currentTime);
currentTime += timeStep;
}
}
I would like to figure this out on my own, could someone give me a general direction? Is using a method within itself the right idea or am I completely off track?
The formula you want to implement is the recursive representation of a sequence, mathematiacally speaking.
Recursive sequences need a starting point, e.g.
v(0) = 0 (because a negative time does not make sense)
and a rule to calculate the next elements, e.g.
v(t) = v(t-∆t) + (g - [(drag x crossArea x airDensity) / (2*mass)] * v[(t-∆t)^2] ) * (∆t)
(btw: are you sure it has to be v([t-∆t]^2) instead of v([t-∆t])^2?)
So your approach to use recursion (calling a function within itself) to calculate a recursive sequence is correct.
In your implementation, you only forgot one detail: the starting point. How should your program know that v(0) is not defined be the rule, but by a definite value? So you must include it:
if(input value == starting point){
return starting point
}
else{
follow the rule
}
On a side note: you seem to be creating an ascending array of velocities. It would make sense to use the already calculated values in the array instead of recursion, so you don't have to calculate every step again and again.
This only works if you did indeed make a mistake in the rule.
double[] v = new double[maxTime/timeStep];
v[0] = 0; //starting point
for(int t = 1; t < maxSteps; t++){
v[t] = v[t-1] + (g - [(drag x crossArea x airDensity) / (2*mass)] * v[t-1]^2 ) * (∆t)
}

Algorithm mistake, finding areas between lines

I was doing my math hw, and it required me to calculate areas between 2 lines. I solved couple of them, I got them right(its online so it shows me whether i got it wrong or right), then I thought about writing a program that does the calculation for me, I created a nice algorithm, used java for the program, but in the end it didn't give me the right answer.
I put the data from one of the questions I already solved whether i got it right or wrong.
Can you please tell me the mistake in the algorithm??
public class DailyHelper {
public static double f(double x) {
double y = 5*x;
return y;
}
public static double g(double x) {
double y= 4*x*x;
return y;
}
public static void main(String[] args) {
double xLower = 0;
double xHigher = 5/4;
double areaF=0;
double areaG=0;
double change = (xHigher-xLower)/100000;
for(double k=xLower; k<xHigher; k=k+change) {
areaF = areaF+(change*f(k));
}
for(double k=xLower; k<xHigher; k=k+change) {
areaG = areaG+(change*g(k));
}
double area = areaF-areaG;
System.out.println(area);
}
}
Just a quick thought.
Your xHigher variable is always 1, since you're dividing int by int. Try 5/4d
If there are 2 straight random lines and you want to calculate area from vertical lines L1 and L2, L1 < L2, then you must construct that trapezoid (by finding the corner coordinates) and calculate the area. However this doesn't work on non-linear "lines", you'll need to follow a rule like that: http://en.wikipedia.org/wiki/Numerical_integration#Methods_for_one-dimensional_integrals

Java OOP triangle existence

I have recently started with programming. So far I have learned basics and now its time for OOP and so i have some questions as im building basic programs to just understand principals and link to way I would use it in practical ways.
So I am making simple triangle program in Java, so far it calculates perimeter (later will ad other shapes and other parameters), I hit the wall where I want to add Triangle existence (as side can't be negative) and also Id like to allow user input. Thing is i don't know where to put code and how to refer to class. Linear (non OOP) way it is simple, but how its done in OOP, do i have to make another class or in Triangle class via methods?
my code:
public class Trissturis {
private int sideA, sideB, sideC;
private double perimeter;
public Trissturis(int a, int b, int c) {
sideA = a;
sideB = b;
sideC = c;
}
public double getPerimeter() {
return sideA + sideB + sideC;
}
}
public class TestTri {
public static void main(String[] args) {
Trissturis t1 = new Trissturis(10, 20, 30);
System.out.println("perimeter is " + t1.getPerimeter());
Trissturis t2 = new Trissturis(-1, 20, 30);
}
}
To validate the triangle you have to check that all sides have a length greater than zero, and that no side is longer than the sum of the other two. An method that would accomplish this is:
public boolean isValid(){
return (sideA>0)&&(sideB>0)&&(sideC>0)&&(sideA+sideB>sideC)&&(sideA+sideC>sideB)&&(sideC+sideB>sideA);
}
For the user to input values, it is better to have separate user-interface classes. If this will be a desktop application, you could use some of the Swing classes, for example (although there are alternatives).
interface TriangleFactory {
Triangle create();
}
class ConsoleTriangleFactory implements TriangleFactory {
#Override
Triangle create() {
// read perimeter from console here with some nice prompt
// check that every side is > 0,
// if it's not a number or less than 0 - then do some alert
}
}
Your code to check that the triangle is constructed correctly (with non-negative values, etc) belongs in the Triangle class.
Code to take user input can go in main() in your Test for a small program, but could go in a separate UI namespace for a larger application.
hth

Java exercise for checkout

I'm trying some Java recently and look for some review of my style. If You like to look at this exercise placed in the image, and tell me if my style is good enought? Or maybe it is not good enought, so You can tell me on what aspect I should work more, so You can help me to improve it?
exercise for my question
/*
* File: MathQuiz.java
*
* This program produces Math Quiz.
*/
import acm.program.*;
import acm.util.*;
public class MathQuiz extends ConsoleProgram {
/* Class constants for Quiz settings. */
private static final int CHANCES = 3;
private static final int QUESTIONS = 5;
private static final int MIN = 0;
private static final int MAX = 20;
/* Start program. Number of questions to ask is assigned here. */
public void run() {
println("Welcome to Math Quiz");
while(answered != QUESTIONS) {
produceNumbers();
askForAnswer();
}
println("End of program.");
}
/* Ask for answer, and check them. Number of chances includes
* first one, where user is asked for reply. */
private void askForAnswer() {
int answer = -1;
if(type)
answer = readInt("What is " + x + "+" + y + "?");
else
answer = readInt("What is " + x + "-" + y + "?");
for(int i = 1; i < CHANCES+1; i++) {
if(answer != solution) {
if(i == CHANCES) {
println("No. The answer is " + solution + ".");
break;
}
answer = readInt("That's incorrect - try a different answer: ");
} else {
println("That's the answer!");
break;
}
}
answered++;
}
/* Produces type and two numbers until they qualify. */
private void produceNumbers() {
produceType();
produceFirst();
produceSecond();
if(type)
while(x+y >= MAX) {
produceFirst();
produceSecond();
}
else
while(x-y <= MIN) {
produceFirst();
produceSecond();
}
calculateSolution();
}
/* Calculates equation solution. */
private void calculateSolution() {
if(type) solution = x + y;
else solution = x - y;
}
/* Type of the equation. True is from plus, false is for minus. */
private void produceType() {
type = rgen.nextBoolean();
}
/* Produces first number. */
private void produceFirst() {
x = rgen.nextInt(0, 20);
}
/* Produces second number. */
private void produceSecond() {
y = rgen.nextInt(0, 20);
}
/* Class variables for numbers and type of the equation. */
private static boolean type;
private static int x;
private static int y;
/* Class variables for equation solution. */
private static int solution;
/* Class variable counting number of answered equations,
* so if it reaches number of provided questions, it ends */
private static int answered = 0;
/* Random generator constructor. */
RandomGenerator rgen = new RandomGenerator();
}
One thing I noticed was that all of your methods take no parameters and return void.
I think it would be clearer if you use method parameters and return values to show the flow of data through your program instead of using the object's state to store everything.
There are a few things you should do differently, and a couple you could do differently.
The things you should do differently:
Keep all fields together.
static fields should always be in THIS_FORM
you've used the static modifier for what clearly look like instance fields. (type,x,y,solution, answered). This means you can only ever run one MathsQuiz at a time per JVM. Not a big deal in this case, but will cause problems for more complex programs.
produceFirst and produceSecond use hardcoded parameters to nextInt rather than using MAX and MIN as provided by the class
There is no apparent need for answered to be a field. It could easily be a local variable in run.
Things you should do differently:
There is a small possibility (however tiny), that produceNumbers might not end. Instead of producing two random numbers and hoping they work. Produce one random number and then constrain the second so that a solution will always be formed. eg. say we are doing and addition and x is 6 and max is 20. We know that y cannot be larger than 14. So instead of trying nextInt(0,20), you could do nextInt(0,14) and be assured that you would get a feasible question.
For loop isn't really the right construct for askForAnswer as the desired behaviour is to ask for an answer CHANCES number of times or until a correct answer is received, whichever comes first. A for loop is usually used when you wish to do something a set number of times. Indeed the while loop in run is a good candidate for a for loop. A sample while loop might look like:
int i = 1;
boolean correct = (solution == readInt("What is " + x + "+" + y + "?"));
while (i < CHANCES && !correct) {
correct = (solution == readInt("Wrong, try again."));
i++;
}
if (correct) {
println("Well done!");
} else {
println("Nope, the answer is: "+solution);
}
Looks like a very clean program style. I would move all variables to the top instead of having some at the bottom, but other than that it is very readable.
Here is something I'd improve: the boolean type that is used to indicate whether we have an addition or subtraction:
private void produceType() {
type = rgen.nextBoolean();
}
produceType tells, that something is generated and I'd expect something to be returned. And I'd define enums to represent the type of the quiz. Here's my suggestion:
private QuizType produceType() {
boolean type = rgen.nextBoolean();
if (type == true)
return QuizType.PLUS;
else
return QuizType.MINUS;
}
The enum is defined like this:
public enum QuizType { PLUS, MINUS }
Almost good I have only a few improvements:
variables moves to the top
Inside produceNumbers and your while you have small repeat. I recommend refactor this
Small advice: Code should be like books - easy readable - in your run() method firstly you call produceNumber and then askForAnswer. So it will be better if in your code you will have the same order in definitions, so implementation askForAnswer before produceNumber. But it isn't necessary
Pay attention to have small methods. A method shouldn't have much to do - I think that askForAnswer you could split to two methods

Categories

Resources