I am very new at Java so forgive me if I ask the stupid question.
I have an assignment where I need to calculate pow of two numbers (lets say it's 2.0^3) also I am supposed to use public static double pow() .
But for some reason I get the following errors:
for the Ecpipse it's- Error: Could not find or load main class exp.e
and for the DrJava- Static Error: No method in static pow has name 'main'
class pow
{
public static double pow( double x, long y )
{
x = 2.0;
y = 3;
double exp = Math.pow(x,y);
return exp;
}
}
I must be missing something very basic. But I already spent the significant amount of time on it and I can't figure it out. Could someone please point me out what am I doing wrong
You are missing your main method.
Every program in Java starts by calling a public static void main(String[]) method. Your code doesn't seem to have this method. You need to create one, for the program to run. All the other methods should be called from this main, or for some method called by main, or some method called by some method called by main. This is the so-called Call Stack, because the methods are "stacked" when called and removed from stack when they are over (hopefully by the return statement, such as in your method). When this stack uses too much memory, we have a "Stack Overflow" error - which, not by a coincidence, is the name of this very website.
Hope that helps.
Related
I'm trying to create a unique static variable for each function pointer and I need to use a function pointer because I plan on using them inside of a struct.
I tried creating a function pointer to a function with a static variable but it's the same variable in both of them.
#include <stdio.h>
void foo()
{
static int test = 10;
test++;
printf("%d\n", test);
}
void (*bar)() = foo;
int main()
{
foo();
bar();
return 0;
}
I expected this to give me 11 and 11 but I get 11 and 12 so it must increment the same variable twice.
This is not something a function pointer can do.
Instead of function pointers, you probably want some kind of object-orientation so you can have several objects each with its own private test field, but sharing the same code.
For this, you need to go to C++ instead of plain C.
(If for some reason this is not available to you and you have to do your stuff in C, there's no real way around giving the function an extra context pointer as a parameter. Or, if you need only finitely many instances of the function, write it several times. They can share a helper function that does the real stuff, but each instance needs to declare their own memory for the helper function to operate on).
I have the following situation:
int aFunction() {
int cc;
double *doubleValue;
double atod();
*doubleValue = atod ("12.3", &cc);
}
As far I understand atod is a pointer to a function, which returns a double, and it seems that, when atod is called, it is not initialized.
Am I right? What can happen, as this code is running in production?
Drop that code from production immediately.
You're dereferencing an uninitialized pointer and getting undefined behavior:
double *doubleValue;
*doubleValue = ...
Anyway this:
double atod();
is a prototype of a function which takes an unspecified number of arguments (and might cause a linking error if it's not defined elsewhere) and returns a double. To have a function pointer you should rather write:
double function() {
return 2.0;
}
int main()
{
double (*atod)(); // Pointer to function
atod = &function;
}
Example
A function pointer would look like this:
double (*atod)();
What you have is a prototype. If you put it outside your function, you'd recognise it:
double atod();
int aFunction() {
int cc;
double *doubleValue = malloc(sizeof(double));
*doubleValue = atod ("12.3", &cc);
}
The compiler/linker simply finds the atod function from somewhere else.
I don't why anybody would choose to do this, rather than include the proper header file.
(I'm assuming the error with the uninitialized double * is caused by you reducing the example.)
There are the following problems I can see from the first glance:
doubleValue is a pointer. You have to allocate a memory underneath before you're trying to dereference it and assign atod() result into.
atod() should be implemented in some library (or imported from shared library), otherwise your code will compile and will not link.
What confused me was I didn't know one could put prototypes inside the body of a function, so I, wrongly, assumed it was a pointer for a function that was never initialized. I searched and found the atod function defined in another library. The code is correct.
The doubleValue error was my mistake, in trying to simplify the problem whne presenting it to you. In reality doubleValue is a parameter of the aFunction, and is already initialized.
Thanks everybody again. You have been very quick and helpfull.
I'm currently learning C, and I was wondering if there was a really elegant way of a struct variable being able to self assign to it's member variables.
i.e.
typedef struct {
double x; double y; double magn = sqrt(pow(x,2) + pow(y, 2));
} vector2d_t
Clearly this does not work. Is it possible to make some type of pre-proc macro, or wrap the structure within something else so the magnitude is automatically assigned every time the members x, y are changed?
Is there some sort of agreed upon method for doing this, or is it necessary to create a function:
void magnitude(vector2d_t *A){A->magn = sqrt(pow(A->x, 2) + pow(A->y, 2));}
and call it every time you create a new vector2d_t?
Unfortunately this is not supported in C and never will be. C is the kind of programming language that allows you do to almost everything, this means everything manually!
Best you can do is create functions of macros that autoupdate this for you:
void update_x(vector2d_t * v, double x) {
v->x = x;
v->magn = sqrt(pow(x,2) + pow(v->y, 2));
}
Your magn is not a property. It is a value depending on two other values. Not only it can't be done automatically, I think that it should never be done automatically. It is yours, as a programmer, choice when and how such value is updated. Lazily, whenever you access it, or proactively, whenever x or y are changed. Or maybe you want to update it periodically, whenever, say, a frame is rendered?
Moreover, logically, mathematically, magn is a function. It is a function with two other parameters. It seems kid of logical to make a function which handles this value somehow.
I am trying to do something interesting(automation) in C. What I want to do is.
Expose an interface to the outer world(developer).
That interface declares some argument based functions.
Will invite developers to implement those functions.
The backend system will calculate some parameters.
Then it will call those implementations by passing the parameters(something like callback).`
It should not be a big deal. But I didn't get any success after going through "think-try" loop so many times. I am getting more and more confused every time.
I am unable to write the code to achieve the functionality. Here is some example code.
Assume I created the below interface.
MyInterface.h
int doSomething(int x, int y);
int doSomethingElseNow(int x, int y);
And asked the developer to include the header file and implement his own definitions for the above specified functions.(some thing like "Interfaces" in Java). Assume he did something like this(might be wrong)
Developer1.c
#include<"MyInterface.h">
int doSomething(int x, int y)
{
return x*y;
}
int doSomethingElseNow(int x, int y)
{
return x+y;
}
Now, my question is how do I call these functions(defined) from some other class. What should be my backend code. This code has to generic. It will be written before developer are asked to define functions. Want to do something like.(is wrong for sure)
Backend.c
#include<"MyInterface.h">
int main()
{
int x, y;
// calculating values of x and y
//calling doSomething
int result = doSomething(x, y);
// doing some calculations on result variable
//calling doSomethingElseNow
int result2 = doSomethingElseNow(x, result);
//do something with result2...
}
Please show me some direction to move on.
You definitely can do this by only declaring and then not implementing your callback functions, as this will force the developer to implement them (if he doesn't, he'll get a linker error). This is what e. g. the SDL library does when it re-#defines the main() function (ugh!)
However, this is not a too wise solution. ("you can" doesn't mean "you should".) Here's why:
On some systems, you won't be able to make a dynamic library out of your code because linkage will fail with undefined symbols.
Only one callback function can be implemented per executable. Sometimes that's insufficient.
The developer can't call his callback function what he wants to call it. This may even introduce name collisions.
It is ugly and conceptually backwards.
So, instead of forcing the implementation of some arbitrarily named functions, make your interfaces expect an explicit pointer to a callback function. This is way more flexible and elegant than your current approach.
I think that some newer languages like JS can do this natively, but I forget the term for it (make a "temporary" function in-line just to pass as a callback)
What I want to do is ...
I'm writing unit tests where I set up expected input & output messages at compile time. Later at run time, I want to do some checks when each output is received or input has been processed, so I added a parameter for a callback function.
That's working fine and I could leave it & move on, but ... I am just curious ...
sometimes a function is overkill and I just need a single comparison; sometime a small block of code would do. Perhaps I could just evaluate these to a zero/non-zero value at run time? But how to pass as a parameter?
At the moment my function has the following signature
void AddExpectedCommand(E_peripheralType peripheral,
communicationBlock_t commandBlock,
errorMessage_t errorMessage,
void *(*DoRunTimeChecks)(E_boolean));
where the final parameter is pointer to a callback function returning boolean.
Is there any way that I could pass a code expression as a parameter instead?
Or does a function seem "cleaner"?
Thanks in advance for any help ...
Update: oops, I got my declaration wrong. I want to pass a pointer so a function which has no parameters and returns an e_Boolean ... how do I do that?
With C++11 you can do the following:
Function taking a function that returns a bool:
void f(function<bool()>);
Call it with a lambda capturing a local variable:
int x = ...;
f([&x]() { return (x > 2); });
Or call it with some function g that returns a bool:
bool g();
f(g);
Or bind some function h that takes an int and returns a bool:
bool h(int x);
f(bind(h, 2)); // ie creates a nullary function from h(2)
What you're looking for is called a "lambda expression" or "anonymous function." And they don't exist in C (but do in C++ with certain qualifications).