Results 1 to 8 of 8
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

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

    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;
    	}
    		
    }
    Tester

    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());
    
    		
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You indentation makes it very hard to read your code.
    Java Code:
    else if(this.destination == 'H'){
        // code
        this.total = cost;
    }
    It would seem that the only time you actual assign a value to total is when the destination is 'H'.

  3. #3
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    Oh ok. Yer 'H' does return a value. Why is that?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  5. #5
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    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;
    	}
    		
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Another point (nitpick)
    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;
    }
    What is the point of that if statement?

  8. #8
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    Another point (nitpick)
    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;
    }
    What is the point of that if statement?
    Lol. Ok thanks for the help..... Really appreciate it.

Similar Threads

  1. Jsp n html simple program
    By jugalrockz in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 01-29-2011, 08:47 PM
  2. Simple program help
    By jtyler in forum New To Java
    Replies: 3
    Last Post: 09-20-2010, 08:43 AM
  3. simple program
    By blastoff in forum New To Java
    Replies: 5
    Last Post: 04-15-2010, 12:25 AM
  4. Simple Program
    By TheRocket in forum Advanced Java
    Replies: 15
    Last Post: 12-30-2008, 03:35 PM
  5. Stuck - simple program
    By dirtycash in forum New To Java
    Replies: 4
    Last Post: 11-24-2008, 08:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •