Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Resetting the values in a variable -- Need Help

1. ## Resetting the values in a variable -- Need Help

package try1;

public class Try1 {

public static class Point {
private double x;
private double y;
private double z;
public Point(double x, double y, double z){
this.x = x;
this.y = y;
this.z = z;
}
public void print(){
System.out.println("(" + x + "," + y + "," + z + ")");
}
public void scaledown(){
x = x/2;
y = y/2;
z = z/2;
}
public void scaleup(){
x = x*2;
y = y*2;
z = z*2;
}

}

public static void main(String[] args){

Point p = new Point(16.0,16.0,16.0);
for (int i = 0; i<4; i++){
p.scaledown();
p.print();
}
//Here
for (int i = 0; i<4; i++){
p.scaleup();
p.print();
}

}

I need to know how to reset the values of x, y, and z after the scaledown() function is ran. How would I go about this?

• Code:
```private void xyzReset()
{
x = 0;
y = 0;
z = 0;
}```
?

may (but I don't see why) need
Code:
```private void xyzReset()
{
this.x = 0;
this.y = 0;
this.z = 0;
}```
I tend to avoid naming things like x,y,z unless I am in a for loop, and I tend to name fields with underscores to avoid any this. confusion

• See? it can get messy...
Code:
```class Demo
{
private int x, y, z;

private void example(int x, int y, int z)
{
int x = 2; // references argument int x
this.x = 3; // references field x
// etc
}
}```
but this is much less confusing
Code:
```class Demo
{
private int _x, _y, _z;

private void example(int x, int y, int z)
{
// self explanatory
int x = 2;
_x = 3;
// etc
}
}```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•