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