These exercises allow you to practice displaying values
on the screen using printf().
Put your code in a new empty Java file named "PrintfPractice.java", and make sure to name the class PrintfPractice.
Start with a structure similar to VariableExercises.java. You will have a method called main() and then four other methods: part1() through part4().
Write a method that creates three variables: an int a double, and a String. Put an integer value of your choosing
into the first variable, a real number into the second, and a
word or phrase into the third.
Then, display the values of these three variables on the screen, one per line.
For this part, you must use printf() only. You
may not use print() or println().
1024 4.417 Dante's Inferno
Store your birth month and age into two variables and
display their values on the screen with printf().
You may not use print() or println().
I was born in July and this year I will be 17 years old.
Create three variables: an int a
double, and a String.
Put appropriate values in each one.
Then, display the values using interesting formatting as dictated by the table below.
| line | type of value | special formatting |
|---|---|---|
| 1 | decimal (int) | none |
| 2 | decimal (int) | width of 6 |
| 3 | decimal (int) | width of 8 |
| 4 | decimal (int) | width of 8, padded with zeros |
| 5 | decimal (int) | width of 6, padded with zeros |
| 6 | blank line | |
| 7 | floating-point (double) | none |
| 8 | floating-point (double) | round to 1 place |
| 9 | floating-point (double) | round to 2 places, width of 10 |
| 10 | blank line | |
| 11 | string | none |
| 12 | string | width of 25 |
| 13 | string | width of 25, aligned left |
|1024| | 1024| | 1024| |00001024| |001024| |4.417000| |4.4| | 4.42| |Dante's Inferno| | Dante's Inferno| |Dante's Inferno |
Use several variables to store the names and other information of some hypothetical students. Then, display a nice little table displaying everything.
Your table doesn't need to look exactly like this, but unlike the previous exercise, it must line up.
For this part, you must use printf() only. You
may not use print() or println().
Also, if you're trying to add spaces all over the place to get it to line up, you're doing it wrong. Also, all of the widths within a certain column should have the same number, or you're also probably doing it wrong.
There's some code below you can steal to save you a bit of time.
+-------------------------------------------+ | Bell Faye 833055 9 103.03 | | Chatterton Michelle 820335 11 92.00 | | Holder Anthony 848040 10 82.11 | | Rush Ed 822535 12 101.08 | | Longhurst Kevan 832985 9 96.40 | +-------------------------------------------+
String last1 = "Bell"; String last2 = "Chatterton"; String last3 = "Holder"; String last4 = "Rush"; String last5 = "Longhurst"; String first1 = "Faye"; String first2 = "Michelle"; String first3 = "Anthony"; String first4 = "Ed"; String first5 = "Kevan"; int stuid1 = 833055; int stuid2 = 820335; int stuid3 = 848040; int stuid4 = 822535; int stuid5 = 832985; int gr1 = 9; int gr2 = 11; int gr3 = 10; int gr4 = 12; int gr5 = 9; double gpa1 = 103.03; double gpa2 = 92; double gpa3 = 82.11; double gpa4 = 101.08; double gpa5 = 96.4;