Pages

Saturday, 11 March 2017

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

33.5 (Addition quiz) Write a JSF program that generates addition quizzes randomly, as shown in Figure 33.29a. After the user answers all questions, it displays the result, as shown in Figure 33.29b.


import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped

public class Exercise05 {
    private int size = 10;
    private List<Numbers> numbers  = new ArrayList<Numbers>();
    private int correctCount = 0;
    
    public Exercise05() {
        renew();
    }    
    
    
    public void renew() {
        numbers.clear();
        for (int i = 0; i < size; i++) {
            numbers.add(new Numbers());
        }        
        correctCount = 0;
    }
    
    public List<Numbers> getNumbers() {
        return numbers;
    }

    public int getCorrectCount() {
        return correctCount;
    }
    
    public void resetCorrectCount() {
        correctCount = 0;
    }
    
    public int getSize() {
        return size;
    }
    
    public class Numbers {
        private int number1;
        private int number2;
        private int result;
        private String guess = "";

        public Numbers() {
            number1 = (int)(Math.random() * 20);
            number2 = (int)(Math.random() * 20);
            result = number1 + number2;
        }
        
        public Numbers(int number1, int number2) {
            this.number1 = number1;
            this.number2 = number2;
            result = number1 + number2;
        }
        
        public int getNumber1() {
            return number1;
        }

        public int getNumber2() {
            return number2;
        }

        public int getResult() {
            return result;
        }

        public String getGuess() {
            return guess;
        }
        
        

        public void setNumber1(int number1) {
            this.number1 = number1;
            result = number1 + number2;
        }

        public void setNumber2(int number2) {
            this.number2 = number2;
            result = number1 + number2;
        }

        public void setGuess(String guess) {
            this.guess = guess;
        }
        
        public String check() {
            try {
                if(Integer.parseInt(guess) == result) {
                    correctCount++;
                    return "Correct";
                } else {
                    return "Wrong";
                }            
            } catch (NumberFormatException e) {
                return "Wrong";
            }
        }
    
    }
    
}

<?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://java.sun.com/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Exercise05</title>
    </h:head>
    <h:body>
        <br />
        <br />
        <center>
        <h:form>
            <table>
                <ui:repeat value="#{exercise05.numbers}" var="number">
                    <tr>
                        <td height="30"><h:outputLabel value="#{number.number1}"/></td>
                        <td height="30"><h:outputLabel value="+"/></td>
                        <td height="30"><h:outputLabel value="#{number.number2}"/></td>
                        <td height="30"><h:outputLabel value="="/></td>
                        <td height="30"><h:inputText size = "3" value="#{number.guess}" /></td>
                    </tr>
                </ui:repeat>
            </table>
            <br />
            <h:commandButton value="Submit" action = "Exercise05_2"/>
            <h:commandButton value="Generate New Questions" action = "#{exercise05.renew}"/>
        </h:form>
        </center>
    </h:body>
</html>
Exercise05_2
<?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://java.sun.com/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Exercise05_2</title>
    </h:head>
    <h:body>
        <br />
        <br />
        <center>
            <table>
                <ui:repeat value="#{exercise05.numbers}" var="number">
                    <tr>
                        <td height="30"><h:outputLabel value="#{number.number1}"/></td>
                        <td height="30"><h:outputLabel value="+"/></td>
                        <td height="30"><h:outputLabel value="#{number.number2}"/></td>
                        <td height="30"><h:outputLabel value="="/></td>
                        <td height="30"><h:outputLabel value="#{number.guess}"/></td>
                        <td height="30"><h:outputLabel value="#{number.check()}"/></td>
                    </tr>
                </ui:repeat>
            </table>
            <br />
            There are <h:outputLabel value="#{exercise05.correctCount}"/> correct guesses
            #{exercise05.resetCorrectCount()}
        </center>
    </h:body>
</html>

No comments:

Post a Comment