I don't understand why I am getting an error of an incompatible data type, I am using a float
for a financial value, I do not want to go past two decimal places!
I was under the impression that you could do this with a float
, however I am getting an error returned to me saying,
Constructor Magazine cannot be applied to given types.
When I just make the float
7 as opposed to 7.99, it works fine!
Have I misunderstood what a float
is, do I need to use a double
instead?
I'll just show my magazine class and a bit of my test class to demonstrate.
Test class:
The following is a segment from my test class trying to use a float
to two decimal places:
public static void main()
{
Magazine magazine1 = new Magazine("SanYonic Publishing", "Ayup Magazine", 7.99, "Yeshumenku Suni", "12/09/2011");
System.out.println();
magazine1.getEditor();
magazine1.getDate();
magazine1.getPublisher();
magazine1.getPublicationTitle();
magazine1.getPrice();
System.out.println();
…
}
Magazine
class:
/**
* Magazine Class - This class represents Magazine Objects
*/
public class Magazine extends Publication
{
private String editor;
private String date;
public Magazine(String publisherIn , String publicationTitleIn, float priceIn, String editorIn, String dateIn)
{
super (publisherIn , publicationTitleIn, priceIn);
editor = editorIn;
date = dateIn;
}
public void setPublication(String publisherIn, String publicationTitleIn, float priceIn)
{
publisherIn = publisher;
publicationTitleIn = publicationTitle;
priceIn = price;
}
public String getEditor()
{
System.out.println("The editor of this magazine is " + editor);
return (editor);
}
public String getDate()
{
System.out.println("The publication date of this magazine is " + date);
return (date);
}
public String getPublisher()
{
System.out.println("The publisher of this magazine is " + publisher);
return (publisher);
}
public String getPublicationTitle()
{
System.out.println("The publication title of this magazine is " + publicationTitle);
return (publicationTitle);
}
public float getPrice()
{
System.out.println("The price of this magazine is £" + price);
return (price);
}
}
you need
Magazine magazine1 = new Magazine ("SanYonic Publishing", "Ayup Magazine", 7.99f, "Yeshumenku Suni", "12/09/2011");
note the 7.99f
to fix the compile issue.
Note that both floats and doubles are not suitable for monetary calculations (if you care about accuracy), since they can only represent a discrete set of values. All currency calculations should be done with BigDecimal.