• Post Reply Bookmark Topic Watch Topic
  • New Topic

Conversion  RSS feed

 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I am not sure how to go about . Can anyone please explain me how go with the convertYards() method.SO tht the value passed is converted to yards.

Q : Convert a feet variable of type Test. For example ,6002 feet is converted to 2feet,240 yards,1mile.



 
Jimmy Die
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Your paramater variable 'yards'(which is of type long) is the same as your local variable 'yards' which is of type Test. This is a conflict and will be a problem.

Next thing you want to do is probably divide the number of yards by 1760. This will give you miles, and should be assigned to the miles private variable.

Next step will be to use the modulus operator '%' to find the remainder of 'number of yards' % 1760. This remainder should be assigned to yards private variable.

Stick with it!
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I corrected the first step ,I did not see to it properly,thanx. But
I did not get the other steps. Can you please explain more in detail.
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody pleaseeeeeeee help me with the above question.
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q : Convert a feet variable of type Test. For example ,6002 feet is converted to 2feet,240 yards,1mile.

I am not sure of the example because 6002 feet will be 2000.66 yards and 2000.66 yards will be 1.137 miles as per the static final variables you have defined in your code.
Is it that you want the answer to be 2feet,240 yards,1mile???

public static Test convertYards(long yards)
// Returns yards converted into a variable of Test. { Test yards = new Test(); }
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q : Convert a feet variable of type Test. For example ,6002 feet is converted to 2feet,240 yards,1mile.

I am not sure of the example because 6002 feet will be 2000.66 yards and 2000.66 yards will be 1.137 miles as per the static final variables you have defined in your code.
Is it that you want the answer to be 2feet,240 yards,1mile???

public static Test convertYards(long ipYards //yards))
// Returns yards converted into a variable of Test.
{
Test testYards //yards = new Test();
testYards.yards = (int)ipYards;(precision will be lost)
or you can also use
testYards.yards = new Long(ipYards).intValue();
return testYards;
}

Now I am still not sure of your question.
Hope this helps

Thanks
Kareem
[ March 28, 2005: Message edited by: Kareem Qureshi ]
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it even better by defining getter and setter methods on your variables and the code will be cleaner.
Let me know if you still need help.

Thanks
Kareem
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I got the logic ,but when I try to put it in the program I ma not getting the ouput. Can anybody please expalin me how to do it.

Logic :I have 6002 feet. how many feet in a mile? call it 4500(not sure)
6002/4500 = 1mile
6002%4500 = 1502 feet remaining
now how many feet in a yard? (1 feet = 3 yards)
1502/3 = 500 yards
1502%3 = 2 feet remainder.

so I got 1 mile, 500 yards, 2 feet out of 6002 feet
--------------------------------------------------------
public static Distances convertYards(long Yards)
// Returns yards converted into a variable of Distances.
{
Distances testyards = new Distances();
testyards.yards = (int)Yards;
testyards.miles = (int) (testyards.yards/5280);
testyards.yards = testyards.miles/3;
testyards.feet = testyards.miles/3;
return testyards;
}
public static void main(String[] args)
{
Distances d = new Distances();
System.out.println(Distances.convertYards(6002));
}
-----------------------------------------------------------------

the output is shown as istances@10b62c9

Please help me out.
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Distances is an Object. You will have to override the toString() to get the String format of your answer.

for example
public String toString(){
String actualDistance = miles+" miles, "+yards+" yards, "+feet+" feet";
return actualDistance;
}
add this method to Distance class and
You can modify main() System.out.println(Distances.convertYards(6002).toString())

or

//this will print on the Console
System.out.println(miles+" miles, "+yards+" yards, "+feet+" feet");
add this as the last line in your convertYards() method.

Hope this helps.

Thanks
Kareem
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!