This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zestix
Zestix wrote ...

2020/3/20

Help with recursion

Zestix Zestix

2020/3/20

#
Hello, I've been trying to use a recursive method to decode morse, and I consistently get some sort of error message. The last time I got a StackOverFlow error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
private String getLetter(String pCode, BinaryTree<String> pTree)
    {
        int loop1 = 0;
        int loop2 = 0;
        BinaryTree<String> tree = morsetree;
        if (!pCode.isEmpty())
        {
            if (loop2 <= pCode.length())
            {
                while (pCode.substring(loop, loop+1) == " ")
                {
                    loop2++;
                }
            }
            if (loop2 > 5)
            {
                return null;
            }
            for (int i=0;i<loop2;i++)
            {
                if (pCode.substring(i, i+1) == "-")
                {
                    tree = tree.getRightTree();
                }
                else
                {
                    tree = tree.getLeftTree();
                }
            }
            System.out.println(tree.getContent());
            return getLetter(pCode.substring(loop2, pCode.length())
                                                           ,morsetree);
        }
        return null;
    }
danpost danpost

2020/3/21

#
Lines 6 and 34 (and 17) do not seem to be consistent with each other. That is -- null is not the same as isEmpty.
danpost danpost

2020/3/21

#
Where can I find the BinaryTree class?
Zestix Zestix

2020/3/21

#
danpost wrote...
Where can I find the BinaryTree class?
It's quite a bit of coding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
 * Beschreiben Sie hier die Klasse List.
 *
 * @author (Ihr Name)
 * @version (eine Versionsnummer oder ein Datum)
 */
public class List<Zahl>
{
    int rep = 0;
    int help = 0;
    boolean sorted = false;
    ListNode p;
    public class ListNode
    {
        ListNode next;
        private ListNode first;
        private ListNode last;
        private ListNode current;
        public Zahl content;
        public ListNode(Zahl pContent)
        {
            content = pContent;
        }
 
        public void setNext(ListNode pNode)
        {
            next = pNode;
        }
 
        public ListNode getNext()
        {
            return next;
        }
 
        public Zahl getContent()
        {
            return content;
        }
 
        public void setFirst(ListNode pNode)
        {
            first = pNode;
        }
 
        public void setLast(ListNode pNode)
        {
            last = pNode;
        }
    }
    private ListNode first;
    private ListNode last;
    private ListNode current;
    public List()
    {
    }
 
    public void toFirst()
    {
        current = first;
    }
 
    public void toLast()
    {
        current = last;
    }
 
 
    public void next()
    {
        toFirst();
        if (!isEmpty())
        {
            if (current.getNext() == null || current == null)
            {
                toFirst();
            }
            else
            {
                current = current.getNext();
            }
            if (!hasAccess())
            {
                toFirst();
            }
            else if (hasAccess())
            {
            }
        }
    }
 
    public Zahl getContent()
    {
        if (!isEmpty())
        {
            if (hasAccess())
            {
                return current.content;
            }
        }
        return null;
    }
 
    public boolean isEmpty()
    {
        if (first == null)
        {
            return true;
        }
        return false;
    }
 
    public boolean hasAccess()
    {
        if (current != null)
        {
            return true;
        }
        return false;
    }
 
    public void fcol()
    {
        if (!hasAccess())
        {
            toFirst();
        }
    }
 
    public void append(Zahl pContent)
    {
        ListNode pNode = new ListNode(pContent);
        if (!isEmpty())
        {
            last.setNext(pNode);
            last = pNode;
        }
        else
        {
            first = pNode;
            last = pNode;
            current = pNode;
        }
    }
 
    public void setContent(Zahl pContent)
    {
        Zahl smth = getContent();
        smth = pContent;
    }
     
    public void test()
    {
        System.out.println("AHHHHHHHHHHH");
    }
 
    public void concat (List<Zahl> pList)
    {
        if (pList.hasAccess())
        {
            if (pList != null)
            {
                while (!pList.isEmpty())
                {
                    pList.toFirst();
                    append(pList.getContent());
                    pList.remove();
                }
            }
        }
    }
 
    public void aid()
    {
        ListNode aid;
        aid = current;
        toFirst();
        while (hasAccess())
        {
            System.out.println(""+current.getContent());
            next();
        }
    }
 
    public void swap()
    {
        ListNode p;
        p = current;
        current = current.getNext();
        next();
        current = p;
    }
 
    public void remove()
    {
        ListNode Maverick;
        Maverick = current;
        if (hasAccess())
        {
            toFirst();
            if (current == Maverick)
            {
                first = null;
                if (current.getNext() == null)
                {
                    last = null;
                    current = null;
                }
                current = null;
            }
            else if (current.getNext() == Maverick)
            {
                first = last;
                current = last;
            }
            else
            {
                while (current.getNext() != Maverick)
                {
                    if (current.getNext() == null)
                    {
                        toFirst();
                    }
                    next();
                }
                Maverick = current;
                next();
                next();
                Maverick.setNext(current);
            }
        }
    }
 
    public ListNode getCurrent()
    {
        return current;
    }
 
    public void insert(Zahl pContent)
    {
        ListNode Maverick;
        Maverick = current;
        ListNode pNode = new ListNode(pContent);
        if (!hasAccess())
        {
            if (first == null)
            {
                first = pNode;
                last = pNode;
                current = pNode;
            }
        }
        if (hasAccess())
        {
            toFirst();
            if (current == Maverick)
            {
                pNode.setNext(first);
                first = pNode;
            }
            else
            {
                while (current.getNext() != Maverick)
                {
                    if (current.getNext() == null)
                    {
                        toFirst();
                    }
                    next();
                }
                pNode.setNext(Maverick);
                current.setNext(pNode);
                Maverick = current;
                next();
                next();
                Maverick.setNext(current);
            }
        }
    }
}
Zestix Zestix

2020/3/21

#
You can generally disregard the last 200 lines, though
danpost danpost

2020/3/21

#
Zestix wrote...
It's quite a bit of coding << Code Omitted >>
That looks to be a List class, not a BinaryTree class.
Zestix Zestix

2020/3/22

#
danpost wrote...
Zestix wrote...
It's quite a bit of coding << Code Omitted >>
That looks to be a List class, not a BinaryTree class.
That is a List class. I'm sorry for making it confusing, but I was using the List to create a BinaryTree
Zestix Zestix

2020/3/22

#
Zestix wrote...
danpost wrote...
Zestix wrote...
It's quite a bit of coding << Code Omitted >>
That looks to be a List class, not a BinaryTree class.
That is a List class. I'm sorry for making it confusing, but I was using the List to create a BinaryTree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
BinaryTree<String> left = new BinaryTree<String>("h");
        BinaryTree<String> right = new BinaryTree<String>("v");
        BinaryTree<String> tree1 = new BinaryTree<String>("s",left,right);
        left = new BinaryTree<String>("f");
        right = new BinaryTree<String>("ü");
        BinaryTree<String> tree2 = new BinaryTree<String>("u",left,right);
        left = new BinaryTree<String>("l");
        right = new BinaryTree<String>("ä");
        BinaryTree<String> tree3 = new BinaryTree<String>("r",left,right);
        left = new BinaryTree<String>("p");
        right = new BinaryTree<String>("j");
        BinaryTree<String> tree4 = new BinaryTree<String>("w",left,right);
        left = new BinaryTree<String>("b");
        right = new BinaryTree<String>("x");
        BinaryTree<String> tree5 = new BinaryTree<String>("d",left,right);
        left = new BinaryTree<String>("c");
        right = new BinaryTree<String>("y");
        BinaryTree<String> tree6 = new BinaryTree<String>("k",left,right);
        left = new BinaryTree<String>("z");
        right = new BinaryTree<String>("q");
        BinaryTree<String> tree7 = new BinaryTree<String>("g",left,right);
        left = new BinaryTree<String>("ö");
        right = new BinaryTree<String>("ch");
        BinaryTree<String> tree8 = new BinaryTree<String>("o",left,right);
        tree1 = new BinaryTree<String>("i",tree1,tree2);
        tree2 = new BinaryTree<String>("a",tree3,tree4);
        tree3 = new BinaryTree<String>("n",tree5,tree6);
        tree4 = new BinaryTree<String>("m",tree7,tree8);
        tree1 = new BinaryTree<String>("e",tree1,tree2);
        tree2 = new BinaryTree<String>("t",tree3,tree4);
        morsetree = new BinaryTree<String>("#",tree1,tree2);
Here's the morsetree itself
danpost danpost

2020/3/22

#
Zestix wrote...
That is a List class. I'm sorry for making it confusing, but I was using the List to create a BinaryTree
Yes -- but you are using BinaryTree as a class name. Where is that class?
Zestix Zestix

2020/3/22

#
danpost wrote...
Zestix wrote...
That is a List class. I'm sorry for making it confusing, but I was using the List to create a BinaryTree
Yes -- but you are using BinaryTree as a class name. Where is that class?
What I sent was the BinaryTree class. The List class was just renamed to that - it has the same methods and coding
danpost danpost

2020/3/22

#
Zestix wrote...
What I sent was the BinaryTree class. The List class was just renamed to that - it has the same methods and coding
Then, where is the constructor with a String parameter -- with the signature: List(String) or the other one: List(String, List, List) ?
Zestix Zestix

2020/3/22

#
danpost wrote...
Zestix wrote...
What I sent was the BinaryTree class. The List class was just renamed to that - it has the same methods and coding
Then, where is the constructor with a String parameter -- with the signature: List(String) or the other one: List(String, List, List) ?
It's in line 7. I had been using Zahl for a different project, which is why it's there. It's alright, though, I'm currently inept. Thanks for trying to help me despite the trouble, I'll pick up the project some other day. Take care
danpost danpost

2020/3/22

#
Zestix wrote...
It's in line 7. I had been using Zahl for a different project, which is why it's there.
Line 7 is a class header line -- not a constructor header.
Zestix Zestix

2020/3/22

#
danpost wrote...
Zestix wrote...
It's in line 7. I had been using Zahl for a different project, which is why it's there.
Line 7 is a class header line -- not a constructor header.
Pardon, it's in line 20. No need to help, though
danpost danpost

2020/3/22

#
Zestix wrote...
Pardon, it's in line 20. No need to help, though
Oh. So, ListNode is the BinaryTree class -- not List. I still do not see a 2nd constructor, however. Good luck.
You need to login to post a reply.