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;
Related
I need to put this (Math.random() * 37) into a working method.
This is what I made with a tutorial but there is an error on the first row "identifier expected". What is wrong with this code? Please help.
public static double hodKulickou (double)
{
return (Math.random() * 37);
}
You almost got it right. You should consider understanding the principles behind your methods declaration.
In your case you did not provide a local name for the method to assign to the double value it was expecting. Read on if you're interested in understanding the principles as I understand them.
Each word in the method declaration serves a purpose. From left to right.
public - means this method can be accessed publicly and not just by code in that class.
static - means the methods is static and belongs to that class, is not a object member.
void - means the methods does not return any value.
yourmethod name - any name you see fit.
Now the interesting part. The method parameters. These are the values you may pass into the method. In the paramaters you include the type and a name for the value.
eg: methodName(int anInteger)
This means the method can expect an integer type to be passed to is and the method will call that integer anInteger for use within it's body. For whatever purpose you see fit.
You have to give a name to the double parameter of your method.
public static double hodKulickou (double name) {
return (Math.random() * 37);
}
Of course, since you are not using the double parameter, you can just remove it :
public static double hodKulickou () {
return (Math.random() * 37);
}
Did you really followed the instructions of the tutorial ? You must provid an identifier to your parameters. That's exactly what the error message tells you btw.
public static double hodKulickou (double identifier) {
return (Math.random() * 37);
}
However, you pass a parameter to your function but don't use it so actually it should be :
public static double hodKulickou () {
return (Math.random() * 37);
}
Also, since you don't use the parameter, you can omit it:
public static double hodKulickou () { // <-- skipped
return (Math.random() * 37);
}
Your method doesn't appear to need a parameter, this
public static double hodKulickou (double)
should be
public static double hodKulickou ()
or
public static double hodKulickou (int val) { return (Math.random() * val); }
if you want to pass the "37" in.
Here is the code that keeps saying illegal start of expression:
public static conversionRate= 4.546;
Here is the full code:
/**
* Write a description of class VolumeConversion here.
*
* #author (Aneeqa Rustam)
* #version (07/08/2014)
*/
public class VolumeConversion
{
// instance variables - replace the example below with your own
/**
* Constructor for objects of class VolumeConversion
*/
public VolumeConversion()
{
public static conversionRate= 4.546;
znaslcmlkmlskm(String[]args)
//Declare the variable and constants
double litres= 0;
double gallon= 14;
//Perform the conversion calculation
litres= gallon* conversionRate;
//This is the output result that is going to be shown to the user
System.out.println("The number of litres in "+gallons+ "gallons is" +litres);
}
}
You need a type for the variable. For example:
public static float conversionRate = 4.546f;
You also want to place that outside of the constructor, as a class level variable.
Type is missing in the variable declaration
The variable conversionRate doesn't have a type in its declaration.
Possible solutions:
public static float conversionRate = 4.546f;
public static double conversionRate = 4.546;
Besides that you try to declare this variable in the constructor (a "method"). That does not work. It has to be declared within class and not in methods.
Well, this one is quite obvious - you haven't defined the datatype for your conversionRate variable. What you'd probably want to use here is the double datatype, but I'd also suggest looking into BigDecimals for further reference.
Code sample:
public static double CONVERSION_RATE = 4.546;
I would personally recommend against using the float datatype (as #MrTux recommended) in real life projects, as it tends to make your code clumsier (unnecessary casting & parsing) and has the obvious limitation of a restricted value range. The performance penalty that results from the use of double in place of float, however, is miniscule in most cases.
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);
}
So, I created a simple class named Test, as follows:
import prog.utili.IntegerB;
//It's a custom class
class Test
{
public static void main(String[] args)
{
IntegerB a = new IntegerB(1);
IntegerB b = new IntegerB(2);
IntegerB sum = a.plus(b);
System.out.println(sum);
}
}
I wanted to practise with inheritance so I created two custom classes. Fraction...
package prog.utili;
public class Fraction
{
private int num;
private int den;
public Fraction(int x, int y)
{
[...]
}
public Fraction(int x)
{
this(x, 1);
}
public Fraction plus(Fraction f)
{
int n = this.num * f.den + this.den * f.num;
int d = this.den * f.den;
return new Fraction(n, d);
}
[...]
}
...and IntegerB:
package prog.utili;
public class IntegerB extends Fraction
{
public IntegerB(int num)
{
super(num);
}
public IntegerB plus(IntegerB other)
{
return (IntegerB)this.plus(other);
}
}
The problem is I keep getting the same error:
at prog.utili.IntegerB.plus(IntegerB.java:11)
I know I could simply solve the problem by just deleting the last method on IntegerB and replacing the 9th line of Test.java with
IntegerB sum = (IntegerB)a.plus(b)
but I absolutely want to do it using the inheritance rules over the "plus" method!
To implement the method plus(IntegerB), you call plus(IntegerB), which calls plus(IntegerB), etc. etc. until you get a StackOverflowError.
Provide an actual implementation for your method:
return new IntegerB(this.getNum() + other.getNum());
or
return new IntegerB(super.plus(other).getNum());
Also note that replacing the last line of Test.java by
IntegerB sum = (IntegerB)a.plus(b);
wouldn't work, since the plus() method in Fraction doesn't return an IntegerB, but a Fraction. You would thus get a ClassCastException.
The problem here is that IntegerB.plus does not override Fraction.plus, it overloads it. This is because the argument types are different. Thus when IntegerB.plus calls this.plus(other), it ends up calling itself, which then calls itself, which then calls itself until you get a StackOverflow (thus sending you to stackoverflow : )).
It seems like you want to call plus(Fraction) instead of plus(IntegerB). To do that, you can explicitly upcast other:
return plus((Fraction) other);
This cast has no effect other than to tell the compiler that you want to call the version of plus that handles Fractions, even though you know you have an IntegerB.
However, this method would not return an IntegerB, but just a Fraction whose denominator is 1. You could conceivably override plus to return an IntegerB if the denominator of the result is 1, but that might lead to unexpected situations where a.plus(b) is not equal to b.plus(a) because one is a Fraction while the other is an IntegerB. Alternatively, you could return IntegerB objects from Fraction.plus when possible.
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.