Java ACM Exercise

Discussion in 'Software' started by takwas, Nov 15, 2012.

  1. takwas

    takwas Private E-2

    I came across an exercise (in the book "The Art and Science of Java" by Eric Roberts) that requires using only GArc and GLine classes to create a lettering library which draws your initials on the canvas. This should be made independent of the GLabel class.
    I'd like to know the correct approach to use in solving this problem. I'm not sure what I have so far is good enough (I'm thinking it's too long). The questions requires that I use a good Top-Down approach.

    Here's my code: http://pastebin.com/FiuK3Zgw

    I created a class (GLetter.java) with arrays for GArc and GLine objects. They are positioned in certain ways thereby turning certain Glines and/or GArcs on or off (changing visiblity) would create a pattern for a letter. This Gletter uses the if/else statements to determine which pattern to create - this makes me feel my code is too long.
    There is another class (DrawInitials.java) that simulates a GraphicsProgram and allows the user to pass certain letters as arguments to the GLetter object. I've used 'L' and 'O' as examples.
    However, I posted this because I'm not sure I'm using the right approach. That's why I need your help.
    I feel MY CODE IS TOO LONG!
    The code linked above is not the complete project...it only draws letters 'L' and 'O' for now.
     
  2. PC-XT

    PC-XT Master Sergeant

    Most letter drawing routines are long. TrueType fonts use lines, arcs, and things to draw letters. It makes the letters better quality at more sizes than bitmap fonts, but it is usually more complicated.
     
  3. takwas

    takwas Private E-2

    Okay...but what's your view of my approach? I just need to know I'm still on track
     
  4. PC-XT

    PC-XT Master Sergeant

    If I were doing this, I would basically only differ in coding style. It probably wouldn't shorten the code too much. Logically, there isn't much that can be shortened without adding complications, and it's good to keep it simple when you are formulating the ideas. Since you have it basically ironed out, you may want to try encoding the letter parts like a segmented display does, to see if you like it better:

    You have 24 lines and 4 arcs, for 28 parts. Segmented displays often encode characters as integers, with one bit corresponding to each part. You could use 32-bit integers, (which can be written 0b00000001010001011010000101000101, for example.) For each line or arc, check whether its corresponding bit is set. I would only draw and set up each part if its bit was set, because it's simpler for me, but your method of turning on the appropriate parts more closely resembles what a real segmented display does. (To make it look segmented, you would vary the color, making it all visible, only more or less noticeable.) Individual bits can be tested by ANDing them with a binary number with only the one bit set. This would probably shorten the compiled bytecode more than the sourcecode, and could make the source harder to read, and more complicated.

    Other differences from my coding style would be that I would probably make a method in GLetter to add a full string, taking the calculations out of DrawInitials' run method, and I would mark the ones you have for single characters, rather than String.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds