PDA

View Full Version : Dissecting the code 1:OOP


masterof_none
25-05-2003, 10:16 PM
"Programming is understanding"
- Kristen Nygaard (http://www.wikipedia.org/wiki/Kristen_Nygaard)
-creator of Simula.

Before we dissect HelloRC.java code, we need to understand the programming concept called object oriented programming(OOP).
(Consider System.out.println("Blah")). Why they have too many "dots" here. what does that mean?)

So, What is OOP?
OOP is just another way of organizing your program.
Before OOP, they have what they called modular programming (using functions). However, a guy from Europe (Norway) created a programmming language called Simula, the first OOP language.

What is function?
So in Math, we are thought about function right?(linear function, quadratic function). The same applied here.Consider this:
f(x) = 2x + 1 .
This is a linear function. So, what is this supposed to mean?.
Given a value x, if we apply to that equation, it would become sth else(increase/decrease in value etc). For x equal to 2, f(x) now become 5.
The same thing here in programming. In any programming language,usually they refer the x above as a "parameter".

conceptually , looks like this:
//this is the main function. This function will be called automatically


main() {
//I'll explain this later
int result;
int x = 9;

//result would hold the result of computation of the function
//note: x IS 9 from above

result = f(x);

}

//this is the function
int f(x)
{
//do whatever computation.
int func_result;

func_result = 2x + 1;

//need to pass back to the calling function in the main func above:
return func_result;
}

This example is just meant to illustrate the usage of function (just like math). But computation is a little bitt fancy(pass the parameter to function, and pass back to calling function(main) .

Of course, you can directly compute from the main func:

main() {
int result;
int x=9;

result = 2x+ 1;
}

This yield the same result. but that's not our point.


OK, So...?

So, after we know function, we want to explore the definition of variable.
What is variable?
You already knew. The x above. But in C, you'll find something like this:
int result;

what is this?. That means, result would hold variable integer only. This declaration is to make sure the compiler allocate memory just enough for result to hold integer. No decimal point. If the result has the decimal point, it would cut off. e.g 14.234 become 14 only.

So what if we still want to store the .234 ?
Use float. e.g

float result.

if we want to store a string, we want to use:

char blah[20];
(allocating 20 block of character)
float, char, and int , hence can be referred as
data structure
Now, we're a little bit curious. It seems like we have some limitation here. Instead of storing only number, like int result above, or string only , char blah[20]what if we want to store the both under one common name?

for example, you want to create a dinasour, you want the name, and the age of the dinasour .
for example,
This is my favorite dinasour :

Name: Barney
Age : 5 years old (because Barney like to play with kids)

so, of course, we can;t simply put
int barney;
this will only store the age of Barney, which is 5, or,

char barney[10] = "Barney" ;

this only store the name of the dinasour, but not the age. So, How can we resolve it?

in C, they have what they call, struct (for structure, I suppose)
e.g

struct Dinasour {

char name[10] = "Barney";
int age = 5;
}

now only people know what is the name of this dinasour and the age.

I guess I should stop here. Take a break. Do your own reading.
Post it here if you got confused. I'll try my best to help you.
Next, we'll explore the class concept. (it turns out it is something similar to struct).
to be continued...

masterof_none
26-05-2003, 01:56 AM
so the summary is:
function in C:
copy and paste the following,save as calc.c:
and compile

#include <stdio.h>

int main()
{
int result=0; //initialize the result
int x = 9 ; //value of x;

//calculating result:

result = f(x) ; //note : you can;t do this --> wrong-->f(x)=result;

}
int f(x)
{
int func_result;
func_result = 2x+1;
return func_result;
}

Compiler:
you can get compiler from gcc.gnu.org but you have to be familiar with Unix

or

get Microsoft/Borland .
but you have to pay. (oh!)
usually the school provide it. so check that out.

Ponder:
I thought we learn Java?
As I mention, since Java evolve from C/C++, we better learn a little C first.

how abt struct ?
we'll learn later....!

please email me :
hasran@<hidden>

for more info.

bachok83
26-05-2003, 07:38 AM
i think we have a problem here....i still cannot compile HelloRC.java

can you provide explaination on how to compile. Others will also benefit from this

:lol:

masterof_none
27-05-2003, 12:13 AM
The correct syntax for struct in C is:

typedef struct {
//data

} Dinasour ;


Sorry.

littlebigone
04-06-2003, 03:35 AM
bachok,

What are the errors you get when you try to compile HelloRC.java?

I can only think of 3 possiblilties rite now:

1) you don't have the java sdk installed. i think you can get the java package from the sun website

2) you saved the name of the file wrong. Java requires that the file name that be the same as the name of the class that contains your main method. Correct me if i'm wrong here master of none

3) if you're trying to run javac through the command prompt for windows, you must be in the same directory as where you saved the file

hope this helps

littlebigone
18-09-2003, 12:08 AM
what happened to teaching java?

littlebigone
18-09-2003, 12:08 AM
I'm a friend!!!!!A FRIEND!!!!!!!!

masterof_none
18-09-2003, 12:13 AM
You're a BEST FRIEND!