Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Limit To


  • Please log in to reply
6 replies to this topic

#1 ussr1943

ussr1943

  • Members
  • 490 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:03:23 AM

Posted 17 August 2006 - 10:00 PM

just what it says. right now i am making alot of simple small programs that are useful some ranging from how far away from a wall you are based on sound and the time it takes for echo, conversion tables ect. anyways i was thinking, about this code i was working on, it is a conversion table gallons/liters (note i would like to eventualy be able to just input for one or other and solve it, but thats later on, im excited : ) )
and the loop goes out to 100 gallons. but what would it do if i put the loop specifications to go up to say,
100,000,000,000,000?
what would it do, would it actually output all of that?! or would it not go, or would it make my comp really mad at me ? :thumbsup:
i don't think this can do much harm to comp but i am interested in knowing what would happen. and i am not sure if i should or not.


*i forgot to mention that i had table increase by 1 ( or ++ w/e floats ur boat)*

Edited by ussr1943, 17 August 2006 - 10:02 PM.

"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:23 AM

Posted 17 August 2006 - 10:32 PM

I am really tired, so maybe I am misunderstanding what you are trying to do. Why would you need loops to do a conversion? As far as making a loop that went up that high, you could not do it with ints. I'll leave the why you cannot use ints up to you.

As far as doing conversions, that is a simple multiplier. For converting gallons to liters, it is simply a matter of multiplying gallons by 3.7854118. For converting liters to gallons, you divide by 3.7854118. I can't see any reason that you would need a loop at all.

Unless you are just trying to figure out all of the conversions per gallon up to 100 gallons?

#3 ussr1943

ussr1943
  • Topic Starter

  • Members
  • 490 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:03:23 AM

Posted 18 August 2006 - 05:48 PM

i have it done, what i made was a table, it keeps out putting every conversion from 1 gallon to 100 gallons, i was woundering what if you made it go longer and if int won't hold it then i would just use long instead of int becuase int hold 32 bits(-2,147,483,648 upto 2,147,483,648,whereas long can hold up to 64 bits,and i'm too lazy to type out the range of that), anyways i was just woundering after i created the table.(i know this process isn't that great, its not efficient and its alot to scroll through but that is how i am learning it right now since i'm new)

*putting in the code, may help if not understandable still*
/*
  This program displays a conversion 
  table for gallons to liters
  uses the for loop
*/


class GaltoLitTable {
 public static void main(String args[]) {
  double gallons, liters;
  int counter;
  
  counter = 0; //start counter at 0
  for(gallons = 1; gallons <= 100; gallons++) {
   liters = gallons * 3.7845; // conversion
   System.out.println(gallons+" gallons = "+liters+" liters.");


   counter++; //increment line counter (by 1) with each loop iteration
   //every 10th line print a blank for easy readablity
   if(counter == 10) { 
   System.out.println();
   counter = 0; //reset the counter
   }
  }
 }
}

Edited by ussr1943, 18 August 2006 - 08:22 PM.

"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:23 AM

Posted 18 August 2006 - 11:14 PM

Do you want any constructive criticism? There are a few programming conventions that I could point out.

#5 ussr1943

ussr1943
  • Topic Starter

  • Members
  • 490 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:03:23 AM

Posted 19 August 2006 - 11:08 AM

i'm intrested in learning, so i'll take whatever you give out. thanks.
(keep in mind i don't know a whole lot, since i just started java a few days ago).

Edited by ussr1943, 19 August 2006 - 11:10 AM.

"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:01:23 AM

Posted 19 August 2006 - 04:57 PM

Just to be clear, you did not do anything wrong. You could make your code look much cleaner though by combining instructions:
//convert gallons to liters
for(double gal = 1; gal <= 100; gall++){
   System.out.println(gal+" gallons = "+ gal*3.7845 + " liters.");

   //every 10th line print a blank for easy readablity
   if(gal/10 == 0) System.out.println(" ");
}

I would do it this way because it eliminates two variables. You could also get away with calling gal as an int (in which case you would learn a little about casting). You will also need to look at how a computer does division to understand why the blanks wil be inserted where you want.

#7 ussr1943

ussr1943
  • Topic Starter

  • Members
  • 490 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:03:23 AM

Posted 19 August 2006 - 06:20 PM

good points, and i am just learning about casting so that is interesting.
i think that was a lil bit easyier way to do the convertion right there instead of declaring all that before. hmmm. and i will look up about the division, thanks for all your help.
"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users