Pages

Saturday, 11 March 2017

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

33.6 (Large factorial) Rewrite Exercise 33.1 to handle large factorial. Use the
BigInteger class introduced in Section 10.9.


import java.math.BigInteger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class Exercise06 {
    

    private int number;

    public int getNumber() {
        return number;
    }

    public void setNumber(int newValue) {
        number = newValue;
    }

    public String getFactorial() {
        BigInteger factorial = BigInteger.ONE;
        for (int i = 1; i <= number; i++) {
            factorial = factorial.multiply(new BigInteger(i + ""));
        }
        return factorial.toString();
    }
}

<?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>Exercise06</title>
    </h:head>
    <h:body>
        <h3>Compute Factorial Using a Bean</h3>
        <h:form>
            <h:outputLabel value="Enter new value: "/>
            <h:inputText size = "10" value="#{exercise06.number}" /><br /><br />
            <h:commandButton value="Compute Factorial"/><br /><br />
            Factorial of <h:outputLabel value="#{exercise06.number}"/> is <h:outputLabel value="#{exercise06.factorial}"/>
        </h:form>
    </h:body>
</html>

No comments :

Post a Comment