2 Replies - 1084 Views - Last Post: 09 October 2012 - 01:16 AM Rate Topic: -----

#1 lsutigers43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 12

Matrix 2x2 Construction and toString()

Posted 09 October 2012 - 01:05 AM

I have a quick question regarding using the toString method.

My code:

package mat2by2;


public class Mat2By2 
{
    //instance variables of Mat2By2 Objects
    private double bottomLeft;
    private double bottomRight;
    private double topLeft;
    private double topRight;

    //creates a 2x2 matrix whose entries are all 0
    public Mat2By2(){}
    
    //creates a 2x2 matrix with the specified parameters
    public Mat2By2(double tL, double tR, double bL, double bR)
    {
        
        topLeft = tL;
        topRight = tR;
        bottomLeft = bL;
        bottomRight = bR;
        
    }
    
    public Mat2By2 add(Mat2By2 m)
    {
        return new Mat2By2(topLeft + m.topLeft, topRight + m.topRight, bottomLeft + m.bottomLeft, bottomRight + m.bottomRight); 
    }
    
    @Override
    public String toString()
    {
        return String.format("{{%d,%d},{%d,%d}}",topLeft, topRight, bottomLeft, bottomRight);
    }
    
    public static void main(String[] args) 
    {
        Mat2By2 m1 = new Mat2By2(1,2,3,4);
        Mat2By2 m2 = new Mat2By2(2,3,4,1);
        
        Mat2By2 m3 = m1.add(m2);

        System.out.println(m2);
        
    }
}



I'm getting several formatting errors when I'm trying to print out the values of the Matrix.

Quote

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2709)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2661)
at java.util.Formatter.format(Formatter.java:2433)
at java.util.Formatter.format(Formatter.java:2367)
at java.lang.String.format(String.java:2769)
at mat2by2.Mat2By2.toString(Mat2By2.java:35)
at java.lang.String.valueOf(String.java:2826)
at java.io.PrintStream.println(PrintStream.java:771)
at mat2by2.Mat2By2.main(Mat2By2.java:43)
Java Result: 1


I have followed every example I could find, but I am obviously missing something or this is a special case that requires a few additions.

The sample run given should look like this:

Quote

m1 + m2 = {{1.000000,2.000000},{3.000000,4.000000}}


I have tried a plethora of variations to the toString() Class, and I also varied how I implemented the class in the tester application.

Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Matrix 2x2 Construction and toString()

#2 lsutigers43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 12

Re: Matrix 2x2 Construction and toString()

Posted 09 October 2012 - 01:11 AM

2 Minutes after posting I realize %f is the format specifier, not $d.

Fixed the problem.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • Posts: 3,121
  • Joined: 05-April 11

Re: Matrix 2x2 Construction and toString()

Posted 09 October 2012 - 01:16 AM

%d are for decimal integers, you will have to use f for doubles

return String.format("{{%f,%f},{%f,%f}}",topLeft, topRight, bottomLeft, bottomRight);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1