Difference between revisions of "Fix Code Sample"
From CompSciWiki
Line 4: | Line 4: | ||
<BR> | <BR> | ||
<pre> | <pre> | ||
− | public class | + | 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 | + | } |
− | { | + | |
− | + | 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; | ||
+ | } | ||
} | } | ||
</pre> | </pre> | ||
− | |SideSectionTitle= | + | |SideSectionTitle=by Students... |
|SideSection= | |SideSection= | ||
− | |||
− | |Solution=The problem with this code is | + | |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. | |
− | + | ||
− | + | ||
<pre> | <pre> | ||
− | public | + | public MyClass( double b ) |
{ | { | ||
− | + | this.a = 5; | |
− | + | this.b = b; | |
− | + | numInst++; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
+ | </pre> | ||
− | public | + | 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 | ||
{ | { | ||
− | + | private static int numInst = 0; | |
− | + | int a; | |
− | + | double b; | |
− | + | ||
+ | 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
ProblemIdentify 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... | |
---|---|---|
SolutionThe 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 |