Advertisement
niksudan

Swift Structures and Tuples

Sep 23rd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. struct Shape {
  2. var width: Int
  3. var height: Int
  4. var unit: String
  5.  
  6. func getArea() -> Int {
  7. return width * height
  8. }
  9.  
  10. func getType() -> String {
  11. return (width == height) ? "Square" : "Rectangle";
  12. }
  13.  
  14. func theDimensions() -> Void {
  15. println("Dimensions: \(width) \(unit) by \(height) \(unit)")
  16. }
  17.  
  18. func theArea() -> Void {
  19. println("Area: \(getArea()) \(unit)")
  20. }
  21.  
  22. func theType() -> Void {
  23. println("Shape is a \(getType())")
  24. }
  25.  
  26. func make3D(length:Int) -> Shape3D {
  27. println("Converted shape to 3D")
  28. return Shape3D(width: width, height: height, length: length, unit: "\(unit)3");
  29. }
  30. }
  31.  
  32. struct Shape3D {
  33. var width: Int
  34. var height: Int
  35. var length: Int
  36. var unit: String
  37.  
  38. func getVolume() -> Int {
  39. return width * height * length
  40. }
  41.  
  42. func getType() -> String {
  43. return (width == height && width == length) ? "Cube" : "Cuboid";
  44. }
  45.  
  46. func theDimensions() -> Void {
  47. println("Dimensions: \(width) \(unit) by \(height) \(unit) by \(length) \(unit)")
  48. }
  49.  
  50. func theVolume() -> Void {
  51. println("Volume: \(getVolume()) \(unit)")
  52. }
  53.  
  54. func theType() -> Void {
  55. println("3D Shape is a \(getType())")
  56. }
  57. }
  58.  
  59. var square = Shape(width: 10, height:10, unit:"cm")
  60. square.theDimensions()
  61. square.theType()
  62. square.theArea()
  63.  
  64. println()
  65.  
  66. var rectangle = Shape(width:25, height: 5, unit:"mm")
  67. rectangle.theDimensions()
  68. rectangle.theType()
  69. rectangle.theArea()
  70.  
  71. println()
  72.  
  73. var cube = square.make3D(10)
  74. cube.theType()
  75. cube.theDimensions()
  76. cube.theVolume()
  77.  
  78. println()
  79.  
  80. var cuboid = Shape3D(width:15, height:5, length:100, unit:"m3")
  81. cuboid.theType()
  82. cuboid.theDimensions()
  83. cuboid.theVolume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement