Object Orientation in Code

Page 1 of 1

11 Replies - 344 Views - Last Post: 19 January 2013 - 07:51 AM Rate Topic: **--- 3 Votes

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Object Orientation in Code

Posted 18 January 2013 - 03:13 PM

Hello,

Recently I was working on a project on Genetic Algorithms that required a lot of calculation and data manipulation. Therefore my language of choice was C#.
However due to my meager OOP experience my code looked very much like standard C code and (although I wrote few classes) when I needed to change some bits in the code it almost became unmanageable and gave erroneous results thus teaching me why OOP is so important. And now I want to start from scratch using bits of my code to build a easily modifiable program. And I need your help ! Please guide me how I should break my code in manageable classes, blocks etc. I don't want code just the idea of how to break it into small parts.

Here what my code does:

  • Already defined/hard coded : A mathematical equation whose minima is to be found.
  • Takes 4 inputs from user : Bit Size, Population Size, Cross Over Length, Generations.
  • Generates pseudo-random bits (number of bits generated is equal to population size) of given size (bit size) and stores them in separate lists (number of list depends on the number of variables in equation provided).
  • Converts the bits to decimal value mathematically manipulates the decimal to make it fit for putting in the equation and finally stores the value of the solution of the equation in another list.
  • Finds the minimum solution and copies the corresponding binary value of the next generation to a new list.
  • Average of all solutions is calculated.
  • Below average value gets passed to the next generation (that is copied to a new list), above average values are complemented and then copied.
  • The corresponding to bit strings are crossed and stored in the same list, i.e. assume bit length is 5 and crossover length is 3 and I have 2 bits as 11000 and 01111 after crossover the will look like 11111 and 01000 i.e. the last 3 digits are interchanged.
  • With this new population the same steps are repeated starting from conversion to decimal and so on and so forth for the number of times the user wants.
  • Finally when the iteration ends all intermediate steps etc are put in a text file as output.


Also, will using a database be better than using list?

Thanks.

This post has been edited by Jeet.in: 18 January 2013 - 03:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Object Orientation in Code

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Object Orientation in Code

Posted 18 January 2013 - 03:24 PM

Take a look in my signature block. See the tutorial link for "Classes and objects" ? There is a LOT of explanation in that tutorial on how to *think* in an OOP way so you can see how how to break down your project into logical classes etc.
Was This Post Helpful? 2
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Object Orientation in Code

Posted 18 January 2013 - 03:27 PM

View PosttlhIn`toq, on 19 January 2013 - 03:54 AM, said:

Take a look in my signature block. See the tutorial link for "Classes and objects" ? There is a LOT of explanation in that tutorial on how to *think* in an OOP way so you can see how how to break down your project into logical classes etc.


Thank you very much tlhIn`toq ! If I have problems/questions based on your tutorials, should I PM you or post it here? Any specific insights about my problem?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Object Orientation in Code

Posted 18 January 2013 - 03:28 PM

Also, my article on how to approach homework is heavy on planning before typing and helps new C# students with seeing the big elephant as a lot of little bites.
Was This Post Helpful? 1
  • +
  • -

#5 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Object Orientation in Code

Posted 18 January 2013 - 03:33 PM

View PosttlhIn`toq, on 19 January 2013 - 03:58 AM, said:

Also, my article on how to approach homework is heavy on planning before typing and helps new C# students with seeing the big elephant as a lot of little bites.


Good One !
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Object Orientation in Code

Posted 18 January 2013 - 03:37 PM

View PostJeet.in, on 18 January 2013 - 04:27 PM, said:

Thank you very much tlhIn`toq ! If I have problems/questions based on your tutorials, should I PM you or post it here?


If there is a question on a tutorial post it in the tutorial thread so we keep everything tidy.
As you get code for this project specifically, post it here.


Quote

Any specific insights about my problem?

All programs are the same, only different. They all have their needs and requirements and problems. You just have to visualize it as tiny pieces and keep to the rules of single-purpose methods without side effects... code in little black boxes: parameters in, results out so a change in one method doesn't break everything...

But more than anything else... PLAN, PLAN, PLAN. Whiteboards are king.
You really have two jobs here:
  • One: Architect the design of the application and workflow. This really doesn't have anything to do with language. Many program archetects can't write code. They just need to see the black boxes, know that a collection of objects goes 'here' etc.
  • Two: Code the design.


You should have a complete design (or 90%) before you start typing. Think as if you were going to design the program then hand it to someone else. Then you be that someone else that receives the plans and starts being the code monkey that finishes what someone else architected.
Was This Post Helpful? 1
  • +
  • -

#7 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Object Orientation in Code

Posted 18 January 2013 - 10:17 PM

View PosttlhIn`toq, on 19 January 2013 - 04:07 AM, said:

View PostJeet.in, on 18 January 2013 - 04:27 PM, said:

Thank you very much tlhIn`toq ! If I have problems/questions based on your tutorials, should I PM you or post it here?


If there is a question on a tutorial post it in the tutorial thread so we keep everything tidy.
As you get code for this project specifically, post it here.


Quote

Any specific insights about my problem?

All programs are the same, only different. They all have their needs and requirements and problems. You just have to visualize it as tiny pieces and keep to the rules of single-purpose methods without side effects... code in little black boxes: parameters in, results out so a change in one method doesn't break everything...

But more than anything else... PLAN, PLAN, PLAN. Whiteboards are king.
You really have two jobs here:
  • One: Architect the design of the application and workflow. This really doesn't have anything to do with language. Many program archetects can't write code. They just need to see the black boxes, know that a collection of objects goes 'here' etc.
  • Two: Code the design.


You should have a complete design (or 90%) before you start typing. Think as if you were going to design the program then hand it to someone else. Then you be that someone else that receives the plans and starts being the code monkey that finishes what someone else architected.


Thanks for the insightful tips !
Was This Post Helpful? 0
  • +
  • -

#8 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Object Orientation in Code

Posted 19 January 2013 - 12:02 AM

tlhIn`toq I read in an online tutorial that it is customary/good practice to define a constructor at the end of a class. What is a constructor and what are it's utilities? Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Object Orientation in Code

Posted 19 January 2013 - 04:22 AM

A constructor is a method that is called whenever you create an instance of the class. You can define multiple constructors with various signatures to give value to the member variables of the class. For example, if you had a class with two int variables, you might set the value of these in the constructor, so that whenever you use the new keyword to instantiate an object, those variables are already set, and so you do not need to manually write code to change them. Alternately, you might have a constructor that takes 2 ints as arguments, and assigns the values from them to your ints, so you could write MyClass instance = new MyClass(4, 7); instead of MyClass instance = new MyClass(); instance.VariableA = 4; instance.VariableB = 7; It's quicker, and means you won't mistakenly forget to assign values.

As for a constructor being at the end of the class... I've never heard of that "good practice", and I always have constructors at the top of the code file. But I can see why they say that: when you write the constructor, you should know all the members and methods of the class well enough. However, it indicates a lack of planning.

Check out the MSDN page for constructors - MSDN is your new best friend.
Was This Post Helpful? 1
  • +
  • -

#10 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Object Orientation in Code

Posted 19 January 2013 - 04:38 AM

MrShoes Thanks for your input ! I really need to do more OOP to fully appreciate the capabilities if OOPS. Alas ! wish my college taught C# ! Self learning is rather tough :online2long: but DIC saves the day :balloon: !
Was This Post Helpful? 0
  • +
  • -

#11 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Object Orientation in Code

Posted 19 January 2013 - 07:06 AM

Also another question came to my mind, why the constructor has to have the name of the class? Is it only to separate in from other methods of the class? Also I am very confused with the term members of a class.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Object Orientation in Code

Posted 19 January 2013 - 07:51 AM

I'm going to stop this thread now.

These questions are chapter one of any textbook or "Learn C# in 30 days" type book. We can't be your private, unpaid tutors throughout your entire C# education just because you don't want to crack open a book.


My standard beginner resources post - Updated DEC 2012

You can try to learn C# by dismantling snippets and googling terms - basically you can take a hit-n-miss, shotgun approach. Or you could just follow a proper training program whether it be a couple intro books, school, on-line course or series of tutorials like the ones we offer. I'll tell you from experience that just fumbling around in the dark and trying to teach yourself with no guidance doesn't work. Its like stumbling across a Harrier Jump Jet and trying to teach yourself how to fly with no background in piloting: You simply lack any groundwork to start from. How can you lay out your own training course if you don't already know the material? Would you go to a university where the teacher says "I don't know any of this but we'll fumble through it together?"

We have a tutorials section and a learning C# series of articles.

First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover. Do a couple hundred on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.

There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Oct 2012
Spoiler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1