Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Shape {
- var width: Int
- var height: Int
- var unit: String
- func getArea() -> Int {
- return width * height
- }
- func getType() -> String {
- return (width == height) ? "Square" : "Rectangle";
- }
- func theDimensions() -> Void {
- println("Dimensions: \(width) \(unit) by \(height) \(unit)")
- }
- func theArea() -> Void {
- println("Area: \(getArea()) \(unit)")
- }
- func theType() -> Void {
- println("Shape is a \(getType())")
- }
- func make3D(length:Int) -> Shape3D {
- println("Converted shape to 3D")
- return Shape3D(width: width, height: height, length: length, unit: "\(unit)3");
- }
- }
- struct Shape3D {
- var width: Int
- var height: Int
- var length: Int
- var unit: String
- func getVolume() -> Int {
- return width * height * length
- }
- func getType() -> String {
- return (width == height && width == length) ? "Cube" : "Cuboid";
- }
- func theDimensions() -> Void {
- println("Dimensions: \(width) \(unit) by \(height) \(unit) by \(length) \(unit)")
- }
- func theVolume() -> Void {
- println("Volume: \(getVolume()) \(unit)")
- }
- func theType() -> Void {
- println("3D Shape is a \(getType())")
- }
- }
- var square = Shape(width: 10, height:10, unit:"cm")
- square.theDimensions()
- square.theType()
- square.theArea()
- println()
- var rectangle = Shape(width:25, height: 5, unit:"mm")
- rectangle.theDimensions()
- rectangle.theType()
- rectangle.theArea()
- println()
- var cube = square.make3D(10)
- cube.theType()
- cube.theDimensions()
- cube.theVolume()
- println()
- var cuboid = Shape3D(width:15, height:5, length:100, unit:"m3")
- cuboid.theType()
- cuboid.theDimensions()
- cuboid.theVolume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement