Search code examples
javatrace

Why is the route I'm taking while tracing the code wrong?


I am supposed to be tracing this code for my class but no matter how many times I go through it I seem to get the wrong answer.

The code below has me trace it and find the output. The route I take is as soon as I hit one(tweets) I jump straight to it before continuing to the for loop after it.

I get the answer

Index0:a is -4 b is 1

Index0:a is 14 b is -5

Index0:a is 16 b is 1

c is 6

public class Tweet
{
   private int a;
   private int b;
   public static int c = 0;

   public Tweet(int a, int b)
   {
      this.a = a;
      this.b = b;
      c += this.a + this.b;
   }

   public int getA()
   {
      return this.a;
   }

   public int getB()
   {
      return this.b;
   }

   public void setA(int a)
   {
      this.a = a;
   }

   public void setB(int b)
   {
      this.b = b;
   }
}

public class Homework4
{
   public static void main(String[] args)
   {
      Tweet t1 = new Tweet(3, 8);
      Tweet t2 = new Tweet(-3, 7);
      Tweet[] tweets = { t1, t2, new Tweet(0, -5) };
      one(tweets);
      for (int i = 0; i < tweets.length; i++)
      {
         System.out.print("Index 0: ");
         System.out.print("a is " + tweets[i].getA());
         System.out.println(" b is " + tweets[i].getB());
      }
      System.out.println("c is " + Tweet.c);
   }
   public static void one(Tweet[] tws)
   {
      for (int i = 0; i < tws.length - 1; i++)
      {
         Tweet w = tws[i];
         Tweet x = tws[i + 1];
         w.setA(2 * x.getB());
         w.setB(-2 + x.getA());
      }
      tws[2] = tws[0];
      tws[0].setA(-4);
   }
}

This is my first time posting on here so if more info is needed let me know, I just need to know what I'm doing wrong here.


Solution

  • I'm not clear about where you're making the mistake, so let's look from the top. I'm going to use the notation Tweet(a,b) for brevity.

    Three tweets are created and put in an array. Since c is a static field of Tweet, I'll represent it separately:

    c = 0
    t[0] = Tweet(3, 8),  c = 11
    t[1] = Tweet(-3, 7), c = 11 + 4 = 15
    t[2] = Tweet(0, -5), c = 15 - 5 = 10
    

    The function one(Tweet[]) is called:

    w = t[0], x = t[1]
    w = Tweet(2 * x.getB(), -2 + x.getA())
      = Tweet(14, -5)
    
    w = t[0], x = t[1]
    w = Tweet(2 * x.getB(), -2 + x.getA())
      = Tweet(-10, -2)
    
    t[2] = t[0] // the value of t2 is, effectively, discarded
    
    t[0] = Tweet(-4, -5)
    t[1] = Tweet(-10, -2)
    t[2] = t[0] = Tweet(-4, -5)
    

    So, using the format described by Andrew Regan in his answer, the output should be:

    Index 0: a is -4 b is -5
    Index 1: a is -10 b is -2
    Index 2: a is -4 b is -5
    c is 10
    

    If you're having trouble tracing a block of code, some great tools are:

    • Any good debugger will have the options to step through code and add breakpoints. If you're manually invoking java and javac you should already have jdb available to you. IntelliJ, along with any other IDEs will have debuggers baked into them.
    • Another good resource (particularly for smaller programs) is a Java Visualizer available here: http://cscircles.cemc.uwaterloo.ca/java_visualize