-
Challenge from A Little Kid
Recently I have been asked by a minor if I could discover the problems of
the following Java code:
import java.awt.*;
class AClass {
public int a;
}
public class TestProgram {
void swap(AClass c1, AClass c2) {
AClass tmp=c1;
c1=c2;
c2=tmp;
}
public static void main(String arg[]) {
AClass c1;
AClass c2;
c1.a=1;
c2.a=2;
swap(c1,c2);
System.out.println("c1.a is " + c1.a + " c2.a is "+c2.a);
}
}
I myself am not a genius in Java and I don't have a Java compiler to test
it. By using C++ logic, I don't see any problem of the code. But the minor
said there is more than one bug.
Could anyone tell what the bugs are? And how the correct code should appear?
-
Re: Challenge from A Little Kid
Basically his AClass doesn't have a constructor, so any reference to it in
the mainline will throw a nullPointerException. Of course it won't compile
because the compiler will complain that the variables c1 and c2 have not
been initialized.
Oh and the swap method will not work because it is only dealing with copies
of the data passed to the method. You can move the method body to the mainline
which will do the trick, or since its a simple class you can change the variable
in each class to the other (via method calls)
class AClass {
public int a;
public AClass(int a) {
this.a = a;
}
}
public class TestProgram {
static void swap(AClass c1, AClass c2) {
int temp = c1.a;
c1.a = c2.a;
c2.a = temp;
}
public static void main(String arg[]) {
AClass c1 = new AClass(1);
AClass c2 = new AClass(2);
swap(c1, c2);
System.out.println("c1.a is " + c1.a + " c2.a is "+c2.a);
}
}
"Taylor" wrote:
>
>Recently I have been asked by a minor if I could discover the problems of
>the following Java code:
>
>import java.awt.*;
>
>class AClass {
> public int a;
>}
>
>public class TestProgram {
> void swap(AClass c1, AClass c2) {
> AClass tmp=c1;
> c1=c2;
> c2=tmp;
> }
>
> public static void main(String arg[]) {
> AClass c1;
> AClass c2;
> c1.a=1;
> c2.a=2;
> swap(c1,c2);
> System.out.println("c1.a is " + c1.a + " c2.a is "+c2.a);
> }
>}
>
>
>I myself am not a genius in Java and I don't have a Java compiler to test
>it. By using C++ logic, I don't see any problem of the code. But the minor
>said there is more than one bug.
>
>Could anyone tell what the bugs are? And how the correct code should appear?
-
Re: Challenge from A Little Kid
Well, the first bug is that the program imports java.awt.* when it doesn't
need to. Another bug is that the program contains two classes and so won't
compile. Yet another bug is that the "swap" method does nothing. What is
the correct code? Depends what the programmer wanted to do. The programmer
may have wanted to change c1 and c2 so that they referred to the objects
originally referred to by c2 and c1, or may have wanted to swap the contents
of c1 and c2. To achieve the first objective, remove the three lines of
code from the swap method and put them into the main method, replacing the
call to swap. To achieve the second objective, replace the body of the swap
method with int tmp=c1.a; c1.a=c2.a; c2.a=tmp;
<Taylor> wrote in message news:39be4a33$1@news.devx.com...
>
> Recently I have been asked by a minor if I could discover the problems of
> the following Java code:
>
> import java.awt.*;
>
> class AClass {
> public int a;
> }
>
> public class TestProgram {
> void swap(AClass c1, AClass c2) {
> AClass tmp=c1;
> c1=c2;
> c2=tmp;
> }
>
> public static void main(String arg[]) {
> AClass c1;
> AClass c2;
> c1.a=1;
> c2.a=2;
> swap(c1,c2);
> System.out.println("c1.a is " + c1.a + " c2.a is "+c2.a);
> }
> }
>
>
> I myself am not a genius in Java and I don't have a Java compiler to test
> it. By using C++ logic, I don't see any problem of the code. But the
minor
> said there is more than one bug.
>
> Could anyone tell what the bugs are? And how the correct code should
appear?
-
Re: Challenge from A Little Kid
List of bugs in the code:
1) import is not needed
2) in the main method c1 and c2 need to be constructed
e.g. c1 = new AClass();
3) in order to run the code (swap method) from main method you have to make
it static
static void swap(..)
4) swap will not work properly here as the arguments are pass by value. In
this case it can be done like this
int tmpint = c1.a;
c1.a = c2.a;
c2.a = tmpint ;
instead of
// AClass tmp=c1;
// c1=c2;
// c2=tmp;
Cheers,
Nikhil
"Taylor" wrote:
>
>Recently I have been asked by a minor if I could discover the problems of
>the following Java code:
>
>import java.awt.*;
>
>class AClass {
> public int a;
>}
>
>public class TestProgram {
> void swap(AClass c1, AClass c2) {
> AClass tmp=c1;
> c1=c2;
> c2=tmp;
> }
>
> public static void main(String arg[]) {
> AClass c1;
> AClass c2;
> c1.a=1;
> c2.a=2;
> swap(c1,c2);
> System.out.println("c1.a is " + c1.a + " c2.a is "+c2.a);
> }
>}
>
>
>I myself am not a genius in Java and I don't have a Java compiler to test
>it. By using C++ logic, I don't see any problem of the code. But the minor
>said there is more than one bug.
>
>Could anyone tell what the bugs are? And how the correct code should appear?
-
Re: Challenge from A Little Kid
Hi Paul,
Thanks for your multiple approaches. But one more question: will the output
from your two approaches differ?
With a Java compiler, I would expect the output after debugging to be:
c1.a is 2 c2.a is 1
Please point out where I am wrong.
Taylor
"Paul Clapham" <pclapham@core-mark.com> wrote:
>Well, the first bug is that the program imports java.awt.* when it doesn't
>need to. Another bug is that the program contains two classes and so won't
>compile. Yet another bug is that the "swap" method does nothing. What
is
>the correct code? Depends what the programmer wanted to do. The programmer
>may have wanted to change c1 and c2 so that they referred to the objects
>originally referred to by c2 and c1, or may have wanted to swap the contents
>of c1 and c2. To achieve the first objective, remove the three lines of
>code from the swap method and put them into the main method, replacing the
>call to swap. To achieve the second objective, replace the body of the
swap
>method with int tmp=c1.a; c1.a=c2.a; c2.a=tmp;
-
Re: Challenge from A Little Kid
If this were a real program, I wouldn't be tinkering with it to make it
work, I'd be rewriting it. In a real-life Java program there are very few
reasons to swap two objects (whatever that means) except to sort an array --
and for that there are the Arrays.sort and Collections.sort methods.
<Taylor> wrote in message news:39c05da8$1@news.devx.com...
>
> Hi Paul,
>
> Thanks for your multiple approaches. But one more question: will the
output
> from your two approaches differ?
>
> With a Java compiler, I would expect the output after debugging to be:
>
> c1.a is 2 c2.a is 1
>
> Please point out where I am wrong.
>
>
> Taylor
>
-
Re: Challenge from A Little Kid
Thank you for all responded. Your comprehensive approaches have surrounded
the little kid. Hope those could help her.
Again, many thanks!
-
Re: Challenge from A Little Kid
hi,
I know why u are not able to run this program.
when u compile this u will get three errors:
1.c:\windows\desktop\TestProgram.java:17: Variable c1 may not have been initialize
d.
c1.a=1;
In java u have to explicitly instantiate a class using new operator
so u have to do Aclass c1 = new Aclass();
2.the second one also u have to do the same thing .
Aclass c1 = new Aclass();
3.main is a static method. u can only refer to static variables and static
methods from a static method.
u have to make swap as static to access it from main.
here i am giving the corrected code
"Taylor" wrote:
>
>Recently I have been asked by a minor if I could discover the problems of
>the following Java code:
>
>import java.awt.*;
>
>class AClass {
> public int a;
>}
>
>public class TestProgram {
> void static swap(AClass c1, AClass c2) {
> AClass tmp=c1;
> c1=c2;
> c2=tmp;
> }
>
> public static void main(String arg[]) {
> AClass c1 = new Aclass();
> AClass c2 = new Aclass();
> c1.a=1;
> c2.a=2;
> swap(c1,c2);
> System.out.println("c1.a is " + c1.a + " c2.a is "+c2.a);
> }
>}
>
>
>I myself am not a genius in Java and I don't have a Java compiler to test
>it. By using C++ logic, I don't see any problem of the code. But the minor
>said there is more than one bug.
>
>Could anyone tell what the bugs are? And how the correct code should appear?
-
Re: Challenge from A Little Kid
"Taylor" wrote:
>
>Recently I have been asked by a minor if I could discover the problems of
>the following Java code:
>
> public static void main(String arg[]) {
> AClass c1;
> AClass c2;
> c1.a=1;
> c2.a=2;
> swap(c1,c2);
> System.out.println("c1.a is " + c1.a + " c2.a is "+c2.a);
> }
>}
For a start, AClass c1 and c2 are not intialized using the "new" keyword.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks