why am i getting an "error: not a statement" message? - java

The idea for the program is to establish a users name, ask for 3 variables that i assigned as doubles, and use these variables to calculate various equasions. My first error is on the line that states;
System.out.print ("The area of a trapazoid is ") + (h * (a + b)/2);
I am getting an 'error: not a statement' with an arrow pointing to the first plus sign. ANY help would be greatly appreciated.
import java.util.Scanner;
public class LineberryCorey
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String userName;
double a;
double b;
double h;
System.out.print ("What is your first name?");
userName = stdIn.nextString();
System.out.print ("Alright, " + userName + ". Give me a number.");
a = stdIn.nextDouble();
System.out.print ("give me another number.");
b = stdIn.nextDouble();
System.out.print ("Give me one more number.");
h = stdIn.nextDouble();
System.out.print ("using the date you provided...");
System.out.print ("The area of a trapazoid is ") + (h * (a + b)/2);
System.out.print ("The surface area of a box is ") + (2 * ((a * b) + (a * h) + (b * h)));
System.out.print ("The surface area of a sphere with radius ") + a + (" is ") + (4 * MATH.PI * (a * a));
System.out.print ("The volume of a sphere with radius ") + b + (" is ") + (4 * MATH.PI * (b * b * b) / 3);
System.out.print ("The volume of a spherical cap is ") + (MATH.PI * (h * h) * (3 * a - h) / 3);
System.out.print ("The volume of a frustum is ") + (MATH.PI * h * ((a * a) + (a * b) + (b * b)) / 3);
System.out.print ("The volume of a torus with radii of ") + a + (" and ") + b + (" is "((MATH.PI * MATH.PI) *
(((a + b) * (b - a)) * ((a + b) * (b - a))) / 4);
}
}

Correct all System.out.print() syntax:
//correct the code:
System.out.print ("The area of a trapazoid is" + (h * (a + b)/2));
System.out.print ("The surface area of a box is " + (2 * ((a * b) + (a * h) + (b * h))));
System.out.print ("The surface area of a sphere with radius " + a + " is " + (4 * MATH.PI * (a * a)));
System.out.print ("The volume of a sphere with radius " + b + " is " + (4 * MATH.PI * (b * b * b) / 3));
System.out.print ("The volume of a spherical cap is " + (MATH.PI * (h * h) * (3 * a - h) / 3));
System.out.print ("The volume of a frustum is " + (MATH.PI * h * ((a * a) + (a * b) + (b * b)) / 3));
Why you are concatenating sysout statements,
(" is ")// here parenthesis not required.

Related

How to find the surface area of a pyramid in Java programming (CodeHS)?

I have an assignment on CodeHS to program a calculator for the surface area of a pyramid and it prints out the wrong surface area off by a few decimals. I don't see how this is incorrect (code below).
I've already tried plugging in the formula from Google for surface area and it did not work and printed the wrong number.
public double surfaceArea() {
double hw = (double)width/2;
double hl = (double)length/2;
double slantHeight1 = ((double)Math.sqrt( (double)height*height +
(double)hw*hw ));
double slantHeight2 = ((double)Math.sqrt( (double)height*height + (double)hl*hl ));
return (double)(((double)0.5 * 2 * slantHeight1 * width)
+ ((double)0.5 * 2 * slantHeight2 * length)
+ (length * width));
Example: for a pyramid with length 1, width 3, and height 5 it is supposed to print 23.29 but it prints 23.69 and I don't know why?
Another alternative solution: this is the equation for surface area of a right rectangular pyramid:
This can be simply written as:
public static void main(String[] args) {
double length = 1;
double width = 3;
double height = 5;
double resultPyramidArea = (length * width) + (length * Math.sqrt(Math.pow(width / 2, 2) +
Math.pow(height, 2))) + (width * Math.sqrt(Math.pow(length / 2, 2) + Math.pow(height, 2)));
System.out.println(resultPyramidArea);
}
change this:
return (double)(((double)0.5 * 2 * slantHeight1 * width)
+ ((double)0.5 * 2 * slantHeight2 * length)
+ (length * width));
to this:
return (double)(((double)0.5 * 2 * slantHeight1 * length)
+ ((double)0.5 * 2 * slantHeight2 * width)
+ (length * width));
you got the formula wrong

Java - Formatting Double data types, getting error

Here is the code for my homework assignment. It's long overdue and I didn't do so well on it, but wanted to try to work on my mistakes and problems to actually do better in school; but anyways:
double x1, y1, x2, y2, x3, y3; // Three points for three vertices.
// (x1, y1), (x2, y2), (x3, y3)
Scanner input = new Scanner(System.in);
// The program asks the user what the name of the triangle is. Then the program
// asks the user for the cordinates of each point.
System.out.print("Enter a three letter name for the triangle: ");
String triangleName = input.next();
System.out.print("Coordinates of vertex " + triangleName.substring(0,1).toUpperCase() + ":");
x1 = input.nextDouble();
y1 = input.nextDouble();
System.out.print("Coordinates of vertex " + triangleName.substring(1,2).toUpperCase() + ":");
x2 = input.nextDouble();
y2 = input.nextDouble();
System.out.print("Coordinates of vertex " + triangleName.substring(2,3).toUpperCase() + ":");
x3 = input.nextDouble();
y3 = input.nextDouble();
double a, b, c; // These values will serve as the three side
// lengths between the points.
// The program displays side lengths in this block.
System.out.println("--- Side lengths ---");
a = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2));
System.out.printf(triangleName.substring(0,1).toUpperCase()
+ triangleName.substring(1,2).toUpperCase() + ":"
+ "%.3f", a);
System.out.println();
b = Math.sqrt(Math.pow(x3-x2, 2) + Math.pow(y3-y2, 2));
System.out.printf(triangleName.substring(1,2).toUpperCase()
+ triangleName.substring(2,3).toUpperCase() + ":"
+ "%.3f", b);
System.out.println();
c = Math.sqrt(Math.pow(x3-x1, 2) + Math.pow(y3-y1, 2));
System.out.printf(triangleName.substring(2,3).toUpperCase()
+ triangleName.substring(0,1).toUpperCase() + ":"
+ "%.3f", c);
System.out.println();
double perimeter, area, xCentroid, yCentroid, inRadius, inArea, semiPerimeter, triangleHeight, triangleBase;
// The program displays other measures in this block.
System.out.println("--- Other measures ---");
perimeter = a + b + c;
System.out.printf("Perimeter = " + "%.3f", perimeter);
System.out.println();
triangleHeight = y3 - 0.1;
triangleBase = x2 - x1;
area = (triangleHeight * triangleBase) / 2;
System.out.printf("Area = " + "%.3f", area);
System.out.println();
xCentroid = (x1 + x2 + x3) / 3;
yCentroid = (y1 + y2 + y3) / 3;
System.out.printf("Centroid = ( " + "%.3f", xCentroid + ", " + yCentroid + ")");
System.out.println();
semiPerimeter = 0.5 * perimeter;
inRadius = area / semiPerimeter;
System.out.format("Incircle radius = " + "%.3f", inRadius);
System.out.println();
inArea = Math.PI * Math.pow(inRadius, 2);
System.out.format("Incircle area = " + "%.3f", inArea);
System.out.println();
The problem in the code resides on the line for formatting the xCentroid and yCentroid values respectively. I am getting an error that says "java.util.IllegalFormatConversionException: f != java.lang.String". The math in the program runs fine and works too. I've removed the "%.3f"'s in the statement to see if I get the correct answers, which I do. It's just this format is pissing me off. Please respond, and help. I'll be up for awhile and will be patient.
NEW CODE (FIXED) :
System.out.print("Centroid = ");
System.out.print("(");
System.out.printf("%.3f", xCentroid);
System.out.print(", ");
System.out.printf("%.3f", yCentroid);
System.out.print(")");
System.out.println();
In this code
System.out.printf("Centroid = ( " + "%.3f", xCentroid + ", "
+ yCentroid + ")");
especially this
xCentroid + ", " + yCentroid + ")"
is not going to be a number, but a String
I guess what you want is
System.out.printf("Centroid = (%.3f)", xCentroid + yCentroid);

how to convert os grid reference to longitude and latitude in java?

I'm looking to convert OS Grid Reference to longitude and latitude, I'm using the jcoord library in Android studio, http://www.jstott.me.uk/jcoord/
I'm interested how I would connect this to a button that on on activity you can type in a grid reference on a edit text, and click convert (Which will do the magic formula) and will show the longitude and latitude below?
Basically I want to make a user interface, with being able to test this on my phone
I believe the formula will be done in the OSRef.java, with the code being the following:
public class OSRef extends Activity implements View.OnClickListener {
EditText osGridNumber;
View convertButton;
TextView latLongBox;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
convertButton = findViewById(R.id.cmdConvert);
convertButton.setOnClickListener(this);
osGridNumber = (EditText) findViewById(R.id.edtOS);
latLongBox = (TextView) findViewById(R.id.txtLngLat);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cmdConvert:
break;
}
}
/**
* Easting
*/
private double easting;
/**
* Northing
*/
private double northing;
/**
* Create a new Ordnance Survey grid reference.
*
* #param easting the easting in metres
* #param northing the northing in metres
* #since 1.0
*/
public OSRef(double easting, double northing) {
this.easting = easting;
this.northing = northing;
}
/**
* Take a string formatted as a six-figure OS grid reference (e.g. "TG514131")
* and create a new OSRef object that represents that grid reference. The
* first character must be H, N, S, O or T. The second character can be any
* uppercase character from A through Z excluding I.
*
* #param ref a String representing a six-figure Ordnance Survey grid reference
* in the form XY123456
* #throws IllegalArgumentException if ref is not of the form XY123456
* #since 1.0
*/
public OSRef(String ref) throws IllegalArgumentException {
// if (ref.matches(""))
char char1 = ref.charAt(0);
char char2 = ref.charAt(1);
// Thanks to Nick Holloway for pointing out the radix bug here
int east = Integer.parseInt(ref.substring(2, 5)) * 100;
int north = Integer.parseInt(ref.substring(5, 8)) * 100;
if (char1 == 'H') {
north += 1000000;
} else if (char1 == 'N') {
north += 500000;
} else if (char1 == 'O') {
north += 500000;
east += 500000;
} else if (char1 == 'T') {
east += 500000;
}
int char2ord = char2;
if (char2ord > 73)
char2ord--; // Adjust for no I
double nx = ((char2ord - 65) % 5) * 100000;
double ny = (4 - Math.floor((char2ord - 65) / 5)) * 100000;
easting = east + nx;
northing = north + ny;
}
/**
* Return a String representation of this OSGB grid reference showing the
* easting and northing.
*
* #return a String represenation of this OSGB grid reference
* #since 1.0
*/
public String toString() {
return "(" + easting + ", " + northing + ")";
}
/**
* Return a String representation of this OSGB grid reference using the
* six-figure notation in the form XY123456
*
* #return a String representing this OSGB grid reference in six-figure
* notation
* #since 1.0
*/
public String toSixFigureString() {
int hundredkmE = (int) Math.floor(easting / 100000);
int hundredkmN = (int) Math.floor(northing / 100000);
String firstLetter;
if (hundredkmN < 5) {
if (hundredkmE < 5) {
firstLetter = "S";
} else {
firstLetter = "T";
}
} else if (hundredkmN < 10) {
if (hundredkmE < 5) {
firstLetter = "N";
} else {
firstLetter = "O";
}
} else {
firstLetter = "H";
}
int index = 65 + ((4 - (hundredkmN % 5)) * 5) + (hundredkmE % 5);
// int ti = index;
if (index >= 73)
index++;
String secondLetter = Character.toString((char) index);
int e = (int) Math.floor((easting - (100000 * hundredkmE)) / 100);
int n = (int) Math.floor((northing - (100000 * hundredkmN)) / 100);
String es = "" + e;
if (e < 100)
es = "0" + es;
if (e < 10)
es = "0" + es;
String ns = "" + n;
if (n < 100)
ns = "0" + ns;
if (n < 10)
ns = "0" + ns;
return firstLetter + secondLetter + es + ns;
}
/**
* Convert this OSGB grid reference to a latitude/longitude pair using the
* OSGB36 datum. Note that, the LatLng object may need to be converted to the
* WGS84 datum depending on the application.
*
* #return a LatLng object representing this OSGB grid reference using the
* OSGB36 datum
* #since 1.0
*/
public LatLng toLatLng() {
double OSGB_F0 = 0.9996012717;
double N0 = -100000.0;
double E0 = 400000.0;
double phi0 = Math.toRadians(49.0);
double lambda0 = Math.toRadians(-2.0);
double a = RefEll.AIRY_1830.getMaj();
double b = RefEll.AIRY_1830.getMin();
double eSquared = RefEll.AIRY_1830.getEcc();
double phi = 0.0;
double lambda = 0.0;
double E = this.easting;
double N = this.northing;
double n = (a - b) / (a + b);
double M = 0.0;
double phiPrime = ((N - N0) / (a * OSGB_F0)) + phi0;
do {
M =
(b * OSGB_F0)
* (((1 + n + ((5.0 / 4.0) * n * n) + ((5.0 / 4.0) * n * n * n)) * (phiPrime - phi0))
- (((3 * n) + (3 * n * n) + ((21.0 / 8.0) * n * n * n))
* Math.sin(phiPrime - phi0) * Math.cos(phiPrime + phi0))
+ ((((15.0 / 8.0) * n * n) + ((15.0 / 8.0) * n * n * n))
* Math.sin(2.0 * (phiPrime - phi0)) * Math
.cos(2.0 * (phiPrime + phi0))) - (((35.0 / 24.0) * n * n * n)
* Math.sin(3.0 * (phiPrime - phi0)) * Math
.cos(3.0 * (phiPrime + phi0))));
phiPrime += (N - N0 - M) / (a * OSGB_F0);
} while ((N - N0 - M) >= 0.001);
double v =
a * OSGB_F0
* Math.pow(1.0 - eSquared * Util.sinSquared(phiPrime), -0.5);
double rho =
a * OSGB_F0 * (1.0 - eSquared)
* Math.pow(1.0 - eSquared * Util.sinSquared(phiPrime), -1.5);
double etaSquared = (v / rho) - 1.0;
double VII = Math.tan(phiPrime) / (2 * rho * v);
double VIII =
(Math.tan(phiPrime) / (24.0 * rho * Math.pow(v, 3.0)))
* (5.0 + (3.0 * Util.tanSquared(phiPrime)) + etaSquared - (9.0 * Util
.tanSquared(phiPrime) * etaSquared));
double IX =
(Math.tan(phiPrime) / (720.0 * rho * Math.pow(v, 5.0)))
* (61.0 + (90.0 * Util.tanSquared(phiPrime)) + (45.0 * Util
.tanSquared(phiPrime) * Util.tanSquared(phiPrime)));
double X = Util.sec(phiPrime) / v;
double XI =
(Util.sec(phiPrime) / (6.0 * v * v * v))
* ((v / rho) + (2 * Util.tanSquared(phiPrime)));
double XII =
(Util.sec(phiPrime) / (120.0 * Math.pow(v, 5.0)))
* (5.0 + (28.0 * Util.tanSquared(phiPrime)) + (24.0 * Util
.tanSquared(phiPrime) * Util.tanSquared(phiPrime)));
double XIIA =
(Util.sec(phiPrime) / (5040.0 * Math.pow(v, 7.0)))
* (61.0
+ (662.0 * Util.tanSquared(phiPrime))
+ (1320.0 * Util.tanSquared(phiPrime) * Util
.tanSquared(phiPrime)) + (720.0 * Util.tanSquared(phiPrime)
* Util.tanSquared(phiPrime) * Util.tanSquared(phiPrime)));
phi =
phiPrime - (VII * Math.pow(E - E0, 2.0))
+ (VIII * Math.pow(E - E0, 4.0)) - (IX * Math.pow(E - E0, 6.0));
lambda =
lambda0 + (X * (E - E0)) - (XI * Math.pow(E - E0, 3.0))
+ (XII * Math.pow(E - E0, 5.0)) - (XIIA * Math.pow(E - E0, 7.0));
return new LatLng(Math.toDegrees(phi), Math.toDegrees(lambda));
}
/**
* Get the easting.
*
* #return the easting in metres
* #since 1.0
*/
public double getEasting() {
return easting;
}
/**
* Get the northing.
*
* #return the northing in metres
* #since 1.0
*/
public double getNorthing() {
return northing;
}
}
Thanks in advance

Three random points on a circle

I'm trying to create a program where three random points in a circle are created and result in the creation of an inscribed triangle. However, the angles I'm getting are all screwed up
Here is my code:
public static void main(String[] args) {
double r = 40.0;
double angle1 = Math.random()* (2 * Math.PI);
double angle2 = Math.random()* (2 * Math.PI);
double angle3 = Math.random()* (2 * Math.PI);
double x_1 = r * Math.cos(angle1);
double y_1 = r * Math.sin(angle1);
double x_2 = r * Math.cos(angle2);
double y_2 = r * Math.sin(angle2);
double x_3 = r * Math.cos(angle3);
double y_3 = r * Math.sin(angle3);
System.out.println("The coordinates of the three points are:
(" + x_1 + ", " + y_1 + ")
(" + x_2 + ", " + y_2 + ")
(" + x_3 + ", " + y_3 + ")");
//Get length of each side
double distanceFrom1To2 = Math.sqrt(Math.pow(x_2 - x_1, 2) +
Math.pow(y_2 - y_1, 2));
double distanceFrom2To3 = Math.sqrt(Math.pow(x_3 - x_2, 2) +
Math.pow(y_3 - y_2, 2));
double distanceFrom3To1 = Math.sqrt(Math.pow(x_1 - x_3, 2) +
Math.pow(y_1 - y_3, 2));
//Get angles ***
double triangleAngle1 = Math.atan(distanceFrom1To2 / distanceFrom2To3);
double triangleAngle2 = Math.atan(distanceFrom2To3 / distanceFrom3To1);
double triangleAngle3 = Math.atan(distanceFrom3To1 / distanceFrom1To2);
System.out.println("The three angles are " + triangleAngle1 + " " +
triangleAngle2 + " " + triangleAngle3);
System.out.println(triangleAngle1 + triangleAngle2 + triangleAngle3);
}
I definitely know that the means of getting the angles are screwed up. Here is a sample run of my program:
The coordinates of the three points are: (5.224534224725408,
-39.65733528787168) (-29.696946087404676, 26.79722733944279)
(32.70889681040468, -23.02451018906371)
The three angles are 0.7545364726122026 1.18830825410364
0.40435068059871415
Total angle sum: 2.347195407314557
The angles all add up to much greater than Pi / 2 radians. I've considered the law of sines but you have to know at least one angle for that...
Figured it out
Here is the fixed code:
//Get length of each side
double a = Math.sqrt(Math.pow(x_2 - x_1, 2) + Math.pow(y_2 - y_1, 2)); // distance from 1 to 2
double b = Math.sqrt(Math.pow(x_3 - x_2, 2) + Math.pow(y_3 - y_2, 2)); // distance from 2 to 3
double c = Math.sqrt(Math.pow(x_1 - x_3, 2) + Math.pow(y_1 - y_3, 2)); // distance from 3 to 1
//Get angles ***
double triangleAngle1 = Math.acos((Math.pow(a, 2) + Math.pow(b, 2) - Math.pow(c, 2)) / (2 * a * b));
double triangleAngle2 = Math.acos((Math.pow(b, 2) + Math.pow(c, 2) - Math.pow(a, 2)) / (2 * c * b));
double triangleAngle3 = Math.acos((Math.pow(c, 2) + Math.pow(a, 2) - Math.pow(b, 2)) / (2 * a * c));
I changed it to use the law of cosines.

Apache POI rate formula inconsistency with long periods

In order to emulate Excel's rate function, I'm using the Apache POI rate function I grabbed from the svn:
private double calculateRate(double nper, double pmt, double pv, double fv, double type, double guess) {
//FROM MS http://office.microsoft.com/en-us/excel-help/rate-HP005209232.aspx
int FINANCIAL_MAX_ITERATIONS = 20; //Bet accuracy with 128
double FINANCIAL_PRECISION = 0.0000001; //1.0e-8
double y, y0, y1, x0, x1 = 0, f = 0, i = 0;
double rate = guess;
if (Math.abs(rate) < FINANCIAL_PRECISION) {
y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
}
else {
f = Math.exp(nper * Math.log(1 + rate));
y = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;
}
y0 = pv + pmt * nper + fv;
y1 = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;
// Find root by the Newton secant method
i = x0 = 0.0;
x1 = rate;
while ((Math.abs(y0 - y1) > FINANCIAL_PRECISION) && (i < FINANCIAL_MAX_ITERATIONS)) {
rate = (y1 * x0 - y0 * x1) / (y1 - y0);
x0 = x1;
x1 = rate;
if (Math.abs(rate) < FINANCIAL_PRECISION) {
y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
}
else {
f = Math.exp(nper * Math.log(1 + rate));
y = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;
}
y0 = y1;
y1 = y;
++i;
}
return rate;
}
For calculateRate(120, 28.1, -2400, 0, 0, 0.1)), the output is the same as Excel: 0.599
But if I try the same calculation, this time with the values:
calculateRate(360, 15.9, -2400, 0, 0, 0.1))
In Excel I get 0.580, and the program returns -1.1500428517726355. Any hints?
There are a bunch of things that are wrong with this code that you have pasted in your question.
It assumes that a rate is always found (not true) and makes no provision for instances when a rate is not found.
Some of the statements will throw an error which could have been avoided by using a more appropriate programming statement. For instance, take the following statement from your code:
f = Math.exp(nper * Math.log(1 + rate));
This will throw an error when attempting to find Log of a negative or zero value. It could have been rewritten as
f = Math.pow(1 + rate, nper);
The comment in iterative calculations states that it is programming the secant method, yet the iterative calculations are checked for convergence of the wrong variable. It is testing for convergence of a future value when it should be testing for convergence of the interest rate.
I copy pasted your code in Notepad and removed the variable declaration of Java and replaced these with JavaScript variable declarations to test the code with your sample data. And just as I said, the code stops at the second iteration since the difference of future values goes out of error bound and since there is no test in place to see whether a rate is found, the code returns the interest rate as is and one which is wrong.
I am not sure why this code works in instances where it does report a correct rate as is the case with first data set. I would suggest re-coding of the function in a correct manner.
public double rate(double nper, double pmt, double pv)
{
//System.out.println("function rate : " + nper + " " + pmt + " pv " + pv);
double error = 0.0000001;
double high = 1.00;
double low = 0.00;
double rate = (2.0 * (nper * pmt - pv)) / (pv * nper);
while(true) {
// Check for error margin
double calc = Math.pow(1 + rate, nper);
calc = (rate * calc) / (calc - 1.0);
calc -= pmt / pv;
if (calc > error) {
// Guess is too high, lower the guess
high = rate;
rate = (high + low) / 2;
}
else if (calc < -error) {
// Guess is too low, higher the guess.
low = rate;
rate = (high + low) / 2;
}
else {
// Acceptable guess
break;
}
}
//System.out.println("Rate : " + rate);
return rate;
}
Example: =RATE(60, 2112500, 65000000) returns 0.025198; the same with Excel (correct).

Categories