• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone help me with my homework. First year CS student.  RSS feed

 
Joseph Abney
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i am supposed to write a program where you can enter any number of bytes in the input and it is supposed to tell you how many gigabytes, megabytes, kilobytes, and bytes you just entered was. I will post down below what I have so far but I am stumbled. I know somehow you can use the Math.pow functions since KB is 2^10th bytes, and MB is 2^20 bytes, and GB is 2^30 bytes. I am just unsure on how to implement them. Thanks for your help!

 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to Coderanch

While posting code, make sure to UseCodeTags, it makes code readable and easier to understand.
Now coming to your problem, are you required to mention the bytes in GB, MB and KB or just the nearest unit.
Either case, you should not require the use of Math.pow(). Simply dividing the number by 1024 repeatedly gives you the required unit.
And since you are using division, you are better off using doubles instead of ints.

For example 898745489 bytes is 877681.1416 KB, 857.11049 MB or 0.83702 GB.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a way of getting KiB, MiB, GiB and TiB as integers by using the division and remainder operators together. You can read about those operators in the Java® Language Specification, but that can be difficult to understand. If you want GiB you are better off with a long than with an int, to give you enough capacity for all the digits.
There is another way to get those figures with the bitshift and bitwise AND operators, but that is an advanced topic.

By the way: there are two measurement types. There are kB. MB, GB and TB which go up in ratios of 1000, and there are KiB MiB GiB and Tib which go up in ratios of 2¹⁰ = 1024. Knowing that removes the need to use Math#pow, a Amit has already told you. It probably doesn't matter whether you use 1024 or 1000, but maybe you should ask about that.
 
Keshav Madhav Jha
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joseph Abney,

Replace //Calculations with the following code:



*** Also try to take all variableas as Long type because int has limited range. Hope you will get the desired output
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please do not try to give complete solutions. Please read what it says on the title page of this forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
It does not help to give a complete solution before the original poster has had a chance to work out their own solution.
Don't be annoyed but I have pulled rank and deleted the answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!