update
This commit is contained in:
116
Rectangle/julia/main.jl
Normal file
116
Rectangle/julia/main.jl
Normal file
@@ -0,0 +1,116 @@
|
||||
# Concepts: Structs, methods, associated functions
|
||||
|
||||
# Define Rectangle struct with width and height. Implement:
|
||||
|
||||
# area(&self) -> f64
|
||||
|
||||
# can_hold(&self, other: &Rectangle) -> bool
|
||||
|
||||
# square(size: f64) -> Rectangle (associated function, a.k.a "static method")
|
||||
|
||||
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
|
||||
|
||||
struct Rectangle
|
||||
width::AbstractFloat
|
||||
hight::AbstractFloat
|
||||
end
|
||||
|
||||
function square_rec(l:AbstractFloat)
|
||||
return Rectangle(l, l)
|
||||
end
|
||||
|
||||
function area(rec::Rectangle)::AbstractFloat
|
||||
return rec.width * rec.hight
|
||||
end
|
||||
|
||||
function can_hold(rec_holder::Rectangle, rec_insert::Rectangle)::Bool
|
||||
width_holdable = false
|
||||
hight_holdable = false
|
||||
if rec_holder.width > rec_insert.width || rec_holder.width > rec_insert.hight
|
||||
width_holdable = true
|
||||
end
|
||||
|
||||
if rec_holder.hight > rec_insert.hight || rec_holder.hight > rec_insert.width
|
||||
hight_holdable = true
|
||||
end
|
||||
|
||||
return hight_holdable == width_holdable
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user