ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Plz have a look in this small program and the error that compiler returns
/*
* book.java
*
* Created on 09 April 2004, 01:07
*/
package Package;
/**
*
* @author alaios1
*/
public class book {
String title;
String code;
String last;
String isbn;
Integer quantity;
Float price;
/** Creates a new instance of book */
public book(String mytitle,String mycode,String mylast) {
title=mytitle;mycode=code;mylast=last;
}
public book(String mytitle,String mycode,String mylast,String myisbn,Integer myquantity,Float myprice) {
title=mytitle;code=mycode;last=mylast;isbn=myisbn;quantity=myquantity;price=myprice;
}
public void setTitle(String mytitle) {title=mytitle;}
public void setCode(String mycode) {code=mycode;}
public void setLast(String mylast) {last=mylast;}
public void setIsbn(String myisbn) {isbn=myisbn;}
public void setQuantity(Integer myquantity) {quantity=myquantity;}
public void setPrice(Float myprice) {price=myprice;}
public String getTitle(){return title;}
public String getCode(){return code;}
public String getLast(){return last;}
public String getIsbn(){return isbn;}
public Integer getQuantity(){return quantity;}
public Float getPrice(){return price;}
public Float getValue(Integer discount){return (float)(discount*price);} //Find out what to return here
public void showBook (){} //How i must show the book?
}
/*---Compiler Erros --- */
public Float getValue(Integer discount){return (float)(discount*price);} //Find out what to return here
^
Package/book.java [40:1] incompatible types
found : float
required: java.lang.Float
public Float getValue(Integer discount){return (float)(discount*price);} //Find out what to return here
^
2 errors
Errors compiling book.
As you can see i cannot conver to float.... Why ..? What i must do?
Your mistake was that you defined the return type of getValue as Float (the class java.lang.Float - note the capital F) but you were trying to cast the integer into the java primitive type of float (lower case f). The two aren't the same thing. The Float is an actual class which contains all sorts of methods for dealing with floating point numbers; float is just a basic primitive type to hold a number, it doesn't have any methods: it isn't a class.
Yup, as it stands with java you can only cast primitive types, there's an execption, but overall this is the case, Float is a wrapper as mentioned, a class within itself, so java see's it and says, what ya on about, nobody told me you knew each other?
Big hint aswell, Sun havae provied online and downloadable probably the most clearest, accesbile and succint api documentation I have ever see
Last edited by Looking_Lost; 04-24-2004 at 02:20 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.