Results 1 to 20 of 25
Thread: Recursion
- 12-23-2011, 01:07 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Recursion
Hello i'm new to this forum, so i will introduce me a bit at first. My name is Kassandra and i live in the netherlands but im originally Danish. I learning to program in Java, I am now busy with programming with recursion. The goal is this: I want an applet with 4 boxes. in the first you put a number. in the second you put the Numeral system of the first number. and in the third you put the numeral system which you want to convert the number (you did put in the first box ) in. The calculation has to be on a recursive way. the applet what i made is this code:
But how do you convert the number in a recursive way?? to convert an int to char and backwards i have made this code:Java Code:import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.JTextField; import java.awt.Rectangle; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.Font; public class Applet1 extends Applet { public Applet1() { } public void init() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { jTFGetal.setBounds(new Rectangle(30, 50, 300, 19)); this.setLayout(null); jTFTT1.setBounds(new Rectangle(30, 93, 35, 19)); jTFTT2.setBounds(new Rectangle(30, 136, 35, 19)); jTFUitvoer.setBounds(new Rectangle(30, 236, 300, 19)); jLbl.setText("the number you want to convert:"); jLbl.setBounds(new Rectangle(30, 34, 173, 14)); jLbl2.setText("numeral system of the number:"); jLbl2.setBounds(new Rectangle(30, 77, 176, 14)); jLlb3.setText("numeral system you want to change it in:"); jLlb3.setBounds(new Rectangle(30, 123, 230, 14)); jLabel1.setText("answer:"); jLabel1.setBounds(new Rectangle(30, 221, 94, 14)); jBtnOK.setBounds(new Rectangle(30, 188, 77, 25)); jBtnOK.setText("Run"); jBtnOK.addActionListener(new Applet1_jBtnOK_actionAdapter(this)); this.add(jTFTT1); this.add(jTFTT2); this.add(jTFGetal); this.add(jLbl); this.add(jLbl2); this.add(jLlb3); this.add(jTFAnswer); this.add(jLabel1); this.add(jBtnOK); } JTextField jTFGetal = new JTextField(); JTextField jTFTT1 = new JTextField(); JTextField jTFTT2 = new JTextField(); JTextField jTFAnswer = new JTextField(); JLabel jLbl = new JLabel(); JLabel jLbl2 = new JLabel(); JLabel jLlb3 = new JLabel(); JLabel jLabel1 = new JLabel(); JButton jBtnOK = new JButton();
I hope there are some people who can help me with my problem.Java Code:private int charToInt(char c) { if (c >'9') { c &= ~('A' ^ 'a'); } return (c>'9')? c-'A'+10 : c-'0'; // A-Z = 10-36 : 0-9 = 0-9 } private char intToChar(int i) { if (i >= 0 && i <= 9) return (char) (i + '0'); if (i >= 10 && i <= 35) return (char) (i - 10 + 'A'); else return 0; }
thanks alot.
greetings Kas
- 12-23-2011, 01:42 AM #2
Re: Recursion
What is the valid range of values in the int variable? Can it be > 9? If so, you will need an array of char to hold the converted value.to convert an int to char
What are the valid char values that you want to convert to an int? '0' to '9'?
Since char values can be treated as numeric types, you can subtract '0' from the char to get the int value.
- 12-23-2011, 12:42 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: Recursion
the range is bigger then 0 - 9. its possible to put a number like B5 in it.
I do substract the 0 from it right?or isnt this what you mean?Java Code:return (c>'9')? c-'A'+10 : c-'0'; // A-Z = 10-36 : 0-9 = 0-9
- 12-23-2011, 01:19 PM #4
Re: Recursion
What kind of number is that? What would be its int value?a number like B5
- 12-23-2011, 02:23 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: Recursion
B5 with numeral system 16( hex) should give.. 181 ( if you want numeralsystem 10 ( decimal) )
B is int 66?? or am i wrong?
- 12-23-2011, 02:29 PM #6
Re: Recursion
You need to define what the input is going to be first.
For example: what base(s) for the input: decimal, binary, hexadecimal, octal, etc
And is there a limit on the number of characters that can be input?
Are you referring to the ASCII value of the char 'B'?B is int 66
- 12-23-2011, 02:55 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: Recursion
yes but thats what it has to do. put the numeral system in the box that defines if it is a decimal, binary etc. but i dont know how i make that.
yes, isn't that what the int is? the ascii value of the character?Are you referring to the ASCII value of the char 'B'?
- 12-23-2011, 04:15 PM #8
Re: Recursion
It all depends on what you are doing.isn't that what the int is? the ascii value of the character?
If the String is supposed to contain hexadecimal digits, then 'b' or 'B' is 11.
- 01-03-2012, 01:48 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: Recursion
okay i've changed my program and made a recursive way to calculate the answer. But now i cant get the answer on my panel
what do i have to put in at line: 46. GetalUit.setText(??);
Java Code:package week4; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Invoerpaneel extends JPanel { private JTextField Stelsel1, Stelsel2, GetalIn, GetalUit; public Invoerpaneel(){ setLayout( new GridLayout(5, 2)); Stelsel1 = new JTextField (10); Stelsel2 = new JTextField (10); GetalIn = new JTextField (10); GetalUit = new JTextField (10); GetalUit.setEditable(false); GetalUit.setBackground( Color.green); add(new JLabel("Stelsel invoer")); add(Stelsel1); add(new JLabel("Stelsel uitvoer")); add(Stelsel2); add(new JLabel("Getal")); add(GetalIn); add(new JLabel("Uitkomst")); add(GetalUit); JButton berekenknop = new JButton("Bereken"); berekenknop.addActionListener(new KnopHandler()); add( new JLabel()); // leeg vak add(berekenknop); } private class KnopHandler implements ActionListener{ public void actionPerformed(ActionEvent e) { GetalUit.setText(uitkomst); class Omrekening { public int recuForw(int inInt, String inStr) { int length = inStr.length(); char c = inStr.charAt(0); int temp = charToInt(c); if (length > 1) return (temp*(int) Math.pow(inInt, (length-1)))+recuForw (inInt, inStr.substring(1, length)); else return temp; } public String recuBackw(int invoer, int getalstlsl){ int uitkomst = invoer % getalstlsl; invoer /= getalstlsl; if (invoer != 0) return recuBackw(invoer, getalstlsl) + intToChar(uitkomst); else return Character.toString(intToChar(uitkomst)); } private int charToInt(char c) { // omrekening van de de character naar een Int if (c >'9'){ // als c is groter dan 9 c &= ~('A'^'a'); // maakt alles kleine letter } if (c>'9') return (c-'A'+10); else return (c-'0'); } private char intToChar(int i){ if (i >= 0 && i <= 9) return (char) (i+'0'); if (i >= 10 && i <= 35) return (char) (i-10+'A'); else return 0; } } } } }
- 01-03-2012, 01:51 PM #10
Re: Recursion
Do you get any errors? Please copy and paste here the full text of the error messages.
- 01-03-2012, 03:40 PM #11
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: Recursion
o sorry , When i run i dont get an error. but when i push the button to generate the answer i got this:
Exception in thread "AWT-EventQueue-1" java.lang.Error: Unresolved compilation problems:
Syntax error on token "int", delete this token
i cannot be resolved to a variable
Syntax error on token "char", delete this token
c cannot be resolved to a variable
at week4.Invoerpaneel$KnopHandler.actionPerformed(Inv oerpaneel.java:46)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 01-03-2012, 03:44 PM #12
Re: Recursion
Can you compile the program and get the errors from the compiler. Your IDE(???) is hiding the compilation errors.
The compiler will show the source line with the error and place a ^ underneath the error.
Here is a sample:
Java Code:TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^
- 01-03-2012, 04:21 PM #13
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: Recursion
this error is in the compiler:
The method setText(String) in the type JTextComponent is not applicable for the arguments () Invoerpaneel.java /Recursie/src/week4 line 46 Java Problem
btw. this part is also a class who has to be in the same project.Java Code:package week4; import javax.swing.JApplet; public class Display extends JApplet{ public void init(){ setContentPane( new Invoerpaneel()); } }
- 01-03-2012, 04:29 PM #14
Re: Recursion
I'm not sure I understand your post.
Where is the source statement where the error occurred?
The error message mentions the setText() method, but I do not see that method used in the code you just posted.
The message could be saying that you did NOT give the setText() method a String argument, which is required.
- 01-12-2012, 12:52 PM #15
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: Recursion
I seperated the two classes. one with the panel ( invoerpaneel) and the calculation ( bereken) . Now I have another problem.
I uses JTextfield to get the input. but this is a string?? i want to convert those into Integers. whats the best way to do that?
Java Code:package week4; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Invoerpaneel extends JPanel { private JTextField Stelsel1, Stelsel2, GetalIn, GetalUit; Bereken recCal; public Invoerpaneel(){ setLayout( new GridLayout(5, 2)); Stelsel1 = new JTextField (10); // int stlsl1 = Integer.parseInt(Stelsel1); // Dont work Stelsel2 = new JTextField (10); GetalIn = new JTextField (10); GetalUit = new JTextField (10); GetalUit.setEditable(false); GetalUit.setBackground( Color.green); recCal = new Bereken(); add(new JLabel("Stelsel invoer")); add(Stelsel1); add(new JLabel("Stelsel uitvoer")); add(Stelsel2); add(new JLabel("Getal")); add(GetalIn); add(new JLabel("Uitkomst")); add(GetalUit); JButton berekenknop = new JButton("Bereken"); berekenknop.addActionListener(new KnopHandler()); add( new JLabel()); // leeg vak add(berekenknop); } public class KnopHandler implements ActionListener { public void actionPerformed(ActionEvent e) { // int stlsl1 = Stelsel1.getInt(); int stlsl2 = Stelsel2.getInt(); int getalIn = GetalIn.getInt(); GetalUit.clear(); int GetalInInt = recCal.recuForw(stlsl1, getalIn); String uitkomst = recCal.recuBackw(GetalInInt, stlsl2); GetalUit.setText(uitkomst); } } }Java Code:package week4; public class Bereken { public int recuForw(int inInt, String inStr) { int length = inStr.length(); char c = inStr.charAt(0); int temp = charToInt(c); if (length > 1) return (temp*(int) Math.pow(inInt, (length-1)))+recuForw(inInt, inStr.substring(1, length)); else return temp; } public String recuBackw(int GetalIn, int tlstlsl){ int uitkomst = GetalIn % tlstlsl; GetalIn /= tlstlsl; if (GetalIn != 0) return recuBackw(GetalIn, tlstlsl) + intToChar(uitkomst); else return Character.toString(intToChar(uitkomst)); } private int charToInt(char c) { // omrekening van de de character naar een Int if (c >'9'){ // als c is groter dan 9 c &= ~('A'^'a'); // maakt alles kleine letter } if (c>'9') return (c-'A'+10); else return (c-'0'); } private char intToChar(int i){ // int to char if (i >= 0 && i <= 9) return (char) (i+'0'); if (i >= 10 && i <= 35) return (char) (i-10+'A'); else return 0; } }
- 01-12-2012, 12:57 PM #16
Re: Recursion
Look at the Integer class. It has a method that you could use for that.convert those into Integers.
- 01-12-2012, 01:05 PM #17
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: Recursion
you mean the char to int ? can i use that for a string to int?
- 01-12-2012, 01:17 PM #18
Re: Recursion
Sorry, what method and class are you talking about?
- 01-12-2012, 01:23 PM #19
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: Recursion
i thought you meaned this method?
Java Code:private int charToInt(char c) { // omrekening van de de character naar een Int if (c >'9'){ // als c is groter dan 9 c &= ~('A'^'a'); // maakt alles kleine letter } if (c>'9') return (c-'A'+10); else return (c-'0'); }
- 01-12-2012, 01:26 PM #20
Similar Threads
-
Need help with recursion
By Playboytrey in forum New To JavaReplies: 2Last Post: 04-29-2011, 01:36 AM -
Recursion?
By Fingerz in forum New To JavaReplies: 10Last Post: 01-08-2011, 02:25 AM -
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM -
Recursion
By Mika in forum New To JavaReplies: 5Last Post: 01-04-2009, 01:13 AM -
recursion
By kdeighan in forum New To JavaReplies: 3Last Post: 01-25-2008, 09:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks