Question

In: Computer Science

Create a User struct that has properties for basic information about a user. At a minimum, it should have properties to represent a user's name, age, height, weight, and activity level.

Create a User struct that has properties for basic information about a user. At a minimum, it should have properties to represent a user's name, age, height, weight, and activity level. You could do this by having name be a String, age be an Int, height and weight be of type Double, and activityLevel be an Int that will represent a scoring 1-10 of how active they are. Implement this now.

Create a variable instance of User and call it your name. Use the memberwise initializer to pass in information about yourself. Then print out a description of your User instance using the instance's properties.

Create a Distance struct that will represent distance in various units of measurement. At a minimum, it should have a meters property and a feet property. Create a custom initializer corresponding to each property (i.e. if you only have the two properties for meters and feet you will then have two initializers) that will take in a distance in one unit of measurement and assign the correct value to both units of measurements. Hint: 1 meter = 3.28084 feet.

Solutions

Expert Solution

Create a User struct that has properties for basic information about a user. At a minimum, it should have properties to represent a user's name, age, height, weight, and activity level. You could do this by having name be a String, age be an Int, height and weight be of type Double, and activityLevel be an Int that will represent a scoring 1-10 of how active they are

struct User {
var name: String
var age: Int
var height:Double
var weight:Double
var activityLevel:Int
}

Create a variable instance of User and call it your name. Use the memberwise initializer to pass in information about yourself.

let Deep = User(name: "Deep", age: 42, height: 165.0, weight: 72.0, activityLevel: 8)

Then print out a description of your User instance using the instance's properties.

print(Deep.name)
print(Deep.age)
print(Deep.height)
print(Deep.weight)
print(Deep.activityLevel)

Create a Distance struct that will represent distance in various units of measurement. At a minimum, it should have a meters property and a feet property. Create a custom initializer corresponding to each property (i.e. if you only have the two properties for meters and feet you will then have two initializers) that will take in a distance in one unit of measurement and assign the correct value to both units of measurements. Hint: 1 meter = 3.28084 feet.

struct Distance {
var meters:Double
var feet:Double
//custom initializer for meters property that take in distance in meters and assign the correct value to both units of measurements.
init(meters:Double){
self.meters = meters
self.feet = meters * 3.28084
}
//custom initializer for feet property that take in distance in feet and assign the correct value to both units of measurements.
init(feet:Double){
self.meters = feet / 3.28084
self.feet = feet
}
}
var distance = Distance(meters: 100.0)
distance = Distance(feet:123.456)


Related Solutions

Define a struct pet with properties name, age, weight, and type. Use appropriate data types for the different properties.
use C source code to complete the following:Define a struct pet with properties name, age, weight, and type. Use appropriate data types for the different properties. You may assume that the strings will never be longer than 19 chars. Don’t forget to add an extra char for the NULL character that terminates the strings.
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT