Results 1 to 11 of 11
- 02-22-2013, 06:06 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
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.
- 02-22-2013, 06:31 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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.
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-22-2013, 06:36 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Re: Problem getting value from 2 variables
Ahh okay thanks so much I knew it was something simple.
- 02-22-2013, 06:42 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
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: "; } }
- 02-23-2013, 11:28 AM #5
Senior Member
- Join Date
- Oct 2010
- Posts
- 393
- Rep Power
- 11
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.
- 02-23-2013, 06:42 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Problem getting value from 2 variables
Build a wall around Donald Trump; I'll pay for it.
- 02-23-2013, 07:32 PM #7
Senior Member
- Join Date
- Oct 2010
- Posts
- 393
- Rep Power
- 11
Re: Problem getting value from 2 variables
Ah yes, I stand corrected. I concentrated on the constructor and missed the set methods.
Regards.
- 02-23-2013, 08:56 PM #8
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Re: Problem getting value from 2 variables
Okay Thank you all for the posts sorry it took so long to get back with you.
- 02-23-2013, 09:09 PM #9
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
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: "; } }
- 02-24-2013, 10:27 PM #10
Senior Member
- Join Date
- Oct 2010
- Posts
- 393
- Rep Power
- 11
- 02-25-2013, 05:07 AM #11
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Similar Threads
-
Problem accessing variables in other objects
By falconfetus8 in forum New To JavaReplies: 10Last Post: 11-15-2011, 03:08 AM -
Having a problem with a program telling me that variables are not being used
By Deafsilver in forum New To JavaReplies: 14Last Post: 09-07-2011, 02:16 AM -
Array with multiple variables - Problem with input
By Xinc in forum New To JavaReplies: 5Last Post: 03-17-2011, 05:19 PM -
problem with variables
By javahead in forum New To JavaReplies: 4Last Post: 12-09-2008, 06:55 PM -
Problem with variables in java
By carl in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 08:50 PM
Bookmarks