oorexx homework assignment having issues

oorexx homework assignment having issues

Post by bhawkins41 » Fri, 20 Mar 2009 15:00:53


I have a project due soon that does exchange rates between objects, I
have converted the java solution to rexx to the best of my ability but
it does not work properly, to me it seems the node class is having
issues creating nodes as in my code below, if anyone could please help
it would be greatly appreciated. Or if there is an easier way let me
know.

say 'Project 1 reading file...'

T = .Trader~new
T~assert(63, mango, 14, papaya)
T~query(papaya, mango)


::class Trader
::method Init
expose taga adjList visited
taga = .Tagger~new
adjList = .Node~new
visited = .Array~new

::method assert
expose taga adjList visited
use arg m, thing1, n, thing2
tag1 = taga~tag(thing1)
tag2 = taga~tag(thing2)

adjList~put(tag1, .Node~new~Node(.Ratio~new~Ratio(n, m), tag2,
adjList~get(tag1)))
adjList~put(tag2, .Node~new~Node(.Ratio~new~Ratio(m, n), tag1,
adjList~get(tag2)))

::method query
expose taga adjList visited
use arg thing1, thing2
do i = 1 to 60
visited[i] = 0
end
r = .Ratio~new~Ratio(1,1)
if dfs(taga~tag(thing1), r, taga~tag(thing2)) then do
say r~gdenom ' ' thing1 ' = ' r~gnumer ' ' thing2
exit
end
else do
say '? ' thing1 ' = ? ' thing2
exit
end

dfs:
use arg i, r, j
if i == j
then
return 1
else
visited[i] = 1
p = adjList~get(i)
do while p != nil

if ! visited[p~gtarget]
then
if dfs(p~gtarget, r~multiplyBy(p~rate), j)
then
return 1
else
r~divideBy(p~rate)


p = p~next
end
return 0



::class Tagger
::method Init
expose name count
name = .Array~new
count = 0

::method tag
expose name count
use arg s
if name~hasItem(s) then return name~index(s)
else
count = count + 1
return count

::class Node
::method Init
expose arr
arr = .array~new

::method Node
expose rate next target
use arg rate, target, next
self.rate = rate
self.target = target
self.next = next
--say rate target next
return self


::method gtarget
expose target
return target

::method get
expose rate next target arr
use arg i
a = arr[i]
return a

::method put
expose rate next target arr
use arg i x
arr[i] = x

::class Ratio

::method gdenom
expose denom
return denom

::method gnumer
expose numer
return numer


::method Ratio
expose numer denom
use arg n,d
numer = n
denom = d
g = gcd(numer, denom)
numer = numer / g
denom = denom / g

gcd:
parse arg a, b
if b = 0 then return a
return gcd( b, a // b )

::method divideBy
expose numer denom
use arg r
numer = r.denom * numer
denom = r.numer * denom
g = gcd(numer, denom)
numer = numer / g
denom = denom / g
return self

gcd:
parse arg a, b
if b = 0 then return a
return gcd( b, a // b )

::method multiplyBy
expose numer denom
use arg r
numer = r.numer * numer
denom = r.denom * denom
g = gcd(numer, denom)
numer = numer / g
denom = denom / g
return self
gcd:
parse arg a, b
if b = 0 then return a
return gcd( b, a // b )

My current output is ? Papaya = ? mango should be 2 papaya = 9 mango
 
 
 

oorexx homework assignment having issues

Post by Sahanand » Fri, 20 Mar 2009 16:02:27

I don't want to do your homework for you, but a few things which it
might help if you bore in mind are:

The message operator in ooRexx is '~' whereas '.' can be used to
denote compound variables (aka stems)
self is a special variable pointing to the current instance.
not equals is indicated by '\=' or '<>' whereas '!' is not an operator
in ooRexx
The new class method always runs the instance init method if it
exists, and arguments to one are passed to the other.

hth

 
 
 

oorexx homework assignment having issues

Post by bhawkins41 » Fri, 20 Mar 2009 17:24:46

n Mar 19, 2:02m, Sahananda < XXXX@XXXXX.COM > wrote:

Thanks for the pointers have made some changes and its starting to
work better but ... I have a bug in it that I can not seem to solve,
it works great when you are comparing something that has been stated
but when it has to trade an item for another to make the exchange
happen as in the example in my program it does not work. It also fails
if there is no match to the exchange rate. This is the first time I
have ever seen rexx or used it so that is why I am having issues.
Thanks again for your help

Newest Code:

say 'Project 1 reading file...'

T = .Trader~new
T~assert(63, mango, 14, papaya)
T~query(papaya, mango)
T~assert(5, passionfruit, 31, guava)
T~query(guava, mango)
T~assert(1, thermonuclearwarhead, 3, secretrockethovercar)
T~assert(2, thermonuclearwarhead, 4, armoredcommandotruck)
T~query(armoredcommandotruck, secretrockethovercar)





::class Trader
::method Init
expose taga adjList visited
taga = .Tagger~new
adjList = .Node~new
visited = .Array~new

::method assert
expose taga adjList visited
use arg m, thing1, n, thing2
tag1 = taga~tag(thing1)
tag2 = taga~tag(thing2)

adjList~put(tag1, .Node~new~Node(.Ratio~new~Ratio(n, m), tag2,
adjList~get(tag1)))
adjList~put(tag2, .Node~new~Node(.Ratio~new~Ratio(m, n), tag1,
adjList~get(tag2)))

::method query
expose taga adjList visited
use arg thing1, thing2
do i = 1 to 60
visited[i] = 0
end

if dfs(taga~tag(thing1), .Ratio~new~Ratio(1,1), taga~tag(thing2)) then
do
say r~gnumer thing1 '=' r~gdenom thing2
exit
end
else do
say '? ' thing1 ' = ? ' thing2
exit
end

dfs:
use arg i, r, j
i = i - 2
j = j - 2
if i == j
then
return 1
else
visited[i] = 1
p = adjList~get(i)
do while p \= nil
if \ visited[p~gtarget]
then
if dfs(p~gtarget, r~multiplyBy(p~rate), j)
then
return 1
else
r~divideBy(p~rate)


p = p~next
end
return 0



::class Tagger
::method Init
expose name count
name = .Array~new
count = 0

::method tag
expose name count
use arg s
if name~hasItem(s) then return name~index(s)
else
count = count + 1
return count

::class Node
::method Init
expose arr
arr = .array~new

::method Node
expose rate next target
use arg rate, target, next
rate = rate
target = target
next = next
--say rate target next
return self


::method gtarget
expose target
return target

::method get
expose rate next target arr
use arg i
a = arr[i]
return a

::method put
expose rate next target arr
use arg i x
arr[i] = x
::method rate
expose rate
return rate

::class Ratio

::method gdenom
expose denom
return denom

::method gnumer
expose numer
return numer


::method Ratio
expose numer denom
use arg n,d
numer = n
denom = d
g = gcd(numer, denom)
numer = numer / g
denom = denom / g
return self

gcd:
parse arg a, b
if b = 0 then return a
return gcd( b, a // b )


::method divideBy
expose numer denom
use arg r
numer = r~gdenom * numer
denom = r~gnumer * denom
g = gcd(numer, denom)
numer = numer / g
denom = denom / g
return self

gcd:
parse arg a, b
if b = 0 then return a
return gcd( b, a // b )

::method multiplyBy
expose numer denom
use arg r
numer = r~gnumer * numer
denom = r~gdenom * denom
g = gcd(numer, denom)
numer = numer / g
denom = denom / g
return self
gcd:
parse arg a, b
if b = 0 then return a
return gcd( b, a // b )

outputs s
 
 
 

oorexx homework assignment having issues

Post by bhawkins41 » Sat, 21 Mar 2009 08:12:48

have one error left if someone could help spot it, everything works
but the one case
of 2 armoredcommandotruck = 3 secretrockethovercar. my homework is due
today so any help would be appreciated

Thanks
Correct output is:

2 papaya = 9 mango
1 mango = 1 mango
? guava = ? mango
3 armoredcommandotruck = 2 secretrockethovercar

I have as my output:

2 papaya = 9 mango
1 mango = 1 mango
? guava = ? mango
? armoredcommandotruck = ? secretrockethovercar

Code:

say 'Project 1 reading file...'

T = .Trader~new
T~assert(63, mango, 14, papaya)
T~query(papaya, mango)
T~query(mango, mango)
T~assert(5, passionfruit, 31, guava)
T~query(guava, mango)
T~assert(1, thermonuclearwarhead, 3, secretrockethovercar)
T~assert(2, thermonuclearwarhead, 4, armoredcommandotruck)
T~query(armoredcommandotruck, secretrockethovercar)





::class Trader
::method Init
expose taga adjList visited
taga = .Tagger~new
adjList = .Node~new
visited = .Array~new

::method assert
expose taga adjList visited
use arg m, thing1, n, thing2
tag1 = taga~tag(thing1)
taga~incount
tag2 = taga~tag(thing2)
taga~incount
--say tag1 tag2
adjList~put(tag1, adjList~Node(.Ratio~new~Ratio(n, m), tag2,
adjList~get(tag1)))
adjList~put(tag2, adjList~Node(.Ratio~new~Ratio(m, n), tag1,
adjList~get(tag2)))

::method query
expose taga adjList visited
use arg thing1, thing2
do i = 1 to 60
visited~put(0, i)
end
r = .Ratio~new~Ratio(1,1)
if self~dfs(taga~tag(thing1), r, taga~tag(thing2))
then
say r~gdenom thing1 '=' r~gnumer thing2

else do
say '?' thing1 '= ?' thing2
--taga~returnim
end

::method dfs
expose taga adjList visited
use arg i, r, j
if i == j
then
return 1
else
visited[i] = 1
p = adjList~get(i)
--say p
do while p \= .nil
if \ visited[p~gtarget]
then do
if self~dfs(p~gtarget, r~multiplyBy(p~grate), j)
then
return 1
else
r~divideBy(p~grate)

end
p = p~gnext
end
return 0

::class Tagger
::method Init
expose name count
name = .Array~new
count = 1

::method incount
expose count
count = count + 1

::method tag
expose name count
use arg s
if name~hasItem(s)
then do
a = name~index(s)
return a
end
else do
name[count] = s
--say 'count' count s
return count
end
::method returnim
expose name
do as over name~allItems
say as
end



::class Node
::method Init
expose arr
arr = .array~new

::method Node
expose rate next target
use arg r, t, n
rate = r
target = t
next = n
--say next
return self

::method gnext
expose next
--say next
return next

::method gtarget
expose target
--say target
return target

::method get
expose rate next target arr
use arg i
a = arr[i]
return a

::method put
expose rate next target arr
use arg i x
arr[i] = x
::method grate
expose rate
return rate

::class Ratio

::method gdenom
expose denom
return denom

::method gnumer
expose numer
return numer


::method Ratio
expose numer denom
use arg n,d
numer = n
denom = d
g = gcd(numer, denom)
numer = numer / g
denom = denom / g
return self

gcd:
parse arg a, b
if b = 0 then return a
return gcd( b, a // b )


::method divideBy
expose numer denom
use arg r
numer = r~gdenom * numer
denom = r~gnumer * denom
g = gcd(numer, denom)
numer = numer / g
denom = denom / g
return self

gcd:
parse arg a, b
if b = 0 then return a
return gcd( b, a // b )

::method multiplyBy
expose numer denom
 
 
 

oorexx homework assignment having issues

Post by Sahanand » Sat, 21 Mar 2009 16:15:45

I see that you have used the say command to try debugging what is
going on. DO you know about trace and interactive trace.
If you put trace ?i in your code where you need to debug (which looks
to me likely to be the dfs method of your trader class) then the
interpreter will stop at each line, and you can enter commands that
will act on the current contents of all variables etc. so you could
type something like

say p

or

do index = 1 to visited~items ; say index':'visited[index] ; end

good luck with it.