Trouble converting exponent equation into Java code [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I need to convert a=n×s^2 into Java. My code looks something like this:
public float ABC( float n , float s)
{
a = n*s;
return a*a;
}
However if i print this out it returns the following error:
This method must return a result of type float
because of this I don't think my solution is correct. Would anyone be able to offer any solutions? I'm very new to Java.

You should declare the type of the variable a:
public static float ABC( float n , float s) {
float a = n * s;
return a * a;
}

You need to declare the type for a, and since you are not using any instance variables, you can make the method static.
Furthermore, you could pass the result to Math.pow and cast the resulting double to a float.
public class Question64613567 {
public static void main(String[] args) {
System.out.println(abc(2, 3)); // 36.0
System.out.println(abc2(2, 3)); // 36.0
}
public static float abc(float n, float s) {
float a = n * s;
return a * a;
}
public static float abc2(float n, float s) {
return (float) Math.pow(n * s, 2);
}
}

Related

What causes this code not to compile correctly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Im building a program to round to two decimal places. The user types in a float that they what rounded and it returns that double rounded to the nearest two decimal places. Theres no errors in the code but after compiling the code returns 0.0. Greatly appreciate the help!
package Exercises;
import java.util.Scanner;
class Rounding {
float precise;
float Precision(float f){
f = precise;
precise = (float) Math.round(f*100)/100;
return precise;
}
}
public class Round {
public static void main(String[] args) {
float originalfloat, roundedfloat;
System.out.println(" Type in float number");
Scanner type = new Scanner(System.in);
originalfloat = type.nextFloat();
Rounding ro = new Rounding();
roundedfloat = ro.Precision(originalfloat);
System.out.println(" Original float number :" + originalfloat);
System.out.println();
System.out.println("Rounded float number " + roundedfloat);
}
}
The problem is in this part:
float Precision(float f){
f = precise;
precise = (float) Math.round(f*100)/100;
return precise;
}
When you instantiate a Rounding object here:
Rounding ro = new Rounding();
you initialize precise to 0, because that's what Java does when you don't explicitly initialize primitive numeric types:
float precise;
One you instantiate Precision with a value, you are replacing it with the contents of precise, which has been initialized to 0:
float Precision(float f){
f = precise; // <-- right here, 'f' becomes zero
precise = (float) Math.round(f*100)/100;
return precise;
}
Since f is now 0, Math.round(f*100)/100 will also be 0, and thus the result of the function is 0.
Just remove this line: f = precise; because you dont use given parameter named f then. You just use default value of precise which is 0.0.
float preciseNumber(float f){
precise = (float) Math.round(f * 100)/100;
return precise;
}
I would change this line
precise = (float) Math.round(f*100)/100;
With this line
precise = (float) Math.floor(f*100)/100;

How to calculate gravitational force? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am getting the wrong output for the code below. G = 6.674 x 10^-11. I am supposed to get 3.559466666666664e+22 but I am getting 8.0088E44. Can someone please explain what I have wrong on my code? I will appreciate it. The inputs are 2e30 6e24 1.5e11
import stdlib.StdOut;
public class GraviForce {
public static void main(String[] args) {
double m1 = Double.parseDouble(args [0]);
double m2 = Double.parseDouble(args [1]);
double r = Double.parseDouble(args [2]);
double G = 6.674e-11;
double f = G * (m1 * m2) / r * r;
StdOut.println(f);
A simple operator associativity mistake - missing parens:
double f = G * (m1 * m2) / (r * r);

why i cant take one float and one Boolean variable to convert gallons to letre [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
// convert gallons to leter
class galtolit
{
public static void main(String[] args)
{
double liter;// if this variable is float it throws incompatible type error
// why i can't take float?
double gallons =10;
liter= gallons * 3.7854;
System.out.println(gallons + " gallons = "+ liter+" liter");
}
}
if this variable is float it throws incompatible type error
why i can't take float?
Because you can't take a double value and downgrade it to a float without an explicit conversion, because you'd lose precision in the process. Remember: float is a 32-bit IEEE-754 single precision floating-point value; double is a 64-it IEEE-754 double precision floating-point value. (More in the JLS)
There's no reason to use float here. Stick with the double, which gives you greater precision.
To convert the double to float, you will have to add "f" for type casting. See below:
class galtolit {
public static void main(String[] args) {
float liter;// if this variable is float it throws incompatible type
// error
// why i can't take float?
float gallons = 10;
liter = gallons * 3.7854f;
System.out.println(gallons + " gallons = " + liter + " liter");
}
}

Quick java clarification needed for student [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am reviewing a piece of code that looks like this:
float x = 9;
float y = 5;
int z = (int)(x / y);
Question:
I am wondering why there is a second int on line 3 when it is already declared that z is an int. Thanks in advance.
The result of the division x / y is a float. Java doesn't allow you to assign to an int variable like this, with a narrowing primitive conversion (here, to an int), because this would potentially lose precision. But Java will allow you to do this with an explicit cast, in which Java assumes you know what you're doing.
You have to declare the result (x/y) to be int, otherwise you are trying to set the value of an int variable with a float, which generates a compiler error. This purposeful declaration is required when reducing the precision or range of a number.
According to compilers we are kiddo's.. therefore he(the compiler) wants us to explicitly mention that what we are doing is on purpose and not a mistake, hence we need to specify explicitly..
These are the two scenarios:
1) With (int):
**Program:**
class fox
{
public static void main(String args[])
{
float x = 9;
float y = 5;
int z = (int)(x / y);
System.out.print(z);
}
}
**Output:**
# 1: hide clone input 8 seconds ago
result: success time: 0.07s memory: 380224 kB returned value: 0
input: no
output:
1
2) Without (int):
**Program:**
class fox
{
public static void main(String args[])
{
float x = 9;
float y = 5;
int z = (x / y);
System.out.print(z);
}
}
**Output:**
Main.java:7: error: possible loss of precision
int z = (x / y);
^
required: int
found: float
1 error
# 1: hide clone 6 seconds ago
result: compilation error
// see the compiler cannot understand that we wish to do it purposefully,,

Meaning of the asterisk symbol in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to change the meaning of the asterisk symbol (multiplication operator) in Java?
I want to do this:
int ex = 600 * 0.98;
It fails because you can't convert from double to int. Can I make it so that I'm able to convert the double to an integer? Or does the
asterisk only have one meaning that can't be changed?
I just want the OPERATION in math, not a string.
To have an operation, you must have code. One way of doing this is to have a method like
public static double ex(double factor) {
return factor * 0.98;
}
or if the factor is a field
private double factor = 600;
public double ex() {
return factor * 98 / 100;
}
public static void main(String... ignored) {
Main m = new Main();
System.out.println("factor: "+m.factor+" ex: "+ m.ex());
m.factor = 700;
System.out.println("factor: "+m.factor+" ex: "+ m.ex());
}
prints
factor: 600.0 ex: 588.0
factor: 700.0 ex: 686.0
As you can see ex() is re-evaluated each time it is used.
Why do I * 98 / 100? I do this as each value can be represented exactly however 0.98 is not represent exactly and can have a small error.
System.out.println(new BigDecimal(0.98));
prints the closest representable value to 0.98
0.979999999999999982236431605997495353221893310546875
If you want to store text you need to do something like
String ex = "600 * 0.98";
An int value is for storing a whole number which is a 32-bit signed value, nothing else.
Uhhh.
The short answer is that you can't do that. A variable of type int can only store an integer value. The expression you assign to the int has to evaluate to an integer.
What integer value do you want assigned to ex?
int ex = 588; // 600 * 0.98
What meaning are you associating with the asterisk between two numeric values that is not multiplication?
If you want to store an array of characters, then:
char[] ex = "600 * 0.98".toCharArray();

Categories

Resources