Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialReagen Rahardjo
3,770 Pointsplease review my code im stuck
Hi,
Im confuse with this challenge. why i cant call location to the while loop function?
class Point {
var x: Int
var y: Int
init(x: Int, y: Int){
self.x = x
self.y = y
}
}
class Machine {
var location: Point
init() {
self.location = Point(x: 0, y: 0)
}
func move(direction: String) {
print("Do nothing! I'm a machine!")
}
}
// Enter your code below
class Robot: Machine {
override init() {
super.init()
}
override func move(direction: String) {
switch direction {
case "Up" : return (location.y + 1)
case "Down": return (location.y - 1)
case "left": return (location.x - 1)
case "right" : return (location.x + 1)
default: break
}
}
}
1 Answer
Reagen Rahardjo
3,770 PointsHi Thx alot for the answer it helps!
I have questions if you dont mind.
- how can you know to use += instead of location.y + 1. I know its different but I dont know when to apply it.
- from your answer you don't make another init for the sub class? if im not mistaken class always have to have an init to declare right? why the test is pass eventhough it doesnt have init? is it considerable inherite the init from the super class? eventhough we not declaring it?
Thank You
Berry Loeffen
4,303 Points- For example in the case "Up", you want to have the y coordinate go up by 1.
So in this example the point on the map is x:0 and y:0. (Subclass inherits location (x:0,y:0) from superclass)
you want your new location to be x:0,y:1
so your new location will be -> (new)location.y = (old)location.y + 1
now your (new)location.y is 1. so the coordinate point now is x:0 and y: 1
writing the += compound operator as it is called takes the burden away of having to write location.y = location.y +1.
in stead you can use the += compound operator (location.y += 1) so you don't have to write location.y again. -> Less typing is less work, and both location.y = location.y + 1 and location.y += 1 are the same thing.
- Yes in classes you need initializers. in this case it would be
override init() {
super.init()
}
But since the superclass had a fixed value for point (x:0,y:0) for its stored property as initialized in the superclass Machine, in this case you can omit it. What actually happens is that the subclass Robot has the same position (x:0,y:0)
Berry Loeffen
4,303 PointsBerry Loeffen
4,303 PointsHi there,
Writing location.y + 1 is the issue.
In this code you did not add 1 to the y coordinate.
NEW location.y = location.y + 1
Or use the compound operator instead:
There are 2 ways to solve this challenge. With a switch statement like above or with an if statement.
Good luck!