Pages

Saturday, 11 March 2017

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

33.1 (Factorial table in JSF) Write a JSF page that displays a factorial page as
shown in Figure 33.25. Display the table in an h:outputText component. Set
its escape property to false to display it as HTML contents.


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

@ManagedBean
@RequestScoped
public class Exercise01 {
    
    public String getTable() {
        String result = "<h1><b>Display Factorials!</b></h1><table border=\"1\"><tr><td>Number</td><td>Factorial</td></tr>";
        for (int i = 0; i <= 10; i++) {
            result += "<tr><td>" + i + "</td><td>" + computeFactorial(i) + "</td></tr>";
        }
        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>Exercise01</title>
    </h:head>
    <h:body>
        <center>
            <h:outputText escape="false" value="#{exercise01.table}" />
        </center>
    </h:body>
</html>

No comments:

Post a Comment