Results 1 to 2 of 2
  1. #1
    jtr1991 is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    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 arent specifically bugs. All source comments have been deliberately removed and shouldnt 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
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: New Developer, Finding Bugs Exercise

    Already got replies in their crosspost: New Developer, Finding Bugs Exercise
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Bugs
    By Nazneen Ali in forum New To Java
    Replies: 5
    Last Post: 10-12-2013, 02:26 PM
  2. Anti-Aliasing, and other bugs
    By ResidentBiscuit in forum New To Java
    Replies: 11
    Last Post: 11-18-2011, 03:39 PM
  3. Bugs in calculator
    By kumarv75 in forum CLDC and MIDP
    Replies: 2
    Last Post: 06-28-2010, 01:12 PM
  4. who let the bugs out, who-who-who!
    By FON in forum Introductions
    Replies: 1
    Last Post: 12-11-2009, 03:05 PM
  5. Replies: 3
    Last Post: 08-19-2009, 03:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •