// // ViewController.swift // Binary Numbers // import UIKit class ViewController: UIViewController { // Computers store data as a series of 1's and 0's. // If a bit is a 1, we have a value of a certain amount, else we do not have anything. // When we represent something like a number or // a character, each bit represents a certain amount. // The rightmost bit has a value of how many 1's // The next bit has a value of how many 2's // The next bit has a value of how many 4's // The next bit has a value of how many 8's // The next bit has a value of how many 16's // The next bit has a value of how many 32's // The next bit has a value of how many 64's // etc. // If we have a number in binary 0000110, // then this represents the number 6, since // we have 1 2 plus 1 4 which is 6. // Here are some more examples: // 0000001 = 1 // 0000010 = 2 // 0000011 = 3 (2 + 1) // 0000100 = 4 // 0000101 = 5 (4 + 1) // 0000110 = 6 (4 + 2) // 0000111 = 7 (4 + 2 + 1) // 0001000 = 8 // 0001001 = 9 (8 + 1) // FINISH ME // create a variable called timer var ???? = Timer() // FINISH ME // create a variable called timerNumber and set it to 0 // this variable is used for the slide show of numbers ??? // FINISH ME // LINK your slider to this IBOutlet variable @IBOutlet weak var slider: UISlider! // Here we create 3 arrays (or lists) var binaryButtons = [UIButton]() var binaryButtons2 = [UIButton]() var binaryButtonValues = [Int]() // FINISH ME // LINK your 1's button to this reference variable @IBOutlet weak var button1: UIButton! // FINISH ME // LINK your 2's button to this reference variable @IBOutlet weak var button2: UIButton! // FINISH ME // LINK your 4's button to this reference variable @IBOutlet weak var button4: UIButton! // FINISH ME // LINK your 8's button to this reference variable @IBOutlet weak var button8: UIButton! // FINISH ME // LINK your 16's button to this reference variable @IBOutlet weak var button16: UIButton! // FINISH ME // LINK your 32's button to this reference variable @IBOutlet weak var button32: UIButton! // FINISH ME // LINK your 64's button to this reference variable @IBOutlet weak var button64: UIButton! // FINISH ME // Link your Binary Number: label @IBOutlet weak var binaryNumberLabel: UILabel! // FINISH ME // Link your Character: label @IBOutlet weak var characterLabel: UILabel! func updateBinaryNumberAndCharacter() { // now we will find the number and character // FINISH ME // create a variable called value and set it to 0 ??? // FINISH ME // create a variable called i and set it to -1 ??? // FINISH ME // create a variable called digit and set it to "0" ??? // now we will loop through all of the binary buttons for button in binaryButtons { digit = button.title(for: .normal)! // FINISH ME // add 1 to i ??? // FINISH ME // check and see if digit is equal to "1" if digit == "1" { // FINISH ME // add to value binaryButtonValues[i] ??? = ???? } } // FINISH ME // set binaryNumberLabel.text to hold "Binary Number: " + // String(value) (you can also use interpolation) binaryNumberLabel.???? = "Binary Number: " + ????? if value >= 32 && value <= 127 { let characterValue = UnicodeScalar(value)! // we must unwrap let ch = Character(characterValue) // FINISH ME // set characterLabel.text to "Character: \(ch)" ????? } else { // FINISH ME // set characterLabel.text to "Character: ?" ???? } } // this function returns num converted to a binary number // which is stored as a String func getBinaryString(_ num:Int) -> String { // FINISH ME // convert the num into a base 2 (binary) number // You will be creating a String object String() // the first parameter will be num // the second parameter will be radix:? // where ? should be replaced by the base (2) return ????(num, radix:2) } func displayNumberOnButtons(_ x:Int) { var bin = getBinaryString(x) // print(bin) let startCount = bin.count + 1 if startCount <= 7 { for _ in startCount...7 { // FINISH ME // Join (using +) "0" to the left side of bin bin = ???? + bin } } // here we update the buttons var p = -1 for character in bin { // FINISH ME // add 1 to variable p ???? // print(character) if character == "0" { binaryButtons2[p].setTitle("0", for: .normal) } else { binaryButtons2[p].setTitle("1", for: .normal) } } } // FINISH ME // Link your top 7 buttons to this action event // DO NOT link your bottom 7 buttons to any event @IBAction func buttonEvent(_ sender: UIButton) { // sender is the temporary name of the button // that was clicked // this gets the text on the button that // got clicked let textOnButton = sender.title(for: .normal)! // this flips from "1" to "0" // or from "0" to "1" on the button // FINISH ME // check to see if textOnButton is equal to "1" if ??? == ??? { // FINISH ME // call the button's setTitle method // and set the title to a "0" sender.??????("?", for: .normal) } else { // FINISH ME // call the button's setTitle method // and set the title to a "1" sender.?????("?", for: .normal) } // FINISH ME // call method updateBinaryNumberAndCharacter() ????? } // FINISH ME // Link your Clear Binary Digits button to this // action event @IBAction func clearBinaryDigitsEvent(_ sender: UIButton) { // FINISH ME // set each of your binary buttons to "0" // we will loop through each binaryButton // in the list for button in binaryButtons { button.setTitle(???, for: .normal) } // FINISH ME // now we update the labels // call the method // updateBinaryNumberAndCharacter() ??? } @IBAction func startSlideShowButtonEvent(_ sender: UIButton) { // FINISH ME // set timerNumber to 0 // FINISH ME // convert slider.value to a Double type let seconds = ????(slider.value) timer = Timer.scheduledTimer(timeInterval: seconds, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) } @IBAction func stopSlideShowButtonEvent(_ sender: UIButton) { timer.invalidate() } @objc func timerAction() { displayNumberOnButtons(timerNumber) // FINISH ME // Call method updateBinaryNumberAndCharacter() ????? // FINISH ME // add 1 to the variable timerNumber ???? // FINISH ME // check to see if timerNumber is greater than or equal to 128 if ??? >= ??? { timer.invalidate() } } @IBAction func sliderEvent(_ sender: UISlider) { timer.invalidate() // FINISH ME // convert slider.value to a Double type let seconds = ?????(slider.value) timer = Timer.scheduledTimer(timeInterval: seconds, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // Here we create the list of binaryButtons backwards binaryButtons.append(button1) binaryButtons.append(button2) binaryButtons.append(button4) binaryButtons.append(button8) binaryButtons.append(button16) binaryButtons.append(button32) binaryButtons.append(button64) // Here we create the list of binaryButtons forwards binaryButtons2.append(button64) binaryButtons2.append(button32) binaryButtons2.append(button16) binaryButtons2.append(button8) binaryButtons2.append(button4) binaryButtons2.append(button2) binaryButtons2.append(button1) // Here we create the list of binaryButtonValues binaryButtonValues.append(1) binaryButtonValues.append(2) binaryButtonValues.append(4) binaryButtonValues.append(8) binaryButtonValues.append(16) binaryButtonValues.append(32) binaryButtonValues.append(64) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }