Welcome back to my Java programming tutorial series! In today’s tutorial we will look at type conversion, variables, a few other small tips, as well as formatting.
First, we will look at incrementing variables. ++ increments a variable by 1 and – decrements a variable by one. There are many other expressions that can be used to do math on variables so be sure to check them out in the Java API’s.
Type casting is used to change an expression from one data type to another. It would using a command that looks like =(datatype)(expression). We can also round numbers before casting them. This can be done by adding .5 to the number before casting it or by using the round function from the Math class.
Printf is a great way to format statements. You can use it to set width, precision, and other characters such as new line or a percent symbol. Today’s tutorial will use printf to format numbers.
The last topic before we get into our tutorial programs is the scanner class. The scanner class is used so that the information can be entered by the program user. This information would be set to a variable. In order to use the scanner class there are three steps:
1. Import the scanner class. This would use a import statement: import java.util.*;
2. Define a new scanner object. If we don’t define this new object, we wont be able to use it. This is done with a statement: scanner console = new Scanner(System.in);
3. Read the input. Lastly we must read the input and store it in a variable by using a statement: variable = console.nextInt();. The nextInt can be replaced with other data types as we will see later.
Now on to our programs! The first program will deal with formatting.
1. Assume that the double variable number1 holds the value 0.0329. What format would you use with the printf method to display the number as 0.033.
So to do this we would would declare and initialize the variable: double number1 = 0.0329; Then to format the number we would use the printf method with some formatting: System.out.printf(“The value is: %9.3f “, number1); This statement can be broken down to show what each part does. The % tells the program that we are using formatting, the 9 sets the width for the number(including the 4 preceeding spaces), the .3 sets the number of places after the decimal, the f makes it a floating point number and the returns a new line.
2. Assume that the double variable number2 holds the value 0.0329. What format string would you use with the printf method to display the number as 0.03(7 spaces)?
We would use the same method as part one to complete this part. We need to declare and initialize the variable and then format it.
3. Assume that the double variable number3 holds the value 456198736.3382. What format string would you use with the printf method to display the number as 456,198,736.34?
For our second program today, we will do some simple calculations on money. I will give you the instructions and then we will take a look at my program.
Write a program that will take a dollar amount and convert it to the appropriate number of quarters, dimes, nickels, and pennies. The dollar amount to use for this program is $5.67. By using simple calculations, the program must output the number of quarters, dimes, nickels and pennies needed to create the equivalent dollar amount. For example, if the dollar amount was $2.76, the following would be the output: Quarters: 11 Dimes: 0 Nickels: 0 Pennies: 1. Begin by declaring constant to represent the value of quarters, dimes, nickels, and pennies. Remember that a constant should look like: static final int QUARTERS = 25; From that point, you may want to start by dividing 276 by the number 25. The quotient will be the number of quarters(11), while the remainder will be the amount of change left over. Be sure to use the appropriate variable names: Here is an examples: 276/25 = 11 R1, 1/10 = 0 R1, 1/5 = 0 R1, 1/1 = 1 R0.
Congratulations! If you’ve followed along with the tutorials, you can probably write your own basic Java tutorial! Let me know what you’ve learned so far or what you’d like to learn in the coming tutorials! See you next time!
If you would like to see the finished program and how it looks when it’s run, please visit my blog