I am having trouble inputting the radius of a circle and angle of a circle (deg) while receiving the output with the values given plus the angle in radians, arc length and polar area all as floats. Here is what I have.
import java.util.Scanner;
public class MyCircles {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("1st: Please enter a radius (integer) :");
int radius01 = input.nextInt();
System.out.println("1st: Please enter angle (in degrees between 0 and 360) :");
int angledeg01 = input.nextInt();
input.close();
System.out.printf("%s%14s%15s%19s%24s\n", "Radius (inch)", "Angle (deg)", "Angle (rad)", "ArcLength (inch)", "Polar Area (sq. inch)");
System.out.printf("%13d%14d", radius01, angledeg01);
float anglerad01 = radius01 * (Math.PI / 180.0f);
Trying to achieve this:
Radius(inch) Angle(deg) Angle(rad) ArcLength(inch) Polar Area(sq.inch)
3 31 0.54 1.62 2.43
All in columns but I keep running into cannot convert into float error. Also cannot use the MathtoRadians functions. Must use hard code to show work. Suggestions?
anglerad01 should be double instead of float
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("1st: Please enter a radius (integer) :");
int radius01 = input.nextInt();
System.out.println("1st: Please enter angle (in degrees between 0 and 360) :");
int angledeg01 = input.nextInt();
input.close();
System.out.printf("%s%14s%15s%19s%24s\n", "Radius (inch)", "Angle (deg)", "Angle (rad)", "ArcLength (inch)", "Polar Area (sq. inch)");
System.out.printf("%13d%14d", radius01, angledeg01);
double anglerad01 = radius01 * (Math.PI / 180.0f);
}
}
All you have to do is convert Math.PI to a float since it is a double and not a float:
float anglerad01 = (radius01 * ((float)Math.PI / 180.0f));
The reason we need to do this conversion is because, while both floats and doubles are data types that represent decimals, they are not the same data type since doubles are more precise.
Math.PI gives you a constant in the form of a double, and so without casting to a float, you divide the double by a double, giving you a double, and then multiply it by an int to get a double. This leads you to be giving a float variable the value of a double.
However, once you cast Math.PI to a float, your final value will also be a float, making the variable legal.
First I read it wrong, I made the suggestion on the bases that Math.toRadians you are not able to use in your program due to some error.
So now its for what you have asked I hope.
1.data type double for anglerad01
2.As the expression will result a double its obvious as here we used Math.Pi.
3.float data type cannot take value of double data type.
4.So double anglerad01 should be used.
Also, for Radians = (Degrees × π)/180° . Degree is used to calculate radian not radius of circle.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("1st: Please enter a radius (integer) :");
int radius01 = input.nextInt();
System.out.println("1st: Please enter angle (in degrees between 0 and 360) :");
int angledeg01 = input.nextInt();
input.close();
double anglerad01 = radius01 * (Math.PI / 180.0f);
System.out.printf("%s%14s%15s%19s%24s\n", "Radius (inch)", "Angle (deg)", "Angle (rad)", "ArcLength (inch)", "Polar Area (sq. inch)");
System.out.printf("%13d%14d%18f", radius01, angledeg01, anglerad01 );
}
Related
im trying to make a code block to compute the volume of a cylinder but im unfamiliar with java so this is what i had so far. Is there anyway to re-purpose it so i can compute the volume?
package javaapplication231;
import java.util.Scanner;
public class JavaApplication231 {
public static void main(String[] args) {
int a,b,i,product;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of a and b: ");
a = keyboard.nextInt(); //Line 6
b = keyboard.nextInt();
i=0;
product=0;
while (i<a){
i=i+1;
product= product +b;
}
System.out.println("Product=" + product);
}
}
V =PIr^2h
where r is the radius and h is the height
user input: r and h
PI= 3.1416
To calculate the volume of Cylinder simply evaluate the expression, why do you need loop and do the addition. As you mentioned use the formula to calculate the
Volume=PI*radius2*height
int radius, height;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of radius and Height: ");
radius = keyboard.nextInt(); // Line 6
height = keyboard.nextInt();
double volume = 3.14 * radius * radius * height;
System.out.println("Volume=" + volume);
I'm making a trig calculator to practice aviation problems for fun and can't convert radians to degrees properly in java.
I've tried taking altitude divided by Math.tan(angle) and times it by (180 / Math.PI) but this doesn't give me the answer I'm looking for.
The numbers I've tried include alt = 500, angle of approach = 3. My code will store these values and take 500/tan(3) * (180/Pi) and I'm unsure why this isn't the correct trigonometry behind it.
public static void approachPath() {
System.out.println("FINDING THE IDEAL APPROACH PATH . . . ");
System.out.println("What is the altitude of the aircraft:");
double alt = scan.nextDouble();
System.out.println("What is the angle of approach:");
double angleofapproach = scan.nextDouble();
//line my problem occurs on
double approachPath = (alt / Math.tan(angleofapproach)) * (180 / Math.PI);
System.out.println("The ideal approach path is: " + approachPath);
}
I'm expecting the answer 9,541feet so I can move on to writing the rest of the method to find the final approach path in nautical miles.
You were almost right. Just instead of rad to deg, it should be deg to rad.
double angleofapproach = toRad(scan.nextDouble());
double approachPath = (alt / Math.tan(angleofapproach));
// deg to rad
public static double toRad(double deg) {
return deg * (Math.PI / 180);
}
I was bored and wanted to practice my java coding skills. I made a program that finds the area of a polygon based on what you know (radius, perimeter, apothem).
Here's a portion:
static void pentagon() {
System.out.println("Select what you know");
System.out.println("[1]Perimeter\n[2]Apothem\n[3]Radius");
info = input.nextInt();
if (info == 1) {
System.out.println("What is the perimeter of the pentagon?");
double per = input.nextDouble();
double apothem = per * .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
} else if (info == 2) {
System.out.println("What is the apothem of the pentagon?");
double apothem = input.nextDouble();
double per = apothem / .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
} else if (info == 3) {
System.out.println("What is the radius of the pentagon?");
double rad = input.nextDouble();
double per = rad / .1701301617;
double apothem = per * .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
}
}
Due to the problem that all those decimals (ratio of apothem to perimeter) I had to figure out myself, I could only code a few useful ones.
If I knew how to use tangents, I could figure this out.
Ex: double apothem = length / tan(360/10/2)
(An apothem of a decagon)
Can someone show me how to code the previous line?
The recomended way would be to use java.lang.Math.tan(double a)
double apothem = 1 / java.lang.Math.tan( (2*java.lang.Math.PI)/(10*2))
unless there is some reason why you need extraordinary precision and this does not provide it. Then you may be able to find some third party alternative.
You're looking for the java.lang.Math class which has all the trig functions along with other useful constants like e and PI
So the apothem of a decagon where each side was length long and the equation = length/ 2 tan(180/ n) would be (after importing the Math class by putting at the top of your java file import java.lang.Math; )
EDIT
As user ajb points out, Math.tan() takes radians so you have to convert degrees to radians so you have to use toRadians() to convert from degrees to radians:
double apothem = length / (2 *Math.tan(Math.toRadians(180/10))
I know how to start it out and I know how to put in the scanners and everything, but in school, I've never really learned about longitude and latitude formulas and how to convert those points into radians. So I'm pretty much stuck on this Java problem. Here is what I have so far:
import java.util.*;
class DistanceCalculator {
// Radius of the earth in km; this is the class constant.
public static final double Radius = 6372.795;
/**
* This program computes the spherical distance between two points on the surface of the Earth.
*/
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
intro();
System.out.print("Longitude (degrees.minutes) ");
double Longitude = console.nextDouble();
System.out.print("Latitude (degrees.minutes) ");
double Latitude = console.nextDouble();
}
public static double distFrom(double lat1, double lng1, double lat2, double lng2);
double Latitude = Math.toRadians(...);
}
public static void intro() {
System.out.println("This program computes the spherical distance between two points on the surface of the Earth.");
System.out.println("\tPlease start by entering the longitude and the latitude of location 1.");
}
}
In Java IDE, they say that Longitude and Latitude points (the ones underneath the intro();) are not used, and I know why, since I haven't really defined them yet.
I know I'm missing the formula for longitude and latitude. In my book, it wants me to use the spherical law of cosines, and since I've never learned this at school, no matter how hard I study the formula from the websites I sought out, I don't know how to transfer that into Java language.
Another problem is, how do I transfer degrees and minutes from a longitude/latitude point into radians? Do I have to use Math.toRadians thing? Oh yeah and also, my answer has to be in kilometers.
Updated: The math functions some of you guys are talking about confuses me greatly. In school (I'm a high schooler), even at Math IB SL, my teacher has never taught us how to find long/lat. points...yet. So it's hard for me to grasp. Since the spherical law of cosines formula is online, do I basically just take that formula and convert it into "java language" and plug it into my program?
The key word you need to search for is the "Haversine formula".
An easier to understand method, but one which is not quite so accurate for small distances, is to recall that the angle between two vectors A and B can be calculated using the dot product:
A ⋅ B = |A| * |B| * cos(theta)
so if you convert your polar lat/long pairs into 3D cartesian coordinates (and yes, you'll need to use Math.toRadians(), Math.cos() and Math.sin() to do that, and then calculate the dot product, you'll then get cos(theta), so use Math.acos() to get theta.
You can then work out the distance simply as D = R * theta, where R is the radius of the Earth, and theta remains in radians.
I suggest to read more about WGS84.
Mathematical explanations here.
You may look at this link for the logic.
http://aravindtrue.wordpress.com/2009/06/30/calculate-distance-using-latitude-and-longitude-php-mysql/
Function in PHP... I don't know Java. So some one edit my post. Here is the PHP function:
function getDistanceBetweenPointsNew($latitude1, $longitude1,
$latitude2, $longitude2, $unit = 'Mi')
{
$theta = $longitude1 - $longitude2;
$distance = (sin(deg2rad($latitude1)) *
sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) *
cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
switch($unit)
{
case 'Mi': break;
case 'Km' : $distance = $distance *1.609344;
}
return (round($distance,2));
}
also to get value from MySQL database:
Calculate distance given 2 points, latitude and longitude
I tried to create a java function, I don't know if it work or not.
try this. If any one can help, try edit my java code.
import java.math.BigDecimal;
public static double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
public static double distFrom(double lat1, double lng1, double lat2, double lng2, String unit)
{
double theta = lng1 - lng2;
double distance = (
Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2))
)+(
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta))
);
distance = Math.acos(distance);
distance = Math.toDeg(distance);
distance = distance * 60 * 1.1515;
switch(unit)
{
/* Mi = miles, Km = Kilometers */
case "Mi" :
break;
case "Km" :
distance = distance *1.609344;
break;
}
distance = round(distance, 2, BigDecimal.ROUND_HALF_UP);
return distance;
}
import java.util.*;
public class SphericalDistance {
public static void main(String[] args){
System.out.println(" This program computes the spherical distance\n between two points, 1 and 2.");
System.out.println(" Please enter the latitude and longitude for \n each point as a pair of integers, degrees \n followed by minutes:");
System.out.print("Latitude 1:");
Scanner s=new Scanner(System.in);
double latangledeg = s.nextDouble();
double latanglemin = s.nextDouble()/60;
double phideg = latangledeg + latanglemin;
double phi1 = phideg * Math.PI/180;
System.out.print("Longitude 1:");
double lonangledeg = s.nextDouble();
double lonanglemin = s.nextDouble()/60;
double lambdadeg = lonangledeg + lonanglemin;
double lambda1 = lambdadeg * Math.PI/180;
System.out.println("Latitude 2:");
double latangledeg2 = s.nextDouble();
double latanglemin2 = s.nextDouble()/60;
double phideg2 = latangledeg2 + latanglemin2;
double phi2 = phideg2 * Math.PI/180;
System.out.println("Longitude 2:");
double lonangledeg2 = s.nextDouble();
double lonanglemin2 = s.nextDouble()/60;
double lambdadeg2 = lonangledeg2 + lonanglemin2;
double lambda2 = lambdadeg2 * Math.PI/180;
double lambdaf = lambda2 - lambda1;
double angdistance = Math.acos(Math.sin(phi1)*Math.sin(phi2) + Math.cos(phi1)*Math.cos(phi2)*Math.cos(lambdaf));
System.out.println("Angular Distance = " + angdistance + " radians");
int distancekm = (int)(angdistance * 6372.795);
int distancemi = (int) (distancekm * .621371);
System.out.println("Distance = " + distancekm + " kilometers");
System.out.println("Distance = " + distancemi + " miles");
s.close();
}
}
import java.util.Scanner;
public class a {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("enter #");
long a = in.nextLong();
long b = (long) Math.toRadians(a);
long c = (long) Math.tan(b);
System.out.println(c);
}
}
Above is my attempt to make Math.tan spit out the correct values of an angle in degrees. I know the method math.tan accepts only angles in radians as a parameter. I can't get correct value for angles of 90, 270... Using a double doesn't yield correct answers.
You will need to use doubles for b and c.
However, note that tan of 90 and 270 degrees is undefined, for example see the graph here:
http://www.intmath.com/Trigonometric-graphs/4_Graphs-tangent-cotangent-secant-cosecant.php
Of course you cannot get a nice value for 90 degrees, because tan is not defined for that angle. This is clear from the definition
tan(x) = sin(x) / cos(x).
If x = 90°, then cos(x) = 0, so you get a division by zero. The same thing applies to 270°, and in fact all odd multiples of 90°. (Now, computers are numerical beasts, and pi/2 radians cannot be represented exactly (in any base), in addition to cos being computed numerically, so instead of NaN or INF you will likely get a huge number, because the denominator is very close to zero, but not necessarily equal to zero, because of numerical approximation.)
Also, long is an integer type. Integer precision might be good enought for your needs when it comes to angles expressed in degrees (0, 1, 2, ..., 358, 359). But in radians, one full lap around a circle corresponds to the angles 0 radians to 2π, where 2π ~ 6.28. Hence b cannot be an integer, and neither can c.
This works just fine:
public class TanDemo
{
public static void main(String[] args)
{
for (int degrees = -89; degrees < 90; ++degrees)
{
double radians = Math.toRadians(degrees);
System.out.println("deg: " + degrees + " rad: " + radians + " tan: " + Math.tan(radians));
}
}
}
Use double as a starting point for the input and output of methods like Math.toRadians(double)=>double and Math.tan(double)=>double:
import java.util.Scanner;
public class a {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("enter #");
double a = in.nextDouble();
double b = Math.toRadians(a);
double c = Math.tan(b);
System.out.println(c);
}
}
On a domain note – as Saxon Druce pointed out – the tangent of n*90° (n*π/2 rads) is undefined. Correct answers at exactly n*π/2 for n is an odd whole number are not possible. More specifically tan(x) approaches ∞ as x increases towards n*π/2. It is also true that tan(X) approaches -∞ as x decreases towards n*π/2.