Question

Protocol Exercise 1- Adopt Protocols: CustomStringConvertible, Equatable, and Comparable Create a Human class with two properties:...

Protocol

Exercise 1- Adopt Protocols: CustomStringConvertible, Equatable, and Comparable

Create a Human class with two properties: name of type String and age of type Int. You’ll need to create a memberwise initializer for the class. Initialize two Human instances.
Make the Human class adopt the CustomStringConvertible. Print both of your previously initialized Human objects.
Make the Human class adopt the Equatable protocol. Two instances of Human should be considered equal if their names and ages are identical to one another. Print the result of a Boolean expression evaluating whether or not your two previously initialized Human objects are equal to each other (using ==). Then print the result of a Boolean expression evaluating whether or not your two previously initialized Human objects are not equal to each other (using !=).
Make the Human class adopt the Comparable protocol. Sorting should be based on age. Create another three instances of a Human, then create an array called people of type [Human] with all of the Human objects that you have initialized. Create a new array called sortedPeople of type [Human] that is the people array sorted by age.

solve this using swift code please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

program code to copy

/*:
 ## Exercise - Adopt Protocols: CustomStringConvertible, Equatable, and Comparable
 
 Create a `Human` class with two properties: `name` of type `String`, and `age` of type `Int`. You'll need to create a memberwise initializer for the class. Initialize two `Human` instances.
 */
class Human: CustomStringConvertible, Equatable, Comparable {
    var name: String
    var age: Int
    
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
    
    var description: String {
        return "Human(name: \(name), age: \(age))"
    }
    
    static func ==(lhs: Human, rhs: Human) -> Bool {
        return lhs.name == rhs.name && lhs.age == rhs.age
    }
    
    static func <(lhs: Human, rhs: Human) -> Bool {
        return lhs.age < rhs.age
    }
}

let Billy = Human(name: "Billy", age: 35)
let Sarah = Human(name: "Sarah", age: 48)

/*:
 Make the `Human` class adopt the `CustomStringConvertible`. Print both of your previously initialized `Human` objects.
 */
print(Billy)
print(Sarah)

/*:
 Make the `Human` class adopt the `Equatable` protocol. Two instances of `Human` should be considered equal if their names and ages are identical to one another. Print the result of a boolean expression evaluating whether or not your two previously initialized `Human` objects are equal to eachother (using `==`). Then print the result of a boolean expression evaluating whether or not your two previously initialized `Human` objects are not equal to eachother (using `!=`).
 */
print(Billy == Sarah)
print(Billy != Sarah)

/*:
 Make the `Human` class adopt the `Comparable` protocol. Sorting should be based on age. Create another three instances of a `Human`, then create an array called `people` of type `[Human]` with all of the `Human` objects that you have initialized. Create a new array called `sortedPeople` of type `[Human]` that is the `people` array sorted by age.
 */
let ashley = Human(name: "Ashley", age: 40)
let jonathan = Human(name: "Jonathan", age: 38)
let jodie = Human(name: "Jodie", age: 26)

let people = [ashley, jodie, jonathan, Billy, Sarah]
let sortedPeople = people.sorted(by: <)
print(sortedPeople)
//: page 1 of 5  |  [Next: App Exercise - Printable Workouts](@next)

sample output

input Human (name: Billy, age: 35) Human (name: Sarah, age: 48) false true (Human (name: Jodie, age: 26), Human (name: Billy,

Add a comment
Know the answer?
Add Answer to:
Protocol Exercise 1- Adopt Protocols: CustomStringConvertible, Equatable, and Comparable Create a Human class with two properties:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi need this solved using Swift Programming ! Exercise - Failable Initializers Create a Computer struct with two properties, ram and yearManufactured, where both parameters are of type Int. Create a...

    Hi need this solved using Swift Programming ! Exercise - Failable Initializers Create a Computer struct with two properties, ram and yearManufactured, where both parameters are of type Int. Create a failable initializer that will only create an instance of Computer if ram is greater than 0, and if yearManufactured is greater than 1970, and less than 2017. Create two instances of Computer? using the failable initializer. One instance should use values that will have a value within the optional,...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • c++ driver and car are independed classes 1. Create a class Car, which has a color, engine, horsepower, fuel, FuelLevel, year of manufacturing and driver which can be defined by name, age, licen...

    c++ driver and car are independed classes 1. Create a class Car, which has a color, engine, horsepower, fuel, FuelLevel, year of manufacturing and driver which can be defined by name, age, licenseNumber. Create the corresponding OOP in For each class (Driver, Car) write a header file and the class implementation -In the main function do the following: from that class in main function. L.1 Write a function that will print the total number of objects created 12 Define an...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • plz write if it is in another class or package Question 1: 1. Create a new...

    plz write if it is in another class or package Question 1: 1. Create a new project in Eclipse (File > New > Java Project.) Name it Homework2Q1 2. Create a package for your classes 3. Create an abstract superclass with only non default constructor(s) 4. Create two different subclasses of that superclass 5. Create another class that is not related (you need a total of four classes up to this point) 6. Create an interface 7. Make the class...

  • This is for JAVA programming. Create a test class that contains two arrays and two methods:...

    This is for JAVA programming. Create a test class that contains two arrays and two methods: - The first array has 3 rows and 3 columns and is initialized with type double data - The second array has 4 rows and 4 columns and is also initialized with type double data - The first method ArraysRows will receive an array of type double as an argument and then print out the sum and average of all elements in each ROW....

  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    Create a UML diagram to help design the class described in exercise 3 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public? 3. Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT