Results 1 to 11 of 11
  1. #1
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Problem getting value from 2 variables

    Hey I'm working on an assignment and I have 5 objects setting values for different variables. The problem is I'm trying to multiply length times width and store that value in the variable totalfeet. Well I out printed length and width and both of the values are there but I can't seem to store a value for totalfeet. I out printed it and all it shows is 0.0, so if anyone knows what wrong with it it would be appreciated.


    Here's the main class
    Java Code:
    public class GLL {
    
    	//variables
    	
    	private String address;
    	private String city;
    	private String time;
    	private double width;
    	private double length;
    	
    	//constructors
    	
    	public GLL(){
    		
    	}
    	
    	public GLL(String add,String cit,Double wid,Double len){
    		address = add;
    		city = cit;
    		width = wid;
    		length = len;
    	}
    	
    	//set methods
    	
    	public void setAddress(String newAddress){
    		address = newAddress;
    	}
    	
    	public void setCity(String newCity){
    		city = newCity;
    	}
    	
    	public void setLength(double newLength){
    		length = newLength;
    	}
    	
    	public void setWidth(double newWidth){
    		width = newWidth;
    	}
    	
    	double totalfeet = (length*width);       <<<<< the main problem is here i guess i tried making a private variable and putting it in a method with no luck still
    	
    //find size
    	
    	public void determineSize(){
    		if (totalfeet<=21780){
    			System.out.print("small");
    		}else if(totalfeet<=43560){
    			System.out.print("medium");     <<<<< these aren't working either since the value is 0.0
    		}else if(totalfeet<=87120){
    			System.out.print("large");
    		}else{
    			System.out.print("jumbo");
    		}
    	}
    		
    	//find time
    		
    	public void determineTime(){
    		if (totalfeet<=21780){
    			System.out.print("1 hour");
    		}else if(totalfeet<=43560){
    			System.out.print("2 hours");
    		}else if(totalfeet<=87120){
    			System.out.print("3 hours");
    		}else{
    			System.out.print("4 hours");
    		}
    	}
    	
    //get methods
    	
    	public String getAddress(){
    		return address;
    	}
    		
    	public String getCity(){
    		return city;
    	}
    	
    	public Double getLength(){
    		return length;
    	}
    	
    	public Double getWidth(){
    		return width;
    	}
    	
    	public Double getTotalFeet(){
    		return totalfeet;
    	}
    	
    	//to string
    	
    	public String toString1(){
    		return "Lives at: "+address+", in "+city+"\n"+"Yard size: ";
    	}
    	
    	public String toString2(){
    		return "\nTotal time: ";
    	}
    	
    }

    Tester Class
    Java Code:
    public class GLLTester {
    
    	public static void main(String [] args){
    		
    		GLL customer1 = new GLL("1907 Main Street","Gallifrey Beach",4000.00,4000.00);
    		
    		GLL customer2 = new GLL();
    		customer2.setAddress("1345 West Street");
    		customer2.setCity("Gallifrey Beach");
    		customer2.setWidth(6000.00);
    		customer2.setLength(6000.00);
    		
    		GLL customer3 = new GLL("3456 East Street","Gallifrey Beach",7000.00,7000.00);
    		
    		GLL customer4 = new GLL("6789 North Street","Gallifrey Beach",10000.00,10000.00);
    		
    		GLL customer5 = new GLL("4867 Main Street","Sarisota Beach",5000.00,5000.00);
    		
    		System.out.println("Customer 1");
    		System.out.print("Lives at: ");
    		System.out.print(customer1.getAddress()+", in ");
    		System.out.print(customer1.getCity());
    		System.out.println("");
    		System.out.print("Yard size: ");
    		customer1.determineSize();
    		customer1.getTotalFeet();
    		System.out.println("");
    		System.out.print("Total time: ");
    		customer1.determineTime();
    		System.out.println("");
    		System.out.println("");
    		System.out.println(customer1.getTotalFeet());
    		System.out.println(customer1.getWidth());
    		System.out.println(customer1.getLength());
    		
    		
    		System.out.println("Customer 2");
    		System.out.print(customer2.toString1());
    		customer2.determineSize();
    		System.out.print(customer2.toString2());
    		customer2.determineTime();
    		System.out.println("");
    		System.out.println("");
    		
    		System.out.println("Customer 3");
    		System.out.print(customer3.toString1());
    		customer3.determineSize();
    		System.out.print(customer3.toString2());
    		customer3.determineTime();
    		System.out.println("");
    		System.out.println("");
    		
    		System.out.println("Customer 4");
    		System.out.print(customer4.toString1());
    		customer4.determineSize();
    		System.out.print(customer4.toString2());
    		customer4.determineTime();
    		System.out.println("");
    		System.out.println("");
    		
    		System.out.println("Customer 5");
    		System.out.print(customer5.toString1());
    		customer5.determineSize();
    		System.out.print(customer5.toString2());
    		customer5.determineTime();
    		System.out.println("");
    		System.out.println("");
    		
    	}
    }

    Sorry for the messy out puts the teacher want us to use a tostring method along with the acual get methods.
    Thanks.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: Problem getting value from 2 variables

    The problem is that you are simply setting totalfeet to be initialized to the product of width * length outside of the constructor. So put the declaration of totalfeet in the constructor, not where you presently have it.

    And remember that you need to update totalfeet when you update either length or width.

    I would also recommend changing Double to double in your constructor signature to avoid any unnecessary boxing and unboxing operations.

    Regards.
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Problem getting value from 2 variables

    Ahh okay thanks so much I knew it was something simple.

  4. #4
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Problem getting value from 2 variables

    Should it look something like this because I'm not getting the expected output and the if statements are just defaulting to else.

    Java Code:
    public class GLL {
    
    	//variables
    	
    	private String address;
    	private String city;
    	private String time;
    	private double width;
    	private double length;
    	private double totalfeet;
    	//constructors
    	
    	public GLL(){
    		
    	}
    	
    	public GLL(String add,String cit,double wid,double len){
    		address = add;
    		city = cit;
    		width = wid;
    		length = len;
    		totalfeet = (len*wid);	
    	}
    	
    	//set methods
    	
    	public void setAddress(String newAddress){
    		address = newAddress;
    	}
    	
    	public void setCity(String newCity){
    		city = newCity;
    	}
    	
    	public void setLength(double newLength){
    		length = newLength;
    	}
    	
    	public void setWidth(double newWidth){
    		width = newWidth;
    	}
    	
    	
    	
    //find size
    	
    	public void determineSize(){
    		if (totalfeet<=21780){
    			System.out.print("small");
    		}else if(totalfeet<=43560){
    			System.out.print("medium");
    		}else if(totalfeet<=87120){
    			System.out.print("large");
    		}else{
    			System.out.print("jumbo");
    		}
    	}
    		
    	//find time
    		
    	public void determineTime(){
    		if (totalfeet<=21780){
    			System.out.print("1 hour");
    		}else if(totalfeet<=43560){
    			System.out.print("2 hours");
    		}else if(totalfeet<=87120){
    			System.out.print("3 hours");
    		}else{
    			System.out.print("4 hours");
    		}
    	}
    	
    //get methods
    	
    	public String getAddress(){
    		return address;
    	}
    		
    	public String getCity(){
    		return city;
    	}
    	
    	public Double getLength(){
    		return length;
    	}
    	
    	public Double getWidth(){
    		return width;
    	}
    	
    	public Double getTotalFeet(){
    		return totalfeet;
    	}
    	
    	//to string
    	
    	public String toString1(){
    		return "Lives at: "+address+", in "+city+"\n"+"Yard size: ";
    	}
    	
    	public String toString2(){
    		return "\nTotal time: ";
    	}
    	
    }

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Problem getting value from 2 variables

    The code looks ok. Check the value of totalfeet after the multiplication and compare this to the expected value. It could be values passed in initially are taking the total past 87120.

    Regards.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem getting value from 2 variables

    Quote Originally Posted by Ronin View Post
    The code looks ok.
    But it isn't: if the length or width is updated the totalFeet value isn't updated ... I'd leave out the variable completely and return the product of length and width if a getter wants to return the totalFeet value.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Problem getting value from 2 variables

    Ah yes, I stand corrected. I concentrated on the constructor and missed the set methods.

    Regards.

  8. #8
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Problem getting value from 2 variables

    Okay Thank you all for the posts sorry it took so long to get back with you.

  9. #9
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Problem getting value from 2 variables

    Okay so I got rid of the variable totalfeet. To do the if statements i had to make local variables totalfeet. I think thats okay the main problem is when ever I run them it keeps defaulting to the else in the if statement and when I out print the value it say like 1.6E7 which is a huge number I'm guessing. I'm not sure how in the world it's getting this it should take width and multiply times length. The weird this is both of those values is 4000. So 4000*4000 which is 16000 and theres a 16 in 1.6E7. Any Ideas.

    Java Code:
    public class GLL {
    
    	//variables
    	
    	private String address;
    	private String city;
    	private String time;
    	private double width;
    	private double length;
    	//constructors
    	
    	public GLL(){
    		
    	}
    	
    	public GLL(String add,String cit,double wid,double len){
    		address = add;
    		city = cit;
    		width = wid;
    		length = len;	
    	}
    	
    	//set methods
    	
    	public void setAddress(String newAddress){
    		address = newAddress;
    	}
    	
    	public void setCity(String newCity){
    		city = newCity;
    	}
    	
    	public void setLength(double newLength){
    		length = newLength;
    	}
    	
    	public void setWidth(double newWidth){
    		width = newWidth;
    	}
    	
    	
    	
    //find size
    	
    	public void determineSize(){
    		
    		double totalfeet = length*width;
    		
    		if (totalfeet<=21780){
    			System.out.print("small");
    		}else if(totalfeet<=43560){
    			System.out.print("medium");
    		}else if(totalfeet<=87120){
    			System.out.print("large");
    		}else{
    			System.out.print("jumbo");
    		}
    	}
    		
    	//find time
    		
    	public void determineTime(){
    		double totalfeet = length*width;
    		
    		if (totalfeet<=21780){
    			System.out.print("1 hour");
    		}else if(totalfeet<=43560){
    			System.out.print("2 hours");
    		}else if(totalfeet<=87120){
    			System.out.print("3 hours");
    		}else{
    			System.out.print("4 hours");
    		}
    	}
    	
    //get methods
    	
    	public String getAddress(){
    		return address;
    	}
    		
    	public String getCity(){
    		return city;
    	}
    	
    	public Double getLength(){
    		return length;
    	}
    	
    	public Double getWidth(){
    		return width;
    	}
    	
    	public Double getTotalFeet(){
    		return (length*width);
    	}
    	
    	//to string
    	
    	public String toString1(){
    		return "Lives at: "+address+", in "+city+"\n"+"Yard size: ";
    	}
    	
    	public String toString2(){
    		return "\nTotal time: ";
    	}
    	
    }

  10. #10
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Problem getting value from 2 variables

    Quote Originally Posted by codyjava View Post
    So 4000*4000 which is 16000 and theres a 16 in 1.6E7. Any Ideas.
    As I mentioned in my last post you need to check your maths. 4000*4000 does not equal 16000.

    Regards.

  11. #11
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Problem getting value from 2 variables

    wow I feel so dumb thanks for the help I never thought about that thank you.

Similar Threads

  1. Problem accessing variables in other objects
    By falconfetus8 in forum New To Java
    Replies: 10
    Last Post: 11-15-2011, 03:08 AM
  2. Replies: 14
    Last Post: 09-07-2011, 02:16 AM
  3. Replies: 5
    Last Post: 03-17-2011, 05:19 PM
  4. problem with variables
    By javahead in forum New To Java
    Replies: 4
    Last Post: 12-09-2008, 06:55 PM
  5. Problem with variables in java
    By carl in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:50 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
  •