nunos 0 Light Poster

Hi everyone.

I have an homework assignment due next week. Since I am not that knowledgeable in Java, there are many times I get stuck and find myself goggling for answers which sometimes I can't get that fast.

Therefore, I would like to use this thread to post several quick and easy questions I came across during this assignment.

This said I am going to explain the assignment, which is indeed very simple. Compute the mean and standard deviation of a data set, using your own implementation of linked lists. You'll also have to use unit tests to check the correctness of your program.

What I would like to ask for now, is to you to help me out with the unit tests, since I have never written one from scratch.

My code so far:

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.util.List;
import java.util.LinkedList;

public class MainTest extends TestCase 
{
    private List <Integer> l1;
    private List <Double> l2;

    protected void setUp() throws Exception 
    {
        super.setUp();
        
        l1 = new LinkedList<Integer> ();
        l1.add(160);
        l1.add(591);
        l1.add(114);
        l1.add(229);
        l1.add(230);
        l1.add(270);
        l1.add(128);
        l1.add(1657);
        l1.add(624);
        l1.add(1503);
        
        l2 = new LinkedList<Double> ();
        l2.add(15.0);
        l2.add(69.9);
        l2.add(6.5);
        l2.add(22.4);
        l2.add(28.4);
        l2.add(65.9);
        l2.add(19.4);
        l2.add(198.7);
        l2.add(38.8);
        l2.add(138.2);       
    }

    protected void tearDown() throws Exception 
    {
        super.tearDown();
        l1 = null;
        l2 = null;
    }

    public MainTest(String name) 
    {
        super(name);
    }
    
    public void testEquals()
    {
        assertTrue(550.6, Main.mean(l1);
        assertTrue(572.03, Main.stdDev(l1));
        assertTrue(60.32, Main.mean(l2));
        assertTrue(62.26, Main.stdDev(l2));
    }
    
    public static Test suite()
    {
        TestSuite suite = new TestSuite();
        suite.addTest(new MainTest("testEquals"));
        return suite;
    }
}

Main.java has the methods mean and stdDev. How should I call them from MainTest? Should I instantiate a Main m; and then do m.mean? This looks wierd... Or should I make Main abstract I believe, just like the java Math class, so that I can do Main.mean?

I believe it is possible to do unit tests without using test suites, am I right or not? If it is possible, could you please briefly explain/show how it can it be?

Thanks very much in advance for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.