December 16, 2017, 09:22:43 PM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Read this please.
♦
The Great Kangaroo Escape
♦
Looking for reviews of the 4th ed on Amazon!
♦
Twitter:
@skochan
Home
Help
Search
Login
Register
Chat
Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
>
Old Stuff
>
Chapter Study
>
Chapter 7 - More on Classes
>
Operations on Fractions (2)
Pages: [
1
]
Go Down
« previous
next »
Print
Author
Topic: Operations on Fractions (2) (Read 1667 times)
daviddelmonte
Full Member
Posts: 123
Operations on Fractions (2)
«
on:
November 14, 2009, 11:10:10 AM »
I keep looking at the code to perform arithmetic on Fractions.
I understand (mostly) the capability to use numerator and denominator as instance variables to store both fractions.
However, couldn't I make integers for each numerator and each denominator? It seems to me that they would be easier to manage. What is wrong with that approach?
David
«
Last Edit: November 14, 2009, 11:44:44 AM by daviddelmonte
»
Logged
toby
Full Member
Posts: 104
Re: Operations on Fractions (2)
«
Reply #1 on:
November 14, 2009, 11:54:01 AM »
Sorry I don't follow, what exactly do you mean? Something like instead of having a fractions class, just have several integers in main that held a numerator and denominator for a couple of fractions?
Logged
daviddelmonte
Full Member
Posts: 123
Re: Operations on Fractions (2)
«
Reply #2 on:
November 14, 2009, 11:56:52 AM »
Exactly..
Logged
toby
Full Member
Posts: 104
Re: Operations on Fractions (2)
«
Reply #3 on:
November 14, 2009, 12:24:28 PM »
Off the top of my head two main reasons why you would structure your code this way is organisation and scalability. In terms of organisation you are grouping related values and the code that manipulates those values together, this keeps things quite clean and means that code is organised in a way that it can be reused in many programs. There are probably allot more valid points in organisation than I'm making but I'm pretty tired. So in terms of scalability, you can make however many int's you want in your main, which is pretty coolhowever how many fractions you can handle at the same time depend on how many int's you have created in main. If you make a fraction class (like you have) then you create as many instances as your program requires while its running and so as your user gives the program more fractions to compute you can create more instances to store them in, you can't really do that with int's. You could achieve this effect by using something like malloc (a c function) to allocate memory for an int, and store the memory address in a pointer, and then create a an array of pointers but arrays unless you are using objects are limited in size so you would really end up with a linked list, so you would have a structure containing int's and references all of which were dynamically put together and maintained by your program. This is a serious amount of work that is very similar to many of the things that actually happen when you create a new instance of an object and store it in a NSMutableArray, and its a serious amount more work to get the other flexibility offered by creating a class.
Logged
Pages: [
1
]
Go Up
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
Programming in Objective-C, 5th edition
-----------------------------
=> General Discussions
=> Exercises
-----------------------------
Programming in Objective-C, 4th edition
-----------------------------
=> General Discussion
=> Exercises
===> Chapter 2
===> Chapter 3
===> Chapter 4
===> Chapter 5
===> Chapter 6
===> Chapter 7
===> Chapter 8
===> Chapter 9
===> Chapter 10
===> Chapter 11
===> Chapter 12
===> Chapter 13
===> Chapter 14
===> Chapter 15
===> Chapter 16
===> Chapter 17
===> Chapter 18
===> Chapter 19
===> Chapter 20
===> Chapter 21
=> Chapter 2
=> Chapter 3
=> Chapter 4
=> Chapter 5
=> Chapter 6
=> Chapter 7
=> Chapter 8
=> Chapter 9
=> Chapter 10
=> Chapter 11
=> Chapter 12
=> Chapter 13
=> Chapter 14
=> Chapter 15
=> Chapter 16
=> Chapter 17
=> Chapter 18
=> Chapter 19
=> Chapter 20
=> Chapter 21
=> Errata
===> 1st Printing
===> 2nd Printing
-----------------------------
Members' Corner
-----------------------------
=> Look What I'm Doing...
-----------------------------
Study Group
-----------------------------
=> Objective-C Quizzes
===> Comprehensive Quiz
===> Chapter 1 - Introduction
===> Chapter 2 - Programming in Objective-C
===> Chapter 3 - Classes, Objects, and Methods
===> Chapter 4 - Data Types and Expressions
===> Chapter 5 - Program Looping
===> Chapter 6 - Making Decisions
===> Chapter 7 - More on Classes
===> Chapter 8 - Inheritance
===> Chapter 9 - Polymorphism, Dynamic Typing, and Dynamic Binding
===> Chapter 10 - More on Variables and Data Types
===> Chapter 11 - Categories and Protocols
===> Chapter 12 - The Preprocessor
===> Chapter 15 - Numbers, Strings & Collections
===> Chapter 17 - Memory Management
-----------------------------
iOS Programming
-----------------------------
=> General Questions
-----------------------------
Programming in C, 3rd ed. (Stephen Kochan)
-----------------------------
=> Programming in C webcasts?
=> Answers to Odd-Numbered Exercises
=> Questions and Help
-----------------------------
Old Stuff
-----------------------------
=> Errata, Answers to Exercises, Code, etc.
=> Shared Code Library
=> Welcome
=> News
=> Errata
===> 1st Printing
===> 2nd printing
=> Newbie Corner
=> Chapter Study
===> Chapter 2 - Programming in Objective-C
===> Chapter 3 - Classes, Objects, and Methods
===> Chapter 4 - Data Types and Expressions
===> Chapter 5 - Program Looping
===> Chapter 6 - Making Decisions
===> Chapter 7 - More on Classes
===> Chapter 8 - Inheritance
===> Chapter 9 - Polymorphism, Dynamic Typing, and Dynamic Binding
===> Chapter 10 - More on Variables and Data Types
===> Chapter 11 - Categories and Protocols
===> Chapter 12 - The Preprocessor
===> Chapter 13 - Underlying C Language Features
===> Chapter 15 - Numbers, Strings, and Collections
===> Chapter 16 - Working with Files
===> Chapter 17 - Memory Management
===> Chapter 18 - Copying Objects
===> Chapter 19 - Archiving
===> Chapter 21 - Writing iPhone Applications
=> Answers to Exercises
===> Chapter 2
===> Chapter 3
===> Chapter 4
===> Chapter 5
===> Chapter 6
===> Chapter 7
===> Chapter 8
===> Chapter 9
===> Chapter 10
===> Chapter 11
===> Chapter 12
===> Chapter 13
===> Chapter 15
===> Chapter 16
===> Chapter 17
===> Chapter 18
===> Chapter 19
===> Chapter 21
=> General Discussion
=> Program Examples
===> Chapter 2
===> Chapter 3
===> Chapter 4
===> Chapter 5
===> Chapter 6
===> Chapter 7
===> Chapter 8
===> Chapter 9
===> Chapter 10
===> Chapter 11
===> Chapter 12
===> Chapter 13
===> Chapter 15
===> Chapter 16
===> Chapter 17
===> Chapter 18
===> Chapter 19
===> Chapter 21
=> XCode 4 Help
=> Suggestions
=> Help!!!
=> Fraction Calculator Available from the App Store
=> Resources
=> Errata
===> 1st & 2nd printings
===> 3rd & 4th printings
===> 5th printing
===> Current Errata
=> Suggestion Box
=> Tips & Tricks
=> Part 3
===> Welcome to the Class
===> Special Announcements
===> Assignments
=> Parts 1 and 2
===> Welcome to the Class
===> Questions and Discussion
===> SPECIAL ANNOUNCEMENTS
===> Assignments
===> Chapter 4, Exercise 7
===> Suggestions
=> Webcast Series Part I, Sept. 1 - 24
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Questions and Discussion
===> Assignments
===> Chapter 4, Exercise 7
===> Chapter 6, Exercise 4
===> Suggestions
=> Webcast Series Part II, Oct. 1 - 27
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Questions and Discussion
===> Assignments
===> Example using hasPrefix: and hasSufix:
===> Suggestions
=> Objective-C Workshop Aug 11 - 13
=> Webcast Series Part I, Oct. 29 - Dec. 1
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Assignments
===> Chapter 4, Exercise 7
===> Chapter 6, Exercise 4
===> Suggestions
=> Webcast Series Part II, Jan 5 - 28
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Assignments
===> Jan 7th Solutions
Loading...