Variables and Assignment Statements

Read this chapter, which covers variables and arithmetic operations and order precedence in Java.

8. Several Lines per Statement


Answer:

Maybe.

Several Lines per Statment

You can use several lines for a statement. Anywhere a space character is OK you can split a statement. This means you can't split a statement in the middle of a name, nor between the quote marks of a string literal, nor in the middle of a numeric literal. Recall that a "literal" is an explicit value in the program. Here is the program with some statements correctly put on two lines:

public 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) );
}
}

Although correct, the division of statements is confusing to humans. It is also true that anywhere one space is OK any number of spaces are OK. Here is a better style for the program:


public 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) );
}
}


It is a good idea to indent the second half of a split statement further than the start of the statement.

Question 9:

Is the following correct?

cla
   ss 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  : " + (hours
        Worked * payRate) );

    System.out.println("tax Amount  : " + (
        hoursWorked * payRate * taxRate) );
  }
}