Radford University ITEC

ITEC120

hw02i, hw02g:
Practice writing functions; using Strings

Instructions for all programs

(clarifications are in orange)

As with all programs for this class, you need to include:

hw02i, due Sep.01 (Fri) 4

  1. (10pts, GB2min)

    Do this problem individually.

    You are shopping for mp3 players, and are wondering how much storage you need for your music files. You decide that 1GB of storage5 holds 940 minutes of music, approximately6. Write a function GB2min which converts a number of GB to an estimated number of minutes of mp3 music which that space can hold. For example, GB2min(1) = 940 is one test case.

hw02p, due Sep.05 (Tues)

Do the remaining problems with (exactly one) partner, as discussed in lecture and on the course-info sheet. Your functions below will be added to the class you've did for hw02i. You can choose one or the other of your previous solutions, or even a re-written version based on class discussion

  1. (15pts, GB2albums)

    Write a function which takes as input a number of GB, and returns the number of entire albums which could entirely fit into that number of GB. (A plausible name for this function is GB2albums, but you are free to choose any reasonable name.) For example, 0.5GB might be enough space to store 10.6 albums; this means it can fit 10 albums entirely, so one test case might be GB2albums(0.5)=10. Therefore, the return type of this function should be int (but, see below).

    Use an estimate of 45min/album7. When writing test cases, you'll realize that you might need to round numbers down, since 100min is only enough to fit 2 complete albums. (No fractional albums!) Use the built-in function8 Math.floor to convert a fractional number to the nearest integer below it.

    Circumventing a 'possible loss of precision'

    Rather surprisingly, the number returned by Math.floor is not int, but is actually a double (albeit a double which will be as close to an int as it can be, like 123.000000000000, which implicitly suggests an accuracy of up to ±0.0000000000005.) This glitch is annoying; we'll discuss Java's rationale for this later, when we discuss Java's “nearly-guaranteed-correct” arithmetic).

    Two approaches are allowed for this homework, to overcome this glitch:

    Just so you know, there is a solution which really does return integers and won't ever crash; you can research classes java.math.BigInteger and java.math.BigDecimal if curious9

  2. (15pts, GB2albumMessage) Write a function which takes as input a number of GB, and returns a String along the lines of10 "4GB should be enough to fit 23 albums.". You can name this function GB2albumMessage, or anything else reasonable.

    Remember that your function should return this string, and not print it to the screen using System.out.println.

  3. (0pts) Reading from chapter 2: +Section 2.1 (Strings), +2.3 (primitive data types), + and +the first half of 2.4 (expressions, through Figure 2.4 (table of operator precedence)).

1This includes, but is not limited to, various arcane invocations like: public, static, void, main, [], private. Moreover, you are only allowed to use = (“gets”) for declaring/initializing named constants or local variables; do not assign a new value to an existing variable. We will use each of these keywords later in the semester, but only after we understand when we do (and don't) need them.      back

2There are a few other IDEs which let you write programs w/o the extra keywords, including DrJava and ProfessorJ, which let you write programs without those keywords. They're also free, but you're on your own to get them working for you; for each of them you'll need to set a “language level” preference to be Beginning Java.      back

3Okay, if you want, you can write an additional function which calls GB2albumsMessage and uses System.out.println on the result. You might have this function take one input, or you might have it take zero inputs (?!). Regardless, you must still write GB2albumsMessage.      back

4As always, due by the start of class      back

51GB = 1 gigabyte = 230 bytes =~ 109 bytes, or a billion bytes. Note that while capital B stands for bytes, a small b stands for bits (as in “54Mb/sec wireless”). 1B=8b.      back

6You are encouraged to go ahead and use a different min/GB estimate based on your personal mp3 library, and therefore more representative of the mp3-compression achieved on your preferred genres      back

7Or again, you may use an actual estimate based on one or both of y'all's actual mp3 libraries.      back

8You can find the documentation for math functions by using BlueJ's menu item Tools > Use Library Class > java.lang.Math, and click on Documentation. You also might want to bookmark http://java.sun.com/j2se/1.5.0/docs/api/java; you can also scan BlueJ's menu item Tools > Use Library Class > java.lang.Math. Finally, observe that the documentation on these web pages are actually the same as the documentation which the Java developers put at the start of each of their functions. (Those comments are actually used to generate the documentation---single point of control!)      back

9Note the small ‘m’ in math; this is different than class java.lang.Math. Sigh -- why is arithmetic so difficult?      back

10The string itself needn't contain any quotation marks--that is, "hello" is five letters, not seven.      back


©2006, Ian Barland, Radford University Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu Last modified 2006.Sep.05 (Tue)