Monday, July 19, 2021

CH 6 iOS

Here is my UIViewController class:

//
//  ViewController.swift
//  CH6Ex1Circle
//
//  Created by stu2017 on 10/24/17.
//  Copyright © 2017 stu2017. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tbRad: UITextField!
    @IBOutlet weak var lblCirc: UILabel!
    @IBOutlet weak var lblArea: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func btnCalc(_ sender: UIButton) {
        let rad=Double(tbRad.text!);
        let circ1=Circle(radius:rad!);
        let a=String(format: "%.2f", circ1.getArea());
        let c=String(format: "%.2f", circ1.getCircumference());
        lblArea.text="Area: \(a)";
        lblCirc.text="Circumfrence: \(c)";
    }
}

 Here is my circle class
//
//  Circle.swift
//  CH6Ex1Circle
//
//  Created by stu2017 on 10/24/17.
//  Copyright © 2017 stu2017. All rights reserved.
//

import Foundation


class Circle {
    var radius = 0.0
    
    init(radius: Double) { // Constructor
        self.radius = radius
        
    }
    var Area: Double{
        return 3.14*(radius*radius)
    }
    
    func getArea() -> Double {
        return 3.14*(radius*radius)
    }
    
    var Circumference: Double {
        return 2*3.14*radius
    }
    func getCircumference() -> Double{
        return 2*3.14*radius
    }
}

1 comment:

Note: Only a member of this blog may post a comment.