Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,883
    Thanks
    43
    Thanked 202 Times in 201 Posts
    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

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,883
    Thanks
    43
    Thanked 202 Times in 201 Posts
    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
      }
    }

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

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