OOP2

Adding the remaining files to Git for tracking.

Author
Maarten Vangeneugden
Date
Nov. 21, 2015, 4:07 p.m.
Hash
5fab871ac7a91a310136d82a35850ab48869b026
Parent
ec784ef46729d6aa39254d243bd10d18625e1fad
Modified files
04/calculator/calculator.java
04/calculator/view.java
04/stockManagement/Boek.java
04/stockManagement/beschrijving.txt
04/stockManagement/boek.java
04/stockManagement/gui.java
04/stockManagement/help.java
04/stockManagement/main.java
05/AbstractController.java
05/AbstractView.java
05/Clock.java
05/ClockAnalogView.java
05/ClockController.java
05/ClockDigitalView.java
05/ClockModel.java
05/ClockTools.java
05/ClockUpdate.java
05/Controller.java
05/DigitalClockController.java
05/View.java
05/readme.txt
06/Opdracht2.txt
06/exercises/knopjes.java
06/exercises/tempie/Klok/AbstractController.java
06/exercises/tempie/Klok/AbstractView.java
06/exercises/tempie/Klok/Controller.java
06/exercises/tempie/Klok/View.java
06/exercises/tempie/Klok/clock/Clock.java
06/exercises/tempie/Klok/clock/ClockAnalogView.java
06/exercises/tempie/Klok/clock/ClockController.java
06/exercises/tempie/Klok/clock/ClockDigitalView.java
06/exercises/tempie/Klok/clock/ClockModel.java
06/exercises/tempie/Klok/clock/ClockTools.java
06/exercises/tempie/Klok/clock/ClockUpdate.java
06/exercises/tempie/calculator/Controller.java
06/exercises/tempie/calculator/View.java
06/exercises/tempie/calculator/actionButton.java
06/exercises/tempie/calculator/button.java
06/exercises/tempie/calculator/calculator.java
06/opdracht1.java
06/opdracht3.java
06/test.java
Challenge 1/ChoiceCourse.java
Challenge 1/Course.java
Challenge 1/DetailedCourseView.java
Challenge 1/MainWindow.java
Challenge 1/MandatoryCourse.java
Challenge 1/Planner.java
Challenge 1/StudyPath.java
Challenge 1/ontwerpkeuzes.txt
Challenge 1/opleidingsonderdelen.txt
Challenge 1/study.java

04/calculator/calculator.java

50 additions and 0 deletions.

View changes Hide changes
+
1
		public enum e_action {ADD, SUB, DIV, MUL};
+
2
+
3
		public calculator(int x, int y, e_action action) {
+
4
				setX(x);
+
5
				setY(y);
+
6
				switch(action){
+
7
						case ADD:
+
8
								addition();
+
9
								break;
+
10
						case SUB:
+
11
								substraction();
+
12
								break;
+
13
						case DIV:
+
14
								division();
+
15
								break;
+
16
						case MUL:
+
17
								multiplication();
+
18
								break;
+
19
				}
+
20
		}
+
21
		private int m_x;
+
22
		private int m_y;
+
23
		private int m_result;
+
24
+
25
		public int x() {
+
26
				return m_x;}
+
27
		public void setX(int x) {
+
28
				m_x = x;}
+
29
		
+
30
		public int y() {
+
31
				return m_y;}
+
32
		public void setY(int y) {
+
33
				m_y = y;}
+
34
		
+
35
		public int result() {
+
36
				return m_result;}
+
37
		public void setResult(int result) {
+
38
				m_result = result;}
+
39
+
40
		private addition() {
+
41
				setResult(x() + y());}
+
42
		private void substraction() {
+
43
				setResult(x() - y());}
+
44
		private void division() {
+
45
				setResult(x() / y());}
+
46
		private void multiplication() {
+
47
				setResult(x() * y());}
+
48
}
+
49
+
50

04/calculator/view.java

5 additions and 0 deletions.

View changes Hide changes
+
1
+
2
class view{
+
3
		private int test;
+
4
}
+
5

04/stockManagement/Boek.java

30 additions and 0 deletions.

View changes Hide changes
+
1
		public Boek(String naam, int prijs, e_type type) {
+
2
				setNaam(naam);
+
3
				setPrijs(prijs);
+
4
				setType(type);
+
5
		}
+
6
+
7
		public enum e_type {FOTOBOEK, ROMAN, STRIP};
+
8
		
+
9
		private e_type m_type;
+
10
		private String m_naam;
+
11
		private int m_prijs;
+
12
+
13
		public e_type type() {
+
14
				return m_type;}
+
15
		public void setType(e_type type) {
+
16
				m_type = type;}
+
17
+
18
		public String naam() {
+
19
				return m_naam;}
+
20
		public void setNaam(String naam) {
+
21
				m_naam = naam;}
+
22
		
+
23
		public int prijs() {
+
24
				return m_prijs;}
+
25
		public void setPrijs(int prijs) {
+
26
				m_prijs = prijs;}
+
27
		
+
28
}
+
29
+
30

04/stockManagement/beschrijving.txt

5 additions and 0 deletions.

View changes Hide changes
+
1
In plaats van voor alle verschillende boeken een aparte class te maken, heb ik besloten om de verschillende soorten aan te duiden middels een enum.
+
2
De verschillende soorten boeken bieden geen specialisatie aan. Ook zou inheritance ervoor zorgen dat, als er in de basisklasse iets verandert (een extra variabele), in elke afgeleide klasse dezelfde aanpassingen moeten worden doorgevoerd.
+
3
Deze keuze komt ook tegemoet aan het principe van low coupling.
+
4
Ik heb ook besloten de GUI een aparte class te geven, om tegemoet te komen aan de MVC-architectuur.
+
5

04/stockManagement/boek.java

30 additions and 0 deletions.

View changes Hide changes
+
1
		public Boek(String naam, int prijs, e_type type) {
+
2
				setNaam(naam);
+
3
				setPrijs(prijs);
+
4
				setType(type);
+
5
		}
+
6
+
7
		public enum e_type {FOTOBOEK, ROMAN, STRIP};
+
8
		
+
9
		private e_type m_type;
+
10
		private String m_naam;
+
11
		private int m_prijs;
+
12
+
13
		public e_type type() {
+
14
				return m_type;}
+
15
		public void setType(e_type type) {
+
16
				m_type = type;}
+
17
+
18
		public String naam() {
+
19
				return m_naam;}
+
20
		public void setNaam(String naam) {
+
21
				m_naam = naam;}
+
22
		
+
23
		public int prijs() {
+
24
				return m_prijs;}
+
25
		public void setPrijs(int prijs) {
+
26
				m_prijs = prijs;}
+
27
		
+
28
}
+
29
+
30

04/stockManagement/gui.java

73 additions and 0 deletions.

View changes Hide changes
+
1
		import java.util.*;
+
2
+
3
		class gui extends javax.swing.JFrame {
+
4
				public gui() {
+
5
						//Generating a couple of books:
+
6
						List<Boek> temp = new ArrayList<Boek>();
+
7
						temp.add(new Boek("Fotocollage", 5, Boek.e_type.FOTOBOEK));
+
8
						temp.add(new Boek("Vijftig tinten", 80, Boek.e_type.ROMAN));
+
9
						temp.add(new Boek("De jacht op een voetbal", 2, Boek.e_type.STRIP));
+
10
						setBoeken(temp);
+
11
						
+
12
						//Putting the books in a JList:
+
13
						DefaultListModel list = new DefaultListModel();
+
14
+
15
						for(int i=0; i<temp.size(); i++) {
+
16
								list.addElement(temp.get(i).naam());
+
17
						}
+
18
+
19
						final JList jlist = new JList(list);
+
20
+
21
						jlist.addListSelectionListener( //Dit voegt de listeners toe voor deze list. werk. (By Mannu)
+
22
										new javax.swing.event.ListSelectionListener() {
+
23
												public void valueChanged(javax.swing.event.ListSelectionEvent event) {
+
24
														viewBookDetails(event, jlist); }
+
25
										});
+
26
+
27
						JPanel panel = new JPanel();
+
28
						panel.add(jlist);
+
29
						JLabel label = new JLabel("Een test");
+
30
						panel.add(label);
+
31
+
32
						JFrame frame = new JFrame("Stock management");
+
33
						frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
34
+
35
						frame.getContentPane().add(panel);
+
36
+
37
						
+
38
						frame.pack();
+
39
						frame.setVisible(true);
+
40
				}
+
41
+
42
				public void viewBookDetails(javax.swing.event.ListSelectionEvent event, JList boeken) {
+
43
						Boek boek = boeken.getSelectedValue();
+
44
						//Creating a new panel with book information:
+
45
						JPanel panel = new JPanel();
+
46
						JLabel naam = new JLabel(boek.naam());
+
47
						JLabel prijs = new JLabel(boek.prijs());
+
48
						panel.add(naam);
+
49
						panel.add(prijs);
+
50
						frame().getContentPane.add(panel);
+
51
+
52
						frame.pack();
+
53
						frame.setVisible(true);
+
54
		}
+
55
+
56
+
57
+
58
			private List<Boek> l_boeken;
+
59
			private JPanel m_jpanel;
+
60
+
61
			public List<Boek> boeken() {
+
62
					return l_boeken;}
+
63
			public void setBoeken(List<Boek> boeken) {
+
64
					l_boeken = boeken;}
+
65
		
+
66
			public JPanel jpanel() {
+
67
					return m_jpanel;}
+
68
			public void setPanel(JPanel jpanel) {
+
69
					m_jpanel = jpanel;}
+
70
+
71
}
+
72
+
73

04/stockManagement/help.java

15 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.*;
+
2
class help {
+
3
		public static int selectedIndex(ListSelectionModel list) {
+
4
				//Declaring variables to limit the search to the selected options only:
+
5
				int minimum = list.getMinSelectionIndex();
+
6
				int maximum = list.getMaxSelectionIndex();
+
7
				//Looping through the possible options and checking if it's selected:
+
8
				for(int i = minimum; i <= maximum; i++) {
+
9
						if(list.isSelectedIndex(i))
+
10
								return i;
+
11
				}
+
12
				return -1;
+
13
		}
+
14
}
+
15

04/stockManagement/main.java

6 additions and 0 deletions.

View changes Hide changes
+
1
		public static void main(String[] args) {
+
2
				gui test = new gui();
+
3
+
4
		}
+
5
}
+
6

05/AbstractController.java

37 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 *
+
4
 * @author jvermeulen
+
5
 */
+
6
public abstract class AbstractController implements Controller {
+
7
    private Observable mModel;
+
8
    private View mView;
+
9
    
+
10
    public AbstractController(Observable model) {
+
11
        // Set the model.
+
12
        setModel(model);
+
13
    }
+
14
    
+
15
    
+
16
    @Override
+
17
    public void setView(View view) {
+
18
        mView = view;
+
19
    }
+
20
+
21
    @Override
+
22
    public View getView() {
+
23
        return mView;
+
24
    }
+
25
+
26
    @Override
+
27
    public void setModel(Observable model) {
+
28
        mModel = model;
+
29
    }
+
30
+
31
    @Override
+
32
    public Observable getModel() {
+
33
        return mModel;
+
34
    }
+
35
    
+
36
}
+
37

05/AbstractView.java

73 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Observer;
+
2
+
3
/**
+
4
 *
+
5
 * @author jvermeulen
+
6
 */
+
7
public abstract class AbstractView implements View, Observer {
+
8
+
9
    private Observable mModel;
+
10
    private Controller mController;
+
11
    
+
12
    /**
+
13
     * Empty constructor so that the model and controller can be set later.
+
14
     */
+
15
    public AbstractView() {        
+
16
    }
+
17
    
+
18
    public AbstractView(Observable model, Controller controller) {
+
19
        // Set the model.
+
20
        setModel(model);
+
21
        // If a controller was supplied, use it. Otherwise let the first call to 
+
22
        // getController() create the default controller.
+
23
        if (controller != null) {
+
24
            setController(controller);
+
25
        }
+
26
    }
+
27
    
+
28
    @Override
+
29
    public void setController(Controller controller) {
+
30
        mController = controller;
+
31
        // Tell the controller this object is its view.
+
32
        getController().setView(this);
+
33
    }
+
34
+
35
    @Override
+
36
    public Controller getController() {
+
37
        // If a controller hasn't been defined yet...
+
38
        if (mController == null) {
+
39
            // ...make one. Note that defaultController is normally overriden by 
+
40
            // the AbstractView subclass so that it returns the appropriate 
+
41
            // controller for the view.
+
42
            setController(defaultController(getModel()));
+
43
        }
+
44
        
+
45
        return mController;
+
46
    }
+
47
+
48
    @Override
+
49
    public void setModel(Observable model) {
+
50
        mModel = model;
+
51
    }
+
52
+
53
    @Override
+
54
    public Observable getModel() {
+
55
        return mModel;
+
56
    }
+
57
+
58
    @Override
+
59
    public Controller defaultController(Observable model) {
+
60
        return null;
+
61
    }
+
62
+
63
    /**
+
64
     * A do-nothing implementation of the Observer interface's update method.
+
65
     * Subclasses of AbstractView will provide a concrete implementation for 
+
66
     * this method.
+
67
     */
+
68
    @Override
+
69
    public void update(Observable o, Object arg) {    
+
70
    }
+
71
    
+
72
}
+
73

05/Clock.java

78 additions and 0 deletions.

View changes Hide changes
+
1
import java.awt.event.WindowEvent;
+
2
import javax.swing.BoxLayout;
+
3
import javax.swing.JFrame;
+
4
+
5
/**
+
6
 * Main user interface for the application. Makes sure the model, views and
+
7
 * controllers are connected to each other.
+
8
 * @author jvermeulen
+
9
 */
+
10
public class Clock {
+
11
    // The clock data (i.e., the Model).
+
12
    private ClockModel mModel;
+
13
    
+
14
    // A digital view of the clock.
+
15
    private ClockDigitalView mDigitalView;
+
16
    
+
17
    // A toolbar for controlling the clock.
+
18
    private ClockTools mTools;
+
19
    
+
20
    // An analog view of the clock.
+
21
    private final ClockAnalogView mAnalogView;
+
22
    
+
23
    public Clock(int hour, int minute, int second) {
+
24
        // Create the data model.
+
25
        mModel = new ClockModel();
+
26
+
27
        // Create the digital clock view.
+
28
        mDigitalView = new ClockDigitalView(mModel, null);
+
29
        mModel.addObserver(mDigitalView);
+
30
+
31
        // Create the analog clock view.
+
32
        mAnalogView = new ClockAnalogView(mModel, null);
+
33
        mModel.addObserver(mAnalogView);
+
34
        
+
35
        // Create the clock tools view.
+
36
        mTools = new ClockTools(mModel, null);
+
37
        mModel.addObserver(mTools);
+
38
        
+
39
        // Set the time.
+
40
        mModel.setTime(hour, minute, second);
+
41
+
42
        // Start the clock ticking.
+
43
        mModel.start();
+
44
    }
+
45
    
+
46
    public void createGUI() {
+
47
        JFrame frame = new JFrame("Clock");
+
48
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
+
49
        frame.getContentPane().add(mDigitalView.getUI());
+
50
        frame.getContentPane().add(mAnalogView);
+
51
        frame.getContentPane().add(mTools.getUI());                
+
52
        frame.addWindowListener(new WindowAdapter() {
+
53
            @Override
+
54
            public void windowClosing(WindowEvent e) {
+
55
                System.exit(0);
+
56
            }
+
57
        });
+
58
        frame.pack();
+
59
        frame.setVisible(true);
+
60
    }
+
61
    
+
62
    private static void createAndShowGUI() {
+
63
        Clock clock = new Clock(9, 10, 22);
+
64
        clock.createGUI();
+
65
    }
+
66
    
+
67
    public static void main(String[] args) {
+
68
        //Schedule a job for the event-dispatching thread:
+
69
        //creating and showing this application's GUI.
+
70
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
+
71
            @Override
+
72
            public void run() {
+
73
                createAndShowGUI();
+
74
            }
+
75
        });
+
76
    }
+
77
}
+
78

05/ClockAnalogView.java

218 additions and 0 deletions.

View changes Hide changes
+
1
import java.awt.Dimension;
+
2
import java.awt.Graphics;
+
3
import java.awt.Graphics2D;
+
4
import java.awt.RenderingHints;
+
5
import java.awt.geom.AffineTransform;
+
6
import java.awt.geom.Ellipse2D;
+
7
import java.awt.geom.GeneralPath;
+
8
import java.util.Calendar;
+
9
import java.util.Observable;
+
10
import javax.swing.JComponent;
+
11
+
12
/**
+
13
 * An analog clock View for the ClockModel. This View has no user inputs, so
+
14
 * no controller is required. This class implements the View interface, and
+
15
 * subclasses JComponent directly, to illustrate that not all Views need to
+
16
 * inherit from AbstractView.
+
17
 *
+
18
 * Note: it is not necessary to understand all the details of the rendering 
+
19
 * of the analog clock.
+
20
 *
+
21
 * @author jvermeulen (inspired by IBM Java2D tutorial)
+
22
 */
+
23
public class ClockAnalogView extends JComponent implements View {
+
24
+
25
    private Observable mModel;
+
26
    private Controller mController;
+
27
+
28
    int mHour;
+
29
    int mMinute;
+
30
    int mSecond;
+
31
+
32
    ClockAnalogView(Observable model, Controller controller) {
+
33
        mModel = model;
+
34
        mController = controller;
+
35
    }
+
36
+
37
    @Override
+
38
    public void setController(Controller controller) {
+
39
        mController = controller;
+
40
    }
+
41
+
42
    @Override
+
43
    public Controller getController() {
+
44
        return mController;
+
45
    }
+
46
+
47
    @Override
+
48
    public void setModel(Observable model) {
+
49
        mModel = model;
+
50
    }
+
51
+
52
    @Override
+
53
    public Observable getModel() {
+
54
        return mModel;
+
55
    }
+
56
+
57
    @Override
+
58
    public Controller defaultController(Observable model) {
+
59
        return null;
+
60
    }
+
61
+
62
    @Override
+
63
    public void update(Observable o, Object info) {
+
64
        // Cast info to ClockUpdate type.
+
65
        ClockUpdate clockInfo = (ClockUpdate) info;
+
66
+
67
        // Store hour, minute, second for repainting.
+
68
        mHour = clockInfo.getHour()+5; // +5 Geeft een latere tijdzone weer. Dit is namelijk opdracht 3.
+
69
        mMinute = clockInfo.getMinute();
+
70
        mSecond = clockInfo.getSecond();
+
71
+
72
        // Using a little trigonometry, set the transforms to rotate
+
73
        // each hand into the proper position.  Center the rotation
+
74
        // around the pivot point (50, 50) instead of the origin
+
75
        hourTransform.setToRotation(((double) mHour) *
+
76
                                            (Math.PI / 6.0), 50, 50);
+
77
        minuteTransform.setToRotation(((double) mMinute) *
+
78
                                            (Math.PI / 30.0), 50, 50);
+
79
        secondTransform.setToRotation(((double) mSecond) *
+
80
                                            (Math.PI / 30.0), 50, 50);
+
81
+
82
        repaint();
+
83
    }
+
84
+
85
+
86
    /**
+
87
     * Sets this components preferred size to 150x150.
+
88
     * @return
+
89
     */
+
90
    @Override
+
91
    public Dimension getPreferredSize() {
+
92
        return new Dimension(100, 100);
+
93
    }
+
94
+
95
    // Create a shape for the face of the clock
+
96
    protected static Ellipse2D face = new Ellipse2D.Float(3, 3, 94, 94);
+
97
+
98
    // Create a path that represents a tick mark
+
99
    protected static GeneralPath tick = new GeneralPath();
+
100
    static // http://www.jusfortechies.com/java/core-java/static-blocks.php
+
101
    {
+
102
        tick.moveTo(100, 100);
+
103
        tick.moveTo(49, 0);
+
104
        tick.lineTo(51, 0);
+
105
        tick.lineTo(51, 6);
+
106
        tick.lineTo(49, 6);
+
107
        tick.lineTo(49, 0);
+
108
    }
+
109
+
110
    // Create a cool hour hand
+
111
    protected static GeneralPath hourHand = new GeneralPath();
+
112
    static
+
113
    {
+
114
        hourHand.moveTo(50, 15);
+
115
        hourHand.lineTo(53, 50);
+
116
        hourHand.lineTo(50, 53);
+
117
        hourHand.lineTo(47, 50);
+
118
        hourHand.lineTo(50, 15);
+
119
    }
+
120
+
121
    // Create a cool minute hand
+
122
    protected static GeneralPath minuteHand = new GeneralPath();
+
123
    static
+
124
    {
+
125
        minuteHand.moveTo(50, 2);
+
126
        minuteHand.lineTo(53, 50);
+
127
        minuteHand.lineTo(50, 58);
+
128
        minuteHand.lineTo(47, 50);
+
129
        minuteHand.lineTo(50, 2);
+
130
    }
+
131
+
132
    // And a cool second hand
+
133
    protected static GeneralPath secondHand = new GeneralPath();
+
134
    static
+
135
    {
+
136
        secondHand.moveTo(49, 5);
+
137
        secondHand.lineTo(51, 5);
+
138
        secondHand.lineTo(51, 62);
+
139
        secondHand.lineTo(49, 62);
+
140
        secondHand.lineTo(49, 5);
+
141
    }
+
142
+
143
    // Create some colors for the pieces of the clock
+
144
    protected static Color faceColor = new Color(220, 220, 220);
+
145
    protected static Color hourColor = Color.red.darker();
+
146
    protected static Color minuteColor = Color.blue.darker();
+
147
    protected static Color secondColor = new Color(180, 180, 0);
+
148
    protected static Color pinColor = Color.gray.brighter();
+
149
+
150
    // Create circles for the pivot and center pin
+
151
    protected Ellipse2D pivot = new Ellipse2D.Float(47, 47, 6, 6);
+
152
    protected Ellipse2D centerPin = new Ellipse2D.Float(49, 49, 2, 2);
+
153
+
154
+
155
    // Create three transforms that center around the pivot point
+
156
    protected AffineTransform hourTransform =
+
157
                    AffineTransform.getRotateInstance(0, 50, 50);
+
158
    protected AffineTransform minuteTransform =
+
159
                    AffineTransform.getRotateInstance(0, 50, 50);
+
160
    protected AffineTransform secondTransform =
+
161
                    AffineTransform.getRotateInstance(0, 50, 50);
+
162
+
163
    // Create a timer that fires once a second and a Calendar
+
164
    // instance for getting the time values
+
165
    // protected Timer timer = new Timer(1000, this);
+
166
    protected Calendar calendar = Calendar.getInstance();
+
167
+
168
    // This is an alternative to creating a UI delegate.  Since JPanel's
+
169
    // paint() method only paints the border and backgound, we can just
+
170
    // override the paint method of the component to do the graphics.
+
171
    public void paint(Graphics g)
+
172
    {
+
173
        // Call the superclass first to paint the border (if one is assigned)
+
174
        super.paint(g);
+
175
+
176
+
177
        // Get the graphics context and turn on anti-aliasing
+
178
        Graphics2D g2 = (Graphics2D) g;
+
179
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+
180
                            RenderingHints.VALUE_ANTIALIAS_ON);
+
181
+
182
        // Set the paint for the clock face and fill it in
+
183
        g2.setPaint(faceColor);
+
184
        g2.fill(face);
+
185
+
186
        // Set the paint to black and draw the clock's outline
+
187
        g2.setPaint(Color.black);
+
188
        g2.draw(face);
+
189
+
190
        // Fill in the 12 ticks around the face of the clock
+
191
        for (double p = 0.0; p < 12.0; p += 1.0)
+
192
        {
+
193
            // This is probably terribly inefficient and should be
+
194
            // done statically or in the constructor - draw the
+
195
            // tick as a transformed shape that is rotated.
+
196
            g2.fill(tick.createTransformedShape(
+
197
                AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,
+
198
                        50, 50)));
+
199
        }
+
200
+
201
        // Set the paint and draw the hour hand.  It is lowest in the
+
202
        // 'z-order' so will appear underneath the other hands.  Notice
+
203
        // how each hand is transformed by a different <AffineTransform>.
+
204
        g2.setPaint(hourColor);
+
205
        g2.fill(hourHand.createTransformedShape(hourTransform));
+
206
+
207
        // Set the paint and draw the minute hand, the second hand,
+
208
        // the pivot and the center pin
+
209
        g2.setPaint(minuteColor);
+
210
        g2.fill(minuteHand.createTransformedShape(minuteTransform));
+
211
        g2.setPaint(secondColor);
+
212
        g2.fill(secondHand.createTransformedShape(secondTransform));
+
213
        g2.fill(pivot);
+
214
        g2.setPaint(pinColor);
+
215
        g2.fill(centerPin);
+
216
    }
+
217
}
+
218

05/ClockController.java

42 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Calendar;
+
2
+
3
/**
+
4
 * A clock controller that allows the clock to be started, stopped and reset.
+
5
 * @author jvermeulen
+
6
 */
+
7
+
8
//Toevoeging van Maarten V.: onSystemTime() voor updaten systeemtijd. Ook het aanpassen van ClockDigitalView wordt hier behandeld.
+
9
//Extra toevoeging voor OEF04: onEdit() als de gebruiker via het tekstveld een aanpassing doorvoert.
+
10
public class ClockController extends AbstractController {
+
11
    public ClockController(Observable model) {
+
12
        super(model);
+
13
    }
+
14
    
+
15
    public void onStart() {
+
16
        ((ClockModel)getModel()).start();
+
17
    }
+
18
    
+
19
    public void onStop() {
+
20
        ((ClockModel)getModel()).stop();
+
21
    }
+
22
    
+
23
    public void onReset() {
+
24
        ((ClockModel)getModel()).setTime(0,0,0);
+
25
    }
+
26
+
27
	public void onSystemTime() {
+
28
			Calendar calendar = Calendar.getInstance();
+
29
			((ClockModel)getModel()).setTime(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
+
30
	}
+
31
+
32
    public void onEdit(String givenTime) {
+
33
			String[] parts = givenTime.split(":");
+
34
			System.out.println("Dit is al iets");
+
35
+
36
        ((ClockModel)getModel()).setTime(
+
37
			Integer.parseInt(parts[0]),
+
38
			Integer.parseInt(parts[1]),
+
39
			Integer.parseInt(parts[2]));
+
40
    }
+
41
}
+
42

05/ClockDigitalView.java

84 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Observable;
+
2
import javax.swing.JComponent;
+
3
import javax.swing.JTextField;
+
4
import java.awt.event.ActionEvent;
+
5
import java.awt.event.ActionListener;
+
6
+
7
/**
+
8
 * A digital clock View for the ClockModel. This View has no user inputs, so no 
+
9
 * Controller is required.
+
10
 * @author jvermeulen
+
11
 */
+
12
//BRING THE CHANGE! This should be updatable, so controllers and stuff will be added.
+
13
public class ClockDigitalView extends AbstractView {
+
14
    
+
15
    // The user interface for this view.
+
16
    private JTextField mClock;
+
17
    
+
18
    public ClockDigitalView(Observable model, DigitalClockController controller) {
+
19
        super(model, controller);
+
20
        init();
+
21
    }
+
22
    
+
23
    /**
+
24
     * Initializes the user interface.
+
25
     */
+
26
    private void init() {
+
27
        mClock = new JTextField();
+
28
		mClock.addActionListener(new ActionListener() {
+
29
				@Override
+
30
				public void actionPerformed(ActionEvent e) {
+
31
					((DigitalClockController)getController()).onEdit(mClock.getText());
+
32
				}
+
33
		});
+
34
    }
+
35
+
36
    /**
+
37
     * Updates the state of the on-screen digital clock.
+
38
     * Invoked automatically by ClockModel.
+
39
     * @param o The ClockModel object that is broadcasting an update
+
40
     * @param info A ClockUpdate instance describing the changes that have 
+
41
     * occurred in the ClockModel
+
42
     */
+
43
    @Override
+
44
    public void update(Observable o, Object info) {    
+
45
        // Cast info to ClockUpdate type.
+
46
        ClockUpdate clockInfo = (ClockUpdate) info;
+
47
        
+
48
        // Create a String representing the time.        
+
49
        String timeString = formatTime(clockInfo);
+
50
+
51
        // Display the new time in the clock text field.
+
52
        mClock.setText(timeString);
+
53
+
54
        // Fade the color of the display if the clock isn't running.
+
55
        if (clockInfo.isRunning()) {
+
56
          mClock.setBackground(Color.white);
+
57
        } else {
+
58
          mClock.setBackground(Color.gray);
+
59
        }
+
60
    }
+
61
    
+
62
    /**
+
63
     * Convenience method to return the user interface component. We don't need 
+
64
     * this if we implement View directly and directly subclass a GUI component.
+
65
     * @return the JComponent representing this View.
+
66
     */
+
67
	@Override
+
68
	public Controller defaultController(Observable model) {
+
69
			return new DigitalClockController(model);
+
70
	}
+
71
+
72
    public JComponent getUI() {
+
73
        return mClock;
+
74
    }
+
75
    
+
76
    private String formatTime(ClockUpdate info) {
+
77
        return String.format("%d:%d:%d", 
+
78
                             info.getHour(), 
+
79
                             info.getMinute(), 
+
80
                             info.getSecond());
+
81
    }        
+
82
    
+
83
}
+
84

05/ClockModel.java

153 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Date;
+
2
import java.util.GregorianCalendar;
+
3
import java.util.Observable;
+
4
import java.util.Timer;
+
5
import java.util.TimerTask;
+
6
import javax.swing.SwingUtilities;
+
7
+
8
/**
+
9
 *
+
10
 * @author jvermeulen
+
11
 */
+
12
public class ClockModel extends Observable {
+
13
    private int mHour;
+
14
    private int mMinute;
+
15
    private int mSecond;
+
16
    
+
17
    private final long mTickInterval = 1000; // tick every second
+
18
    private Timer mTimer;
+
19
    private boolean mIsRunning;
+
20
    
+
21
    public ClockModel() {
+
22
        // By default, set the clock time to the current system time.
+
23
        Calendar now = new GregorianCalendar();        
+
24
        setTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), 
+
25
                now.get(Calendar.SECOND));        
+
26
    }
+
27
    
+
28
	public ClockModel(int hour, int minute, int second) {
+
29
			setTime(hour, minute, second);
+
30
		}
+
31
    /**
+
32
     * Helper class to run the tick() method multiple times 
+
33
     * (e.g., every 1 second).
+
34
     */
+
35
    class TickTask extends TimerTask {
+
36
        public void run() {
+
37
            tick();
+
38
        }
+
39
    }
+
40
    
+
41
    public void start() {
+
42
        if (!mIsRunning) {
+
43
            mIsRunning = true;
+
44
+
45
            // Schedule the tick() method to run every mTickInterval milliseconds.
+
46
            mTimer = new Timer();
+
47
            mTimer.schedule(new TickTask(), 0, mTickInterval); 
+
48
            
+
49
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
50
                                               mIsRunning);
+
51
            setChanged();
+
52
            notifyObservers(info);
+
53
        }
+
54
    }
+
55
    
+
56
    public void stop() {
+
57
        if (mIsRunning) {
+
58
            mIsRunning = false;
+
59
            
+
60
            // Stop the timer.
+
61
            mTimer.cancel();
+
62
            mTimer = null;
+
63
            
+
64
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
65
                                               mIsRunning);
+
66
            setChanged();
+
67
            notifyObservers(info);
+
68
        }
+
69
    }
+
70
    
+
71
    /**
+
72
     * Sets the current time. Notifies observers of any change in time.
+
73
     * @param hour The new hour.
+
74
     * @param minute The new minute
+
75
     * @param second The new second
+
76
     */
+
77
    public void setTime(int hour, int minute, int second) {
+
78
        if (hour != mHour && isValidHour(hour)) {
+
79
            mHour = hour;
+
80
            setChanged();
+
81
        }
+
82
        
+
83
        if (minute != mMinute && isValidMinute(minute)) {
+
84
            mMinute = minute;
+
85
            setChanged();
+
86
        }
+
87
                
+
88
        if (second != mSecond && isValidSecond(second)) {
+
89
            mSecond = second;
+
90
            setChanged();
+
91
        }
+
92
        
+
93
        // If the model has changed, notify Views.
+
94
        if (hasChanged()) {
+
95
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
96
                                               mIsRunning);
+
97
            notifyObservers(info);
+
98
        }
+
99
    }    
+
100
    
+
101
    /**
+
102
     * Checks to see if a number is a valid hour (i.e. in [0,23]).
+
103
     * @param hour The hour to check
+
104
     * @return True if this is a valid hour
+
105
     */
+
106
    private boolean isValidHour(int hour) {
+
107
        return (hour >= 0 && hour <= 23);
+
108
    }
+
109
    
+
110
    /**
+
111
     * Checks to see if a number is a valid minute (i.e. in [0,59]).
+
112
     * @param minute The minute to check
+
113
     * @return True if this is a valid minute
+
114
     */
+
115
    private boolean isValidMinute(int minute) {
+
116
        return (minute >= 0 && minute <= 59);
+
117
    }
+
118
    
+
119
    /**
+
120
     * Checks to see if a number is a valid minute (i.e. in [0,59]).
+
121
     * @param second The second to check
+
122
     * @return True if this is a valid second
+
123
     */
+
124
    private boolean isValidSecond(int second) {
+
125
        return (second >= 0 && second <= 59);
+
126
    }
+
127
    
+
128
    private void tick() {
+
129
        // Get the current time
+
130
        int h = mHour;
+
131
        int m = mMinute;
+
132
        int s = mSecond;
+
133
        
+
134
        // Increment the current second, adjusting the minute and hour 
+
135
        // if necessary.
+
136
        s += 1;
+
137
        if (s > 59) {
+
138
            s = 0;
+
139
            m += 1;
+
140
            if (m > 59) {
+
141
                m = 0;
+
142
                h += 1;
+
143
                if (h > 23) {
+
144
                    h = 0;
+
145
                }
+
146
            }
+
147
        }
+
148
+
149
        // Set the new time.
+
150
        setTime(h, m, s);
+
151
    }
+
152
}
+
153

05/ClockTools.java

110 additions and 0 deletions.

View changes Hide changes
+
1
import java.awt.event.ActionEvent;
+
2
import java.awt.event.ActionListener;
+
3
import java.util.Observable;
+
4
import javax.swing.JButton;
+
5
import javax.swing.JComponent;
+
6
import javax.swing.JPanel;
+
7
+
8
/**
+
9
 *
+
10
 * @author jvermeulen
+
11
 */
+
12
public class ClockTools extends AbstractView {
+
13
    // The user interface for this view.
+
14
    private JPanel mTools;
+
15
    private JButton mStart;
+
16
    private JButton mStop;
+
17
    private JButton mReset;
+
18
	//Toevoeging van Vngngdn: Knop toevoegen die de klok op huidige systeemtijd instelt:
+
19
	private JButton mSystemTime;
+
20
    
+
21
    public ClockTools(Observable model, ClockController controller) {
+
22
        super(model, controller);
+
23
        init();
+
24
    }
+
25
+
26
    private void init() {
+
27
        mTools = new JPanel();
+
28
        mTools.setLayout(new GridLayout(1, 3));
+
29
        mStart = new JButton("Start");
+
30
        mStart.setEnabled(false);
+
31
        mStop = new JButton("Stop");
+
32
        mStop.setEnabled(false);
+
33
        mReset = new JButton("Reset");
+
34
		mSystemTime = new JButton("Systeemtijd");
+
35
        
+
36
        // handle events: pass to controller
+
37
        mStart.addActionListener(new ActionListener() {
+
38
+
39
            @Override
+
40
            public void actionPerformed(ActionEvent e) {
+
41
                ((ClockController)getController()).onStart();
+
42
            }
+
43
        });
+
44
        
+
45
        mStop.addActionListener(new ActionListener() {
+
46
+
47
            @Override
+
48
            public void actionPerformed(ActionEvent e) {
+
49
                ((ClockController)getController()).onStop();
+
50
            }
+
51
        });
+
52
                
+
53
        mReset.addActionListener(new ActionListener() {
+
54
+
55
            @Override
+
56
            public void actionPerformed(ActionEvent e) {
+
57
                ((ClockController)getController()).onReset();
+
58
            }
+
59
        });
+
60
        
+
61
        mSystemTime.addActionListener(new ActionListener() {
+
62
+
63
            @Override
+
64
            public void actionPerformed(ActionEvent e) {
+
65
                ((ClockController)getController()).onSystemTime();
+
66
            }
+
67
		});
+
68
+
69
        mTools.add(mStart);
+
70
        mTools.add(mStop);
+
71
        mTools.add(mReset);       
+
72
 		mTools.add(mSystemTime);
+
73
	}
+
74
    
+
75
        /**
+
76
     * Updates the state of the clock tools.
+
77
     * Invoked automatically by ClockModel.
+
78
     * @param o The ClockModel object that is broadcasting an update
+
79
     * @param info A ClockUpdate instance describing the changes that have 
+
80
     * occurred in the ClockModel
+
81
     */
+
82
    @Override
+
83
    public void update(Observable o, Object info) {    
+
84
        // Cast info to ClockUpdate type.
+
85
        ClockUpdate clockInfo = (ClockUpdate) info;
+
86
        
+
87
        if (clockInfo.isRunning()) {
+
88
            mStop.setEnabled(true);
+
89
            mStart.setEnabled(false);
+
90
        } else {
+
91
            mStart.setEnabled(true);
+
92
            mStop.setEnabled(false);
+
93
        }
+
94
    }
+
95
    
+
96
    @Override
+
97
    public Controller defaultController(Observable model) {
+
98
        return new ClockController(model);        
+
99
    }
+
100
    
+
101
    /**
+
102
     * Convenience method to return the user interface component. We don't need 
+
103
     * this if we implement View directly and directly subclass a GUI component.
+
104
     * @return 
+
105
     */
+
106
    public JComponent getUI() {
+
107
        return mTools;
+
108
    }
+
109
}
+
110

05/ClockUpdate.java

75 additions and 0 deletions.

View changes Hide changes
+
1
 * Represent a clock update including all necessary information.
+
2
 * @author jvermeulen
+
3
 */
+
4
public class ClockUpdate {
+
5
    private int mHour;
+
6
    private int mMinute;
+
7
    private int mSecond;
+
8
    private boolean mIsRunning; 
+
9
    
+
10
    
+
11
    ClockUpdate(int hour, int minute, int second, boolean isRunning) {
+
12
        mHour = hour;
+
13
        mMinute = minute;
+
14
        mSecond = second;
+
15
        mIsRunning = isRunning;
+
16
    }
+
17
+
18
    /**
+
19
     * @return the mHour
+
20
     */
+
21
    public int getHour() {
+
22
        return mHour;
+
23
    }
+
24
+
25
    /**
+
26
     * @param hour the hour to set
+
27
     */
+
28
    public void setHour(int hour) {
+
29
        this.mHour = hour;
+
30
    }
+
31
+
32
    /**
+
33
     * @return the mMinute
+
34
     */
+
35
    public int getMinute() {
+
36
        return mMinute;
+
37
    }
+
38
+
39
    /**
+
40
     * @param minute the minute to set
+
41
     */
+
42
    public void setMinute(int minute) {
+
43
        this.mMinute = minute;
+
44
    }
+
45
+
46
    /**
+
47
     * @return the mSecond
+
48
     */
+
49
    public int getSecond() {
+
50
        return mSecond;
+
51
    }
+
52
+
53
    /**
+
54
     * @param second the second to set
+
55
     */
+
56
    public void setSecond(int second) {
+
57
        this.mSecond = second;
+
58
    }
+
59
+
60
    /**
+
61
     * @return the mIsRunning
+
62
     */
+
63
    public boolean isRunning() {
+
64
        return mIsRunning;
+
65
    }
+
66
+
67
    /**
+
68
     * @param isRunning the isRunning to set
+
69
     */
+
70
    public void setRunning(boolean isRunning) {
+
71
        this.mIsRunning = isRunning;
+
72
    }
+
73
    
+
74
}
+
75

05/Controller.java

13 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 *
+
4
 * @author jvermeulen
+
5
 */
+
6
public interface Controller {
+
7
+
8
    void setView(View view);
+
9
    View getView();
+
10
    void setModel(Observable model);
+
11
    Observable getModel();
+
12
}
+
13

05/DigitalClockController.java

23 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 * A clock controller that allows the clock to be started, stopped and reset.
+
4
 * @author jvermeulen
+
5
 */
+
6
+
7
//Toevoeging van Maarten V.: onSystemTime() voor updaten systeemtijd. Ook het aanpassen van ClockDigitalView wordt hier behandeld.
+
8
//Extra toevoeging voor OEF04: onEdit() als de gebruiker via het tekstveld een aanpassing doorvoert.
+
9
public class DigitalClockController extends AbstractController {
+
10
    public DigitalClockController(Observable model) {
+
11
        super(model);
+
12
    }
+
13
+
14
    public void onEdit(String givenTime) {
+
15
			String[] parts = givenTime.split(":");
+
16
+
17
        ((ClockModel)getModel()).setTime(
+
18
			Integer.parseInt(parts[0]),
+
19
			Integer.parseInt(parts[1]),
+
20
			Integer.parseInt(parts[2]));
+
21
    }
+
22
}
+
23

05/View.java

17 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Observer;
+
2
+
3
/**
+
4
 *
+
5
 * @author jvermeulen
+
6
 */
+
7
public interface View extends Observer {
+
8
      
+
9
    void setController(Controller controller);
+
10
    Controller getController();
+
11
    
+
12
    void setModel(Observable model);
+
13
    Observable getModel();
+
14
    
+
15
    Controller defaultController(Observable model);
+
16
}
+
17

05/readme.txt

3 additions and 0 deletions.

View changes Hide changes
+
1
+
2
Aanpassen gebeurt direct na RETURN. Om te voorkomen dat de cursor steeds verspringt, zet de klok op pauze.
+
3

06/Opdracht2.txt

18 additions and 0 deletions.

View changes Hide changes
+
1
Permission is granted to copy, distribute and/or modify this document
+
2
under the terms of the GNU Free Documentation License, Version 1.3
+
3
or any later version published by the Free Software Foundation;
+
4
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
+
5
A copy of the license can be found at https://gnu.org/licenses/fdl.html
+
6
+
7
BOUNDED TYPE PARAMETERS
+
8
Bij generic typing worden doorgaans alle types toegelaten. Wenst men daarentegen een bepaalde selectie van types toe te laten (Bv. een machtsverheffing kan men moeilijk toepassen op een string), dan maakt men gebruik van bounded type parameters. De voorbeelden op https://docs.oracle.com/javase/tutorial/java/generics/bounded.html demonstreren hoe men dergelijk gedrag kan forceren.
+
9
Merk op dat het woord "extends" geen inheritance beschrijft. Dit is ook waarom het lijkt alsof multiple inheritance in Java gaat.
+
10
(In Java bestaat multiple inheritance, maar da's iets anders)
+
11
+
12
UNBOUNDED WILDCARDS
+
13
Dit is een principe een vorm van generic typing die complementair is met bounded type parameters; een unbounded wildcard accepteert elk mogelijk type.
+
14
Merk ook op dat het niet mogelijk is om objecten toe te voegen aan een list van unbounded wildcards; enkel null is mogelijk.
+
15
+
16
UPPER BOUNDED WILDCARDS
+
17
Dit is niet hetzelfde als bounded type parameters. In principe betekent dit dat men een class type geeft aan een unbounded wildcard (? extends eggs). Dit heeft als gevolg dat enkel de types worden toegelaten die ofwel van het type eggs zijn, of een subclass zijn van eggs (dus een class "spam extends eggs" voldoet aan de wildcard).
+
18

06/exercises/knopjes.java

25 additions and 0 deletions.

View changes Hide changes
+
1
 */
+
2
import javax.swing.*;
+
3
+
4
class knopjes {
+
5
		public static void main(String[] args) {
+
6
				JFrame frame = new JFrame("Knoppentest");
+
7
				JPanel panel = new JPanel();
+
8
				frame.setContentPane(panel);
+
9
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
10
				//frame.setLayout(frame.getContentPane());
+
11
+
12
				JMenu menu = new JMenu();
+
13
				JButton knop = new JButton("Dit is de knop");
+
14
				
+
15
				panel.add(menu);
+
16
				panel.add(knop);
+
17
				frame.pack();
+
18
+
19
+
20
+
21
+
22
				frame.setVisible(true);
+
23
		}
+
24
}
+
25

06/exercises/tempie/Klok/AbstractController.java

39 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Observable;
+
3
+
4
/**
+
5
 *
+
6
 * @author jvermeulen
+
7
 */
+
8
public abstract class AbstractController implements Controller {
+
9
    private Observable mModel;
+
10
    private View mView;
+
11
    
+
12
    public AbstractController(Observable model) {
+
13
        // Set the model.
+
14
        setModel(model);
+
15
    }
+
16
    
+
17
    
+
18
    @Override
+
19
    public void setView(View view) {
+
20
        mView = view;
+
21
    }
+
22
+
23
    @Override
+
24
    public View getView() {
+
25
        return mView;
+
26
    }
+
27
+
28
    @Override
+
29
    public void setModel(Observable model) {
+
30
        mModel = model;
+
31
    }
+
32
+
33
    @Override
+
34
    public Observable getModel() {
+
35
        return mModel;
+
36
    }
+
37
    
+
38
}
+
39

06/exercises/tempie/Klok/AbstractView.java

75 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Observable;
+
3
import java.util.Observer;
+
4
+
5
/**
+
6
 *
+
7
 * @author jvermeulen
+
8
 */
+
9
public abstract class AbstractView implements View, Observer {
+
10
+
11
    private Observable mModel;
+
12
    private Controller mController;
+
13
    
+
14
    /**
+
15
     * Empty constructor so that the model and controller can be set later.
+
16
     */
+
17
    public AbstractView() {        
+
18
    }
+
19
    
+
20
    public AbstractView(Observable model, Controller controller) {
+
21
        // Set the model.
+
22
        setModel(model);
+
23
        // If a controller was supplied, use it. Otherwise let the first call to 
+
24
        // getController() create the default controller.
+
25
        if (controller != null) {
+
26
            setController(controller);
+
27
        }
+
28
    }
+
29
    
+
30
    @Override
+
31
    public void setController(Controller controller) {
+
32
        mController = controller;
+
33
        // Tell the controller this object is its view.
+
34
        getController().setView(this);
+
35
    }
+
36
+
37
    @Override
+
38
    public Controller getController() {
+
39
        // If a controller hasn't been defined yet...
+
40
        if (mController == null) {
+
41
            // ...make one. Note that defaultController is normally overriden by 
+
42
            // the AbstractView subclass so that it returns the appropriate 
+
43
            // controller for the view.
+
44
            setController(defaultController(getModel()));
+
45
        }
+
46
        
+
47
        return mController;
+
48
    }
+
49
+
50
    @Override
+
51
    public void setModel(Observable model) {
+
52
        mModel = model;
+
53
    }
+
54
+
55
    @Override
+
56
    public Observable getModel() {
+
57
        return mModel;
+
58
    }
+
59
+
60
    @Override
+
61
    public Controller defaultController(Observable model) {
+
62
        return null;
+
63
    }
+
64
+
65
    /**
+
66
     * A do-nothing implementation of the Observer interface's update method.
+
67
     * Subclasses of AbstractView will provide a concrete implementation for 
+
68
     * this method.
+
69
     */
+
70
    @Override
+
71
    public void update(Observable o, Object arg) {    
+
72
    }
+
73
    
+
74
}
+
75

06/exercises/tempie/Klok/Controller.java

15 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Observable;
+
3
+
4
/**
+
5
 *
+
6
 * @author jvermeulen
+
7
 */
+
8
public interface Controller {
+
9
+
10
    void setView(View view);
+
11
    View getView();
+
12
    void setModel(Observable model);
+
13
    Observable getModel();
+
14
}
+
15

06/exercises/tempie/Klok/View.java

19 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Observable;
+
3
import java.util.Observer;
+
4
+
5
/**
+
6
 *
+
7
 * @author jvermeulen
+
8
 */
+
9
public interface View extends Observer {
+
10
      
+
11
    void setController(Controller controller);
+
12
    Controller getController();
+
13
    
+
14
    void setModel(Observable model);
+
15
    Observable getModel();
+
16
    
+
17
    Controller defaultController(Observable model);
+
18
}
+
19

06/exercises/tempie/Klok/clock/Clock.java

80 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.awt.event.WindowAdapter;
+
3
import java.awt.event.WindowEvent;
+
4
import javax.swing.BoxLayout;
+
5
import javax.swing.JFrame;
+
6
+
7
/**
+
8
 * Main user interface for the application. Makes sure the model, views and
+
9
 * controllers are connected to each other.
+
10
 * @author jvermeulen
+
11
 */
+
12
public class Clock {
+
13
    // The clock data (i.e., the Model).
+
14
    private ClockModel mModel;
+
15
    
+
16
    // A digital view of the clock.
+
17
    private ClockDigitalView mDigitalView;
+
18
    
+
19
    // A toolbar for controlling the clock.
+
20
    private ClockTools mTools;
+
21
    
+
22
    // An analog view of the clock.
+
23
    private final ClockAnalogView mAnalogView;
+
24
    
+
25
    public Clock(int hour, int minute, int second) {
+
26
        // Create the data model.
+
27
        mModel = new ClockModel();
+
28
+
29
        // Create the digital clock view.
+
30
        mDigitalView = new ClockDigitalView(mModel, null);
+
31
        mModel.addObserver(mDigitalView);
+
32
+
33
        // Create the analog clock view.
+
34
        mAnalogView = new ClockAnalogView(mModel, null);
+
35
        mModel.addObserver(mAnalogView);
+
36
        
+
37
        // Create the clock tools view.
+
38
        mTools = new ClockTools(mModel, null);
+
39
        mModel.addObserver(mTools);
+
40
        
+
41
        // Set the time.
+
42
        mModel.setTime(hour, minute, second);
+
43
+
44
        // Start the clock ticking.
+
45
        mModel.start();
+
46
    }
+
47
    
+
48
    public void createGUI() {
+
49
        JFrame frame = new JFrame("Clock");
+
50
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
+
51
        frame.getContentPane().add(mDigitalView.getUI());
+
52
        frame.getContentPane().add(mAnalogView);
+
53
        frame.getContentPane().add(mTools.getUI());                
+
54
        frame.addWindowListener(new WindowAdapter() {
+
55
            @Override
+
56
            public void windowClosing(WindowEvent e) {
+
57
                System.exit(0);
+
58
            }
+
59
        });
+
60
        frame.pack();
+
61
        frame.setVisible(true);
+
62
    }
+
63
    
+
64
    private static void createAndShowGUI() {
+
65
        Clock clock = new Clock(9, 10, 22);
+
66
        clock.createGUI();
+
67
    }
+
68
    
+
69
    public static void main(String[] args) {
+
70
        //Schedule a job for the event-dispatching thread:
+
71
        //creating and showing this application's GUI.
+
72
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
+
73
            @Override
+
74
            public void run() {
+
75
                createAndShowGUI();
+
76
            }
+
77
        });
+
78
    }
+
79
}
+
80

06/exercises/tempie/Klok/clock/ClockAnalogView.java

222 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import be.uhasselt.oo2.mvc.Controller;
+
3
import be.uhasselt.oo2.mvc.View;
+
4
import java.awt.Color;
+
5
import java.awt.Dimension;
+
6
import java.awt.Graphics;
+
7
import java.awt.Graphics2D;
+
8
import java.awt.RenderingHints;
+
9
import java.awt.geom.AffineTransform;
+
10
import java.awt.geom.Ellipse2D;
+
11
import java.awt.geom.GeneralPath;
+
12
import java.util.Calendar;
+
13
import java.util.Observable;
+
14
import javax.swing.JComponent;
+
15
+
16
/**
+
17
 * An analog clock View for the ClockModel. This View has no user inputs, so
+
18
 * no controller is required. This class implements the View interface, and
+
19
 * subclasses JComponent directly, to illustrate that not all Views need to
+
20
 * inherit from AbstractView.
+
21
 *
+
22
 * Note: it is not necessary to understand all the details of the rendering 
+
23
 * of the analog clock.
+
24
 *
+
25
 * @author jvermeulen (inspired by IBM Java2D tutorial)
+
26
 */
+
27
public class ClockAnalogView extends JComponent implements View {
+
28
+
29
    private Observable mModel;
+
30
    private Controller mController;
+
31
+
32
    int mHour;
+
33
    int mMinute;
+
34
    int mSecond;
+
35
+
36
    ClockAnalogView(Observable model, Controller controller) {
+
37
        mModel = model;
+
38
        mController = controller;
+
39
    }
+
40
+
41
    @Override
+
42
    public void setController(Controller controller) {
+
43
        mController = controller;
+
44
    }
+
45
+
46
    @Override
+
47
    public Controller getController() {
+
48
        return mController;
+
49
    }
+
50
+
51
    @Override
+
52
    public void setModel(Observable model) {
+
53
        mModel = model;
+
54
    }
+
55
+
56
    @Override
+
57
    public Observable getModel() {
+
58
        return mModel;
+
59
    }
+
60
+
61
    @Override
+
62
    public Controller defaultController(Observable model) {
+
63
        return null;
+
64
    }
+
65
+
66
    @Override
+
67
    public void update(Observable o, Object info) {
+
68
        // Cast info to ClockUpdate type.
+
69
        ClockUpdate clockInfo = (ClockUpdate) info;
+
70
+
71
        // Store hour, minute, second for repainting.
+
72
        mHour = clockInfo.getHour();
+
73
        mMinute = clockInfo.getMinute();
+
74
        mSecond = clockInfo.getSecond();
+
75
+
76
        // Using a little trigonometry, set the transforms to rotate
+
77
        // each hand into the proper position.  Center the rotation
+
78
        // around the pivot point (50, 50) instead of the origin
+
79
        hourTransform.setToRotation(((double) mHour) *
+
80
                                            (Math.PI / 6.0), 50, 50);
+
81
        minuteTransform.setToRotation(((double) mMinute) *
+
82
                                            (Math.PI / 30.0), 50, 50);
+
83
        secondTransform.setToRotation(((double) mSecond) *
+
84
                                            (Math.PI / 30.0), 50, 50);
+
85
+
86
        repaint();
+
87
    }
+
88
+
89
+
90
    /**
+
91
     * Sets this components preferred size to 150x150.
+
92
     * @return
+
93
     */
+
94
    @Override
+
95
    public Dimension getPreferredSize() {
+
96
        return new Dimension(100, 100);
+
97
    }
+
98
+
99
    // Create a shape for the face of the clock
+
100
    protected static Ellipse2D face = new Ellipse2D.Float(3, 3, 94, 94);
+
101
+
102
    // Create a path that represents a tick mark
+
103
    protected static GeneralPath tick = new GeneralPath();
+
104
    static // http://www.jusfortechies.com/java/core-java/static-blocks.php
+
105
    {
+
106
        tick.moveTo(100, 100);
+
107
        tick.moveTo(49, 0);
+
108
        tick.lineTo(51, 0);
+
109
        tick.lineTo(51, 6);
+
110
        tick.lineTo(49, 6);
+
111
        tick.lineTo(49, 0);
+
112
    }
+
113
+
114
    // Create a cool hour hand
+
115
    protected static GeneralPath hourHand = new GeneralPath();
+
116
    static
+
117
    {
+
118
        hourHand.moveTo(50, 15);
+
119
        hourHand.lineTo(53, 50);
+
120
        hourHand.lineTo(50, 53);
+
121
        hourHand.lineTo(47, 50);
+
122
        hourHand.lineTo(50, 15);
+
123
    }
+
124
+
125
    // Create a cool minute hand
+
126
    protected static GeneralPath minuteHand = new GeneralPath();
+
127
    static
+
128
    {
+
129
        minuteHand.moveTo(50, 2);
+
130
        minuteHand.lineTo(53, 50);
+
131
        minuteHand.lineTo(50, 58);
+
132
        minuteHand.lineTo(47, 50);
+
133
        minuteHand.lineTo(50, 2);
+
134
    }
+
135
+
136
    // And a cool second hand
+
137
    protected static GeneralPath secondHand = new GeneralPath();
+
138
    static
+
139
    {
+
140
        secondHand.moveTo(49, 5);
+
141
        secondHand.lineTo(51, 5);
+
142
        secondHand.lineTo(51, 62);
+
143
        secondHand.lineTo(49, 62);
+
144
        secondHand.lineTo(49, 5);
+
145
    }
+
146
+
147
    // Create some colors for the pieces of the clock
+
148
    protected static Color faceColor = new Color(220, 220, 220);
+
149
    protected static Color hourColor = Color.red.darker();
+
150
    protected static Color minuteColor = Color.blue.darker();
+
151
    protected static Color secondColor = new Color(180, 180, 0);
+
152
    protected static Color pinColor = Color.gray.brighter();
+
153
+
154
    // Create circles for the pivot and center pin
+
155
    protected Ellipse2D pivot = new Ellipse2D.Float(47, 47, 6, 6);
+
156
    protected Ellipse2D centerPin = new Ellipse2D.Float(49, 49, 2, 2);
+
157
+
158
+
159
    // Create three transforms that center around the pivot point
+
160
    protected AffineTransform hourTransform =
+
161
                    AffineTransform.getRotateInstance(0, 50, 50);
+
162
    protected AffineTransform minuteTransform =
+
163
                    AffineTransform.getRotateInstance(0, 50, 50);
+
164
    protected AffineTransform secondTransform =
+
165
                    AffineTransform.getRotateInstance(0, 50, 50);
+
166
+
167
    // Create a timer that fires once a second and a Calendar
+
168
    // instance for getting the time values
+
169
    // protected Timer timer = new Timer(1000, this);
+
170
    protected Calendar calendar = Calendar.getInstance();
+
171
+
172
    // This is an alternative to creating a UI delegate.  Since JPanel's
+
173
    // paint() method only paints the border and backgound, we can just
+
174
    // override the paint method of the component to do the graphics.
+
175
    public void paint(Graphics g)
+
176
    {
+
177
        // Call the superclass first to paint the border (if one is assigned)
+
178
        super.paint(g);
+
179
+
180
+
181
        // Get the graphics context and turn on anti-aliasing
+
182
        Graphics2D g2 = (Graphics2D) g;
+
183
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+
184
                            RenderingHints.VALUE_ANTIALIAS_ON);
+
185
+
186
        // Set the paint for the clock face and fill it in
+
187
        g2.setPaint(faceColor);
+
188
        g2.fill(face);
+
189
+
190
        // Set the paint to black and draw the clock's outline
+
191
        g2.setPaint(Color.black);
+
192
        g2.draw(face);
+
193
+
194
        // Fill in the 12 ticks around the face of the clock
+
195
        for (double p = 0.0; p < 12.0; p += 1.0)
+
196
        {
+
197
            // This is probably terribly inefficient and should be
+
198
            // done statically or in the constructor - draw the
+
199
            // tick as a transformed shape that is rotated.
+
200
            g2.fill(tick.createTransformedShape(
+
201
                AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,
+
202
                        50, 50)));
+
203
        }
+
204
+
205
        // Set the paint and draw the hour hand.  It is lowest in the
+
206
        // 'z-order' so will appear underneath the other hands.  Notice
+
207
        // how each hand is transformed by a different <AffineTransform>.
+
208
        g2.setPaint(hourColor);
+
209
        g2.fill(hourHand.createTransformedShape(hourTransform));
+
210
+
211
        // Set the paint and draw the minute hand, the second hand,
+
212
        // the pivot and the center pin
+
213
        g2.setPaint(minuteColor);
+
214
        g2.fill(minuteHand.createTransformedShape(minuteTransform));
+
215
        g2.setPaint(secondColor);
+
216
        g2.fill(secondHand.createTransformedShape(secondTransform));
+
217
        g2.fill(pivot);
+
218
        g2.setPaint(pinColor);
+
219
        g2.fill(centerPin);
+
220
    }
+
221
}
+
222

06/exercises/tempie/Klok/clock/ClockController.java

26 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import be.uhasselt.oo2.mvc.AbstractController;
+
3
import java.util.Observable;
+
4
+
5
/**
+
6
 * A clock controller that allows the clock to be started, stopped and reset.
+
7
 * @author jvermeulen
+
8
 */
+
9
public class ClockController extends AbstractController {
+
10
    public ClockController(Observable model) {
+
11
        super(model);
+
12
    }
+
13
    
+
14
    public void onStart() {
+
15
        ((ClockModel)getModel()).start();
+
16
    }
+
17
    
+
18
    public void onStop() {
+
19
        ((ClockModel)getModel()).stop();
+
20
    }
+
21
    
+
22
    public void onReset() {
+
23
        ((ClockModel)getModel()).setTime(0,0,0);
+
24
    }
+
25
}
+
26

06/exercises/tempie/Klok/clock/ClockDigitalView.java

74 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import be.uhasselt.oo2.mvc.AbstractView;
+
3
import be.uhasselt.oo2.mvc.Controller;
+
4
import java.awt.Color;
+
5
import java.util.Observable;
+
6
import javax.swing.JComponent;
+
7
import javax.swing.JTextField;
+
8
+
9
/**
+
10
 * A digital clock View for the ClockModel. This View has no user inputs, so no 
+
11
 * Controller is required.
+
12
 * @author jvermeulen
+
13
 */
+
14
public class ClockDigitalView extends AbstractView {
+
15
    
+
16
    // The user interface for this view.
+
17
    private JTextField mClock;
+
18
    
+
19
    public ClockDigitalView(Observable model, Controller controller) {
+
20
        super(model, controller);
+
21
        init();
+
22
    }
+
23
    
+
24
    /**
+
25
     * Initializes the user interface.
+
26
     */
+
27
    private void init() {
+
28
        mClock = new JTextField();
+
29
    }
+
30
        
+
31
    /**
+
32
     * Updates the state of the on-screen digital clock.
+
33
     * Invoked automatically by ClockModel.
+
34
     * @param o The ClockModel object that is broadcasting an update
+
35
     * @param info A ClockUpdate instance describing the changes that have 
+
36
     * occurred in the ClockModel
+
37
     */
+
38
    @Override
+
39
    public void update(Observable o, Object info) {    
+
40
        // Cast info to ClockUpdate type.
+
41
        ClockUpdate clockInfo = (ClockUpdate) info;
+
42
        
+
43
        // Create a String representing the time.        
+
44
        String timeString = formatTime(clockInfo);
+
45
+
46
        // Display the new time in the clock text field.
+
47
        mClock.setText(timeString);
+
48
+
49
        // Fade the color of the display if the clock isn't running.
+
50
        if (clockInfo.isRunning()) {
+
51
          mClock.setBackground(Color.white);
+
52
        } else {
+
53
          mClock.setBackground(Color.gray);
+
54
        }
+
55
    }
+
56
    
+
57
    /**
+
58
     * Convenience method to return the user interface component. We don't need 
+
59
     * this if we implement View directly and directly subclass a GUI component.
+
60
     * @return the JComponent representing this View.
+
61
     */
+
62
    public JComponent getUI() {
+
63
        return mClock;
+
64
    }
+
65
    
+
66
    private String formatTime(ClockUpdate info) {
+
67
        return String.format("%d:%d:%d", 
+
68
                             info.getHour(), 
+
69
                             info.getMinute(), 
+
70
                             info.getSecond());
+
71
    }        
+
72
    
+
73
}
+
74

06/exercises/tempie/Klok/clock/ClockModel.java

152 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Calendar;
+
3
import java.util.Date;
+
4
import java.util.GregorianCalendar;
+
5
import java.util.Observable;
+
6
import java.util.Timer;
+
7
import java.util.TimerTask;
+
8
import javax.swing.SwingUtilities;
+
9
+
10
/**
+
11
 *
+
12
 * @author jvermeulen
+
13
 */
+
14
public class ClockModel extends Observable {
+
15
    private int mHour;
+
16
    private int mMinute;
+
17
    private int mSecond;
+
18
    
+
19
    private final long mTickInterval = 1000; // tick every second
+
20
    private Timer mTimer;
+
21
    private boolean mIsRunning;
+
22
    
+
23
    public ClockModel() {
+
24
        // By default, set the clock time to the current system time.
+
25
        Calendar now = new GregorianCalendar();        
+
26
        setTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), 
+
27
                now.get(Calendar.SECOND));        
+
28
    }
+
29
    
+
30
    /**
+
31
     * Helper class to run the tick() method multiple times 
+
32
     * (e.g., every 1 second).
+
33
     */
+
34
    class TickTask extends TimerTask {
+
35
        public void run() {
+
36
            tick();
+
37
        }
+
38
    }
+
39
    
+
40
    public void start() {
+
41
        if (!mIsRunning) {
+
42
            mIsRunning = true;
+
43
+
44
            // Schedule the tick() method to run every mTickInterval milliseconds.
+
45
            mTimer = new Timer();
+
46
            mTimer.schedule(new TickTask(), 0, mTickInterval); 
+
47
            
+
48
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
49
                                               mIsRunning);
+
50
            setChanged();
+
51
            notifyObservers(info);
+
52
        }
+
53
    }
+
54
    
+
55
    public void stop() {
+
56
        if (mIsRunning) {
+
57
            mIsRunning = false;
+
58
            
+
59
            // Stop the timer.
+
60
            mTimer.cancel();
+
61
            mTimer = null;
+
62
            
+
63
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
64
                                               mIsRunning);
+
65
            setChanged();
+
66
            notifyObservers(info);
+
67
        }
+
68
    }
+
69
    
+
70
    /**
+
71
     * Sets the current time. Notifies observers of any change in time.
+
72
     * @param hour The new hour.
+
73
     * @param minute The new minute
+
74
     * @param second The new second
+
75
     */
+
76
    public void setTime(int hour, int minute, int second) {
+
77
        if (hour != mHour && isValidHour(hour)) {
+
78
            mHour = hour;
+
79
            setChanged();
+
80
        }
+
81
        
+
82
        if (minute != mMinute && isValidMinute(minute)) {
+
83
            mMinute = minute;
+
84
            setChanged();
+
85
        }
+
86
                
+
87
        if (second != mSecond && isValidSecond(second)) {
+
88
            mSecond = second;
+
89
            setChanged();
+
90
        }
+
91
        
+
92
        // If the model has changed, notify Views.
+
93
        if (hasChanged()) {
+
94
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
95
                                               mIsRunning);
+
96
            notifyObservers(info);
+
97
        }
+
98
    }    
+
99
    
+
100
    /**
+
101
     * Checks to see if a number is a valid hour (i.e. in [0,23]).
+
102
     * @param hour The hour to check
+
103
     * @return True if this is a valid hour
+
104
     */
+
105
    private boolean isValidHour(int hour) {
+
106
        return (hour >= 0 && hour <= 23);
+
107
    }
+
108
    
+
109
    /**
+
110
     * Checks to see if a number is a valid minute (i.e. in [0,59]).
+
111
     * @param minute The minute to check
+
112
     * @return True if this is a valid minute
+
113
     */
+
114
    private boolean isValidMinute(int minute) {
+
115
        return (minute >= 0 && minute <= 59);
+
116
    }
+
117
    
+
118
    /**
+
119
     * Checks to see if a number is a valid minute (i.e. in [0,59]).
+
120
     * @param second The second to check
+
121
     * @return True if this is a valid second
+
122
     */
+
123
    private boolean isValidSecond(int second) {
+
124
        return (second >= 0 && second <= 59);
+
125
    }
+
126
    
+
127
    private void tick() {
+
128
        // Get the current time
+
129
        int h = mHour;
+
130
        int m = mMinute;
+
131
        int s = mSecond;
+
132
        
+
133
        // Increment the current second, adjusting the minute and hour 
+
134
        // if necessary.
+
135
        s += 1;
+
136
        if (s > 59) {
+
137
            s = 0;
+
138
            m += 1;
+
139
            if (m > 59) {
+
140
                m = 0;
+
141
                h += 1;
+
142
                if (h > 23) {
+
143
                    h = 0;
+
144
                }
+
145
            }
+
146
        }
+
147
+
148
        // Set the new time.
+
149
        setTime(h, m, s);
+
150
    }
+
151
}
+
152

06/exercises/tempie/Klok/clock/ClockTools.java

102 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import be.uhasselt.oo2.mvc.AbstractView;
+
3
import be.uhasselt.oo2.mvc.Controller;
+
4
import java.awt.GridLayout;
+
5
import java.awt.event.ActionEvent;
+
6
import java.awt.event.ActionListener;
+
7
import java.util.Observable;
+
8
import javax.swing.JButton;
+
9
import javax.swing.JComponent;
+
10
import javax.swing.JPanel;
+
11
+
12
/**
+
13
 *
+
14
 * @author jvermeulen
+
15
 */
+
16
public class ClockTools extends AbstractView {
+
17
    // The user interface for this view.
+
18
    private JPanel mTools;
+
19
    private JButton mStart;
+
20
    private JButton mStop;
+
21
    private JButton mReset;
+
22
    
+
23
    public ClockTools(Observable model, ClockController controller) {
+
24
        super(model, controller);
+
25
        init();
+
26
    }
+
27
+
28
    private void init() {
+
29
        mTools = new JPanel();
+
30
        mTools.setLayout(new GridLayout(1, 3));
+
31
        mStart = new JButton("Start");
+
32
        mStart.setEnabled(false);
+
33
        mStop = new JButton("Stop");
+
34
        mStop.setEnabled(false);
+
35
        mReset = new JButton("Reset");
+
36
        
+
37
        // handle events: pass to controller
+
38
        mStart.addActionListener(new ActionListener() {
+
39
+
40
            @Override
+
41
            public void actionPerformed(ActionEvent e) {
+
42
                ((ClockController)getController()).onStart();
+
43
            }
+
44
        });
+
45
        
+
46
        mStop.addActionListener(new ActionListener() {
+
47
+
48
            @Override
+
49
            public void actionPerformed(ActionEvent e) {
+
50
                ((ClockController)getController()).onStop();
+
51
            }
+
52
        });
+
53
                
+
54
        mReset.addActionListener(new ActionListener() {
+
55
+
56
            @Override
+
57
            public void actionPerformed(ActionEvent e) {
+
58
                ((ClockController)getController()).onReset();
+
59
            }
+
60
        });
+
61
        
+
62
        mTools.add(mStart);
+
63
        mTools.add(mStop);
+
64
        mTools.add(mReset);       
+
65
    }
+
66
    
+
67
        /**
+
68
     * Updates the state of the clock tools.
+
69
     * Invoked automatically by ClockModel.
+
70
     * @param o The ClockModel object that is broadcasting an update
+
71
     * @param info A ClockUpdate instance describing the changes that have 
+
72
     * occurred in the ClockModel
+
73
     */
+
74
    @Override
+
75
    public void update(Observable o, Object info) {    
+
76
        // Cast info to ClockUpdate type.
+
77
        ClockUpdate clockInfo = (ClockUpdate) info;
+
78
        
+
79
        if (clockInfo.isRunning()) {
+
80
            mStop.setEnabled(true);
+
81
            mStart.setEnabled(false);
+
82
        } else {
+
83
            mStart.setEnabled(true);
+
84
            mStop.setEnabled(false);
+
85
        }
+
86
    }
+
87
    
+
88
    @Override
+
89
    public Controller defaultController(Observable model) {
+
90
        return new ClockController(model);        
+
91
    }
+
92
    
+
93
    /**
+
94
     * Convenience method to return the user interface component. We don't need 
+
95
     * this if we implement View directly and directly subclass a GUI component.
+
96
     * @return 
+
97
     */
+
98
    public JComponent getUI() {
+
99
        return mTools;
+
100
    }
+
101
}
+
102

06/exercises/tempie/Klok/clock/ClockUpdate.java

77 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 * Represent a clock update including all necessary information.
+
4
 * @author jvermeulen
+
5
 */
+
6
public class ClockUpdate {
+
7
    private int mHour;
+
8
    private int mMinute;
+
9
    private int mSecond;
+
10
    private boolean mIsRunning; 
+
11
    
+
12
    
+
13
    ClockUpdate(int hour, int minute, int second, boolean isRunning) {
+
14
        mHour = hour;
+
15
        mMinute = minute;
+
16
        mSecond = second;
+
17
        mIsRunning = isRunning;
+
18
    }
+
19
+
20
    /**
+
21
     * @return the mHour
+
22
     */
+
23
    public int getHour() {
+
24
        return mHour;
+
25
    }
+
26
+
27
    /**
+
28
     * @param hour the hour to set
+
29
     */
+
30
    public void setHour(int hour) {
+
31
        this.mHour = hour;
+
32
    }
+
33
+
34
    /**
+
35
     * @return the mMinute
+
36
     */
+
37
    public int getMinute() {
+
38
        return mMinute;
+
39
    }
+
40
+
41
    /**
+
42
     * @param minute the minute to set
+
43
     */
+
44
    public void setMinute(int minute) {
+
45
        this.mMinute = minute;
+
46
    }
+
47
+
48
    /**
+
49
     * @return the mSecond
+
50
     */
+
51
    public int getSecond() {
+
52
        return mSecond;
+
53
    }
+
54
+
55
    /**
+
56
     * @param second the second to set
+
57
     */
+
58
    public void setSecond(int second) {
+
59
        this.mSecond = second;
+
60
    }
+
61
+
62
    /**
+
63
     * @return the mIsRunning
+
64
     */
+
65
    public boolean isRunning() {
+
66
        return mIsRunning;
+
67
    }
+
68
+
69
    /**
+
70
     * @param isRunning the isRunning to set
+
71
     */
+
72
    public void setRunning(boolean isRunning) {
+
73
        this.mIsRunning = isRunning;
+
74
    }
+
75
    
+
76
}
+
77

06/exercises/tempie/calculator/Controller.java

25 additions and 0 deletions.

View changes Hide changes
+
1
+
2
abstract class Controller {
+
3
		private View m_view;
+
4
		public View view() {
+
5
				if(m_view == null) // If null, throw exception:
+
6
						throw new NullPointerException("No view was given.");
+
7
				else
+
8
						return m_view;
+
9
		}
+
10
		public void setView(View view) {
+
11
				m_view = view;
+
12
		}
+
13
+
14
		private Observable m_model;
+
15
		public Observable model() {
+
16
				if(m_model == null) // If null, throw exception:
+
17
						throw new NullPointerException("No model was given.");
+
18
				else
+
19
						return m_model;
+
20
		}
+
21
		public void setModel(Observable model) {
+
22
				m_model = model;
+
23
		}
+
24
}		
+
25

06/exercises/tempie/calculator/View.java

54 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Observable; // Required to have a private member of type Observable.
+
2
//import java.lang.NullPointerException; // Required to throw said exception.
+
3
+
4
//class UpdateNotImplementedException extends Exception {}
+
5
+
6
// NOTE: This is a medium between the java core stuff and the implementation of the view of this program. It handles the boilerplate that is the same in all other view implementations.
+
7
+
8
public abstract class View implements Observer { // This is an abstract class. See note above for further information.
+
9
		// m_model - Member: The model that is being observed. This class must inherit from Observable.
+
10
		private Observable m_model;
+
11
		public Observable model() {
+
12
				if(m_model == null) // The model must be specified. If not, initiate core meltdown:
+
13
						throw new NullPointerException("No model was specified.");
+
14
				else
+
15
						return m_model;
+
16
		}
+
17
		public void setModel(Observable model) {
+
18
				m_model = model;
+
19
		}
+
20
+
21
		// m_controller - Member: Represents the controlling class. This class must inherit from Controller.
+
22
		private Controller m_controller;
+
23
		public Controller controller() {
+
24
				if(m_controller == null) // The programmer has refused to use a controller. Thee shall be punished with ye exception.
+
25
						throw new NullPointerException("No controller was specified.");
+
26
				else
+
27
						return m_controller;
+
28
		}
+
29
		public void setController(Controller controller) {
+
30
				m_controller = controller;
+
31
		}
+
32
+
33
		// INFO: The given empty constructor is there because one sometimes can't give all required values up front. Abuse of this function will not be tolerated (See getters in this interface). Use with caution!
+
34
		/*public viewTemplate() {
+
35
			System.out.println("Empty declaration of viewTemplate."); // Printing a notification to the console. It makes debugging easier.
+
36
		}*/
+
37
		// Commented out the empty constructor, since it can be easily integrated in the default constructor without any hassle.
+
38
		public View(Observable model, Controller controller) { // The default constructor.
+
39
				// Generating notifications if no model and/or controller are given yet:
+
40
				if(model == null)
+
41
						System.out.println("In viewTemplate default constructor: model was a null pointer.");
+
42
				if(controller == null)
+
43
						System.out.println("In viewTemplate default constructor: controller was a null pointer.");
+
44
+
45
				setModel(model);
+
46
				setController(controller);
+
47
		}
+
48
+
49
		// NOTE: Implementing Observer requires one to implement update(). The reason I do that here, is because my compiler will otherwise commit suicide. But, this will only print a notification that the given update has had no effect whatsoever on the class (This is an abstract class after all).
+
50
		public void update(Observable observable, Object args) {
+
51
				System.out.println("update() was called, but no implementation was found.");
+
52
		}
+
53
}
+
54

06/exercises/tempie/calculator/actionButton.java

61 additions and 0 deletions.

View changes Hide changes
+
1
import java.lang.Math;
+
2
+
3
enum Action {
+
4
		ADDITION, DIFFERENCE, MULTIPLICATION, DIVISION, NEGATION, ABSOLUTE_VALUE, SINUS, COSINUS, TANGENS
+
5
}
+
6
+
7
class actionButton extends JButton { // Makes a button that, when pressed, does a given action.
+
8
		private Action m_action;
+
9
		public Action action() {
+
10
				return m_action;
+
11
		}
+
12
		public void setAction(Action action) {
+
13
				m_action = action;
+
14
				actionUpdated(); // The action has been updated, update the symbol too.
+
15
		}
+
16
+
17
		private void actionUpdated() { // Fired when the action has been altered, so the presented symbol can be updated accordingly.
+
18
				switch(action()) {
+
19
						case ADDITION:
+
20
								setText("+");
+
21
								break;
+
22
						case DIFFERENCE:
+
23
								setText("-");
+
24
								break;
+
25
						case MULTIPLICATION:
+
26
								setText("x");
+
27
								break;
+
28
						case DIVISION:
+
29
								setText("/");
+
30
								break;
+
31
						case NEGATION:
+
32
								setText("neg");
+
33
								break;
+
34
						case ABSOLUTE_VALUE:
+
35
								setText("abs");
+
36
								break;
+
37
						case SINUS:
+
38
								setText("sin");
+
39
								break;
+
40
						case COSINUS:
+
41
								setText("cos");
+
42
								break;
+
43
						case TANGENS:
+
44
								setText("tan");
+
45
								break;
+
46
				}
+
47
		}
+
48
+
49
/*		public actionButton(Action newAction, boolean enabled = true) {
+
50
				setAction(newAction); // Set the given action to this button.
+
51
				setEnabled(enabled); // Set the state of the button.
+
52
				// handle events: pass to controller
+
53
		        addActionListener(new ActionListener() {
+
54
	            		@Override
+
55
			            public void actionPerformed(ActionEvent e) {
+
56
				                ((ClockController)getController()).onStart();
+
57
	            		}
+
58
        		});
+
59
		}*/
+
60
}
+
61

06/exercises/tempie/calculator/button.java

11 additions and 0 deletions.

View changes Hide changes
+
1
+
2
// Class representing a button on a calculator that adds a number. Its value is passed during instantiation.
+
3
class NumberButton extends JButton {
+
4
		private int m_value; // The numeric value this button represents.
+
5
+
6
		public NumberButton(int number) {
+
7
				setText(Integer.toString(number));
+
8
				m_value = number;
+
9
		}
+
10
}
+
11

06/exercises/tempie/calculator/calculator.java

21 additions and 0 deletions.

View changes Hide changes
+
1
+
2
class calculator {
+
3
		public calculator() {
+
4
				JPanel panel = new JPanel();
+
5
				createGUI();
+
6
		}
+
7
+
8
		public void createGUI() {
+
9
				JFrame window = new JFrame("Rekenmachine");
+
10
+
11
				for(int i = 0; i<10; i++) {
+
12
						JButton button = new NumberButton(i);
+
13
						window.add(button);
+
14
				}
+
15
				window.setVisible(true);
+
16
		}
+
17
+
18
		public static void main(String[] args) {
+
19
		}
+
20
}
+
21

06/opdracht1.java

38 additions and 0 deletions.

View changes Hide changes
+
1
 Copyright 2015 Maarten Vangeneugden
+
2
+
3
	Licensed under the Apache License, Version 2.0 (the "License");
+
4
	you may not use this file except in compliance with the License.
+
5
	You may obtain a copy of the License at
+
6
+
7
	https://www.apache.org/licenses/LICENSE-2.0
+
8
+
9
	Unless required by applicable law or agreed to in writing, software
+
10
	distributed under the License is distributed on an "AS IS" BASIS,
+
11
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+
12
	See the License for the specific language governing permissions and
+
13
	limitations under the License.*/
+
14
+
15
class opdracht1 {
+
16
		/**
+
17
		 * Generic function that will swap two values, based on the given indexes. After this, it prints the entire value, in the new order.
+
18
		 * @param array The array in which the swap needs to take place.
+
19
		 * @param i The index of the value that will be swapped with the value in j.
+
20
		 * @param j The index of the value that will be swapped with the value in i.
+
21
		 * @pre The array is a list of types that needs 2 objects to be swapped with each other.
+
22
		 * @post The array is equal to the given array, except for the values at the given indexes, which have been swapped.
+
23
		 * @exception IndexOutOfBoundsException Thrown when one or more given indexes do not appear in the given array.
+
24
		 * @exception NullPointerException The given array does not exist.
+
25
		 * @return No actual return value, but this function will print the new order of the array to the console.
+
26
		 */
+
27
		public static <T> void genericArraySwap(T[] array, int i, int j) {
+
28
				T tempValue = array[i]; // A temporary value is required to swap with another value.
+
29
				array[i] = array[j]; // Replacing i by j.
+
30
				array[j] = tempValue; // Replacing j by i
+
31
+
32
				for(T value : array) { // Looping through all values of the array.
+
33
						System.out.print(value + ", "); // Printing a value.
+
34
				}
+
35
				System.out.println(); // Simulating a RETURN.
+
36
		}
+
37
}
+
38

06/opdracht3.java

62 additions and 0 deletions.

View changes Hide changes
+
1
 Copyright 2015 Maarten Vangeneugden
+
2
+
3
	Licensed under the Apache License, Version 2.0 (the "License");
+
4
	you may not use this file except in compliance with the License.
+
5
	You may obtain a copy of the License at
+
6
+
7
	https://www.apache.org/licenses/LICENSE-2.0
+
8
+
9
	Unless required by applicable law or agreed to in writing, software
+
10
	distributed under the License is distributed on an "AS IS" BASIS,
+
11
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+
12
	See the License for the specific language governing permissions and
+
13
	limitations under the License.*/
+
14
+
15
import java.util.ArrayList;
+
16
import java.util.Random;
+
17
+
18
class ItHurtsWhenIP {
+
19
		/** Sorts a given ArrayList using quicksort.
+
20
		 * @param array An ArrayList of items to be sorted.
+
21
		 * @return The same ArrayList, but sorted.
+
22
		 * @pre The given list is of type ArrayList. The items in the list implement the class Comparable.
+
23
		 * @post The items in the list are sorted according to their implementation of Comparable.
+
24
		 * @exception NullPointerException Thrown when the given array is not there.
+
25
		 */
+
26
+
27
		public static <T extends Comparable> ArrayList<T> quickSort(ArrayList<T> array) {
+
28
				int index = new Random().nextInt(array.size()); // A random pivot between 0 and the size of the array.
+
29
				T pivot = array.get(index); // Taking the pivot element.
+
30
+
31
				if(array.size() == 0 || array.size() == 1) { // If the size of the array is too small to sort, return it as is.
+
32
						return array;
+
33
				}
+
34
				
+
35
				for (T value : array) { // Else, loop trough the elements, and sort them according to the pivot.
+
36
					if(value.compareTo(pivot) < 0) { // If the value is smaller than the pivot:
+
37
							if(array.indexOf(value) > index) { //If the value comes after the pivot, replace:
+
38
									array.set(array.indexOf(value), pivot);
+
39
									array.set(index, value);
+
40
							}
+
41
					}
+
42
					else { // The value is greater than the pivot:
+
43
							if(array.indexOf(value) < index) { // If the value comes before the pivot, replace:
+
44
									array.set(array.indexOf(value), pivot);
+
45
									array.set(index, value);
+
46
							}
+
47
					}
+
48
				}
+
49
+
50
				if(array.size() == 2 || array.size() == 3) // If the array is only 2 or 3 long, do not go recursive. instead, return it.
+
51
						return array;
+
52
				else { // If the array needs additional quicksorting, do so:
+
53
						ArrayList<T> lowerPart = quickSort((ArrayList)array.subList(0, index)); // Sorting the lower list.
+
54
						lowerPart.add(pivot); // Adding the pivot to the sorted lower list.
+
55
						ArrayList<T> upperPart = quickSort((ArrayList)array.subList(index, array.size())); // Sorting the upper list.
+
56
						lowerPart.addAll(upperPart); // Adding the upper sorted list to the lower sorted list.
+
57
+
58
						return lowerPart; // Returning it.
+
59
				}
+
60
		}
+
61
}
+
62

06/test.java

22 additions and 0 deletions.

View changes Hide changes
+
1
 Copyright 2015 Maarten Vangeneugden
+
2
+
3
	Licensed under the Apache License, Version 2.0 (the "License");
+
4
	you may not use this file except in compliance with the License.
+
5
	You may obtain a copy of the License at
+
6
+
7
	https://www.apache.org/licenses/LICENSE-2.0
+
8
+
9
	Unless required by applicable law or agreed to in writing, software
+
10
	distributed under the License is distributed on an "AS IS" BASIS,
+
11
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+
12
	See the License for the specific language governing permissions and
+
13
	limitations under the License.*/
+
14
+
15
class test {
+
16
		public static void main(String[] args) {
+
17
				opdracht1.genericArraySwap(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, 2, 8);
+
18
				opdracht1.genericArraySwap(new String[] {"Is", "deze", "opdracht", "gelukt?"}, 1, 3);
+
19
		}
+
20
+
21
}
+
22

Challenge 1/ChoiceCourse.java

10 additions and 0 deletions.

View changes Hide changes
+
1
 * A course that is not mandatory, therefore offered as a choice to the student.
+
2
 * Note that this is an "empty" class, since its implementation is completely given by Course. yet again, Course does not explicitely states that it is a choice course. Plus, using ChoiceCourse adds to the open/closed principle.
+
3
 * @author Maarten Vangeneugden - 1438256
+
4
 */
+
5
class ChoiceCourse extends Course {
+
6
		public ChoiceCourse(String name, int studyPoints, int semester, int ID, boolean pass) {
+
7
				super(name, studyPoints, semester, ID, pass);
+
8
		}
+
9
}
+
10

Challenge 1/Course.java

63 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 * Course: Represents a course that is a part of a study.
+
4
 * @author Maarten Vangeneugden - 1438256
+
5
 */
+
6
class Course {
+
7
		private String m_name; // The name of the course.
+
8
		public String name() {
+
9
				return m_name;
+
10
		}
+
11
		public void setName(String name) {
+
12
				m_name = name;
+
13
		}
+
14
+
15
		private int m_studyPoints; // An integer value representing how much study points this course has.
+
16
		public int studyPoints() {
+
17
				return m_studyPoints;
+
18
		}
+
19
		public void setStudyPoints(int studyPoints) {
+
20
				m_studyPoints = studyPoints;
+
21
		}
+
22
+
23
		private int m_semester; // Integer that represents in which semester this course takes place.
+
24
		public int semester() {
+
25
				return m_semester;
+
26
		}
+
27
		public void setSemester(int semester) {
+
28
				m_semester = semester;
+
29
		}
+
30
		
+
31
		private int m_ID; // Integer value representing the unique ID of this course.
+
32
		public int ID() {
+
33
				return m_ID;
+
34
		}
+
35
		public void setID(int ID) {
+
36
				m_ID = ID;
+
37
		}
+
38
+
39
		private List<Course> m_requiredCourses; // A list of courses that are required to follow this course.
+
40
		public List<Course> requiredCourses() {
+
41
				return m_requiredCourses;
+
42
		}
+
43
		public void setRequiredCourses(List<Course> requiredCourses) {
+
44
				m_requiredCourses = requiredCourses;
+
45
		}
+
46
+
47
		private boolean m_passed; // A boolean value that holds whether this course has been passed, indicating a credit for it has been acquired by the student.
+
48
		public boolean creditAcquired() {
+
49
				return m_passed;
+
50
		}
+
51
		public void setPassed(boolean pass) {
+
52
				m_passed = pass;
+
53
		}
+
54
		
+
55
		public Course(String name, int studyPoints, int semester, int ID, boolean pass) {
+
56
				setName(name);
+
57
				setStudyPoints(studyPoints);
+
58
				setSemester(semester);
+
59
				setID(ID);
+
60
				setPassed(pass);
+
61
		}
+
62
}
+
63

Challenge 1/DetailedCourseView.java

69 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 * This class creates a JPanel using the given course. This JPanel is then a detailed description of said course.
+
4
 * @author Maarten Vangeneugden - 1438256
+
5
 */
+
6
+
7
class DetailedCourseView {
+
8
		private Course m_course; // The course of this class.
+
9
		public Course course() {
+
10
				return m_course;
+
11
		}
+
12
		public void setCourse(Course course) { // Sets the course, but also triggers to update the panel itself. This is necessary, since the panel will otherwise not be a representation of the course itself.
+
13
				m_course = course;
+
14
				setContent();
+
15
		}
+
16
		
+
17
		private JPanel m_panel; // The panel that will represent the course.
+
18
		public JPanel panel() {
+
19
				return m_panel;
+
20
		}
+
21
		private void setPanel(JPanel panel) {
+
22
				m_panel = panel;
+
23
		}
+
24
		//There is no public panel setter. The panel can only be changed by modifying the course, since this panel represents the course.
+
25
		
+
26
		public DetailedCourseView(Course course) {
+
27
				setCourse(course);
+
28
		}
+
29
+
30
		/**
+
31
		 * Will update the panel of the class according to the given course.
+
32
		 * @pre The course of the class is not null.
+
33
		 * @post The panel of the class is updated and is now an update representation of the given course.
+
34
		 */
+
35
		private void setContent() { // Creates the content for this JPanel. Not that it's private; Its functionality may only be triggered by its own instance.
+
36
				// First, we handle the common members, since we know the base class can't be smaller than inherited classes.
+
37
				JLabel lblName = new JLabel("Naam: " + course().name());
+
38
				JLabel lblID = new JLabel("ID: " + Integer.toString(course().ID()));
+
39
				JLabel lblStudyPoints = new JLabel("Studiepunten: " + Integer.toString(course().studyPoints()));
+
40
				JLabel lblSemester = new JLabel("Semester: " + Integer.toString(course().semester()));
+
41
				JLabel lblType = new JLabel(); // We wil fill this in later.
+
42
+
43
				JLabel lblPass = new JLabel();
+
44
				if(course().creditAcquired())
+
45
						lblPass.setText("Verworven credit");
+
46
				else
+
47
						lblPass.setText("Credit niet verworven");
+
48
				
+
49
				// Second, we do a RTTI on the given course by checking the instance type. (RTTI = RunTime Type Identification)
+
50
				if(course() instanceof MandatoryCourse) {
+
51
						lblType.setText("Verplicht vak - Jaar" + Integer.toString(((MandatoryCourse)course()).year()));
+
52
				}
+
53
				else if(course() instanceof ChoiceCourse) {
+
54
						lblType.setText("Keuzevak");
+
55
				}
+
56
+
57
				// Third, we add all the labels to the panel.
+
58
				JPanel panel = new JPanel();
+
59
				panel.add(lblName);
+
60
				panel.add(lblID);
+
61
				panel.add(lblStudyPoints);
+
62
				panel.add(lblSemester);
+
63
				panel.add(lblType);
+
64
				panel.add(lblPass);
+
65
+
66
				setPanel(panel);
+
67
		}
+
68
}
+
69

Challenge 1/MainWindow.java

32 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 * MainWindow represents the main window of this program. It serves as a container for the other panels.
+
4
 * @author Maarten Vangeneugden - 1438256
+
5
 */
+
6
+
7
class MainWindow {
+
8
		private JFrame m_frame;
+
9
		public JFrame frame() {
+
10
				return m_frame;
+
11
		}
+
12
		public void setFrame(JFrame frame) {
+
13
				m_frame = frame;
+
14
		}
+
15
		
+
16
		public MainWindow() {
+
17
				setFrame(new JFrame());
+
18
				frame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
19
				frame().pack();
+
20
				frame().setVisible(true);
+
21
		}
+
22
		public MainWindow(String title, JPanel contentPanel) {
+
23
				setFrame(new JFrame(title)); // Giving the frame a name.
+
24
				frame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // If the user closes the window, trigger the closing operation.
+
25
+
26
				frame().setContentPane(contentPanel);
+
27
+
28
				frame().pack();
+
29
				frame().setVisible(true);
+
30
				}
+
31
		}
+
32

Challenge 1/MandatoryCourse.java

18 additions and 0 deletions.

View changes Hide changes
+
1
 * A mandatory course, representing a course that must be followed during the study.
+
2
 * @author Maarten Vangeneugden - 1438256
+
3
 */
+
4
class MandatoryCourse extends Course {
+
5
		private int m_year; // The year in which this course is given.
+
6
		public int year() {
+
7
				return m_year;
+
8
		}
+
9
		public void setYear(int year) {
+
10
				m_year = year;
+
11
		}
+
12
+
13
		public MandatoryCourse(String name, int studyPoints, int semester, int ID, boolean pass, int year) {
+
14
				super(name, studyPoints, semester, ID, pass);
+
15
				setYear(year);
+
16
		}
+
17
}
+
18

Challenge 1/Planner.java

11 additions and 0 deletions.

View changes Hide changes
+
1
 * This class will call the appropriate windows to use the program.
+
2
 * @author Maarten Vangeneugden - 1438256
+
3
 */
+
4
class Planner {
+
5
		public static void main(String[] args) {
+
6
				Course course = new MandatoryCourse("Informatiesystemen", 8, 1, 6845, true, 2);
+
7
				DetailedCourseView view = new DetailedCourseView(course);
+
8
				MainWindow window = new MainWindow("Ding", view.panel());
+
9
		}
+
10
}
+
11

Challenge 1/StudyPath.java

57 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 * StudyPath describes a study path that a student wants to follow. it offers algorithms for suggesting paths and for checking the validity of the studypath.
+
4
 * @author Maarten Vangeneugden - 1438256
+
5
 */
+
6
class StudyPath {
+
7
		private Study m_study; // The study the student wants to follow.
+
8
		public Study study() {
+
9
				return m_study;
+
10
		}
+
11
		public void setStudy(Study study) {
+
12
				m_study = study;
+
13
		}
+
14
+
15
		private List<Course> m_courses; // A list containing all courses that can be chosen in this study.
+
16
		public List<Course> courses() {
+
17
				return m_courses;
+
18
		}
+
19
		public void setCourses(List<Course> courses) {
+
20
				m_courses = courses;
+
21
		}
+
22
+
23
		/**
+
24
		 * Checks if the given study path is valid.
+
25
		 * @return A boolean value. False if the given study is not valid, true if the study path can be done.
+
26
		 * @pre The list of courses and the study are complete.
+
27
		 * @post Nothing's changed.
+
28
		 */
+
29
		public boolean isValid() { // Checks whether the given study path is valid.
+
30
				int studyPoints = 0;
+
31
				for(Course course : courses()) {
+
32
						studyPoints += course.studyPoints();
+
33
						if(course.requiredCourses().size() != 0) {
+
34
								for(Course requiredCourse : course.requiredCourses()) {
+
35
										if(course.indexOf(requiredCourse) == -1) { // If the required course is not in the study path:
+
36
												return false;
+
37
										}
+
38
										else if(requiredCourse.creditAcquired() == false) {
+
39
												return false;
+
40
										}
+
41
								}
+
42
						}
+
43
						}
+
44
				if(!courses().containsAll(study().mandatoryCourses())) { // If the path does not contain all required courses:
+
45
						return false;
+
46
				}
+
47
				if(studyPoints<60 || studyPoints > 66) {
+
48
						return false;
+
49
				}
+
50
+
51
				return true;
+
52
		}
+
53
}
+
54
+
55
}
+
56
+
57

Challenge 1/ontwerpkeuzes.txt

12 additions and 0 deletions.

View changes Hide changes
+
1
+
2
Er werd gevraagd om contractueel te programmeren. Dit houdt in dat pre- en postcondities worden gegeven, parameters beschreven, ... Ik heb deze bij de get- en setfuncties desondanks achterwege gelaten, omdat het contract bij deze functies te triviaal is, de tijd beperkt was, en omdat ik verwacht dat andere programmeurs zodanig veel verstand hebben dat ze het contract kunnen afleiden uit de context.
+
3
+
4
Voor de opleidingsonderdelen heb ik besloten om een abstracte basisklasse te maken (Course) waarvan de verplichte vakken (MandatoryCourse) en keuzevakken (ChoiceCourse) zijn afgeleid. Ik tracht hiermee tegemoet te komen aan het open/closed-principe, zodat de code uitbreidbaar en overzichtelijk blijft.
+
5
+
6
Ik heb slechts een GUI-implementatie gemaakt van een gedetailleerde weergave van een opleidingsonderdeel (DetailedCourseView), en een class dat als framework dient voor alle schermen van dit programma (MainWindow). Ik heb besloten om (wegens tijdnood) het Model zo veel mogelijk te implementeren, en een minimale basis voor de View te geven.
+
7
+
8
Planner dient als een aanroepklasse.
+
9
+
10
De code zelf is volledig in het Engels. De GUI en (de inhoud van) opleidingsvakken zijn Nederlands.
+
11
+
12

Challenge 1/opleidingsonderdelen.txt

69 additions and 0 deletions.

View changes Hide changes
+
1
ID = 2166
+
2
SP = 6
+
3
Type = verplicht
+
4
Jaar = 1
+
5
Semester = 1
+
6
Vereiste credit = 0660 (Besturingssystemen), 1816 (Computernetwerken)
+
7
+
8
Compilers
+
9
ID = 2167
+
10
SP = 6
+
11
Type = verplicht
+
12
Jaar = 1
+
13
Semester = 1
+
14
Vereiste credit =
+
15
+
16
Juridische aspecten van informatica
+
17
ID = 1282
+
18
SP = 3
+
19
Type = verplicht
+
20
Jaar = 1
+
21
Semester = 2
+
22
Vereiste credit =
+
23
+
24
Projectmanagement
+
25
ID = 2208
+
26
SP = 3
+
27
Type = verplicht
+
28
Jaar = 1
+
29
Semester = 2
+
30
Vereiste credit =
+
31
+
32
Masterproef
+
33
ID = 2262
+
34
SP = 30
+
35
Type = verplicht
+
36
Jaar = 2
+
37
Semester = 2
+
38
Vereiste credit = 2207 (Bachelorproef)
+
39
+
40
Multimediatechnologie
+
41
ID = 2168
+
42
SP = 6
+
43
Semester = 1
+
44
Vereiste credit =
+
45
+
46
Beeldverwerking
+
47
ID = 2169
+
48
SP = 6
+
49
Semester = 2
+
50
Vereiste credit =
+
51
+
52
Audioverwerking
+
53
ID = 2171
+
54
SP = 6
+
55
Semester = 1
+
56
Vereiste credit = 2168, 2169
+
57
+
58
Security en computernetwerken
+
59
ID = 2178
+
60
SP = 6
+
61
Semester = 2
+
62
Vereiste credit = 1816 (Computernetwerken)
+
63
+
64
Informatievisualisatie
+
65
ID = 2185
+
66
SP = 6
+
67
Semester = 1
+
68
Vereiste credit = 2168
+
69

Challenge 1/study.java

50 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 * Study: A class that describes a study that can be followed at the university.
+
4
 * @author Maarten Vangeugden - 1438256
+
5
 */
+
6
class Study {
+
7
		private String m_name; // The name of the study.
+
8
		public String name() {
+
9
				return m_name;
+
10
		}
+
11
		public void setName(String name) {
+
12
				m_name = name;
+
13
		}
+
14
+
15
		private List<MandatoryCourse> m_mandatoryCourses; // A list containing all mandatory courses tied to this study.
+
16
		public List<MandatoryCourse> mandatoryCourses() {
+
17
				return m_mandatoryCourses;
+
18
		}
+
19
		public void setMandatoryCourses(List<MandatoryCourse> mandatoryCourses) {
+
20
				m_mandatoryCourses = mandatoryCourses;
+
21
		}
+
22
		public void addMandatoryCourse(MandatoryCourse mandatoryCourse) { // This seems long and possibly redundant, but readibility outweighs size in my opinion.
+
23
				mandatoryCourses().add(mandatoryCourse);
+
24
		}
+
25
+
26
		private List<ChoiceCourse> m_choiceCourses; // A list containing all courses that can be chosen in this study.
+
27
		public List<ChoiceCourse> choiceCourses() {
+
28
				return m_choiceCourses;
+
29
		}
+
30
		public void setChoiceCourses(List<ChoiceCourse> choiceCourses) {
+
31
				m_choiceCourses = choiceCourses;
+
32
		}
+
33
+
34
		private int m_duration; // An integer representing the amount of years it takes to complete this study.
+
35
		public int duration() {
+
36
				return m_duration;
+
37
		}
+
38
		public void setDuration(int duration) {
+
39
				m_duration = duration;
+
40
		}
+
41
		
+
42
		private int m_studyPoints; // An integer value representing how much study points this study has.
+
43
		public int studyPoints() {
+
44
				return m_studyPoints;
+
45
		}
+
46
		public void setStudyPoints(int studyPoints) {
+
47
				m_studyPoints = studyPoints;
+
48
		}
+
49
}
+
50