As you can see the Java implementation of Step 1 of 15 Exercises for Learning a new Programming Language was more complicated than the C# implementation. This was largely due to one factor. You can't poll whether someone is typing from the console in Java.

The second problem is that you can't use a KeyEvent handler in the console, which would the second easiest solution to attempt.

Finally a third problem is that by using an event handler you essentially need to use threading so that the loop doesn't interfere with the handler and you are able to interrupt properly.

If there are better solutions I'd be very interested to see them.

package learn2Java1; import java.awt.BorderLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JTextArea; public class Program extends JFrame implements KeyListener { private JTextArea displayArea; private static final long serialVersionUID = 1; public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater( new Runnable( ){ public void run( ) { createAndShowGUI( ); } }); } public Program( String name ) { super( name ); } private static void createAndShowGUI( ) { Program frame = new Program( "Press ESC to exit program." ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.addComponentsToPane( ); frame.pack( ); frame.setVisible( true ); Thread t = new Thread( new MessageLoop( frame ) ); t.start( ); } private void addComponentsToPane( ) { displayArea = new JTextArea( 0 ); displayArea.setEditable( false ); displayArea.addKeyListener( this ); getContentPane( ).add( displayArea, BorderLayout.CENTER ); } public void keyPressed(KeyEvent ke) { checkKey( ke, "KeyPressed: " ); } public void keyReleased(KeyEvent ke) { checkKey( ke, "KeyReleased: " ); } public void keyTyped(KeyEvent ke) { checkKey( ke, "KeyTyped: " ); } private void checkKey( KeyEvent ke, String message ) { System.out.println( message + ke.getKeyCode() ); if ( ke.getID() != KeyEvent.KEY_TYPED && ke.getKeyCode() == KeyEvent.VK_ESCAPE ) System.exit( 0 ); } public void updateArea( String text ) { displayArea.setText( text ); } private static class MessageLoop implements Runnable { Program messageSpace; public MessageLoop( Program thisFrame ) { messageSpace = thisFrame; } public void run( ) { for ( int i = 0; ; i++ ) { messageSpace.updateArea( i + " " ); try { Thread.sleep( 250 ); } catch ( InterruptedException ie ) { } } } } }

posted by dharh 7:15 PM Feb 10th, 2008

Part of the passion in my life, besides life itself, is programming and computers. I especially like the idea of smart computing through smart programs and smart OSes. Which is what I want to focus on in my career, creating intelligence to various degrees to help create better and more efficient user experiences using computers.

Bellow is a table of various code snippets and articles organized by language.

C/C++ C# Java JavaScript
Returning an Array of Pointers javascript trim

15 Exercises for Learning a new Programming Language

This is an exercise to both learn a language, but also to compare different languages. The original idea can be found here.

  1. Display series of numbers (1,2,3,4, 5....etc) in an infinite loop. The program should quit if someone hits a specific key (Say ESCAPE key).
  2. Fibonacci series, swapping two variables, finding maximum/minimum among a list of numbers.
  3. Accepting series of numbers, strings from keyboard and sorting them ascending, descending order.
  4. Reynolds number is calculated using formula (D*v*rho)/mu Where D = Diameter, V= velocity, rho = density mu = viscosity Write a program that will accept all values in appropriate units (Don't worry about unit conversion) If number is < 2100, display Laminar flow, If it's between 2100 and 4000 display 'Transient flow' and if more than '4000', display 'Turbulent Flow' (If, else, then...)
  5. Modify the above program such that it will ask for 'Do you want to calculate again (y/n), if you say 'y', it'll again ask the parameters. If 'n', it'll exit. (Do while loop)

    While running the program give value mu = 0. See what happens. Does it give 'DIVIDE BY ZERO' error? Does it give 'Segmentation fault..core dump?'. How to handle this situation. Is there something built in the language itself? (Exception Handling)
  6. Scientific calculator supporting addition, subtraction, multiplication, division, square-root, square, cube, sin, cos, tan, Factorial, inverse, modulus
  7. Printing output in different formats (say rounding up to 5 decimal places, truncating after 4 decimal places, padding zeros to the right and left, right and left justification)(Input output operations)
  8. Open a text file and convert it into HTML file. (File operations/Strings)
  9. Time and Date : Get system time and convert it in different formats 'DD-MON-YYYY', 'mm-dd-yyyy', 'dd/mm/yy' etc.
  10. Create files with date and time stamp appended to the name
  11. Input is HTML table, Remove all tags and put data in a comma/tab separated file.
  12. Extract uppercase words from a file, extract unique words
  13. Implement word wrapping feature (Observe how word wrap works in windows 'notepad')
  14. Adding/removing items in the beginning, middle and end of the array.
  15. Are these features supported by your language: Operator overloading, virtual functions, references, pointers etc.

C# Java Python
Learn 2 C# 1 Learn 2 Java 1
Learn 2 C# 2
Learn 2 C# 3
Learn 2 C# 4 and 5
Learn 2 C# 6
Learn 2 C# 7
Learn 2 C# 8
Learn 2 C# 9
Learn 2 C# 10
Learn 2 C# 11
Learn 2 C# 12


posted by dharh 2:06 PM Mar 15th, 2006


« Previous
1
Next »


2011: 5 3 1
2010: 12 9 7 1
2009: 12 11 8 5
2008: 12 5 4 3 2 1
2007: 12 11 10 9 8 7 6 5 4 3 2 1
2006: 12 11 10 9 8 7 6 5 4 3 2 1
2005: 12 10 7 6
2004: 10 9 6 5 4 3 2 1