Solved

ToString with assertEquals

Posted on 2010-11-09
17
452 Views
Last Modified: 2012-05-10
I guess it is to easy but I cant solve this.......

how shall the toString class be for this???

public void testToString()
{
assertEquals("En aln är 59.4 centimeter.", mMeasure4.toString());
assertEquals("En tvärhand är 9.88 centimeter.", mMeasure2.toString());
}



public class OldMeasureTest extends junit.framework.TestCase

{             

    private Transform mTransfor1;

    private Measure mMeasure1;

    private Measure mMeasure2;

    private Measure mMeasure3;

    private Measure mMeasure4;

    

    private int mMetre, mDeciMetre; 

    

    public OldMeasureTest()

    {

    }



    

    protected void setUp()

    {

        mTransfor1 = new Transform();

        mMeasure1 = new Measure("tum", 2.47);

        mMeasure2 = new Measure("tvärhand", (4 * mMeasure1.getLength()));

        mMeasure3 = new Measure("fot", 29.7);

        mMeasure4 = new Measure("aln", 59.4);

        mMetre = 100;

        mDeciMetre = 10;



    }

    

       protected void tearDown()

    {

    }

    

        public void testConvertToCentimetre()

    {

        assertEquals(7.41, mTransfor1.convertToCentimetre(3, mMeasure1));

        assertEquals(39.52, mTransfor1.convertToCentimetre(4, mMeasure2));

    }

    

     

   

    public void testConvertFromCentimetre()

	{

		assertEquals(3.367003367003367, mTransfor1.convertFromCentimetre(mMetre, mMeasure3));

		assertEquals(0.16835016835016836, mTransfor1.convertFromCentimetre(mDeciMetre, mMeasure4));

	}

	

		public void testGetDescription()

	{

		assertEquals("tvärhand", mMeasure2.getDescription());

		assertEquals("fot", mMeasure3.getDescription());

	}

	 

	    

	public void testGetLength()

	{

		assertEquals(2.47, mMeasure1.getLength());

		assertEquals(59.4, mMeasure4.getLength());

		assertEquals(mMetre, (mDeciMetre *10));

	}

	

		public void testToString()

	{

		assertEquals("En aln är 59.4 centimeter.", mMeasure4.toString());

		assertEquals("En tvärhand är 9.88 centimeter.", mMeasure2.toString());

	}

}





------------------------------------------------





/**

 * Write a description of class Measure here.

 * 

 * @author (your name) 

 * @version (a version number or a date)

 */

public class Measure

{

    // instance variables - replace the example below with your own

    private double length;

    private String description;



    /**

     * Constructor for objects of class Measure

     */

    public Measure()

    {

        // initialise instance variables

       this.length = 0f;

    }

    

    public  Measure(String description, double length)

    {

        this.length = length;

        this.description = description;

    }



   

    public double getLength()

    {

        return length;

    }

    

    public String getDescription()

    {

        return description;

    }

    

    public String ToString()

	{	    

	    Transform t = new Transform();

	    return "En " +getDescription() +" är " +t.convertToCentimetre(length, this) +" centimeter"; 

	    

	}

    

}



--------------------------------------------------



/**

 * Write a description of class Transform here.

 * 

 * @author (your name) 

 * @version (a version number or a date)

 */

public class Transform

{

    // instance variables - replace the example below with your own

    private int x;



    /**

     * Constructor for objects of class Transform

     */

    public Transform()

    {

        // initialise instance variables

        x = 0;

    }



       

    public double convertToCentimetre(int input, Measure measure)

    {        

       return (double) input * measure.getLength();       

    }

    

    public double convertFromCentimetre(int cm, Measure measure)

    {

        

       return (double) cm / measure.getLength();

    }

    

}

Open in new window

0
Comment
Question by:Mickeys
  • 6
  • 4
  • 3
  • +2
17 Comments
 
LVL 17

Assisted Solution

by:sweetfa2
sweetfa2 earned 166 total points
ID: 34096747
   public String toString()
      {         
          Transform t = new Transform();
          return "En " +getDescription() +" är " +t.convertToCentimetre(length, this) +" centimeter";
      }
0
 
LVL 92

Expert Comment

by:objects
ID: 34097458
>     public String ToString()

should be toString()
0
 

Author Comment

by:Mickeys
ID: 34099738
Atleast now it compiles. :-) Small problem but I still get the wrong answer......in my test.


Namnl-s.jpg
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34099881
since convertToCentimetre multiplies length with length, you need to compare the result of them,

>>assertEquals("En aln är 59.4 centimeter.", mMeasure4.toString());
            assertEquals("En tvärhand är 9.88 centimeter.", mMeasure2.toString());
>>

should be

assertEquals("En aln är " + ( 59.4 * 59.4 ) + " centimeter.", mMeasure4.toString());
            assertEquals("En tvärhand är " + ( 9.88 * 9.88 ) + " centimeter.", mMeasure2.toString());


0
 

Author Comment

by:Mickeys
ID: 34099917
well that I could do but since I am not allowed to change the test class I cant solve it that way. I have already thought of that.
0
 
LVL 92

Accepted Solution

by:
objects earned 167 total points
ID: 34099932
you need to format the value in toString() as required by the test

http://helpdesk.objects.com.au/java/how-to-format-a-double-as-a-string
0
 

Author Comment

by:Mickeys
ID: 34099967
eh you mean put the things in the link into my toString?
0
 
LVL 92

Expert Comment

by:objects
ID: 34100099
yes, you need to format the value returned by t.convertToCentimetre(length, this)
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 167 total points
ID: 34100490
>>you mean put the things in the link into my toString?

you need to just modify,

public String toString()
      {         
          return "En " +getDescription() +" är " + length +" centimeter";
      }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34100518
Try
    public String toString() {

        return String .format("En %s är %.2f centimeter", description, length);

    }

Open in new window

0
 

Author Comment

by:Mickeys
ID: 34100715
Well I get    En aln är 59,4 centimeter.

but the correct should be En aln är 59.4 centimeter.

(the dot!)

Shall I use replace or is there another way?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34100726
which locale you are in? and what code you use now?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34100734
You would choose a different format for a different Locale. That one's using *your* Locale
0
 

Author Comment

by:Mickeys
ID: 34100737
Well I use the code I inputed above and took CHEJ code toString.

But even if it solves the dot.....it wont make any since because then the next will fail

Look here. One decimal....and two decimal.

      public void testToString()
      {
            assertEquals("En aln är 59,4 centimeter.", mMeasure4.toString());
            assertEquals("En tvärhand är 9,88 centimeter.", mMeasure2.toString());
      }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34100749
Or you can choose the Locale at runtime
java -Duser.country=GB -Duser.language=en YourApp

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34100755
I don't think you should be worrying about getting a dot. Leave ti in your own Locale and use the comma decimal separator in your liiterals
0
 

Author Closing Comment

by:Mickeys
ID: 34100938
Using ksivananth code solved the error.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 61
stringclean challenge 26 57
Passing list of object to Oracle Database Procedure 3 42
strDist challenge 35 84
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now