Objects, memory models, and scope Assignment Solution

 

Object Oriented Programming in Java

Module 2 - Programming Assignment Instructions

Objects, memory models, and scope


Question 1

Consider the following code:

public class MyClass
{
  private int a;
  public double b;
  
  public MyClass(int first, double second)
  {
    this.a = first;
    this.b = second;
  }
  public static void main(String[] args)
  {
    MyClass c1 = new MyClass(1020.5);
    MyClass c2 = new MyClass(1031.5);
    System.out.println(c1.a + ", " + c1.b);
  }
}

What would this code print?

Note that this might seem like a compiler error to be accessing c1.a and c1.b from main, but because the method main is defined in the class "MyClass", it will compile just fine (feel free to check it for yourself).

1 / 1 point
Correct

Although "a" is a private variable, because the method "main" is in the class MyClass, it has access to that private variable. So this will run just as you'd expect.


public class MyClass
{
  private int a;
  public double b;
  
  public MyClass(int first, double second)
  {
    this.a = first;
    this.b = second;
  }
  public static void main(String[] args)
  {
    MyClass c1 = new MyClass(1020.5);
    MyClass c2 = new MyClass(1031.5);
    // lines below are changed from the question above
    c2 = c1;   
    c1.a = 2;
    System.out.println(c2.a);
  }
}


Question 2

What is the result of running the code above? Hint - draw a memory model!

1 / 1 point
Correct

Yes, both c1 and c2 refer to the same object. So when you change c1.a, you have effectively changed c2.a as well.


Question 3

In the code from question 2 above, after executing c2 = c1, how can you get back the object which c2 previously pointed to (the one with a as 10 and b as 31.5)?

1 / 1 point
Correct

Without any references pointing to that object originally referred to by c2, the garbage collector can destroy the object. If you needed to keep that object around for later, you'd need to assign it to a temporary reference.

Question 4

Please review the code below:

public class MyClass
{
  private int a;
  public double b;
  
  public MyClass(int first, double second)
  {
    this.a = first;
    this.b = second;
  }

  // new method
  public static void incrementBoth(MyClass c1) {
    c1.a = c1.a + 1;
    c1.b = c1.b + 1.0;
  }

  public static void main(String[] args)
  {
    MyClass c1 = new MyClass(1020.5);
    MyClass c2 = new MyClass(1031.5);
    // different code below
    incrementBoth(c2);
    System.out.println(c1.a + ", "+ c2.a);
  }
}


Question 4

Please review the code below:

What would be the output from running main?

1 / 1 point
Correct

This is the correct response. c2's values are incremented by passing it as a parameter to incrementBoth.

Question 5

Please review the code below:

public class MyClass
{
  private int a;
  public double b;
  
  public MyClass(int first, double second)
  {
    this.a = first;
    this.b = second;
  }
  
  public static void incrementBoth(MyClass c1) {
    c1.a = c1.a + 1;
    c1.b = c1.b + 1.0;
  }
  
  // new method
  public static void incrementA(int first)
  {
    first = first + 1;
  }
  
  // new method
  public static void incrementB(double second)
  {
    second = second + 1.0;
  }
  
  public static void main(String[] args)
  {
    MyClass c1 = new MyClass(1020.5);
    MyClass c2 = new MyClass(1031.5);
    // different code below
    incrementA(c2.a);
    incrementB(c2.b);
    System.out.println(c2.a + ", "+ c2.b);
  }
}

Question 5

Please review the code below:

What is the output from running this code?

1 / 1 point
Correct

This is the correct response. This is a tricky question, because methods incrementA and incrementB have no real purpose. Because Java is pass by value, a copy of that value is passed into the method. Changing the copy of the primitive type will have no impact on the parameter.


Post a Comment

0 Comments