7.2

#### Assignment 3: Designing methods for Complex Data, Practice with Lists and Trees, Drawing

Goals: Practice working with lists. Learn to design methods and practice designing with accumulators. Learn to use the image library.

##### 3.1Instructions

Be very, very careful with naming! Again, the solution files expect your submissions to be named a certain way, so that they can define their own Examples class with which to test your code. Therefore, whenever the assignment specifies:
• the names of classes,

• the names and types of the fields within classes,

• the names, types and order of the arguments to the constructor,

• the names, types and order of arguments to methods, or

• filenames,

...be sure that your submission uses exactly those names.

Make sure you follow the style guidelines that handin enforces. For now the most important ones are: using spaces instead of tabs, indenting by 2 characters, following the naming conventions (data type names start with a capital letter, names of fields and methods start with a lower case letter), and having spaces before curly braces.

You will submit this assignment by the deadline using the handin submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the system may have a long queue of submissions which means it takes longer for your code to be submitted - so try to finish early.

There will be a separate submission for each problem - it makes it easier to grade each problem, and to provide you with the feedback for each problem you work on.

Due Date:
• Thursday, January 30th, 9:00pm.

##### Practice Problems

Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.

• Problems 18.1 - 18.4 on page 225

• Problem 18.5 on page 229

• Problem 18.6 on page 234

• Problem 19.4 on page 263

### Problem 1:Translating RNA

For all questions in this problem, be sure to follow the design recipe carefully:
• Give sufficient examples of data, and sufficient tests, to test your methods thoroughly.

• If you find yourself wanting to use a field-of-field, stop. Fill out the template for each method, and figure out another design.

• Think carefully about how to use dynamic dispatch, and where to define methods, to keep your code as simple and clean as possible.

Note: you may want to use the String class’s .length() method, which returns the number of characters in the string.

##### Introduction

A DNA sequence is made of DNA bases, represented by "A", "T", "C" or "G". An RNA sequence is similar, except it uses the base "U" instead of "T". A DNA sequence may be transcribed into an RNA sequence, by transforming each base uniquely. For example, a transcription can be done using the table below:
 From... ...to A U T A C G G C

##### Translation of RNA into proteins

RNA is translated into proteins in three-letter chunks, known as codons. The chunks do not overlap. For example, the RNA ACAUUG has two distinct codons: ACA and UUG. A protein is a sequence of codons.

Translation begins usually at the start of an RNA (a simplistic assumption for now). It identifies 3-letter codons, and adds them to the protein. Translation stops in the following cases:

• There are no more codons left in the RNA sequence. This includes a last, “incomplete” codon of less than 3 bases.

• A special codon, called a stop codon is encountered. Stop codons are UAG, UAA or UGA. The stop codon is not part of the resulting protein.

When translation stops, the current protein is finished. If the first encountered codon is a stop codon (or a stop codon immediately follows another stop codon) then an empty protein is produced. If there are more codons after the stop codon (i.e. the RNA sequence is not finished), then translation begins again and new proteins are produced.

For example, the RNA sequence ACAGAUAG translates into a single protein (ACA, GAU). Note that although the last three bases make a stop codon, this codon is not encountered when the sequence is processed one codon at a time starting from the left.

For another example, ACAAAGUAGUUG translates into two proteins: (ACA, AAG) and (UUG).

##### What to do

In this problem, you must represent an RNA sequence as a list of strings, each string of unit length. You must represent this list as ILoString, implemented as union data. Similarly, you must represent a protein as a list of strings, each string of length 3 representing one codon. Finally you must represent a list of proteins as a “list of list of strings”, using ILoLoString. Each list of string in this list represents one protein.

It may have been easier to represent RNA sequences as Strings, but in this problem you are not allowed to do this. You must adhere to the specifications as listed.

As an optional, ungraded warm-up exercise, design a method transcribe that produces an RNA sequence from this DNA sequence (also represented as ILoString), using the above table.

To solve the actual problem, you must design a method translate that transforms an RNA sequence (an ILoString) into a list of proteins (i.e., an ILoLoString). This will be the only method we directly call to test your code; however, you will most likely need to design one or more helper methods. Our tests will not care what order you produce the proteins in, but they will care that each protein’s codons are in the correct order.

Hints:

• Using accumulators will be helpful to solve this problem. You may need to use one or more accumulator arguments in at least one helper method. There are several valid designs for this problem. It is recommended that you think through the problem systematically and work through a wish-list process to determine what helpers will be useful.

• It is not required that you add methods in every interface and class that you write for this problem. Write only what you will meaningfully use.

• Be sure to test your implementation properly, including cases where an RNA sequence can produce proteins longer than one codon, etc. Testing your solution effectively may itself require helper methods.

##### What to submit

Submit your solution in a single file named rna.java.

There are no specifications for the name of the examples class, so name it appropriately.

### Problem 2:Drawing trees

In this problem, you will be drawing and working with trees.

 import java.awt.Color; interface ITree { /* see methods below */ } class Leaf implements ITree { int size; // represents the radius of the leaf Color color; // the color to draw it } class Stem implements ITree { // How long this stick is int length; // The angle (in degrees) of this stem, relative to the +x axis double theta; // The rest of the tree ITree tree; } class Branch implements ITree { // How long the left and right branches are int leftLength; int rightLength; // The angle (in degrees) of the two branches, relative to the +x axis, double leftTheta; double rightTheta; // The remaining parts of the tree ITree left; ITree right; }

A Stem at an angle of 90 degrees is growing straight up; a Branch with a left angle of 135 degrees and a right angle of 45 degrees points on both upward diagonals. (Leaves don’t need angles, since we can just approximate them with circles.)

Hints: To design many of the methods for this problem, it helps to refresh some basic trigonometry.

• The reference for angles is usually the +X axis.

• Let us say we given a point (x,y). Join this point to the origin, creating a line of length $l$ and an angle $\theta$ in radians. Then x=$l \cos\theta$, and y=$l \sin\theta$. In Java you can compute cosines and sines using Math.cos and Math.sin respectively.

For each method, it will help to visualize or draw on paper what you are trying to achieve before you attempt to design and write code.

• Design the method WorldImage draw(), that renders your ITree to a picture. Draw all branches and stems using LineImages (this will make the coordinate manipulations easier than if you use rectangles) of whatever color you choose. Understanding the concept of pinholes in your image will be very helpful, so make sure to read the quick-start guide below, and the documentation for more information. Try to draw some simple images unrelated to this problem to get a feel for the library, before writing this specific method.

You do not have to write extensive tests for this method, but you should write at least some simple sanity checks. See below for more information about getting started with the image library. We will not write automated tests for this method, but will grade it manually.

• Design the method boolean isDrooping(), that computes whether any of the twigs in the tree (either stems or branches) are pointing downward rather than upward.

• Design the method ITree combine(int leftLength, int rightLength double leftTheta, double rightTheta, ITree otherTree). This method takes the current tree and a given tree and produces a Branch using the given arguments, with this tree on the left and the given tree on the right... but with a twist, literally.

Here are two trees, with the stems and branches drawn in different colors so we can keep them apart:

tree1 = new Branch(30, 30, 135, 40, new Leaf(10, Color.RED), new Leaf(15, Color.BLUE))

tree2 = new Branch(30, 30, 115, 65, new Leaf(15, Color.GREEN), new Leaf(8, Color.ORANGE))

If we attach each of them to stems growing at 90 degrees (i.e., straight up), we get:

new Stem(40, 90, tree1)

new Stem(50, 90, tree2)

Now suppose we want to attach those two stems to form a branch, with the left branch at 150 degrees and the right branch at 30 degrees (for example). We would get

tree1.combine(40, 50, 150, 30, tree2)

Your method should implement this combination step. Note that this result is different from simply placing both trees in a branch directly:

new Branch(40, 50, 150, 30, tree1, tree2)

You can deduce why this is from the interpretations of the various fields in the data definitions above, and design helper methods to implement the desired behavior accordingly.

• Design the method double getWidth() that returns the width of the tree. Assume that leaves are drawn as circles, and their size is used as their radius.

##### Using the javalib library

The javalib library provides the support for the design of interactive games and creating images composed by combining geometric shapes as well as image files. See The Image Library for more information.

At the top of the .java file where the library is used, add the following import statements:

 import tester.*; // The tester library import javalib.worldimages.*; // images, like RectangleImage or OverlayImages import javalib.funworld.*; // the abstract World class and the big-bang library import java.awt.Color; // general colors (as triples of red,green,blue values) // and predefined colors (Color.RED, Color.GRAY, etc.)

You can test images using check-expect just as you can check other values:
 boolean testImages(Tester t) { return t.checkExpect(new RectangleImage(30, 20, OutlineMode.SOLID, Color.GRAY), new RectangleImage(30, 20, OutlineMode.SOLID, Color.GRAY)); }

But note that you must construct the images in the same manner: for example, the following test will fail:
 boolean testFailure(Tester t) { return t.checkExpect( new ScaleImageXY(new RectangleImage(60, 40, OutlineMode.SOLID, Color.GRAY), 0.5, 0.25), new RectangleImage(30, 15, OutlineMode.SOLID, Color.GRAY)); }

Finally, you can display your images so that you can see whether you’re on the right track, as follows:

 boolean testDrawTree(Tester t) { WorldCanvas c = new WorldCanvas(500, 500); WorldScene s = new WorldScene(500, 500); return c.drawScene(s.placeImageXY(myTree.draw(), 250, 250)) && c.show(); }