This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
cs330_f2016:lab5 [2016/09/16 20:44] dcostello |
cs330_f2016:lab5 [2021/06/30 23:42] (current) |
||
|---|---|---|---|
| Line 8: | Line 8: | ||
| ====Preparation:==== | ====Preparation:==== | ||
| - | For this lab, you should use Julia v.0.4. You may use the binary hosted in Dr. Wingate's home directory, at | + | For this lab, you should use at least Julia v.0.4. You may use the binary hosted in Dr. Wingate's home directory, at |
| ''/users/faculty/wingated/cs330/languages/julia-2e358ce975/bin'' | ''/users/faculty/wingated/cs330/languages/julia-2e358ce975/bin'' | ||
| Line 23: | Line 23: | ||
| * ''greyscale'' | * ''greyscale'' | ||
| * ''invert'' | * ''invert'' | ||
| + | * ''count_person'' | ||
| + | * ''average_age'' | ||
| * ''tree_map'' | * ''tree_map'' | ||
| * ''add_last_name'' | * ''add_last_name'' | ||
| * ''eye_colors'' | * ''eye_colors'' | ||
| + | |||
| Line 31: | Line 34: | ||
| Define the following data structures. | Define the following data structures. | ||
| <code Julia> | <code Julia> | ||
| - | abstract shape | + | abstract Shape |
| - | type position | + | type Position |
| x::Real | x::Real | ||
| y::Real | y::Real | ||
| end | end | ||
| - | type circ <: shape | + | type Circ <: Shape |
| - | center::position | + | center::Position |
| radius::Real | radius::Real | ||
| end | end | ||
| - | type square <: shape | + | type Square <: Shape |
| - | upper_left::position | + | upper_left::Position |
| length::Real | length::Real | ||
| end | end | ||
| - | type rect <: shape | + | type Rect <: Shape |
| - | upper_left::position | + | upper_left::Position |
| width::Real | width::Real | ||
| height::Real | height::Real | ||
| Line 58: | Line 61: | ||
| === Area === | === Area === | ||
| <code Julia> | <code Julia> | ||
| - | function area(Shape) ... | + | function area(shape) ... |
| </code> | </code> | ||
| Returns the area of the given shape. | Returns the area of the given shape. | ||
| Line 65: | Line 68: | ||
| === In_Shape=== | === In_Shape=== | ||
| <code Julia> | <code Julia> | ||
| - | function inside(Shape::shape, Position::position) ... | + | function in_shape(shape::Shape, position::Position) ... |
| </code> | </code> | ||
| Returns if the position is inside the shape. Assume standard math coordinates where y is positive going up. The bounds are inclusive. | Returns if the position is inside the shape. Assume standard math coordinates where y is positive going up. The bounds are inclusive. | ||
| Line 72: | Line 75: | ||
| Define the Following type in your code | Define the Following type in your code | ||
| <code Julia> | <code Julia> | ||
| - | type pixel | + | type Pixel |
| r::Real | r::Real | ||
| - | b::Real | ||
| g::Real | g::Real | ||
| + | b::Real | ||
| end | end | ||
| </code> | </code> | ||
| Line 81: | Line 84: | ||
| === Greyscale == | === Greyscale == | ||
| <code Julia> | <code Julia> | ||
| - | function greyscale(picture::Array{pixel,2}) ... | + | function greyscale(picture::Array{Pixel,2}) ... |
| </code> | </code> | ||
| Takes a 2d array of pixels and greys out each pixel. This is done by averaging together the r g b values and then setting r g b to the average. | Takes a 2d array of pixels and greys out each pixel. This is done by averaging together the r g b values and then setting r g b to the average. | ||
| Line 89: | Line 92: | ||
| === invert === | === invert === | ||
| <code Julia> | <code Julia> | ||
| - | function invert(picture::Array{pixel,2}) ... | + | function invert(picture::Array{Pixel,2}) ... |
| </code> | </code> | ||
| Takes a 2d array and pixels and inverts each one. Inverting is done by setting r to `255 - r` and the other two in the same manner. | Takes a 2d array and pixels and inverts each one. Inverting is done by setting r to `255 - r` and the other two in the same manner. | ||
| === Trees == | === Trees == | ||
| - | Define the folowing types in your code | + | Define the following types in your code |
| <code Julia> | <code Julia> | ||
| - | abstract treeItem | + | abstract TreeItem |
| - | type person <: treeItem | + | type Person <: TreeItem |
| name::AbstractString | name::AbstractString | ||
| birthyear::Integer | birthyear::Integer | ||
| eyecolor::Symbol | eyecolor::Symbol | ||
| - | father::treeItem | + | father::TreeItem |
| - | mother::treeItem | + | mother::TreeItem |
| end | end | ||
| - | type unknown <: treeItem | + | type Unknown <: TreeItem |
| end | end | ||
| </code> | </code> | ||
| Line 112: | Line 115: | ||
| === Count Persons === | === Count Persons === | ||
| <code Julia> | <code Julia> | ||
| - | function count_persons(tree::treeItem) | + | function count_persons(tree) |
| </code> | </code> | ||
| Counts the number of people in the tree. Unknowns do not count as people. | Counts the number of people in the tree. Unknowns do not count as people. | ||
| Line 118: | Line 121: | ||
| === Average Age === | === Average Age === | ||
| <code Julia> | <code Julia> | ||
| - | function average_age(tree::treeItem) | + | function average_age(tree) |
| </code> | </code> | ||
| - | Calculates the average age of the people in the tree. Use this years date 2016. Unknowns do not count as people. | + | Calculates the average age of the people in the tree. Use this years date 2017. Unknowns do not count as people. |
| === tree map === | === tree map === | ||
| <code Julia> | <code Julia> | ||
| - | function tree_map(f, tree::treeItem) | + | function tree_map(f, tree) |
| </code> | </code> | ||
| - | Takes a function f and applies it to each known member of the tree. The function f takes one parameter and returns a treeItem, that | + | Takes a function f and applies it to each known member of the tree. The function f takes one parameter and returns a treeItem. Tree_map will return a *new* tree. Tree_map should leave the original tree unchanged, unless f applies a mutation. |
| === add last name=== | === add last name=== | ||
| <code Julia> | <code Julia> | ||
| - | function add_last_name(name::AbstractString, tree::treeItem) | + | function add_last_name(name::AbstractString, tree) |
| </code> | </code> | ||
| - | Appends name to the end of the name of each member of the tree. | + | Appends name to the end of the name of each member of the tree. You should use tree_map. You do not need to add a space in front of the name. |
| Hint: Julia uses ''*'' for string concatenation. | Hint: Julia uses ''*'' for string concatenation. | ||
| Line 138: | Line 141: | ||
| === eye_colors === | === eye_colors === | ||
| <code Julia> | <code Julia> | ||
| - | function eye_colors(tree::treeItem) | + | function eye_colors(tree) |
| </code> | </code> | ||
| - | Returns a list of all eye colors in the tree. A color can appear more than once. For grading visit the father side of the tree first | + | Returns a list of all eye colors in the tree. A color can appear more than once. For grading visit the order child, father, mother. |
| + | |||
| + | hint: Julia arrays can be appended using the ; for example [a;b;c] will create a list with a, b, and c in it. The empty list [] is ignored. | ||
| - | hint: Julia arrays can be appended using the ; for example [a;b;c] will create a list with a, b, and c in it. The empty list [] is ignored | ||