SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Uml And Patterns
Exercise:
Programming Projects
Question:5 | ISBN:9780132830317 | Edition: 5

Question

It is possible to purchase “geek” watches that output the time of day in binary. To illustrate the flexibility of the Model-View-Controller pattern, modify the view class (CounterView) of the previous problem so that the display outputs the counter’s value in binary.

Test your new program by counting values in binary. You should not have to change the model or controller classes.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

// Main.java (unchanged)
public class Main 
{
    public static void main(String[] args)
    {
        // Create a new CounterModel and CounterView
        CounterModel model = new CounterModel();
        CounterView view = new CounterView();

        // Create a new CounterController and set the model and view
        CounterController controller = new CounterController();
        controller.setModel(model);
        controller.setView(view);

        // Add the view as an observer to the model
        model.addObserver(view);

        // Increment the counter ten times to see the binary representation
        for (int i = 0; i < 10; i++) 
        {
            controller.increment();
            // Add a delay to observe the changes (only for demonstration purposes)
            try {
                Thread.sleep(1000);
            } 
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
        }
    }
}

 

OUTPUT:

0
1
10
11
100
101
110
111
1000
1001

 

0 0

Discussions

Post the discussion to improve the above solution.