Difference between revisions of "Fix Code Sample"

From CompSciWiki
Jump to: navigation, search
Line 4: Line 4:
 
<BR>
 
<BR>
 
<pre>
 
<pre>
public class Triangle
+
public class MyClass
 
{
 
{
  int base;
+
private static int numInst = 0;
  int height;
+
int a;
 
+
double b;
  public static int getArea()
+
  {
+
public static MyClass( double b )
      return ( this.base * this.height / 2 );
+
{
  }
+
this.a = 5;
}
+
this.b = b;
 
+
numInst++;
public static void main()
+
}
{
+
  Triangle tri = new Triangle();
+
public static double average( int n1, int n2, int b )
  int area;
+
{
 
+
int total;
  area = tri.getArea();
+
 +
total = n1 + n2 + this.b;
 +
 +
return (double)(total / 3);
 +
}
 +
 +
public static int getNumInst()
 +
{
 +
return this.numInst;
 +
}
 +
 +
public static double getB()
 +
{
 +
return b;
 +
}
 +
 +
public void setNumInst( int newSum )
 +
{
 +
numInst = newSum;
 +
}
 +
 +
public static void setA( int newA )
 +
{
 +
a = newA;
 +
}
 
}
 
}
 
</pre>
 
</pre>
  
|SideSectionTitle=Fixing Code
+
|SideSectionTitle=by Students...
  
 
|SideSection=
 
|SideSection=
[[Image:wiki_method02.jpg|center]]<BR>
 
  
|Solution=The problem with this code is that it attempts to use instance variables in a static method.
+
|Solution=The first problem with this code is the static constructor. Since this needs to access instance variables,
To fix it, you must pass the arguments in as parameters.
+
the method must be a instance method.
 
+
|SolutionCode=
+
 
<pre>
 
<pre>
public class Triangle
+
public MyClass( double b )
 
{
 
{
  int base;
+
this.a = 5;
  int height;
+
this.b = b;
 
+
numInst++;
  public static int getArea( int height, int base )
+
  {
+
      return ( base * height / 2 );
+
  }
+
 
}
 
}
 +
</pre>
  
public static void main()
+
The next problem is that our average method is referencing the instance variable <b>b</b>, rather than the <b>int b</b> passed in as
 +
a parameter. To solve this, we just need to remove the <b>this.</b> in front of the reference to <b>b</b>.
 +
<pre>
 +
total = n1 + n2 + b;
 +
</pre>
 +
 
 +
|SolutionCode=
 +
<pre>
 +
public class MyClass
 
{
 
{
  Triangle tri = new Triangle();
+
private static int numInst = 0;
  int area;
+
int a;
 
+
double b;
  area = Triangle.getArea( tri.getBase(), tri.getHeight() );
+
 +
public MyClass( double b )
 +
{
 +
this.a = 5;
 +
this.b = b;
 +
numInst++;
 +
}
 +
 +
public static double average( int n1, int n2, int b )
 +
{
 +
int total;
 +
 +
total = n1 + n2 + b;
 +
 +
return (double)(total / 3);
 +
}
 +
 +
public static int getNumInst()
 +
{
 +
return numInst;
 +
}
 +
 +
public double getB()
 +
{
 +
return b;
 +
}
 +
 +
public void setNumInst( int newSum )
 +
{
 +
numInst = newSum;
 +
}
 +
 +
public void setA( int newA )
 +
{
 +
a = newA;
 +
}
 
}
 
}
 
</pre>
 
</pre>
  
 
}}
 
}}

Revision as of 20:51, 7 April 2010

Back to the Program-A-Day homepage

Problem

Identify the problems in the code sample below. Fix the errors so that the code works correctly.

public class MyClass
{
	private static int numInst = 0;
	int a;
	double b;
	
	public static MyClass( double b )
	{
		this.a = 5;
		this.b = b;
		numInst++;
	}
	
	public static double average( int n1, int n2, int b )
	{
		int total;
		
		total = n1 + n2 + this.b;
		
		return (double)(total / 3);
	}
	
	public static int getNumInst()
	{
		return this.numInst;
	}
	
	public static double getB()
	{
		return b;
	}
	
	public void setNumInst( int newSum )
	{
		numInst = newSum;
	}
	
	public static void setA( int newA )
	{
		a = newA;
	}
}
 

by Students...

Solution

The first problem with this code is the static constructor. Since this needs to access instance variables, the method must be a instance method.

public MyClass( double b )
{
	this.a = 5;
	this.b = b;
	numInst++;
}

The next problem is that our average method is referencing the instance variable b, rather than the int b passed in as a parameter. To solve this, we just need to remove the this. in front of the reference to b.

total = n1 + n2 + b;

Code

Solution Code

Back to the Program-A-Day homepage