Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

programming challenge - 3.5/4 DONE- calculator display

 
Arvin tirboman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not really object oriented. You should try to figure out how you could create a class hierarchy that represents digits in a Seven-segment display way. (http://en.wikipedia.org/wiki/Seven-segment_display).



This image gives you quite a lot of help. I would implement this as a base class, (AbstractSevenSegmentDigit) containing booleans if the segment should be printed or not. By default, everything is off.

Then I would create subclasses where I in the constructor sets the segments that are to be lit. The class OneSevenSegmentDigit would look something like this:



What I would like the digits to do is to return to me a two dimensional boolean array that is a multiple of the integer I send into it. Then you can iterate over that array to print "light the board".
 
Stephan van Hulst
Bartender
Posts: 6326
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making a separate class for each digit is *very* heavy handed. Instead I would use a single class that can assert or clear the segments individually, and return a collection of asserted segments.

My example stores the state of the display internally as an int value, and has a hard-coded list of values for each digit.

Note that I only added a toString() method to the SegmentDisplay class to illustrate the correctness of the class. I normally wouldn't add a toString() method that contains line separators. The hard-coded Strings in the Segment enum can then also be removed.

The original poster could add a String[] toLines(int size) method, that returns a String for each line of digit, depending on the length of each segment. These lines can then simply be merged together when there are multiple digits to be printed. For this, methods isHorizontal()/isVertical() could be added to the Segment enum.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arvin, we don't have too many rules here, but we do ask that you BeForthrightWhenCrossPostingToOtherSites.

http://www.java-forums.org/advanced-java/48597-programming-challenge-calculator-display.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic