I tried to run a simple matlab code in java (I'm new to Java).
In matlab, I created this function :
Function [y] = square[x]
y = sqrt(x)
end
I named the class: square
But when I run the function in Eclipse, I couldn't make it work.
Here is the code in Eclipse:
import square.*;
import com.mathworks.*;
import com.mathworks.toolbox.javabuilder.*;
public class square {
/**
* #param args
*/
public static void main(String[] args) {
square x = new square();
Double z = x.square(8);
}
}
The error is: The method square(int) is undefined for the type square
Any idea? Thanks so much!
You can use the Math.Pow() function in Java to square a number. If you wanted to write your own function, you could do:
class Mymaths
{
public static double Square(double exponent, double number)
{
return Math.pow(number,exponent);
}
}
Then you could use that inside your main method:
public static void main(String[] args)
{
Mymaths.Square(2.0,8.0); //should return 64
}
Sorry if I misunderstood, but that's what I read.
The problem you have is that you do not have defined the method square. That is exactly what the compiler complains about.
Define it like this to return a Double object:
private Double square(int x) {
// do whatever you like here
}
However I think it will be better if you use the primitive double type for simple tests (just be aware for the precision).
Another option is to use one of the methods defined in the Math utility class.
Related
I was wandering how to make custom classes that can be used through out all of java eclipse. For example if you make any basic program you can call pre determined classes, like you can just type Math.abs(x); instead of having to go out of your way and typing
if (x<0) x = x * -1;
else x = x;
I would like to design my own custom classes for basic functions that I use a lot that i can use I in any program regardless of if they are in the same project or not.
custom classes that can be used through out all of java eclipse...
you can do that with any class as soon as you import that and define the right visibility for its data....
class MyRandom{
public static int MultiplybyTwo(int number){
return number * 2;
}
public static int DividebyTwo(int number){
return number / 2;
}
}
import MyRandom;
class Test{
public static void main(string[] args){
int x = MyRandom.MultiplyByThow(9);
System.out.println("the result is: " + x);
}
}
on the other hand, this makes no sense:
else x=x;
So, I have to create 68 different summing methods using the datatypes, int, float, double and short. The class is called, OverloadingSums. Here is an example of one the methods.
public double sum(double x, float y, int z)
{
return x+y+z;
}
Now I'm being asked to create a class called ZeroSum, essentially copying OverloadingSum and returning everything to zero. Here is an example.
public double sum(double x, float y, int z)
{
return 0;
}
Then I'm being asked to create another class called RealSum, which will extend the ZeroSum class. I'm a little confused about the wording of this assignment, not sure if the stackoverflow community could help but I'm just extremly confused.
Here is the assignment requirements:
Now that we have thoroughly explored overloading we are going to
explore overriding. Create a class called ZeroSum.java. Take all of
the methods from OverloadingSum.java and change them to return all
zeros in ZeroSum.java. Next, create a class called RealSum.java. This
class, RealSum, will extend the ZeroSum class. Having all zeros for
our sums isn't very useful. We will need to override the parent, or
super class methods to produce real results. Create the necessary
methods to override all of those in the ZeroSum.java. When you are
done run your classes against DrivingSum.java.
This is what I have in my main method:
public class DrivingSum {
public static void main(String[] args) {
int x = 10;
int y = 20;
// x + y = 30....yay?
ZeroSum zero= new ZeroSum();
RealSum real= new RealSum();
System.out.println("Calling ZeroSum " + zero.sum(x,y) );
System.out.println("Calling ZeroSum " + real.sum(x,y) );
}
}
So, from how I'm understanding this I wrote the following code:
public class ZeroSum extends RealSum{
public double sum(double x, float y, int z)
{
return super.sum(x,y,z);
}
This will grab the method from RealSum, instead of using the sum method located in the ZeroSum class. So when I run the main method, zero.sum(x,y) gives me 30.
The confusion comes from the fact that the assignment asks me to set everything in ZeroSum returning to zero. If I extend ZeroSum to RealSum, it doesn't really make a difference. Am I doing this correctly? or Am I just overthinking this way too much?
The goal of this assignment is to explore overriding concept, where function executed depends on type of object it called upon on run time, so you will have something like this :
public class ZeroSum {
public double sum(double x, float y)
{
return 0;
}
}
public class RealSum extends ZeroSum{
public double sum(double x, float y)
{
return x+y;
}
}
public class DrivingSum {
public static void main(String[] args) {
int x = 10;
int y = 20;
// x + y = 30....yay?
ZeroSum zero= new ZeroSum();
ZeroSum real= new RealSum();
System.out.println("Calling ZeroSum " + zero.sum(x,y) ); //here the sum will return zero
System.out.println("Calling ZeroSum " + real.sum(x,y) ); //here the sum will return 30
}
I believe he's trying to make a point in which whatever you write in the parent class is transferred (this is called inheritage) to the child. You can override this by writing a method in the child class using the same name and arguments as the parent class' method.
I also believe you read the assignment a bit wrong, it said:
This class, RealSum, will extend the ZeroSum class.
I interpret this as RealSum is the child to ZeroSum, not the other way around as such:
public class RealSum extends ZeroSum{
//code code code
}
This means everything in ZeroSum is set to 0 and RealSum set new values, not using the super. I'm not betting my hand this is correct but try to read the assignment again after a break and some fresh air :)
Hope this helps!
For some reason I am getting a precision error when I try to compile my code. The precision error comes in the return of my second method where I am trying to calculate the circumference. What am I doing incorrectly?
public class Methods2
{
public static final double PI = 3.14;
public static double calcCirc(double x)
{
return PI*x;
}
public static int calcCirc(int x)
{
return (2*(double) x)*PI;
}
public static void main(String[]args)
{
System.out.println(calcCirc(10.2));
System.out.println(calcCirc(4));
}
}
You are attempting to return a double value in a method declared to return an int. Java won't let you implicitly narrow your value like that.
If you're okay with the loss of precision, then explicitly cast the value to int -- Java will let you do that.
return (int) ((2*(double) x)*PI);
However, I would change the method to return double instead, not to lose precision:
public static double calcCirc(int x)
... as you already did with your other calcCirc method.
Both versions of calcCirc() ought to return doubles.
Also, side note--consider using different method names since they accept inputs that differ not only in type but also in semantics.
E.g. calcCircFromRadius(double radius), calcCircFromDiameter(double diameter). There's not really a reason to take an int as an input type here since Java will automatically cast ints to doubles for you.
try
public static int calcCirc(int x){
return (int)((2*x)*PI);
}
I need to translate some c++ into java, but I have I few issues.
How to I have to deal with pointers when they are declared as arguments in the Method?
static void test( double *output){}
Also what is and how can I replace struct?
struct test { int t;
int arg;
float *pva;
double *array;
}
And then in the code they use:
double test(struct test *test)
{}
Oh and a last one.. this is also inside struct test, what means :
test->arg
static void test( double *output){}
This needs more context, it can mean a pointer to a double or an array of doubles.
struct test { int t;
int arg;
float *pva;
double *array;
}
A struct is a class with default public access level. You can replace it with a class with public members.
test->arg
This accesses the arg member in test.
You do not have pointers in Java, only references and only for objects (not for primitive types). References are like pointers, but you use the '.' notation instead of '*' or '->'. Also, you do not need to delete objects in Java, you just stop using them and eventually the garbage collector with destroy them.
Answering your points above, from bottom to top:
If test is a pointer to a struct or class in C++, and arg is a member variable of test, then
test->arg
is used to access the member through the pointer. This would map to
test.arg
in Java, if arg is a public member variable of the test object.
I would translate the following:
struct test
{
int t;
int arg;
float *pva;
double *array;
}
...
double test(struct test *test)
{}
to
public class Test
{
public int t;
public int arg;
float [] pva;
double [] array;
}
...
public static double test(Test test)
{}
For the first case, i.e. the function
static void test( double *output){}
you cannot pass a pointer to double and modify the double in Java. You have to return a double. If you need the double also as an input parameter, you specify it as a normal parameter that is passed by value:
static double test(double output)
{}
I hope this helps.
In the simple cases you can replace float * and double * with float [] and double [] However C++ allows you to do all sorts of unpleasant things which are difficult to translate into Java because it is not allowed in Java.
You can replace a struct with a class
If you want to understand basic C++ syntax, I suggest you read a guide on how to program in C++.
(1) static void test( double *output){}
Here double* can be replaced with a Double[] (assume that you do a new Double[]) and the method can be put inside a class.
class testMethod {
public static void test (Double []output) { }
}
(2) how can I replace struct?
It can be replaced with a class.
class test {
public int t;
public int arg;
public Float []pva;
public Double []array;
}
(3) double test(struct test *test) {}
It can be,
double test (test t) {}
(4) test->arg
Java doesn't have pointers (though it's implemented with reference); so above statement will be test.arg
I have some simple java code to call a method with a value to calculate the volume. I just get error like missing ; in JCreator? What is wrong? This is new beginners non object programming level course. Therefore i guess there should be no public static in the method?
public class Matematik {
public static void main(String[] args) {
System.out.println(volym(10));
double volym(int tal){
return round((4 * math.pi * math.pow(tal,3) / 3),2);
}
}
}
The declaration for volym should not be in main:
public class Matematik {
public static double volym(int tal){
return round((4 * math.pi * math.pow(tal,3) / 3),2);
}
public static void main(String[] args) {
System.out.println(volym(10));
}
}
Edit: it's worth noting that you have other issues too. Namely, java.lang.Math.PI (note the casing) and Math.pow. And Math.round....
You can't define a method inside a method. This would be more obvious if you use the IDEs code formatting. Move one of the } up to before the second method (BTW it must be static as well)
Also its Math not math and Math.round on take one value which it rounds to an integer.
If you want to round to two decimal places you can do
Math.round(x * 100) / 100.0;