What does *= mean in Java? [closed] - java

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'm going through a tutorial and I found this operator but I'm not sure what it does.
int number = Integer.parseInt(tfInput.getText());
number *= number;
tfResult.setText(number + "");
Thanks.

a *= b; is equivalent to a = a * b;
You're probably (maybe?) familiar with the += operator. There is a similar operator for all the basic math functions.
+=: a += b; is equivalent to a = a + b;
-=: a -= b; is equivalent to a = a - b;
*=: a *= b; is equivalent to a = a * b;
/=: a /= b; is equivalent to a = a / b;
%=: a %= b; is equivalent to a = a % b;
And please make note of #ruakh's comment:
Note that a *= b evaluates a only once, whereas a = a * b evaluates it
twice. (That doesn't make a difference if a is just a variable or
field name, but if it's a more complicated expression, such as f().x
or f.g.x, that can matter a great deal.)

Related

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);

Function floor of BigInteger [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 6 years ago.
Improve this question
I'm looking for a function such as Math.floor(int x) but for a variable of type BigInteger. Does anyone know how to do it?
Let's suppose a and b are int variables. In that case a/b is an int. Test case:
int a = 5;
int b = 2;
System.out.println(a / b); //expected output is 2
Let's suppose a and b are integers, but we intend to convert the result of the division into a float. The result depends on the way of conversion. Test case:
int a = 5;
int b = 2;
System.out.println((float)(a / b)); //expected output is 0.0
System.out.println(((float)a) / b); //expected output is 2.5
Let's suppose a and b are BigInteger variables. a.divide(b) will return a BigInteger value (so floor is not needed) unless b is 0 in which case it will throw an exception. Source.

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,,

java program for division of one number of any range by another [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a program in java to divide one number by another. But these number can have any number of digits(means the numbers may be of 100,200 or more than these digits.)
If a number can be of virtually any length, use BigInteger. BigInteger provides a divide method.
Here's an example:
BigInteger a = new BigInteger("7583584848488756569");
BigInteger b = new BigInteger("-357457473437373");
BigInteger x = a.divide(b);
The java.math.BigInteger.divide(BigInteger val) returns a BigInteger whose value is (this / val).
Using BigInteger
BigInteger bi1, bi2, bi3;
bi1 = new BigInteger("-100");
bi2 = new BigInteger("3");
// divide bi1 with bi2
bi3 = bi1.divide(bi2);
String str = "Division result is " +bi3;
// print bi3 value
System.out.println( str );
The output will be
Division result is -33
A facetious answer:
For a numeric value 'x' and a numeric value 'y' there is a special operator / that when placed between these two values evaluates to a number representing the number of times the second can "fit" into the first i.e.
int x = 200;
int y = 100;
System.out.println(x / y);
will return
2

Solve an Integral in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to develop a program in Java to solve some integrals. Integrals like this:
I've looked for some functions to do this, in java.Math but I didn't find anything.
Has anyone an idea to get a solution for this? (Maybe some extra libraries or something like that).
The Wikipedia article on Numerical Integration has a section on methods for one-dimensional integrals.
You should have no problem implementing the "trapezoidal" or "rectangle" rule.
The Apache Commons Math library contains, in the Numerical Analysis section, four different numerical integrators:
Romberg's method
Simpson's method
trapezoid method
Legendre-Gauss method
Take a look at JScience
Check out Simpson's Rule on Wikipedia.
/*------------------------------------------------------------------------------------------------------
* Small program that numerically calculates an integral according to
* Simpson's algorithm. Before executing it, you must enter:
* - the expression of the function f: line 12;
* - the lower and upper limits b of the integral: lines 39 and 40;
* - the number of measurements n (n is integer !!!): line 41.
*------------------------------------------------------------------------------------------------------*/
// Class function: Defines Simpson's rule
class Function{
// Define the function to integrate
double f (double x) {
return Math.Cos(x);
}
// Simpson's method for integral calculus
// a = lower bound
// b = upper bound of integration
// n = number of passes (higher = less margin of error, but takes longer)
double IntSimpson(double a, double b,int n){
int i,z;
double h,s;
n=n+n;
s = f(a)*f(b);
h = (b-a)/n;
z = 4;
for(i = 1; i<n; i++){
s = s + z * f(a+i*h);
z = 6 - z;
}
return (s * h)/3;
}
}
class integration{
// Class result: calculates the integral and displays the result.
public static void main(String args[]){
// Call class function
Function function;
function = new Function();
// ENTER the desired values of a, b and n !!!
double a = ???? ;
double b = ???? ;
int n = ???? ;
// Applies simpson method to function
double result = function.IntSimpson(a,b,n);
// Show results
System.out.println("Integral is: " + result);
}
}

Categories

Resources