Results 1 to 8 of 8
- 02-14-2011, 12:10 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 38
- Rep Power
- 0
Please help with simple program.. Very simple.
Hi can someone help me. I want this program to return a double value but all i am getting is 0 returned??? Thanks...
TesterJava Code:public class Parcel { private double weight; private char destination; double total; public Parcel(double weight, char destination){ this.weight = weight; this.destination = destination; } public double getWeight(){ return this.weight; } public char getDest(){ return this.destination; } public void getCost(){ double cost = 1.0; if(this.destination == 'S'){ if (this.weight < 1.0){ cost = 4.65; } else if (this.weight >= 1 && this.weight < 2){ cost = 4.65; } else if (this.weight >= 2){ cost = 4.65; } } else if(this.destination == 'M' || this.destination == 'B'){ if (this.weight < 1.0){ cost = 6.55; } else if (this.weight >= 1 && this.weight < 2){ cost = 7.0; } else if (this.weight >= 2){ cost = 7.45; } } else if(this.destination == 'A'){ if (this.weight < 1.0){ cost = 6.8; } else if (this.weight >= 1 && this.weight < 2){ cost = 7.5; } else if (this.weight >= 2){ cost = 8.2; } } else if(this.destination == 'P'){ if (this.weight < 1.0){ cost = 8.1; } else if (this.weight >= 1 && this.weight < 2){ cost = 10.10; } else if (this.weight >= 2){ cost = 12.10; } } else if(this.destination == 'H'){ if (this.weight < 1.0){ cost = 7.65; } else if (this.weight >= 1 && this.weight < 2){ cost = 9.2; } else if (this.weight >= 2){ cost = 10.75; } this.total = cost; } } public double getTotal(){ return this.total; } }
Java Code:public class ParcelTest { public static void main(String[] args){ Parcel p1 = new Parcel (0.8, 'S'); p1.getCost(); System.out.println(p1.total); System.out.println(p1.getTotal()); Parcel p2 = new Parcel (0.8, 'D'); p2.getTotal(); System.out.println(p2.total); System.out.println(p2.getTotal()); Parcel p3 = new Parcel (0.8, 'P'); p3.getTotal(); System.out.println(p3.total); System.out.println(p3.getTotal()); Parcel p4 = new Parcel (0.8, 'A'); p4.getTotal(); System.out.println(p4.total); System.out.println(p4.getTotal()); } }
- 02-14-2011, 12:16 AM #2
You indentation makes it very hard to read your code.
It would seem that the only time you actual assign a value to total is when the destination is 'H'.Java Code:else if(this.destination == 'H'){ // code this.total = cost; }
- 02-14-2011, 12:23 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 38
- Rep Power
- 0
Oh ok. Yer 'H' does return a value. Why is that?
- 02-14-2011, 12:27 AM #4
Because of where you placed the total = ... statement. As I said your indentation is hard to read. If you used a better style it would easier to spot your mistake and where the line of code should be instead.
- 02-14-2011, 12:36 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 38
- Rep Power
- 0
Does this make it easier? Sorry but im new to this.
Java Code:public class Parcel { private double weight; private char destination; double total; public Parcel(double weight, char destination){ this.weight = weight; this.destination = destination; } public double getWeight(){ return this.weight; } public char getDest(){ return this.destination; } public void getCost(){ double cost = 1.0; if(this.destination == 'S'){ if (this.weight < 1.0){ cost = 4.65; } else if (this.weight >= 1 && this.weight < 2){ cost = 4.65; } else if (this.weight >= 2){ cost = 4.65; } } else if(this.destination == 'M' || this.destination == 'B'){ if (this.weight < 1.0){ cost = 6.55; } else if (this.weight >= 1 && this.weight < 2){ cost = 7.0; } else if (this.weight >= 2){ cost = 7.45; } } else if(this.destination == 'A'){ if (this.weight < 1.0){ cost = 6.8; } else if (this.weight >= 1 && this.weight < 2){ cost = 7.5; } else if (this.weight >= 2){ cost = 8.2; } } else if(this.destination == 'P'){ if (this.weight < 1.0){ cost = 8.1; } else if (this.weight >= 1 && this.weight < 2){ cost = 10.10; } else if (this.weight >= 2){ cost = 12.10; } } else if(this.destination == 'H'){ if (this.weight < 1.0){ cost = 7.65; } else if (this.weight >= 1 && this.weight < 2){ cost = 9.2; } else if (this.weight >= 2){ cost = 10.75; } this.total = cost; } } public double getTotal(){ return this.total; } }
- 02-14-2011, 12:40 AM #6
Gawd no, that's worse.
Anyway I told you what the problem is, you only set total when destination is 'H' but you want total to be set regardless of what the destination is. Surely it should be simple to figure out where that line of code should be instead of where you have it.
- 02-14-2011, 12:42 AM #7
Another point (nitpick)
What is the point of that if statement?Java Code:if (this.weight < 1.0){ cost = 4.65; } else if (this.weight >= 1 && this.weight < 2){ cost = 4.65; } else if (this.weight >= 2){ cost = 4.65; }
- 02-14-2011, 12:44 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
Jsp n html simple program
By jugalrockz in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 01-29-2011, 07:47 PM -
Simple program help
By jtyler in forum New To JavaReplies: 3Last Post: 09-20-2010, 07:43 AM -
simple program
By blastoff in forum New To JavaReplies: 5Last Post: 04-14-2010, 11:25 PM -
Simple Program
By TheRocket in forum Advanced JavaReplies: 15Last Post: 12-30-2008, 02:35 PM -
Stuck - simple program
By dirtycash in forum New To JavaReplies: 4Last Post: 11-24-2008, 07:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks