BlueLight Wrote:Pudding Earl Wrote:BlueLight Wrote:
- Code: Select All Code
public void someMethod(){
int redDice;
int bluDice;
Random r = new Random();
redDice = r.nextInt();
blueDice = r.nextint();
if(redDice == blueDice){
redDice = r.nextInt();
blueDice = r.nextint();
}
if(redDice < blueDice){System.out.println("Blue Wins");}
if(redDice > blueDice){System.out.println("Red Wins");}
}
The stacks' been covered, but if you asked me to explain it I wouldn't know how. Like, I get what it is, I get how it functions, generally, but explaining it is...eh. That's mostly lack of confidence and anxiety I've got it all wrong. Garbage collection is as simple as, if nothing is pointing to this object anymore, this object gets removed. Also what exactly is wrong with the code you presented? Lack of practical experience has me wondering where the problem is exactly.
So i'd personally have to look up stack again, but it's basically a part of the memory (Ram) where the object is stored. Knowing details like this can help make your program more efficient. You don't really have to worry about that at the scale of are programs... I don't think anyone when first learning to program really understands the concept of stack.
on to garbage collection... Thank god. I was worried I'd have to do it manually if i moved over to C#
Okay so the code i presented, is only really good if your trying to get your program working.
I'm going to do a simple modification because something about it is just bugging me.
also, the r.nextInt(int i); takes a integer value.
- Code: Select All Code
public void someMethod(){
int redDice;
int bluDice;
Random r = new Random();
redDice = r.nextInt(6);
blueDice = r.nextint(6);
if(redDice < blueDice){System.out.println("Blue Wins");}
else if(redDice > blueDice){System.out.println("Red Wins");}
else if (redDice == blueDice){someMethod();}
}
r.nextInt(i) returns a random value between 0 and i - 1. lets say i want to change my program so that redDice and blueDice get a value between 1 and 6 here is how i have to change my program.
- Code: Select All Code
public void someMethod(){
int redDice;
int bluDice;
Random r = new Random();
redDice = r.nextInt(6) + 1;
blueDice = r.nextint(6);
if(redDice < blueDice){System.out.println("Blue Wins");}
else if(redDice > blueDice){System.out.println("Red Wins");}
else if (redDice == blueDice){someMethod();}
}
now if i check the value with the debugger i get return value from both values and i get 3, & 3; however redDice wins. If we check the program there is a error. I made a mistake (likely story). I forgot to make line 8 (" blueDice = r.nextint(6); ") to be this (" blueDice = r.nextint(6) + 1; ") when you're program gets to 1,000's lines of code (which my GUI' alone, tend to do.) than errors like that can creep up a lot.
however here's a different modification that's object oriented.
- Code: Select All Code
private int getRandomDiceRoll(int i){
Random r = new Random();
return r.nextInt(i) + 1;}
private int getRandomDiceRoll(){
return this.getRandomDiceRoll(6);
}
public void someMethod(){
int redDice;
int bluDice;
redDice = this.getRandomDiceRoll();
bluDice = this.getRandomDiceRoll();
if(redDice < blueDice){System.out.println("Blue Wins");}
else if(redDice > bluDice){System.out.println("Red Wins");}
else if (redDice == bluDice){someMethod();}
}
Now lets for some odd reason we need to roll a 100 sided dice for some reason. We can just call the getRandomDiceRoll(100); and we just use a 100 and not 6.
My dad is OCD about object oriented programming. while working a full time job; he was able to program part time for my family business (The only programmer) and was able to keep up to pace in features with the competition that had 3 full time programmers. So ya, I've been drilled hard about OO programming.
Some times you just need to brute force something to work, but for maintainability using OO Programming is better. It lets you focus on make the core features and avoid recreated the same line of code over and over.
For instants. Lets say i have a program that relies heavy on exponents.
- Code: Select All Code
2^3
okay well if i was to program this i would do something like this.
- Code: Select All Code
void a (){
int value = 2;
int exponent = 3;
int reminderValue = value;
for(int i = 0; i < exponent; i = i +1){// ya i know. I could do i++;, but that's less clear than i = i+1;
value = value*reminderValue
}
// do something with value....
}
I could do all that hard work and even then it wont give me a value if i did something like 8^(1/3) or (1/8)^(-1/3). we're assume that my program likely uses this more than 10 times in different ways, so what i could do each time to void reporgramming this is this.
- Code: Select All Code
void a (){
int value = Math.pow(2.0, 3.0);
///do something with value....
}
and now i got past the pains of even creating that set of code, once. It follows all the rules of exponents unlike mine and it's less likely to have a error.
Ya, i know; kinda seems like a cheating way to claim OO programming is better but it's in fact a prime example of object oriented programming.
....Right. So like...I followed a fair bit of that, but it felt like a really round about way to get to a conclusion that was given half way up the page. Just saying.