Sunday, December 6, 2009

timer

If you want to do the stopwatch using only Timer class then you will need to decide when and how you start it. The textbook starts it immediately but you need to read the textbox and convert the content to an integer variable (say named "count").

The timer cannot be started inside the actionPerformed method since it will work only once but have to be set in some external method so that once started it will call actionPerformed every time it changes to do the updates of the graphics values and manage the count by decrementing it.

Then if you create such external method like:
public void start() { if (count!=0) timer.start( ); }

then inside actionPerformed you need to have two conditions checked:
1. if ( src == “your button” ) then get the text, convert it into an integer count and issue:
start(); ( see the method above)
2. if ( src == timer ) , meaning that timer value has changed since it has been started AFTER the previous condition has been processed, then do the operations on count by decrementing it and updating values necessary for graphics.

Wednesday, December 2, 2009

About Project...

The min requirements for a completed project include having a working game/simulator with all the mandatory components (see the project description). But this is for marking the project as completed and passing into the final evaluation stage. The grade variance after that will depend on the main grading factors:
User view: visual quality, value-adding supporting services and user-friendliness.
Systems architect view: sophistication of the whole design, good project description (you understand what you did, what was valuable, and what can be improved in the near future).
Programmer view: technical level of implementation.

Therefore, it is up to you to see what level of quality you want to bring your project to. The more good stuff you put in your project the better.

Advice 1: after you have your project satisfying the completion requirements - save it and do not change. Make a copy and enhance the copy only in order to have a working version in case it will stop working during the improvements.

Advice 2 on advanced efforts that can give you some grade enhancement:
1. The design of the game itself. How interesting and user-friendly it is? Do you manage bets, win/loose statistic, other game supporting services?
2. Advanced use of classes (not only for pictures, but may be for separating some operations - like accounting, etc. into a separate class, which communicates with the main interface).
3. Advanced algorithm elements like some intelligence allowing the computer to play against human (just a bunch of IF-ELSE statements).
4. Threads, including animations.
5. Good user input validation (like what happens if instead of a number they type in a letter, etc.)
6. Visually nice layout of the playfield, buttons and other components.
7. Good internal structure of the program with methods representing logically separate parts of program logic and other methods calling them when necessary. Absence of repetitive operations that could have been done with loops. Sufficient commentary in the program text.
8. Good paper explaining your project, the game, the approach used, the advanced methods, etc. - showing all the best and important that you have and making your project well understood.

Friday, November 13, 2009

Grades for week 9

Everybody completed the minimal task for this week. I hope you read more on classes in this blog since it is the most fundamental topic in Java. Although we do not have much time to dedicate to it because the course caters more to the Web development line of use of your knowledge, reread the topic and go as deep into it as you can. Good ways of using classes (in addition to separating some parts of graphics will add to your project value.

Although the minimal requirements were satisfied by all, as I said in the beginning of the course, the contenders for A-level grade should try to maximize the effect of every task (not just formally satisfy it). This week we saw how some students took the idea of size and color control to a higher level allowing to do it dynamically. This supports the dominant program development pattern: MVC (model-view-controller) where the way the user tries to control the systems behavior and the way the backbone model works (image in this case) are separated into different classes, allowing to change the model (images) without changing the way users interface with the system or change interface behavior on the same internal models.

Tuesday, November 3, 2009

Classes

Classes are a form of structuring software projects in modern programming languages. Read more on object-oriented design (if interested).

The T9.3 task will not be checked (there are no points for it) but just to remind you that the described feature should be in your project. T9.2 is a simpler task dealing only with modification of the example. Of course, you could do it by changing your project (like having some graphics moved to another class), which is more than the T9.2 calls for. If you understand classes well then I would suggest to do T9.2 as it is required (will be a small piece of work for those who understands classes) and then without hurry redesign your project into may be several classes (the better use of technology - the higher the grade for the project will be). One class might handle the interface, another handles logic of the control panel, and the third one deals with the game engine (as game state generation, getting user input, and win/loose calculation with no connection to the GUI interface). But if you want to start with one class for now (like some graphics in your project) you can do it. The class design that I just described corresponds to the modern MVC pattern (read about Model View Controller pattern on the Web). The View here will be not just the picture but the whole layout (although art could be a small subclass). The picture will probably change in its implementation when we get to the threads (as another form of concern separation) anyway. The general rule is to keep something in a separate class (object-oriented design), which will have a life (changes) of its own. So if you plan to play with picture by changing it as a result of the event processing - then it should be another class serving the proper image based on the user choices and resulting system’s reaction.

A simple description of the typical class structure and a simple example are on pp.237-239.

If you want to know more about classes – read: http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html . Somewhat more advanced tutorial is here: http://home.cogeco.ca/~ve3ll/jatutor4.htm . For deeper understanding of classes: http://leepoint.net/JavaBasics/oop/oop-10-intro.html see all pages and also the whole chapter 3 in http://docstore.mik.ua/orelly/java/javanut/ch03_01.htm .

Monday, October 26, 2009

Plan for exercise 12

After you set up the layout and put the buttons/checkboxes on it then the program plan/structure might be the following: 1. Pick three random values between 0 and 2 for each button/checkbox 2. Add listeners for each button/checkbox 3. Check which event took place (which button or checkbox were used). For each choice write logic where some counter (from 0 to 2) will be incremented (in a circular way: from 2 goes back to 0) for each time the button has been clicked and set the new color for each counter state for each button (for example, if the counter wa 0 then button1.setIcon(green); and so on) 4. If the button "Try me" has been clicked then check if the sequence of the counters of all buttons corresponds to the sequence of three random generators and post an appropriate message in the message label
5. Repaint

Saturday, October 24, 2009

Pseudocode use (example of Tic-Tac-Toe)

Now your programs are becoming bigger and you need to exercise some discipline by planning the structure of your program before rushing to code.
For example in Tic-Tac-Toe task the plan (sometimes called pseudocode) can be more or less detailed in the beginning but rather more detailed when you are approaching actual coding. Initially it might look like the following:

  • Set up your JPanel with GridLayout
  • Declare an integer counter variable
  • Declare 9 JButtons (for example named b1, b2, etc.)
  • Add ActionLiteners for all buttons in init() method and add each button to the display panel of the game field
  • Add a listener method itself using actionPerformed(ActionEvent e)
  • Add logic for each possible variation of clicked button along with the counter logic checking if it is an odd click or even when in one case use (for example if b1 is clicked) something like b1.setText(“X”) ; or setting the text to “O” and also add button logic like if(actionCommand==”1”){… where you will be setting text to the proper letter and incrementing the counter variable by one (so that next click will setText to a different letter).

Note1: the last logic assumes that you define buttons like: JButton b1=new JButton("1");

Note2: you can use actionCommand in your IF-logic, which uses the label that you gave the button (see Note1) or you can use the system name (b1 - this case) if you get it from a.getSource() method like in the Example 8-14, where a comes from actionPerformed(ActionEvent a)

Remember that if you need more info oin any method - use google.


If after this you have a clear understanding of your programming sections – start coding. If you need to iron out more details – add these details to the pseudocode.

Of course the program can be enhanced in many ways like detecting the winner or even playing against human, etc.

Friday, October 23, 2009

Week 7 problems

At this point many of you are getting really involved with your projects. A word of caution – the course is set in a way allowing you to have web-accessible results from the very beginning of the course, but it doesn’t mean that all that you are doing you understand sufficiently deep.

Here is the main challenge of teaching Java on a good level but from the very beginning (not as Java 1, Java 2 and further courses): you can do simple little exercises without any understanding of how the real working programs work, or you can have a workable environment where some things are not clear but they work and getting deeper understanding by the week with new learning. I chose the second approach. But… it is important that you realize what is happening. You play (modify) some working examples but if you decide in you projects to get into unchartered waters – the lack of really deep understanding will immediately popup in various strange behaviors and errors.

Advice: do not be afraid of suddenly exposed problems but stick closer to the textbook examples in your project development. Don’t run ahead of the train. With every chapter your knowledge (if you really apply yourself) will grow and you will be able to enhance your project more and more. I will be giving you simple tasks as well as tasks on adding new features to your projects. If you suddenly get into problems with more complex use of the material – simplify it in a way to be closer to the textbook (even if some parts and statements you do not deeply understand – just use them as shown).

Of course, if the problem is in the learned material you have to reread the textbook and, possibly, do the Google research on the problem in various online tutorials and explanations. But of you see that the problem involves some more advanced topics – just post your problem to Q&A and if nobody gives a SIMPLE answer – it means that the explanation will have to wait until you will be able to understand the answer based on newly-learned material.

As a result, if you see a GUI problem (like some form of instability) – try to use all learned knowledge and if still no success – post the problem to the Q&A and switch to the key tasks of the current chapter. Thus instead of some complex graphical behavior you can use more simple variations not to fight with what you will learn later but to use your time for learning and using the current material (like logical forks, events, and later loops).

In week 7 such knowledge enhancement of GUI handling (in addition to various event handling methods) is the use of the repaint method in section 8.3.

Thursday, October 22, 2009

W6 Grading

Week 6 grades were assigned as following:
Normal work: 5 points
Extra effort in more changes and more advanced programming: 6points
Outstanding job in terms of sufficient complexity and attempt to fit the project: 7 points
As a result almost all got 5 points by doing small changes to the original code (or even simplifying it)
One person got 6 (for somewhat better than average effort), and nobody has 7.
This is a bit disappointing since the assignment was very simple but the possibilities are rich. As I said before, those who want an A-level grade should demonstrate an extra effort since the minimum passing requirements for many tasks are intentionally simplified allowing all to have some working code, but for the tasks requiring to make changes to the existing code or try to apply the idea of the Calendar – dependency might be taken much further. The more efforts you put into work – the better you will learn Java as well as mastery of using simple Java tools for complex results.
But since you are in the curve – I guess nobody suffers much from simple work… until some students will start putting more efforts into work, which (with the end of the semester approaching) they will.

Friday, October 9, 2009

W4 Results

Week 4gave you a chance to move from pictures to normal interfaces. Yes, it was a lot of work but we have to move at a speed allowing to acquire the necessary skills for the real workplace programming. Week 5 will be easier allowing understanding the interface better and thinking your projects over again not to waste the semester effort for something which is too trivial or what you cannot possibly complete.
The grades for week 4 reflected some deductions. T4.1 used deductions for trivial repetition of the already existing textbook page/code. T4.2 had a requirement of using ALL Swing controls from chapter 4 and the deductions were made for not doing it. Incomplete projects without proper description also led to the point deduction. The discipline of doing all the tasks and deduction punishment is necessary, first of all, for you. If you start running behind the schedule of learning than the amount of accumulated work toward the end of the semester will be so high that you will not be able to complete the course. But if to do every week all the tasks on the proper level - then the increments are not that big and the course will feel much easier.

Wednesday, October 7, 2009

Appletviewer differences

This week you will polish your project layout. I want you to be aware of the fact that although appletviewer is a convenient tool for development it is works very differently than a browser. When you are fine-tuning your interface - it might be good to check it in a real browser. Just double-click on your html file and it will start a page automatically invoking the applet.

Sunday, October 4, 2009

Nesting Layouts

Also pay attention to the possibility of setting one layout inside another (see Example 5-6). So it is possible to use other layouts (like GridLayout) for better component positioning inside the FlowLayout used in the central part of the Welcome page.

I use this post to repeat again the need to start doing the Java home work early leaving time for understanding, experimenting, and making it work! You will definitely need some experimenting with simpler pieces of code to understand how it works.

If you start too late and will think that you can do it in 1 hour - you are wrong. The college requires 9 hours of work per week for all courses... In programming you might get lucky and do it faster but don't bet on it.

Adding Swing components

The task for this week does not allow making changes by just looking at the example code. You actually need to read and understand chapter 4 and 5!
In order for any Swing component to appear on any panel you need to define the component (like JButton newone;) which says that the component with system name “newone” will be of a data type Jbutton. You also need to actually instantiate this component = create a space in the memory and label it “newone” for future references by issuing command:

newone = new JButton( "success" );

This key word “new” is what actually creates a physical instance in the memory of a theoretically defined JButton newone. See page 71 for examples (read the book or you will be lost soon).

Once you have your button (or any other Swing component) in physical existence (instantiated) – the next step is placing it on the proper panel. In a simplified example for the Central part of the Welcome page (welcomePanel) it might look like:

welcomePanel.add( one );

I am leaving the tweaking of the better positioning for your exploration. But if, for example, you want to put a button after welcome message then you put all three mentioned above statements after the statement:

welcomePanel.add( welcomeMsg );

which adds welcomeMsg to the Panel. It will look as following:

welcomePanel.add( welcomeMsg );
JButton newone; // defining the button
newone = new JButton( "success" ); //instantiating the button
welcomePanel.add( one ); //adding button

Wednesday, September 23, 2009

W2 Results

W2 applet programming went very well. Everybody did a great job and I consider that at this point you all are familiar with the working environment and the way to develop, test, and post your results. That was the goal of w1 and w2.

Quiz was a different story. It was very simple for the first quiz and the goal was to show that you need to really read and deeply understand (also remember) the main java tools and methods used in every chapter. Good preparation really pays. Since the group is strong – looks like the main grade variance will be on quizzes (and some difficult tasks later).
Also want to remind you that you should daily read your mail and news forum. Excuses that you didn’t read them and missed some important announcement will not be accepted.

One more advice – read Q&A section even if you didn’t post a problem – since most likely someone else did and the solution has been explained – I noticed new questions were appearing on topics explained in Q&A forums.
Some of you are still trying to make the html file name same as your applet – it is not necessary.

Take week 3 very seriously. Despite a very simplistic explanation of the methods and variables in the book – the topic is verrr-ry important. Additionally read the following material on methods: http://www.otherwise.com/Lessons/MethodsCreationAndUse.html
or/and http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html

Tuesday, September 22, 2009

Java language, finally...

In weeks 1 and 2 you learned and tested basic Java applet environment, settings, and principles. Now in week 3 you can start learning the Java language itself in the same interactive environment. Every language has several main parts:
• Variables, Methods (as mechanisms of passing variables and constants among the parts of the overall software project – “plumbing”), and Data Types;
• Computational flow controls (conditionals and loops, as well as threads on macro level)
• Classes
• Advanced data structures

Variables and Methods will be the topic of week 3. Pay special attention to methods since they can dramatically simplify your programming work if being properly understood and used.

Commentary on W2 grading and problems is coming soon…

Thursday, September 3, 2009

About this blog

Here I will offer additional commentary, tasks modifications (if necessary), help, explanations and... changes in requirements, when necessary. The course is more flexible than traditional courses and more responsive to the learning situation. Using this blog will also allow me to issue special announcements, additions, corrections, updates, etc. from one place regardless of what course management tools we use.
Reading this blog is mandatory.