i m solving this question in order to calculate area with answer of double and int.it didnt work, whats wrong with code?
i try to include double radius and int radius inside, it showed cannot be applied to given types.
public static void main(String args[]){
for (int radius = 0; radius < 5 ; radius++)
PrintArea(radius);
for (double radius = 0; radius < 2.6 ; radius+= 0.5)
PrintArea(radius);
}
public static void PrintArea(int radius,double radius){
double area=3.142*radius*radius;
System.out.println(area);
}
i expect it could be calculated and shows answer in double ,but it shows error.
One thing, you should not use the same name for two parameters of a function.
So PrintArea(int radius, double radius) is incorrect and is probably one of the errors you're getting.
The other one, is that you are calling PrintArea with only one parameter, when it expects two.
Rename your parameters int r1 and double r2 instead, like so:
public static void PrintArea(int r1,double r2){
double area=3.142*radius*radius;
System.out.println(area);
}
Or use a PrintArea with only one parameter, of type double:
public static void PrintArea(double radius){
double area=3.142*radius*radius;
System.out.println(area);
}
There are certain issues with your code, your printArea function accepts two arguments, but you are sending only
one while calling the function. Your printArea function needs only one parameter radius (no need of two parameters). The other issue is
that within your printArea function, the data type of parameter is of type int. So when you call this
function with different parameter other than int, it will throw error, as you are calling the function printArea with parameters of type double in your second for loop.
There's a few things going on that can be cleaned up. First of all, why does the PrintArea function need 2 parameters? You call it with only one, and just thinking generally, we don't need two pieces of information to get the area of a circle. So I'm going to guess that you meant only one parameter.
public static void PrintArea(double radius){
double area=3.142*radius*radius;
System.out.println(area);
}
Which will work for both int and double types. Usually we also use camelCase in Java, which means that for methods like "PrintArea" which is made up of multiple words, the first word starts with a lowercase letter, and every other word after that starts with an uppercase letter.
Related
I am trying to convert some old DSP code written in C into Java and later to C#. I do not understand what the argument "double (*Window)(double)" means and how to reference it in the Java code. The following function has me unsure what to do.
void WinFirI(float LowOmega, float UppOmega,
float *Shape, int Len, double (*Window)(double), float shift)
{ int i; double time,phase,shape;
// printf("WinFirI: %5.3f %5.3f %d\n",LowOmega,UppOmega,Len);
for(i=0; i<Len; i++)
{ time=i+(1.0-shift)-(float)Len/2; phase=2*M_PI*time/Len;
if(time==0) shape=UppOmega-LowOmega;
else shape=(sin(UppOmega*time)-sin(LowOmega*time))/time;
// printf("%2d %5.1f %5.2f %7.4f %7.4f\n",i,time,phase,shape,(*Window) (phase));
Shape[i]=shape*(*Window)(phase)/M_PI; }
}
So far I have written this:
public void WinFirI(float LowOmega, float UppOmega,
float[] Shape, int Len, double[] Window, float shift) {
double time;
double phase;
double shape;
// printf("WinFirI: %5.3f %5.3f %d\n",LowOmega,UppOmega,Len);
for(int i=0; i<Len; i++) {
time = i +(1.0-shift) - (float)Len/2;
phase = 2 * Math.PI * time / Len;
if(time==0) {
shape = UppOmega - LowOmega;
}
else {
shape=(Math.sin(UppOmega*time) - Math.sin(LowOmega*time)) / time;
}
// printf("%2d %5.1f %5.2f %7.4f %7.4f\n",i,time,phase,shape, (*Window)(phase));
//Shape[i]=shape*(*Window)(phase)/M_PI;
Shape[i] = shape * Window[phase]/Math.PI;
}
}
Pointer to function with parameter double returning double.
As an added note, implementing this function pointer behavior in Java would be done through an anonymous class/interface.
double Window(double param);
In Java would be:
interface WindowFnWrapper {
double Window(double param);
}
Then the function signature would be:
public void WinFirI(float LowOmega, float UppOmega,
float[] Shape, int Len, WindowFnWrapper WindowWrapper, float shift)
Every time you call Window() you would replace it with WindowWrapper.Window().
The declaration double (*Window)(double) declares a parameter Window, whose value is a pointer to a function. That function must be one which takes a single double as an argument, and returns a double.
In your C code, (well, actually the commented-out part), that pointer gets dereferenced, and the function called, passing phase as its argument. That is, the value of the expression (*Window) (phase) is just the result of calling the function whose address you passed in.
If you're using Java 8 or above, the nearest available equivalent is the Function generic class, from the java.util.function package. So if you write
import java.util.function.Function;
then you can declare the parameter as
Function<Double,Double> window
and use it as
window.apply(phase);
The only real difference between this and your C code is that the type parameters to Java generics can't be primitive types such as double.
Double is a reference type that works a bit like double, except that a Double can be null, whereas a double can't. In effect, this should make no difference to your code, except that you may want to add some error handling to deal with the case where window.apply(phase) evaluates to null.
I've got this code and i have got the answer for it as well, but I don't understand how they come out like that, specifically what does (x+" "+y) part means as well as the changeUs(x, y), can anyone explain?
public class ChangeParam {
public static void main(String[] args) {
int x = 1;
double y = 3.4;
System.out.println(x+" "+y);
changeUs(x, y);
System.out.println(x+" "+y);
}
public static void changeUs(int x, double y) {
x = 0;
y = 0.0;
System.out.println(x +" "+y);
}
}
The answers are:
1 3.4
0 0.0
1 3.4
This is logic, because Java pass by value and not by reference.
When you call changeUs(x, y) the the x and y will change only in the method, for that you get this result.
public static void main(String[] args) {
int x = 1;
double y = 3.4;
System.out.println(x+" "+y);//<<-------- 1- This will print 1 3.4
changeUs(x, y);
System.out.println(x+" "+y);//<<-------- 3- This will print 1 3.4
}
public static void changeUs(int x, double y) {
x = 0;
y = 0.0;
System.out.println(x +" "+y);//<<-------- 2- This will print 0 0.0
}
You should read and study the Tutorial and the Java Language Specification. The semantics of operators such as + are among the first things explained. There you'll find that when either operand of + is of type String that the other operand is converted to a String via an implicit call to String.valueOf, making the whole expression a String concatenation.
System.out.println() - here, println is a method which takes in a String type as argument (either a String, or a type which has been converted to a String).
So , we pass a String. But, you've two things to display (x and y), how will you do that?
In java, '+' operator acts as a concatenation operator when operands are String type, so x+" "+y will combine 'x' and 'y' with a space in between.
So, if x="HELLO" and y="WORLD" , so x+y will be HELLOWORLD.
Giving a space in between, x+" "+y will give us a cleaner and more readable answer: HELLO WORLD
where as
changeUS()
is a method (specifically a static method) which takes in two parameters (much like a math function which needs two parameters).
So, we pass two "things" to the function. Now, how do I know the function needs two parameters? To know that I must know where the function is defined. As I see, the function is defined as:
public static void changeUs(int x, double y) {
x = 0;
y = 0.0;
System.out.println(x +" "+y);}
The first line: enter code here:public static void changeUs(int x, double y)which says that changeUs takesint and double` as their parameters.
So, pass an integer and a floating point number (double is a higher precision floating point number) .
But, why did we do System.out.println but for changeUs we just called it without any of that prefixes?
This is because changeUs is a 'static' method and doesn't need an object to operate upon.
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
double a=sc.nextInt();
double b=sc.nextInt();
double n=sc.nextInt();
double result=0;
for(int j=0;j<n;j++)
{
double power;
double m;
power = Math.pow(double m, double n);
double result = result+power*b;
System.out.println(result);
}
}
the compiler says .class is required in the statement " power = Math.pow(double m, double n);"
what did i miss in that statement...thank you
You don't specify the types of the arguments when you call a method.
Change
power = Math.pow(double m, double n);
to
power = Math.pow(m, n);
You should, however, give an initial value to m, or the code won't pass compilation.
There are different errors or mistakes in your code. I will go through them from top to bottom.
Your variable a is never used. You could delete the whole double a=sc.nextInt(); line without affecting your program
Your variable m is not initialized and has no value when you use it the first time
When calling a method you don't specify the data types. The data types will be taken from the variables you pass into that method. So there could be methods with the same name but with different data types in their parameter list. Imagine a method int sum(int a, int b) taking the parameters a and b that have to be of integer type. You can easily imagine, that there may be a situation where you don't want to sum integers but doubles. So you could look out for a method double sum (double a, double b) and you could use this method just like the first one but this time for variables/literals of double type. Like I wrote you don't write the data types of the parameters you pass into a method because they are determined automatically. So your Math.pow(..) call has to look like power = Math.pow(m, n);
Your code is lacking two } at the end (for the main method and for the class)
Try to use self-describing names for your variables. A counter named i may be ok but you could easily rename m to base and n to exponent and your program would automatically be easier to read.
Sorry for this basic question, I have tried to check other answers on SO but they haven't been convincing. My code is plain and simple but I do not understand why do I have compiling errors.
Can someone explain this in layman terms? It would be very helpful.
public class gravityCalculator
public static void main(String[] args) {
// TODO Auto-generated method stub
double g= -9.81;
double u= 0.0;
double t= 10.0;
double x_i =0;
double s;
double a=0;
if(a<=0){
a=g;
}
//System.out.println(a);
s= 0.5+a.t^2+u.t+x_i;
}
}
Error Trace
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The primitive type double of a does not have a field t
The primitive type double of u does not have a field t
Edit: This question can also be useful to know about the difference between Datatype and an Object.
Solution : s= 0.5*a*t * t+u*t+x_i;
The multiplication symbol in Java is *, not .. Also, Java does not have an exponentiation operator (and ^ means something different than what you want). Correct syntax for what you're trying to do:
s = 0.5 + a*t*t + u*t + x_i;
Here I just multiplied t*t to get t2. In general, exponentiation can be calculated with the Math.pow() method.
EDIT: Assuming that you want to calculate position at a given time t for a given start position x_i, velocity v and acceleration a, you not only need to correct the syntax as above, but you also need to correct the formula:
s = 0.5*a*t*t + u*t + x_i;
(The 0.5 needs to be a coefficient of at2, not an added term.)
In your expression s= 0.5+a.t^2+u.t+x_i; there is a part u.t that confused the compiler.
It supposed that u had a public property (field) with name t, but it did not. So you get the error.
u has primitive type double. It is not an object, so it does not have any properties.
Rewrite your expression like that: s = 0.5 + a*t*t + u*t + x_i;
It's the line s= 0.5 + a.t
what were you trying to achieve there? perhaps multiplication? in that case it's asterisk:
0.5 + a * t
s= 0.5+a.t^2+u.t+x_i;
You are writing . instead of * for multiplication. . is used to denote methods of fields of the object and primitive objects have no methods or attributes.
Edit: ^ is used as XOR. To square, you have to multiply t*t or use java.lang.Math.pow (I would not recommend using this one as it is way slower.).
The problem is in this part t^2 in java to make a power use Math.pow(), to get t^2 use Math.pow(t,2), also, to multiply in java you have to use * operator instead of . that means object property or attribute, so your complete sentence must be:
Also, to make a correct calculation you must think in operations order, mutiply and divide goes first, so you have to be carefull with parenthesis to get correct result!
s= (0.5+a)*Math.pow(t,2)+u*(t+x_i);
I'm using Heron's formula to find the area of a triangle. Given sides a, b, and c, A = √(s(s-a)(s-b)(s-c)) where s is the semiperimeter (a+b+c)/2. This formula should work perfectly, but I noticed that Math.pow() and Math.sqrt() give different results. Why does this happen and how can I fix it?
I wrote two methods that find the area and determine if it is an integer.
In this first method, I take the square roots and then multiply them:
public static boolean isAreaIntegral(long a, long b, long c)
{
double s = (a+b+c)/2.0;
double area = Math.sqrt(s)*Math.sqrt(s-a)*Math.sqrt(s-b)*Math.sqrt(s-c);
return area%1.0==0.0 && area > 0.0;
}
In this second method, I find the product and then take the square root:
public static boolean isAreaIntegral(long a, long b, long c)
{
double s = (a+b+c)/2.0;
double area = Math.pow(s*(s-a)*(s-b)*(s-c),0.5);
return area%1.0==0.0 && area > 0.0;
}
Can anyone explain why these two methods that are mathematically equivalent give different Values? I'm working on Project Euler Problem 94. My answer comes out to 999990060 the first way and 996784416 the second way. (I know that both answers are very far off the actual)
I would certainly vote for "rounding issues", as you multiply the results of multiple method call in the first method (where every method result gets rounded) compared to the single method call in the second method, where you round only once.
The difference between the answers is larger than I'd expect. Or maybe it isn't. It's late and my mathematical mind crashed a while ago.
I think your issue is with rounding. When you multiply a load of roots together, your answer falls further from the true value.
The second method will be more accurate.
Though, not necessarily as accurate as Euler is asking for.
A calculator is a good bet.
Both methods are problematic. You should in general be very careful when comparing floating point values (that is, also double precision floating point values). Particularly, comparing the result of a computation with == or != is nearly always questionable (and quite often it is just wrong). Comparing two floating point values for "equality" should be done with a method like
private static boolean isEqual(double x, double y)
{
double epsilon = 1e-8;
return Math.abs(x - y) <= epsilon * Math.abs(x);
// see Knuth section 4.2.2 pages 217-218
}
In this case, the floating-point remainder operator will also not have the desired result. Consider the following, classic example
public class PrecisionAgain
{
public static void main(String[] args)
{
double d = 0;
for (int i=0; i<20; i++)
{
d += 0.1;
}
System.out.println(d);
double r = d%1.0;
System.out.println(r);
}
}
Output:
2.0000000000000004
4.440892098500626E-16
In your case, in order to rule out these rounding errors, the return statement could probably (!) something simple like
return (area - Math.round(area) < 1e8);
But in other situations, you should definitely read more about floating point operations. (The site http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html is often recommended, but might be a tough one to start with...)
This still does not really answer your actual question: WHY are the results different? In doubt, the answer is this simple: Because they make different errors (but they both make errors - that's in fact more important here!)