-1

Write a program Spheroid.java that takes respectively parameters a and c as command-line arguments and calculates surface area of an oblate spheroid.

He gave us the formulas to use but when I run it with his example command line arguments I get something totally different. His example gives 6 and 5 and gets 403.050. I did what I thought was correct and got 546.1380388013903. I haven't attempted to round it yet, I just wanted to see if I was close.

public class Spheroid {

    public static void main(String[] args){
        String a = args[0];
        String c = args[1];
        Double A = Double.parseDouble(a);
        Double C = Double.parseDouble(c);
        Double e;
        Double S;
        e = Math.sqrt(1-(Math.pow(C, 2)/Math.pow(A, 2)));
        S = (2 * Math.PI * Math.pow(A, 2)) + (Math.PI * ((Math.pow(C, 2)/ Math.pow(e, 2))) * (Math.log((1+e)/(1-e))));

        System.out.println(S);
    }
}
Requin
  • 11
  • 6
  • 1
    why are you using the 'Double' object and not the primitive 'double' type? – pelumi Oct 04 '15 at 22:31
  • 3
    To find an error in implementing a math formula it would be easier if you provided the formula you tried to implement.. – Deltharis Oct 04 '15 at 22:33
  • 1
    what is the formula? just to be sure you're expressing it correctly. – pelumi Oct 04 '15 at 22:33
  • What are `A`, `C`, and `e` supposed to represent? You should use more meaningful names for your variables. – TNT Oct 04 '15 at 22:35
  • The formula for the surface area can be found from [Wolfram MathWorld's article on Oblate Spheroid](http://mathworld.wolfram.com/OblateSpheroid.html). `A` is the equatorial radius, `c` the polar radius and `e` is the ellipticity. – Mick Mnemonic Oct 04 '15 at 22:37
  • Those are the variables he supplied. I would show you the exact formula he gave but I don't know how to transfer it into here. I don't know what you mean by the "primitive double type" – Requin Oct 04 '15 at 22:40
  • S= 2πa^2 + π (c^2/a^2)ln(1+e/1-e) – Requin Oct 04 '15 at 22:42

3 Answers3

0

The last line should be

S = (2 * Math.PI * Math.pow(A, 2)) + (Math.PI * ((Math.pow(C, 2)/ e)) * (Math.log((1+e)/(1-e))));

You had c^2/e^2 rather than c^2/e

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • Thank you! My eyes are getting blurry after working on these hw problems for the last few hours. – Requin Oct 04 '15 at 22:46
  • @Requin No worries. I'd advise taking pelumi's improvements into account (esp. using `double` rather than `Double`). Also you can do `a * a` rather than `Math.pow(a, 2);`. – Paul Boddington Oct 04 '15 at 22:48
  • I originally had x * x for another problem and he said I should redo it before turning it in to learn the math class. I'm leaving it as it is now cause it works but is there a more elegant way to solve the problem? – Requin Oct 04 '15 at 22:51
  • @Requin No, there is no more elegant way. I just think `x * x` is nicer but I agree it's a good idea to learn the `Math` class. – Paul Boddington Oct 04 '15 at 22:53
  • whats the difference between double and Double? – Requin Oct 04 '15 at 22:58
  • people give answers like http://stackoverflow.com/questions/20437003/difference-between-double-and-double but if you are only in your 3rd week of programming (me), then that is just technical mumbo jumbo – Requin Oct 04 '15 at 23:00
  • `double` is a primitive type. Maths with `double` values is very quick. A `Double` is an object that holds a `double`. Because it is an object is has a load of extra stuff you don't need. Your programs using `Double` will run slower. You should avoid the types that begin with capital letters for stuff like this, – Paul Boddington Oct 04 '15 at 23:01
  • 1
    @Requin I agree it's not particularly relevant if you're just starting out. Just try to remember to use `boolean`, `int` and `double` rather than `Boolean`, `Integer` and `Double` for most basic programs. – Paul Boddington Oct 04 '15 at 23:03
0

In your code you are using Math.pow(e, 2)) to calculate SA. Whereas, in actual formula it's just 'e' and not e to the power 2

0

The formula is not correctly represented in Java. Find below a few changes to your code:

public static void main(String[] args){
    String a = args[0]; //"6";
    String c = args[1];; //"5";
    double A = Double.parseDouble(a);
    double C = Double.parseDouble(c);
    double e = Math.sqrt(1-(Math.pow(C, 2)/Math.pow(A, 2)));
    double S = (2 * Math.PI * Math.pow(A, 2)) + (Math.PI * ((Math.pow(C, 2)/ e)) * (Math.log((1+e)/(1-e))));
    System.out.println(S);
}

Output is: 403.0500218861285

To round it off to 3dp you can use decimal format:

DecimalFormat newFormat = new DecimalFormat("#.###");
double threeDecimalPlaces =  Double.valueOf(newFormat.format(S));
System.out.println(threeDecimalPlaces);
pelumi
  • 1,530
  • 12
  • 21