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

New Topic/Question
Reply



MultiQuote



|