epicoder
October 5th, 2012, 03:29 PM
I am by-in-large a python programmer, so I thought it would be a good idea to get some practice in a statically-typed language. I've been learning basic Java, so to test out what I've learned so far I decided to make the obvious joke and make a coffee-based class named Java. Har har har.
My question has to do with this particular bit of code:
float pct = delta/capacity;
int heatdelta = 180*pct;
//int heatdelta = 180 * (delta/capacity);
heat+=heatdelta;The basic idea is to calculate a percentage, then truncate the resulting float. If I use the above code, I get the following error:
main.java:94: error: possible loss of precision
int heatdelta=180*pct;
^
required: int
found: float
1 errorHowever, if I use the commented out line (notice that it is the same operations condensed into 1 line) it compiles fine and behaves as expected.
Here is the rest of the code sans the main class I use to test the Java class:
class JavaException extends Exception {
private String msg;
public JavaException(String err) {
super(err);
msg=err;
}
public String getError() {
return msg;
}
}
class HotException extends JavaException {
private String msg;
public HotException(String err) {
super(err);
msg=err;
}
public HotException() {
super("Ouch!");
msg="Ouch!";
}
public String getError() {
return msg;
}
}
class ColdException extends JavaException {
private String msg;
public ColdException(String err) {
super(err);
msg=err;
}
public ColdException() {
super("Eww!");
msg="Eww!";
}
public String getError() {
return msg;
}
}
class Java {
private int ounces;
private int capacity;
private int heat=180; //in F
public void cool() {
heat-=20;
}
public void drink(int amount) throws HotException, ColdException {
int delta=0;
System.out.println("Mmm, caffeine!");
if (ounces == 0) {
System.out.println("What? None left? Preposterous!");
return;
}
if (amount == 0) {
delta=ounces;
ounces=0;
} else {
delta=amount;
ounces-=delta;
if (ounces < 0) {
delta+=ounces;
ounces=0;
}
}
if (heat > 160) {
//System.out.println("Ow, this java is hot!");
throw new HotException();
} else if (heat < 100) {
//System.out.println("Eww, this java is lukewarm!");
throw new ColdException();
}
if (heat > 70) {
heat-=10;
}
System.out.println(delta + " ounce(s) consumed");
System.out.println(ounces + " ounce(s) remaining");
}
public void refill() {
if (ounces == capacity) {
System.out.println("Your coffee is already full, sir...");
return;
}
int delta=capacity-ounces;
if (delta == capacity) {
heat=180;
} else {
float pct = delta/capacity;
int heatdelta = 180*pct;
//int heatdelta = 180 * (delta/capacity);
heat+=heatdelta;
}
ounces=capacity;
System.out.println("Here you go, sir. Enjoy.");
}
public Java(int iounces) {
ounces=iounces;
capacity=iounces;
}
}
So, what is the proper way to truncate a float to an int?
My question has to do with this particular bit of code:
float pct = delta/capacity;
int heatdelta = 180*pct;
//int heatdelta = 180 * (delta/capacity);
heat+=heatdelta;The basic idea is to calculate a percentage, then truncate the resulting float. If I use the above code, I get the following error:
main.java:94: error: possible loss of precision
int heatdelta=180*pct;
^
required: int
found: float
1 errorHowever, if I use the commented out line (notice that it is the same operations condensed into 1 line) it compiles fine and behaves as expected.
Here is the rest of the code sans the main class I use to test the Java class:
class JavaException extends Exception {
private String msg;
public JavaException(String err) {
super(err);
msg=err;
}
public String getError() {
return msg;
}
}
class HotException extends JavaException {
private String msg;
public HotException(String err) {
super(err);
msg=err;
}
public HotException() {
super("Ouch!");
msg="Ouch!";
}
public String getError() {
return msg;
}
}
class ColdException extends JavaException {
private String msg;
public ColdException(String err) {
super(err);
msg=err;
}
public ColdException() {
super("Eww!");
msg="Eww!";
}
public String getError() {
return msg;
}
}
class Java {
private int ounces;
private int capacity;
private int heat=180; //in F
public void cool() {
heat-=20;
}
public void drink(int amount) throws HotException, ColdException {
int delta=0;
System.out.println("Mmm, caffeine!");
if (ounces == 0) {
System.out.println("What? None left? Preposterous!");
return;
}
if (amount == 0) {
delta=ounces;
ounces=0;
} else {
delta=amount;
ounces-=delta;
if (ounces < 0) {
delta+=ounces;
ounces=0;
}
}
if (heat > 160) {
//System.out.println("Ow, this java is hot!");
throw new HotException();
} else if (heat < 100) {
//System.out.println("Eww, this java is lukewarm!");
throw new ColdException();
}
if (heat > 70) {
heat-=10;
}
System.out.println(delta + " ounce(s) consumed");
System.out.println(ounces + " ounce(s) remaining");
}
public void refill() {
if (ounces == capacity) {
System.out.println("Your coffee is already full, sir...");
return;
}
int delta=capacity-ounces;
if (delta == capacity) {
heat=180;
} else {
float pct = delta/capacity;
int heatdelta = 180*pct;
//int heatdelta = 180 * (delta/capacity);
heat+=heatdelta;
}
ounces=capacity;
System.out.println("Here you go, sir. Enjoy.");
}
public Java(int iounces) {
ounces=iounces;
capacity=iounces;
}
}
So, what is the proper way to truncate a float to an int?