Example Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

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

Example Program

Here is another example program, containing several variable declarations.

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 character * means multiply. In the program, (hoursWorked * payRate) means to multiply the number stored in hoursWorked by the number stored in payRate.

Important Idea: In 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 the window of Notepad, compile and run it. (See Chapter 7.)

QUESTION 7:

Why did the program print the first 40 without a decimal point, but printed the second one with a decimal point as 40.0 ?