Swift Code

We had written code as below, not able to understand what to do for class rectangle and square. Need expertise advice -

import Foundation
--Editable Code--
struct Size{
    var height:Double
    var width:Double
    init(_ height:Double,_ width:Double) {
            self.height = height;
            self.width  = width;
    } 
}

protocol Shape{
    var area:Double {get}
    var perimeter:Double {get}
}

class Rectangle: Shape{
    var size:Size
    init(_ height:Double,_ width:Double){
        self.size=Size.init(height,width)
    }
    init(_ size:Size){
        self.size=size
    }

    var area:Double {
        return size.height*size.width
    }

    var perimeter:Double {
        return 2*size.height+2*size.width
    }

}

class Square: Rectangle{
    init(_ height:Double){
        super.init(Size.init(height,height))
    }
    
    override init(_ size:Size){
        super.init(Size.init(size.height,size.height))
    }
}
--End Editable Code---
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!

guard let sqHeight = Double((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }

guard let sqWidth = Double((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }

guard let height = Double((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }

guard let width = Double((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }

var sqSize = Size(height:sqHeight,width:sqWidth)
var rectSize  = Size(height:height,width:width)

var shape:Shape = Rectangle(size:rectSize)
fileHandle.write(String(shape.perimeter).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
fileHandle.write(String(shape.area).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)

 shape = Square(size:sqSize)
fileHandle.write(String(shape.perimeter).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
fileHandle.write(String(shape.area).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)

Getting error as -

solution.swift:67:18: error: extraneous argument labels 'height:width:' in call
var sqSize = Size(height:sqHeight,width:sqWidth)
                 ^~~~~~~~         ~~~~~~
                                  
solution.swift:68:21: error: extraneous argument labels 'height:width:' in call
var rectSize  = Size(height:height,width:width)
                    ^~~~~~~~       ~~~~~~
struct Size {
   ...
   init(_ height: Double, _ width: Double) {
       ...
   }
}
let size = Size(2.5, 3.5)
struct Size {
   ...
   init(height: Double, width: Double) {
       ...
   }
}
let size = Size(height: 2.5, width: 3.5)
1 Like

If you add _, you do not need label, otherwise, you need to add label.

struct Size {
    var height: Double
    var width: Double
    init(height: Double, width: Double) {
            self.height = height
            self.width  = width
    } 
}

protocol Shape {
    var area: Double { get }
    var perimeter: Double { get }
}

class Rectangle: Shape {
    var size: Size
    init(height: Double, width: Double) {
        self.size = Size(height: height, width: width)
    }
    init(size: Size) {
        self.size = size
    }

    var area: Double {
        return size.height * size.width
    }

    var perimeter: Double {
        return 2 * size.height + 2 * size.width
    }

}

class Square: Rectangle {
    init(height: Double) {
        super.init(size: Size(height: height, width: height))
    }
    
    override init(size: Size) {
        super.init(size: Size(height: size.height, width: size.height))
    }
}

thanks :slight_smile:

Terms of Service

Privacy Policy

Cookie Policy