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 7 of 7

Thread: Help with main method TestCase.java

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with main method TestCase.java

    Hello friends,

    As a kind of an amateur, I have some difficulties in understanding what exactly happens when the code below, which is the main method Test.Case.java, is executed. Is there any friendly soul that could help me understand the code? And if I'm in the wrong forum, please do excuse me.

    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.List;

    public class TestCase {

    SomeHelperClass someHelperClass = new SomeHelperClass();

    public static void main(String[] args) {

    TestCase testCase = new TestCase();

    try {
    testCase.getAll();
    } catch (Exception e) {
    } finally {
    testCase = null;
    }

    }

    public void getAll() throws SystemException {

    List list = null;

    list = someHelperClass.getFigures();
    if (list.get(0) != null) {
    if (list.get(0) instanceof Triangle) {
    Triangle t1 = (Triangle) list.get(0);
    t1.setBase(1);
    t1.setHeight(1);
    } else if (list.get(0) instanceof Square) {
    Square s1 = (Square) list.get(0);
    s1.setBase(1);
    }

    } else {
    throw new SystemException("Empty list");
    }

    FigureComparator comparator = new FigureComparator();
    sortList(list, 1, comparator);

    Iterator iter = list.iterator();
    while (iter.hasNext()) {
    Figure figure = (Figure) iter.next();
    figure.print();
    }

    }

    public List sortList(List list, int sortChoise, Comparator comparator) {

    switch (sortChoise) {

    case 1:
    someHelperClass.sort(list, 1, comparator);
    break;

    default:
    someHelperClass.sort(list, 2, comparator);
    break;

    case 2:
    someHelperClass.sort(list, 2, comparator);

    case 3:
    someHelperClass.sort(list, 3, comparator);
    break;
    }

    return list;
    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,803
    My Mood
    Mellow
    Thanks
    176
    Thanked 626 Times in 613 Posts

    Default Re: Help with main method TestCase.java

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Too much code posted incorrectly. Please edit your post to include code/highlight tags as described in the above link.

    As for understanding the code you posted, I recommend you comment your code with what you do understand, then add comments with questions for the parts you don't understand. Asking someone to completely comment your code for you is unreasonable.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with main method TestCase.java

    Hello GregBrannon,

    I'm very sorry, I didn't see the guidelines.

    [highlight=java] import java.util.Comparator;
    import java.util.Iterator;
    import java.util.List;
     
    public class TestCase {
     
    SomeHelperClass someHelperClass = new SomeHelperClass();
     
    public static void main(String[] args) {
     
    TestCase testCase = new TestCase(); [/highlight]
     
    The highlighted codes, I do understand. 
     
     
    try {
    testCase.getAll();
    } catch (Exception e) {
    } finally {
    testCase = null;
    }
     
    }
     
    public void getAll() throws SystemException {
     
    List list = null;
     
    list = someHelperClass.getFigures();
    if (list.get(0) != null) {
    if (list.get(0) instanceof Triangle) {
    Triangle t1 = (Triangle) list.get(0);
    t1.setBase(1);
    t1.setHeight(1);
    } else if (list.get(0) instanceof Square) {
    Square s1 = (Square) list.get(0);
    s1.setBase(1);
    }
     
    } else {
    throw new SystemException("Empty list");
    }
     
    FigureComparator comparator = new FigureComparator();
    sortList(list, 1, comparator);
     
    Iterator iter = list.iterator();
    while (iter.hasNext()) {
    Figure figure = (Figure) iter.next();
    figure.print();
    }
     
    }
     
     
    public List sortList(List list, int sortChoise, Comparator comparator) {
     
    switch (sortChoise) {
     
    case 1:
    someHelperClass.sort(list, 1, comparator);
    break;
     
    default:
    someHelperClass.sort(list, 2, comparator);
    break;
     
    case 2:
    someHelperClass.sort(list, 2, comparator);
     
    case 3:
    someHelperClass.sort(list, 3, comparator);
    break;
    }
     
    return list;
    }
     
    }

    Does anyone know why SystemException is used in this case, because eclipse underlines it with red color with the text "SystemException cannot be resolved to a type" meaning something is wrong in the syntax? And why is the list = null? Shouldn't it contain some kind of value?

    I wonder what the function for sortChoise is, and the difference between the cases & default is? Is it for the comparator to compare the cases with the default?

    I know it's a couple of questions, but I've really tried to understand it on my own but to no avail. I would appreciate all help. Thanks in advance, and hopefully I posted my question and the codes correctly and more organized.
    Last edited by Jawa; April 1st, 2014 at 09:43 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,149
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with main method TestCase.java

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Also make sure the statements' indentations are preserved.

    the function for sortChoise
    Read about switch statements and how they work:
    http://docs.oracle.com/javase/tutori...ts/switch.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with main method TestCase.java

    Fixed it, thank you for your input.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,149
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Help with main method TestCase.java

    If you've gotten your answer, please mark this thread as solved.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with main method TestCase.java

    Actually there are some questions that I feel are still unanswered, but I'll definitely do that afterwards.

Similar Threads

  1. Java main method
    By JEV23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 13th, 2013, 07:41 AM
  2. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  3. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  4. Java main method args
    By mattxo in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 09:45 AM
  5. Junit3 error: Implicit super constructor TestCase() is not visible
    By albertkao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2011, 11:53 AM