User Tools

Site Tools


cs330_f2016:lab5

This is an old revision of the document!


Objective:

Learn the basics of the Julia Language.


Preparation:

For this lab, you should use 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
  • tree_map
  • add_last_name
  • eye_colors

Shapes

Define the following data structures.

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

Area

function area(Shape) ...

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

function inside(Shape::shape, Position::position) ...

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

type pixel
  r::Real
  b::Real
  g::Real
end

Greyscale

function greyscale(picture::Array{pixel,2}) ...

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

function invert(picture::Array{pixel,2}) ...

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 folowing types in your code

abstract treeItem
 
type person <: treeItem
  name::AbstractString
  birthyear::Integer
  eyecolor::Symbol
  father::treeItem
  mother::treeItem
end
 
type unknown <: treeItem
end

Count Persons

function count_persons(tree::treeItem)

Counts the number of people in the tree. Unknowns do not count as people.

Average Age

function average_age(tree::treeItem)

Calculates the average age of the people in the tree. Use this years date 2016. Unknowns do not count as people.

tree map

function tree_map(f, tree::treeItem)

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

add last name

function add_last_name(name::AbstractString, tree::treeItem)

Appends name to the end of the name of each member of the tree.

Hint: Julia uses * for string concatenation.

eye_colors

function eye_colors(tree::treeItem)

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

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

cs330_f2016/lab5.1474058687.txt.gz · Last modified: 2021/06/30 23:40 (external edit)