This shows you the differences between two versions of the page.
cs330_f2016:lab5 [2017/02/07 20:21] dhart |
cs330_f2016:lab5 [2021/06/30 23:42] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | |||
- | ====Objective:==== | ||
- | |||
- | Learn the basics of the Julia Language. | ||
- | |||
- | ---- | ||
- | |||
- | ====Preparation:==== | ||
- | |||
- | 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'' | ||
- | |||
- | or you may install Julia yourself. | ||
- | |||
- | ---- | ||
- | |||
- | ====Deliverables:==== | ||
- | |||
- | For this lab, you will need to implement the following functions and data structures in Julia: | ||
- | * ''area'' | ||
- | * ''in_shape'' | ||
- | * ''greyscale'' | ||
- | * ''invert'' | ||
- | * ''count_person'' | ||
- | * ''average_age'' | ||
- | * ''tree_map'' | ||
- | * ''add_last_name'' | ||
- | * ''eye_colors'' | ||
- | |||
- | |||
- | |||
- | === Shapes === | ||
- | Define the following data structures. | ||
- | <code Julia> | ||
- | abstract Shape | ||
- | |||
- | type Position | ||
- | x::Real | ||
- | y::Real | ||
- | end | ||
- | |||
- | type Circ <: Shape | ||
- | center::Position | ||
- | radius::Real | ||
- | end | ||
- | |||
- | type Square <: Shape | ||
- | upper_left::Position | ||
- | length::Real | ||
- | end | ||
- | |||
- | type Rect <: Shape | ||
- | upper_left::Position | ||
- | width::Real | ||
- | height::Real | ||
- | end | ||
- | </code> | ||
- | |||
- | |||
- | === Area === | ||
- | <code Julia> | ||
- | function area(shape) ... | ||
- | </code> | ||
- | Returns the area of the given shape. | ||
- | where ''Shape'' is some concrete instance of the abstract shape type class. For this a few of the following instead of testing for class member ship it is easier to write three different functions, one for each concrete instance of shape. ie: ''function area(shape::rect)...'' | ||
- | |||
- | === In_Shape=== | ||
- | <code Julia> | ||
- | function in_shape(shape::Shape, position::Position) ... | ||
- | </code> | ||
- | Returns if the position is inside the shape. Assume standard math coordinates where y is positive going up. The bounds are inclusive. | ||
- | |||
- | === Pixel === | ||
- | Define the Following type in your code | ||
- | <code Julia> | ||
- | type Pixel | ||
- | r::Real | ||
- | g::Real | ||
- | b::Real | ||
- | end | ||
- | </code> | ||
- | |||
- | === Greyscale == | ||
- | <code Julia> | ||
- | function greyscale(picture::Array{Pixel,2}) ... | ||
- | </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. | ||
- | |||
- | Hint: Julias map works over multi-dimensional arrays | ||
- | |||
- | === invert === | ||
- | <code Julia> | ||
- | function invert(picture::Array{Pixel,2}) ... | ||
- | </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. | ||
- | |||
- | === Trees == | ||
- | Define the following types in your code | ||
- | <code Julia> | ||
- | abstract TreeItem | ||
- | |||
- | type Person <: TreeItem | ||
- | name::AbstractString | ||
- | birthyear::Integer | ||
- | eyecolor::Symbol | ||
- | father::TreeItem | ||
- | mother::TreeItem | ||
- | end | ||
- | |||
- | type Unknown <: TreeItem | ||
- | end | ||
- | </code> | ||
- | |||
- | === Count Persons === | ||
- | <code Julia> | ||
- | function count_persons(tree) | ||
- | </code> | ||
- | Counts the number of people in the tree. Unknowns do not count as people. | ||
- | |||
- | === Average Age === | ||
- | <code Julia> | ||
- | function average_age(tree) | ||
- | </code> | ||
- | Calculates the average age of the people in the tree. Use this years date 2017. Unknowns do not count as people. | ||
- | |||
- | === tree map === | ||
- | <code Julia> | ||
- | function tree_map(f, tree) | ||
- | </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. Tree_map will return a *new* tree. Tree_map should leave the original tree unchanged, unless f applies a mutation. | ||
- | |||
- | === add last name=== | ||
- | <code Julia> | ||
- | function add_last_name(name::AbstractString, tree) | ||
- | </code> | ||
- | 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. | ||
- | |||
- | === eye_colors === | ||
- | <code Julia> | ||
- | function eye_colors(tree) | ||
- | </code> | ||
- | 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. | ||
- | |||