I'm struggling with visualizing why this code would return what it returns.
public class IntObject
{
private int myInt;
public IntObject() { myInt = 0; }
public IntObject(int n) { myInt = n; }
public void increment() { myInt++; }
}
Driver file
public class IntObjectTest
{
public static IntObject someMethod(IntObject obj) {
IntObject ans = obj;
ans.increment();
return ans;
}
public static void main(String[] args) {
IntObject x = new IntObject(2);
IntObject y = new IntObject(7);
IntObject a = y;
x = someMethod(y);
a = someMethod(x);}
In my head, the program is going
a is y
x is y + 1 or a + 1, which in this case is 8
a is x(8) + 1. so then it is 9
When the program ends the values i thought they would be are
x = 8
y = 9
a = 9
but the correct values are all 9. I think I'm having trouble with aliases. Could anyone help me out and explain aliases and how they are working in this code?
Those are not aliases, they are references.
IntObject x = new IntObject(2); // <-- 2
IntObject y = new IntObject(7); // <-- 7
IntObject a = y; // <-- 7
x = someMethod(y); // <-- 8
a = someMethod(x); // <-- 9.
When you call someMethod,
IntObject ans = obj; // ans points to obj.
ans.increment(); // same as obj.increment();
return ans;
I think you expected
IntObject ans = obj.clone(); // ans points to a copy of obj.
ans.increment(); // now it won't modify obj.
return ans;
you are loosing actual reference of x and a.
public static void main(String[] args) {
IntObject x = new IntObject(2); // "new" is creating a memory block. lets call it 0x10
IntObject y = new IntObject(7); // another "new" is creating a memory block. lets call it 0x20
IntObject a = y; // here, a = 0x20
x = someMethod(y); //someMethod actually takes an element and give same element. so here, x shows 0x20
a = someMethod(x); //someMethod actually takes an element and give same element. so here, x shows 0x20, then a shows 0x20
// at the and, y does not change anytime. so all objects addressing to 0x20
}
someMethod(y) returns y, so x = y;
someMethod(x) returns x, so a = x;
==>> y = x = a
Related
How to change value of a an object of Integer class ex: here I have declared the values of objects x and y to 5 and 6 respectively and I want to swap the value stored in the objects i.e x.intValue() should be 6 and y.intValue() should be 5. Please help.
class second{
public static void main(String[] args) {
System.out.println(In1.a);
Integer x = new Integer(5);
Integer y = new Integer(6);
System.out.println(x.equals(y));
System.out.println(x.intValue());
}
}
Adding a different answer without using a temporary variable
Integer x = 10;
Integer y = 5;
x = x + y;
y = x - y; //y contains 10
x = x - y; //x contains 5
Try,
Integer temp = x;
x = y;
y = temp;
1 ) Have the value of x in a temporary variable
2 ) Assign value of y to x
3 ) Assign value of temp to y
In this way, your values of x and y will get swapped.
You can use a third variable.
public static void main(String[] args){
Integer a = new Integer(6);
Integer b = new Integer(3);
Integer temp = a;
a = b;
b = temp;
}
I'm trying to get my program to out the first 500 values for this formula: -12*ln(1-x) where x is the return of double next(). I don't know what I'm doing wrong because I can't get the right output. The random number uses this formula x(i+1) = (a * x(i) + c) mod k
public class myRnd {
// Linear values for x(i+1) = (a * x(i) + c) % k
final static int a = 7893;
final static int c = 3517;
final static int k = 8192;
// Current value for returning
int x;
int y;
int z;
public myRnd() {
// Constructor simply sets value to half of k
x = (125*k) /1024;
//y = (125*k) /1024;
}
double next() {
// Calculate next value in sequence
x = (a * x + c) % k;
// Return its 0 to 1 value
return (double)x / k;
}
public static void main(String[] args) {
int situation;
double sec_answer;
// Create a new myRnd instance
myRnd r = new myRnd();
// Output 53 random numbers from it
for (int i = 0; i < 53; i++) {
System.out.println (r.next());
}
System.out.println("random variable");
for(int b = 0; b < 500; b++){
sec_answer = (-12)*Math.log(1- r.next());
System.out.println(sec_answer);
}
}
}
I suppose these are the first 5 values you're expecting from your program in each loop!
0.9302978515625
0.270263671875
0.6204833984375
0.90478515625
0.8985595703125
random variable
31.962289651479345
3.78086405322487
11.626283246646423
28.21943313114782
27.45940262908609
In your main method you have only one instance of the class:
// Create a new myRnd instance
myRnd r = new myRnd();
This initialization is propagated to both for loops.
Simple Solution: Add another instance / initialization of myRnd for the second for loop, as an example you could reuse the same variable as r = new myRnd(); before the second loop.
As I have read online that Java is pass by value and a general swap function won't swap the two values. I also read that it's not possible to swap the values of primitive types. I am wondering why the following program works and displays different valies after swap ?
public class swapMe {
public static void main(String[] args) {
int x = 10 , y = 20;
System.out.println("Before");
System.out.println("First number = " + x);
System.out.println("Second number = " + y);
int temp = x;
x = y;
y = temp;
System.out.println("After");
System.out.println("First number = " + x);
System.out.println("Second number = " + y);
}
}
Is it like somewhere, the original values of x = 10 and y = 20 are still stored somewhere and the swapped values displayed are not correct? Please advise. Thanks
Not entirely sure where you're getting that information, but let's take this one at a time.
As I have read online that Java is pass by value and a general swap function won't swap the two values.
Correct...if the expectation of the swap is to happen by virtue of calling a method.
public void swap(int x, int y) {
int tmp = x;
x = y;
y = tmp;
}
// meanwhile, in main
int x = 10;
int y = 20;
swap(x, y);
System.out.println(x); // still prints 10
System.out.println(y); // still prints 20
Incorrect...if the swap happens inside the method and is utilized somehow.
public void swap(int x, int y) {
int tmp = x;
x = y;
y = tmp;
System.out.println(x); // will print 20 from main
System.out.println(y); // will print 10 from main
}
// meanwhile, in main
int x = 10;
int y = 20;
swap(x, y);
System.out.println(x); // still prints 10
System.out.println(y); // still prints 20
I also read that it's not possible to swap the values of primitive types.
No, this is perfectly possible to do. You can always reassign variables.
As to why your example above works, it's because you're holding onto one of the values while you reassign one of its variables. You're basically putting one value to the side while you copy it over.
Slowly...
int x = 10;
int y = 20;
int tmp = x; // tmp = 10
x = y; // x = 20, tmp = 10
y = tmp; x = 20, y = 10; tmp = 10 (but that doesn't matter)
I am new to java and just learned a little bit about methods and classes, and I am really confused about this whole chapter. This code comes from our review powerpoint and I really don't know how to do it. And I feel like the last section should be in the class? But this is how the powerpoint says. Can someone please explain how does this code work and how to get the printout result specifically step by step?? I really appreciate it, thanks!
public class MyClass {
public void swap(int x, int y){
int temp = x;
x = y;
y = temp;
}
}
int w = 10,
z = 20;
MyClass m = new MyClass();
m.swap(w,z);
System.out.println(w + " " + z);
In order to execute Java you need a main method somewhere.
public static void main(String[] args) {
// Do stuff
}
So you can rewrite the example like so to get it executing:
public class Main {
public static void main(String[] args) {
int w = 10,
z = 20;
MyClass m = new MyClass();
m.swap(w,z);
System.out.println(w + " " + z);
}
}
public class MyClass {
public void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
}
The whole point of this code example is that Java passes primitive types by value and not by reference. Look at the main method. You have two integers, w=10 and z=20. After that you pass w and z to the swap method. Since java passes these primitive types as values, the original w and z are not modified by the swap, which is proven by the println where w is still 10 and z is still 20.
I need to create an array that has a random amount of values from 8 to 12, but it says my variable is incompatible. What do I have to change? Should x not be an int?
Here is the first part of the code that includes the problem:
public class Fish {
int min = 8;
int max = 12;
int x = min + (int)(Math.random() * ((max-min) + 1));
static Fish[] myFish = new Fish[x];
static int Fcount=0;
private float weight;
public Fish(float w) { weight = w;
myFish[Fcount] = this;
Fcount++;
}
public float getWeight( ) { return weight; } }
The second part of my code is:
public class GoFish {
public static void main(String[] args) {
float[] myWeights;
for (int i = 0 ; i < x ; i++){
int min = 1;
int max = 20;
myWeights[i] = min + (int)(Math.random() * ((max-min) + 1));
}
for ( float w : myWeights ) { new Fish(w); }
for ( Fish f : Fish.myFish ) {
System.out.println( f.getWeight() );
} } }
Could you also explain the problem, because I would like to understand what I'm doing wrong. I also have to make the weight a random number between 1 and 20, but I can't get this type of random numbers to work.
Edit: Since we are making the x variable static, how do I use it in the other file? because I need the array values to be random.
x is an instance variable. You're trying to access (javac compiler would say "reference") instance variable (javac would say "non-static variable") from a static context (javac would say the same thing). This won't compile because during the static Fish[] myFish = new Fish[x]; there is no any Fish instance.
You can change your code to:
static int min = 8;
static int max = 12;
static int x = min + (int)(Math.random() * ((max-min) + 1));
This will make non-static variable x static.
Here's the official explanation of static variables (officials prefer to call them class variables).