Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: New Developer, Finding Bugs Exercise

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New Developer, Finding Bugs Exercise

    Hi all,

    Im a new Java Developer and have been developing little applications for a while but when looking online for some exercises i have come across ones that involve finding bugs. I have found this much harder than writing my own as I'm not overly sure on all the features.

    I've copied the exercise in at the bottom. Looking through it i cant see many errors myself, all the syntax seems to be in place and all the code makes logical sense. the one error i think i have found is to Replace id = id with this.id = id in com.result.exam.a.Concept.setId(final String id) { this.id = id; }.

    Would anybody be able to help me out on this or just give me a few pointers at the right this to be looking at?

    Any help would be grateful

    Cheers.

    This is the exercise:

    The following are the sources for four related Java classes. The task is to study the code, find the bugs and suggest how they should be fixed. Additionally, you can comment on any flaws in the design that aren’t specifically bugs. All source comments have been deliberately removed and shouldn’t be counted! Also, generics have deliberately not been used.

    Please highlight your changes in red font.
    com/result/exam/a/Concept.java
    1 package com.result.exam.a;
    2
    3 public abstract class Concept
    4 {
    5 private String id;
    6
    7 protected Concept( String anId )
    8 {
    9 if ( anId == null )
    10 {
    11 throw new NullPointerException( "id must not be null" );
    12 }
    13
    14 id = anId;
    15 }
    16
    17 public String getId()
    18 {
    19 return id;
    20 }
    21
    22 public void setId( final String id )
    23 {
    24 id = id;
    25 }
    26
    27 public boolean equals( Object other )
    28 {
    29 return other != null && other.getClass().equals( getClass() ) && id.equals( ( (Concept) other ).id );
    30 }
    31
    32 public String toString()
    33 {
    34 return "Concept(" + id + ")";
    35 }
    36 }
    com/result/exam/a/ConceptA.java
    1 package com.result.exam.a;
    2
    3 public class ConceptA extends Concept
    4 {
    5 private final Concept parent;
    6
    7 public ConceptA( String anId, Concept aParent )
    8 {
    9 super( anId );
    10
    11 parent = aParent;
    12 }
    13
    14 public Concept getParent()
    15 {
    16 return parent;
    17 }
    18
    19 public String toString()
    20 {
    21 return "ConceptA{" + getId() + ", parent=" + parent + '}';
    22 }
    23 }
    com/result/exam/a/ConceptB.java
    1 package com.result.exam.a;
    2
    3 import java.util.Set;
    4 import java.util.HashSet;
    5 import java.util.Iterator;
    6
    7 public class ConceptB extends ConceptA
    8 {
    9 private final Set children;
    10
    11 public ConceptB( final String anId, final Concept aParent )
    12 {
    13 super( anId, aParent );
    14
    15 children = new HashSet();
    16 }
    17
    18 public int getCount()
    19 {
    20 return children.size();
    21 }
    22
    23 public void addChild( Concept aChild )
    24 {
    25 children.add( aChild );
    26 }
    27
    28 public void removeChild( Concept aChild )
    29 {
    30 children.remove( aChild );
    31 }
    32
    33 public Iterator getChildren()
    34 {
    35 return children.iterator();
    36 }
    37
    38 public int getFamilySize()
    39 {
    40 int count = children.size();
    41
    42 for ( Iterator iter = getChildren(); iter.hasNext(); )
    43 {
    44 count += ( (ConceptB) iter.next() ).getFamilySize();
    45 }
    46
    47 return count;
    48 }
    49
    50 public int getAncestorCount()
    51 {
    52 int count = 0;
    53 Concept ancestor = getParent();
    54
    55 while ( ancestor != null )
    56 {
    57 count++;
    58 if ( ancestor instanceof ConceptA )
    59 {
    60 ancestor = ( (ConceptA) ancestor ).getParent();
    61 }
    62 else
    63 {
    64 ancestor = null;
    65 }
    66 }
    67
    68 return count;
    69 }
    70
    71 public String toString()
    72 {
    73 return "ConceptB{" + getId() + ", parent=" + getParent() + ", children=" + children.size() + "}";
    74 }
    75 }
    com/result/exam/a/ConceptC.java
    1 package com.result.exam.a;
    2
    3 public class ConceptC extends ConceptA
    4 {
    5 private static int nextSerialNo = 0;
    6
    7 public static int getNextSerialNo()
    8 {
    9 return nextSerialNo++;
    10 }
    11
    12 private final int serialNo;
    13
    14 public ConceptC( String anId )
    15 {
    16 super( anId, null );
    17
    18 serialNo = getNextSerialNo();
    19 }
    20
    21 public int getSerialNo()
    22 {
    23 return serialNo;
    24 }
    25
    26 public String toString()
    27 {
    28 return "ConceptC(" + getId() + ", " + serialNo + ")";
    29 }
    30 }


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    48
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: New Developer, Finding Bugs Exercise

    Please post your code in [CODE] tags.

    Quote Originally Posted by jtr1991 View Post
    one error i think i have found is to Replace id = id with this.id = id in com.result.exam.a.Concept.setId(final String id) { this.id = id; }.
    This is correct as you have an instance variable declared as 'id'

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Developer, Finding Bugs Exercise

    Cheers, I don't suppose you've seen anymore have you? I keep looking over it but nothing is jumping out at me. I've double checked all the syntax and it all appears to be correct so as far as i am aware now its all purely in the code

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,668
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: New Developer, Finding Bugs Exercise

    That isn't a bug, its a compiler error:
    error: final parameter id may not be assigned
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Developer, Finding Bugs Exercise

    Quote Originally Posted by Norm View Post
    That isn't a bug, its a compiler error:
    error: final parameter id may not be assigned

    Ok thanks, what would you say constitutes a bug in this type of exercise then?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,668
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: New Developer, Finding Bugs Exercise

    Code that doesn't execute as designed.
    I wouldn't call a syntax error a bug.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Developer, Finding Bugs Exercise

    Quote Originally Posted by Norm View Post
    Code that doesn't execute as designed.
    I wouldn't call a syntax error a bug.
    Ok, thanks. Like I said I'm new to this sort of stuff so any help is grateful. I've been looking over it again and from what you are saying and what i can see there aren't any bugs. All the code apart from the first section seems to be fully functional as far as i am aware. Is it common to get tasks like this that will only have minor errors?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,668
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: New Developer, Finding Bugs Exercise

    Does the code do what is expected when it is executed?

    I've never looked for bugs for fun.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [HELP]Bugs of Java or Wrong Code?
    By vikar in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 30th, 2013, 03:13 AM
  2. Methodology to find bugs
    By tarkal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 10th, 2011, 04:02 PM
  3. Code bugs
    By heptix in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 30th, 2010, 03:32 AM
  4. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM