Problem 2: In the traditional English system of measurement, each inch on a Rule
ID: 3735763 • Letter: P
Question
Problem 2: In the traditional English system of measurement, each inch on a Ruler Viewer ruler is marked off into fractions using tick marks that look like this:T The longest tick mark divides the inch into halves, two shorter tick marks indicate the quarter divisions, and even shorter ones mark the eighths, sixteenths and so on. Write a recursive method drawRuler to draw this. The method is given the coordinates for a rectangle in which to draw the ruler. The method draws a sequence of vertical tick marks. The center tick mark is half the height of the rectangle in which it's drawn. Each smaller tick mark is half the height of the next larger one. Once the tick marks become sufficiently small (say 2 pixels), the recursion terminates. The idea is to recognize that the middlemost tick mark subdivides the rectangle into two smaller rectangles, each of Which is a smaller ruler of its own.Explanation / Answer
Solution:
Java non GUI implementation:
code:
import java.util.*;
import java.lang.*;
import java.util.Scanner;
class RecRuler
{
public static void main(String args[])
{ int max_tick=4;
System.out.println("How many inches (0 - 3) :");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
drawRuler(num,max_tick);
}
public static void drawRuler(int nInches, int ml) {
drawLine(ml, 0); // draw inch 0 line and label
for (int j = 1; j <= nInches; j++) {
drawInterval(ml);
drawLine(ml, j);
}
}
private static void drawInterval(int cl) {
if (cl >= 1) {
drawInterval(cl -1);
drawLine(cl);
drawInterval(cl-1);
}
}
private static void drawLine(int tl) {
drawLine(tl,-1);
}
private static void drawLine(int tl, int tickLabel) {
for (int j = 0; j < tl; j++)
System.out.print("-");
if (tickLabel >= 0)
System.out.print(" " + tickLabel);
System.out.print(" ");
}
}
Python GUI type:
def draw_line(tick_length, tick_label=''):
"""Draw one line with given tick length (followed by optional label)."""
line = '-' * tick_length
if tick_label:
line += ' ' + tick_label
print("%-4s %-8s %4s" % ('||', line, '||'))
def draw_interval(center_length):
"""Draw tick interval based upon a central tick length."""
if center_length > 0: # stop when length drops to 0
draw_interval(center_length - 1) # recursively draw top ticks
draw_line(center_length) # draw center tick
draw_interval(center_length - 1) # recursively draw bottom ticks
def draw_ruler(num_inches, major_length):
"""Draw English ruler with given number of inches and major tick length."""
draw_line(major_length, '0') # draw inch 0 line
for j in range(1, 1 + num_inches):
draw_interval(major_length - 1) # draw interior ticks for inch
draw_line(major_length, str(j)) # draw inch j line and label
if __name__ == '__main__':
print('='*10)
draw_ruler(12,4)
print('='*10)
take care of the indentation when executing the python code.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.