diff --git a/Rectangle/julia/main.jl b/Rectangle/julia/main.jl new file mode 100644 index 0000000..591e9aa --- /dev/null +++ b/Rectangle/julia/main.jl @@ -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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Rectangle/rust/.gitignore b/Rectangle/rust/.gitignore new file mode 100644 index 0000000..9f97022 --- /dev/null +++ b/Rectangle/rust/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/Rectangle/rust/Cargo.lock b/Rectangle/rust/Cargo.lock new file mode 100644 index 0000000..b9d42e1 --- /dev/null +++ b/Rectangle/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "rust" +version = "0.1.0" diff --git a/Rectangle/rust/Cargo.toml b/Rectangle/rust/Cargo.toml new file mode 100644 index 0000000..1c61e9e --- /dev/null +++ b/Rectangle/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/Rectangle/rust/README.md b/Rectangle/rust/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Rectangle/rust/src/main.rs b/Rectangle/rust/src/main.rs new file mode 100644 index 0000000..110535c --- /dev/null +++ b/Rectangle/rust/src/main.rs @@ -0,0 +1,116 @@ +// Problem 4: Rectangle with Methods +// 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:u64, + hight:u64, +} + + +impl Rectangle { + fn square_rectangle(l:u64) -> Rectangle { + return Rectangle { width: l, hight: l }; + } + + fn can_hold(& self, rect_insert:& Rectangle) -> bool { + let mut width_holdable = false; + let mut hight_holdable = false; + if self.width > rect_insert.width || self.width > rect_insert.hight { + width_holdable = true; + } + if self.hight > rect_insert.width || self.hight > rect_insert.hight { + hight_holdable = true; + } + + let mut hold_result = false; + if width_holdable == true && hight_holdable == true { + hold_result = true; + } + + + return hold_result; + } + + fn area(& self) -> u64 { + return self.hight * self.width + } + +} + + + + + + + + + +fn main() { + let rec1 = Rectangle::square_rectangle(20); + let h = rec1.hight; + let w = rec1.width; + println!("{w} {h}"); + + let rec2 = Rectangle{width:6, hight:10}; + let holdable = rec1.can_hold(&rec2); + + println!("{holdable}"); + + let rec2area = rec2.area(); + println!("rec2 area {rec2area}"); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +