Saturday 11 March 2017

Chapter 33 Exercise 2, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

33.2 (Multiplication table) Write a JSF page that displays a multiplication table as shown in Figure 33.26.


import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class Exercise02 {
    
    public String getTable() {
        String result = "<table align=\"center\"  border=\"1\">";
        int tableSize = 9;
        result = result + "<tr><td colspan=" + (tableSize + 1) + " align=\"center\"  bgcolor=\"#cccccc\" height=30><b>Multiplication table</b></td></tr>";
        result = result + "<tr><td  width=\"40\" bgcolor=\"#cccccc\"></td>";
        for (int i = 1; i <= tableSize; i++) {
            result = result + "<td align=\"center\"  width=\"40\"  bgcolor=\"#cccccc\" >" + i + "</td>";
        }
        result = result + "</tr>";
        for (int i = 1; i <= tableSize; i++) {
            result = result + "<tr><td align=\"center\"  bgcolor=\"#cccccc\" >" + i + "</td>";
            for (int j = 1; j <= tableSize; j++) {
                result = result + "<td align=\"center\" >" + (i * j) + "</td>";
            }
            result = result + "</tr>";
        }
        result = result + "</table>";
        return result;
    }
    
    private long computeFactorial(int n) {
        if (n == 0) {
            return 1;
        } else {
            return n * computeFactorial(n - 1);
        }
    }
}

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Exercise02</title>
    </h:head>
    <h:body>
        <center>
            <h:outputText escape="false" value="#{exercise02.table}" />
        </center>
    </h:body>
</html>

No comments :

Post a Comment