Hi RichardOnRails, thank you very much
Recently I'm learning this language
Ok
Version is
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
Operating System is Windows 2003 server (english)
The aim is to bring the code in this java to ruby exactly like this for
learning purposes.
// The code was drawn from the book "Head First Object Oriented Analysis
and Design"
import java.util.*;
class Airplane {
private int speed;
public Airplane() {
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getSpeed() {
return speed;
}
}
class Jet extends Airplane {
private static final int MULTIPLIER = 2;
public Jet() {
super();
}
public void setSpeed(int speed) {
super.setSpeed(speed * MULTIPLIER);
}
// HERE STAY MY PROBLEM!!!!!!!!!
public void accelerate() {
// Access to method "setSpeed" of class Airplane
super.setSpeed(getSpeed() * 2);
}}
class FlyTest {
public static void main(String[] args) {
Airplane biplane = new Airplane();
biplane.setSpeed(212);
System.out.println(biplane.getSpeed());
Jet boeing = new Jet();
boeing.setSpeed(422);
System.out.println(boeing.getSpeed());
int x = 0;
while (x<4) {
boeing.accelerate();
System.out.println(boeing.getSpeed());
if (boeing.getSpeed()>5000) {
biplane.setSpeed(biplane.getSpeed() * 2);
} else {
boeing.accelerate();
}
x++;
}
System.out.println(biplane.getSpeed());
}
}
The output is expected is:
212
844
1688
6752
13504
27008
1696
Hi David,
OK. I understand your problem. You did the natural thing: you
worked hard to convert the Java program directly into a Ruby program.
It was natural, but it was the wrong approach. Nowadays, the
emphasis in programming is Agile Programming and Test-Driven or
Behavior-Driven Programming. That focuses on writing code in small
pieces and making sure they work correctly. Then add more code, and
make sure that’s right.
So the approach I recommend is:
• Make sure the Java code works as advertised. Check out “Test
FlyTest.java” at
http://www.pastie.org/pastes/262610
• Then I show you how to take the first step in “FlyTest1.rb” at
http://www.pastie.org/pastes/262618
• Ditto for the second step in “FlyTest2.rb” at
http://www.pastie.org/pastes/262622
• Ditto for the third step in “FlyTest3.rb” at
http://www.pastie.org/pastes/262624
In each of the Ruby code I include the Java code in a couple of
comments. I do that for convenience as I try to replicate the Java
functionality in Ruby code. If you don’t like that, you could just as
easily keep the Java code open in a separate window.
As you add code try to make it good Ruby code, nor “Rubyized” Java.
If you don’t know how to express something in Ruby, look at the code
I originally sketched out for you or post a narrow question on the
newsgroup. I recommend you do that rather than posting a lot of code.
For more guidance, check out “Agile Ruby”, “Test-Drive Development
(TDD)” and “Behavior-Driven Development (BDD)” in Google. That’ll
give you some more guidance.
Good luck.
HTH,
Richard