Amazon.com Widgets How do you solve 4.6?
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 25, 2014, 02:09:22 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 4
| | | |-+ How do you solve 4.6?
Pages: [1] Go Down
Print
Author Topic: How do you solve 4.6? (Read 746 times)
WorldBuilder
Newbie
*
Posts: 4






on: January 09, 2012, 10:58:51 PM

I've seen a try here, and in Chapter 7, where the exercise is revisited.  I don't agree with the program's written by the forum members.  They just tack on an "i" at the end of the int.  If you tested that, and decided to make it "3i + 3", it wouldn't work.

How do you accept the user's input of "3i" and display it in NSLog?

Is there a way to take two characters from the user and store it in a variable without using a string?  A char is only good for one character, correct?

Thanks,
~Shannon
Logged
jgelling
Newbie
*
Posts: 27






Reply #1 on: January 10, 2012, 02:51:13 AM

4.6 specifies that Complex should only use two doubles for its instance variables, a double for both real and imaginary (as you can see from the declarations for the accessors).

No, you cannot store "i" in a double. As far as allowing the user to input "x+yi" in the terminal (which is definitely beyond the scope of the question), dealing with formatted multi-data type input probably involves fiddling with scanf or some other low-level C method way more than any OOP should have to.

It's not worth bothering with. You're not really going to be building terminal applications to input imaginary numbers on a single line - you're going to use GUIs eventually with an interface like PCalc and working with Core Data objects that have actual I/O and text parsing capabilities.

The author is probably envisioning the easier solution of just getting the real and imaginary numbers (as just doubles) separately from the user and formatting the whole complex number in a separate print method. Or just setting them yourself in the main and ignoring scanf for now.

The key at this point of the book is to focus on how to organize data in classes. Data classes like Complex are ultimately going to be part of the Model you'll use to extend views and controllers (which handle the I/O in real apps) - just ignore the terminal except as a means to debug your program. That's all you need it for.
Last Edit: January 10, 2012, 04:19:27 AM by jgelling Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.