There are some syntax errors:
int myPay, yourPay; // OK long good-by ; // bad identifier: "-" not allowed short shrift = 0; // OK double bubble = 0, toil= 9, trouble = 8 // missing ";" at end. byte the bullet ; // bad identifier: can't contain a space int double; // bad identifier: double is a reserved word char thisMustBeTooLong ; // OK in syntax, but a poor choice // for a variable name int 8ball; // bad identifier: can't start with a digit float a=12.3; b=67.5; c= -45.44; // bad syntax: don't use ";" to separate variables
class Example { public static void main ( String[] args ) { long hoursWorked = 40; double payRate = 10.0, taxRate = 0.10; System.out.println("Hours Worked: " + hoursWorked ); System.out.println("pay Amount : " + (hoursWorked * payRate) ); System.out.println("tax Amount : " + (hoursWorked * payRate * taxRate) ); } }
The example program, contains three variable declarations. The variables are given initial values.
The character *
means multiply.
In the program,
(hoursWorked * payRate)
means to
multiply the number stored in hoursWorked
by the number stored in payRate
.
Important Idea: When used as part of an expression, the name of a variable represents the value it holds.
(An expression is a part of a statement that asks for a value to be calculated.)
When it follows a character string,
+
means to
add characters to the end of the character string. So
"Hours Worked: " + hoursWorked
makes a
character string starting with "Hours Worked: " and ending with
characters for the value of hoursWorked
.
The program prints:
Hours Worked: 40 pay Amount : 400.0 tax Amount : 40.0
Remember that if you want to see this program run, copy it from your Web browser, paste it into Notepad or other text editor, save it to a file, compile, and run it.
Why did the program print the first 40
without a decimal point,
but printed the
second one with a decimal point as 40.0
?
(Hint: look at the variable declarations.)