Needing help on java methods, the main method has no error but it is the additio
ID: 3677050 • Letter: N
Question
Needing help on java methods, the main method has no error but it is the additional methods of trying to find the x and y coordinates where there is error and I was wondering what I neededd to change to make it correct. Here is my program so far:
import java.util.Scanner;
public class methAng
{
public static void main(String arg[])
{
double r = -1;
Scanner in = new Scanner(System.in);
while (r < 0)
{
System.out.println("Enter the magnitude ");
r = in.nextDouble();
}
double ang = -1;
while (ang < 0)
{
System.out.println("Enter the angle");
ang = in.nextDouble();
}
System.out.println("Cartesian coordinates of a vector with polar coordinates r = "+r+" and angle = "+ang+" are equal to:");
}
public static void xvalue(double r,double ang)
{return r*(Math.cos(ang));}
public static void yvalue(double r, double ang)
{return r*(Math.sin(ang));}
}
Explanation / Answer
/*Function have return value then the return type of the function
* should have return type
* The program is modified according to method that returns
* the double type*/
//methAng.java
import java.util.Scanner;
public class methAng
{
public static void main(String arg[])
{
System.out.println();
double r = -1;
Scanner in = new Scanner(System.in);
while (r < 0)
{
System.out.println("Enter the magnitude ");
r = in.nextDouble();
}
double ang = -1;
while (ang < 0)
{
System.out.println("Enter the angle");
ang = in.nextDouble();
}
System.out.println("Cartesian coordinates of a vector with polar coordinates r = "
+r+" and angle = "+ang+" are equal to: ("+xvalue(r, ang)+","+yvalue(r, ang)+")");
}
//Note: Change the return type of the method xvalue to double
public static double xvalue(double r,double ang)
{
return r*(Math.cos(ang));
}
//Note: Change the return type of the method yvalue to double
public static double yvalue(double r, double ang)
{
return r*(Math.sin(ang));
}
}
---------------------------------------- ---------------------------------------- ----------------------------------------
Sample Output:
Enter the magnitude
5
Enter the angle
45
Cartesian coordinates of a vector with polar coordinates r = 5.0 and angle = 45.0 are equal to: (2.626609944088649,4.254517622670592)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.