I have created following class to store the given XML structure class Item { String name; List- items; public List
- getItems() { return items; } } XML Structure:
-
Now,I am trying to search "ITEM-A2" using the following code public Item getItemByName(String name) Item root=; List- ...
|
I want to convert this mutual recursion prog into direct recursion. Any Help how to do this class Arec { int a; int b; static long h(int k) { a++; if (k == 0) return 0; else return h(k - 1) + i(k - 1); } static long i(int k) { b++; if (k == 0) return 1; else return h(k) ... |
Hello, I am trying to iterate through all the windows registry keys and values. This is what I have so far but it only seems to iterate one level deep. Im using JNI to access the windows registry - ICE_JNIRegistry.dll. Thanks for any help. public void visitAllKeys(RegistryKey key) { if(key != null){ System.out.println(key.getName()); try{ Enumeration children = key.keyElements(); while(children.hasMoreElements()){ child = ... |
Hi, I have the following 1) a list of strings. These strings are related to each other as parent and children. For e.g, if my list contains a,b,c,d, then a is parent of c and b, c is parent of d. each child can have only one parent. I need to write logic to pass this list in an iterator and ... |
Hi All, I am trying to encode recursion using Java. I would like to know an efficient approch where I can bring down the nummber of variables being passed as method parameters. - Without adding to the Method Call Stack - I basically would like to come up with an approach which would make use of least number of local variables ... |
Hi, I have a question on recursion. It goes this way: Suppose that were running a recursive algorithm where n is initially 16. Suppose that the base case is n == 1. At each recursive step, two recursive method invocations are made with n/2. a. How many total method invocations are needed (including the initial invocation)? b. What are the most ... |
Hi, I'm having trouble getting my anagram method to work. I have to solve it recursively. The explanation of the problem is in the comment section in the beginning of my code. I can't figure out why it reads false for everything I try to test it with. Can anyone help me point out my mistake? Any guidance or advice would ... |
|
Hello, I'm having trouble with a recursive number problem. Here is the problem: Write a static method printRange that takes integer parameters x and y and that prints the sequential integers between x and y inclusive. The first half of the list should be printed with the greater-than character (">") separating consecutive values. The second half should be printed with the ... |
Hi, I am trying to write a recursive method for determining if a string is palindromic. I have the code as follows: String S = "radar"; int left = 0; int right = S.length()-1; public static void palindrome (int left, int right) { if (left < right || left==right) { if (S.charAt(left)==(S.charAt(right))) palindrome (left+1, right-1); } } I tried setting the ... |
http://cse.unl.edu/~dsadofs/RecursionTutorial/index.php?s=algorithms#recfactalg fact(non-negative integer n) { if (n==0) { return 1; } else { return fact(n-1)*n; } } Well, as stated in the description of a recursive definition, a base case and inductive rule were needed. In this case, fact(0)=1 is the base case. This is a logical place to start, seeing as how the n in fact(n) must be larger than ... |
Hello there, This is my first post at this forum, always an exciting moment. As part of my study I need to learn (the basics i guess) Java, and thats fantastic. I have to work through Headfirst Java and a some other reader. The first 4 chapters of Headfirst went ok, it was hard but finally I understood what they are ... |
Hi! I've been struggling to avoid recursion in this method, but with no luck... Any ideas? If you need a more detailed description of the problem, let me know, but the method is fully functional right now.. I just want to speed it up public static FIList positions(String word, int index, Node rootNode){ FIList positionList = new FIList(); char charAtIndex = ... |
Recursion is when a method calls itself. So far so good, but I need a better vocabulary for discussing the details of recursion. There probably is one. Could you help me learn? E.g. when a method has been called recursivly a few times there will be several - eh- instances(?) of that method active? Or what would you call such an ... |
Your main() method doesn't have a return type declared.... you forgot the "void". [EDIT] Wasn't paying attention. It looks like this isn't the main method called by the JVM, but one that returns a String. So, I guess you need to declare that. [EDIT 2] BTW, I don't see any recursion here. Perhaps you didn't show us everything. And given the ... |
package exceptions; import java.util.InputMismatchException; import java.util.Scanner; public class ArithmeticExpApp { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter divident and diviser"); int y = 0; boolean success = false; while (!success) { try { System.out.println(1); int x = sc.nextInt(); System.out.println(2); y = sc.nextInt(); int i = x / y; System.out.println(i); success ... |
i'm having a problem with my code... i don't know what to do with it... it displays stuff like [{09gsd.... how can i make it display the array elements? thank you! public class MergeSort { private int[] list; public MergeSort(int[] listToSort) { list = listToSort; } public int[] getList() { return list; } public void sort() { list = sort(list); } ... |
i triedx another recursion exercise... but unfortunately... the program runs.. until the end of the inputs... it displays nullpointerexception... its the first time i've encountered this and i just want to know... because i don't know how to handle a nullPointer exception... thanks in advance!!! here it is... import javax.swing.*; public class mergesort3 { public static void main (String args[]) { ... |
Hi, I just started learning recursion, and I am just a bit confused. The following is a simple example that tries to find the area of a triangle that is made of brackets. For example: [ ] [ ] [ ] [ ] [ ] [ ] ....... the width of each bracket is 1. The code is that given a ... |
public static LN insertOrdered (LN l, int value) { if (l == null || value < l.value) return new LN(value,l); else { l.next = insertOrdered(l.next, value); return l; } } // My effort at making the above method void public void insertOrdered (LN l, int value) { if (l == null || value < l.value) l = new LN(value,l); else { ... |
Hey everyone, I've got a question where I'm sort of stuck on writing a java code for a recursive "school method" multiplication where we have input 'n' and two n digit numbers 'a' and 'b' where the output is m=a.b. I also have to use an array representation for representing numbers. Could anyone please help me out? Thanks The basic format ... |
In your example, b is the base of the exponent, so it has to remain the same. It's the result coming from powerOf which changes in each recursion. By definition, power-of is b * (b * (b * (b *(...(1)))). The bit inside the brackets is what's returned by the method each time. Any clearer? You could also stick a statement ... |
I have a following code. The code is to replace the brackets from the formula. private void processBrackets(String formula){ int startingBracket=0; int endingBracket=0; boolean found=false; String procesedFormula=""; char formulaArray[] = formula.toCharArray(); for(int i=0;i |
Hello guys, I making a quadtree from a matrix. Please find attached with this post a sample image of how it is done. I ve used recursion but not able to get to the final result. public quadtree make_quadtree(int a, int b, int c, int d) { //a,b are starting indexes of the matix and c,d are the ending ones. //link_matrix[m][n] ... |
From TopCoder TCHS SRM 21 DIV 1 Problem Statement The students at Byteland Institute of Technology are registering electronically for classes. Registration is open for a predetermined number of time units, and each time unit is assigned a positive number called the goodness. Higher goodness values mean that the network is more likely to be available and responsive during those ... |
void move (int[]arr, int value )// move a released token { System.out.println("which token do u want moved"); int i,b; b = 0; for(i=0;i<4;i++) { if(arr[i]>-1 && arr[i] < 54) { System.out.println(i); } } try // checks for users who will enter a character { b = getsoldier(); } catch(Exception e) { e.printStackTrace(); System.out.println("type the value of the token want to move"); ... |
|
I was wondering if anyone had a good simple explanation of how to work out what the result is from a recursive method? For example the following produces 24 as the result when you pass in 4... (1 * 2 * 3 * 4 = 24) public static long factor(long arg){ if(arg == 1 || arg == 0){ return 1; } ... |
I'm working on creating an expression tree using recursion. It seems to be building the left side ok but whenever I try to setRight the showSub method does not print the < and prints a / meaning that both either left and right are equal to null and that right is equal to null. I am using the prefix expression *+24-34 ... |
The program requires an integer input from a user at the command args and prints out the numbers backward. I have this so far, however, it only prints 1 number, which is the number entered into args. Note: all printouts must occur in main method. Java Code: public class KashiwabaraNicole7 { /******************************************************************************* * Initializes program * @ param commandlineArguments ********************************************************************************/ public ... |
public static void main(String[] commandlineArguments) { if (commandlineArguments.length == 0) { System.out.print("Error: You must enter at least 1 commandline argument "); } else { Integer number = new Integer(0); //initialize number String word = commandlineArguments[1]; try // check to verify if input is an integer { number = Integer.parseInt(commandlineArguments[0]); } catch (NumberFormatException exception) // NumberFormatException { System.out.print(exception); System.out.println(" is not an ... |
public class FormPattern{ public static void main(String[] args){ //a String star = "*"; String [] starArray = newString[10]; for (int i = 0; i < 10; i++); starArray[i] = star; System.out.print(star); System.out.println(); star = star +"*"; } //create space between patterns System.out.println(); System.out.println(); //b for (int i = 0; i < 10; i++){ int j = 9 - i; System.out.print(starArray[j]); System.out.println(); ... |
Ok, so I wrote a program to count the number of vowels in a user inputted string. The program works great. However, I was wondering if there was a way to clean my recursion method up, trying to get things as simple as possible if this isn't it. Need to work on cleaning my code up. Here is the program: Java ... |
|
Hi all, Once again I am in need of help. I am working on a program for my data structures class and I am ready to pull my hair out. Its almost the end of my semester and I have several programs to complete with little help from my prof. The assignment is to write a program that displays images in ... |
I'm trying to use Recursive, I know how to use it and i understand it, but the problem i have is that i have to use 2 input variables, and I don't know how to mix them both. My program should take a number, add an exponential number (user inputed) and do the math. This program work, but i have no ... |
hello, i need some tuning about this issue, what is the difference in the structure of the recursions when the final result is computed? what is the difference in the nature of the base case (the case that is handled within the stop condition of the recursion). maybe some other differences you can think of... thanks, OP |
37. Recursion java-forums.orgI can't seem to figure out how to make the iterative method into a recursive method. The iterative method is basically the square root of x n times within each other. Here is my code. The iterative method works fine but the recursion method needs work. public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); ... |
Hi, I've been given an assignment to generate all the subsets of the argument passed in main(String[] args). The program must be recursive. I think I'm close, and as far as I can tell, it should work, but it doesn't:( Could you please help me iron out the kinks? Here's my code: Java Code: public class SubGenTakeTwo{ public static void main(String[] ... |
|
[COLOR="YellowGreen"]/* checks the number of boxes multiplied by WIDTH is greater than screen width * if false the box width remains unchanged, * if true the box width is reduced by 5 pixels and the method * is called recursively. */[/COLOR] private int checkBoxWidth(int boxes, int boxWidth) { if(boxes*boxWidth>getWidth()){ boxWidth-=5; checkBoxWidth(boxes,boxWidth); } return boxWidth; } |
To explain the "divide & conquer" trick a little better... int n>>>1 gives the same result as int n / 2, but does it in one machine instruction. By shifting all the bits one place to the right, you're dividing by 2 and throwing away the remainder. The line: Java Code: if ((n&1) == 1) return x*p; ...takes care of the ... |
hello I'm writing recursive function but I have problem with it the recursive functions should do the following: it does reordering according to criteria and writes results on file if the file is empty it should stop Java Code: static void Reordering_solution(FileWriter writer_rordering_out,File reordering){ try { for (int y=0; y |
My teacher gave me a program, which i have to add and edit to. I dont understand exactly how to do this. it looks like im supposed to determine if the amount of letters in a string is less than or equal to 1 at one point. how do i determine that, becuase if(pal <= 1) is not allowed it seems. ... |
Hey all. I'm not that new to Java, but it seems like a good topic for other beginners to learn as well. My question concerns recursive techniques for permutations. I lifted a code directly from a website (don't remember which) that permutes a string. My problem is, breaking out of the recursive function before it completes the cycle of all possibilities. ... |
hey, i have some computer knowladge from the past, but i had big problems to keep learning and sitting on the computer for long times, and i stopped at the first problem to understand or when something hard came by. i thought its not my area of interests, but i found this problem in other areas, i decided to try and ... |
This a simple path finding problem. When you have n bulbs construct 2^n vertices representing the position (on/off) of the bulbs. If you have eight bulbs you have 256 vertices. Each vertex has eight edges to other nodes representing a switch toggled, e.g. node 00000000 has an edge to node 00000011 if the rightmost switch is toggled. This makes an undirected ... |
Hi there, I'm currently having difficulties in implementing fully recursive methods (those which do not use loops at all) and was wondering if anyone can give me a hand since i can't seem to grasp this topic. the first method to implement is boolean allEqual(int[] a, int start, int end) that checks if all elements in the array a from start ... |
|
package base; import java.applet.*; import java.util.*; public class base extends Applet implements Runnable { { Scanner input = new Scanner(System.in); System.out.print("Enter number: "); int input_number = input.nextInt(); System.out.printf("%d factorial is %d", input_number, base.factorial(input_number) ); } public static long factorial( int n ) { if( n <= 0 ) return 1; else return n * factorial( n - 1 ); } } ... |
Java Code: public class Neper_number { static double factorial( int n ) { double result; if( n <= 0 ) return 1; else { result = 1 + (n * factorial(n - 1)); return result; } } } I am trying to do recursion in order to solve a problem. Since my original code is not working correctly (I am not ... |
|
Hey this is my 1st thread I have this class Java Code: class Vehicle { private int Aukswn; //Number of cars entered the parking area private String Pinakida; //The cars license plate number private String Type; // Car type private int Position; //The position the car is going to park public Vehicle() { Aukswn = 0; Pinakida = new String(); Type ... |
Hello Hello, I have confused myself quite a bit. I was asked to make a method, somewhat like the inbuilt pow method with the class Math, except it needed to output an int value rather than a double value. I actually found it fairly easy... public static int getPow(int number, int power){ if (power == 0){ return 1; } else{ return ... |
Hi all, I have a work to write about a recursion game. "matches" Game: on the table there is a pile of matches. Two players take from the stack, and off, a number of Matches at a time.The number of matches that can be taken at a time is set according to the rules of the game. The player who takes ... |
|
Hi I have this problem with a recursion method which supposed to convert numbers into roman numerals. I am giving a number, and the num variable I wrote should change by the length of the number, so if it's a 4digit number the given number will be divided by 1000 ,3digits /100.. . From some reason it does that for the ... |
Hi, could you please help me understand the following case. I wrote a method to convert numbers to roman numerals, I insert all the characters into a string variable but the output of the string comes out as the first one character. This is a recursive method and I know all the letters been inserted correctly as when I type system.out.print(roman), ... |
Hi, in this example I'm trying to return a string unsuccessfully. The input number is 3000 and the output should be MMM, I completely don't understand where I go wrong with this recursive method... -------------------------------------------------------------------------- public static String num2rom (int a){ String m = "M"; if (a == 0) { return ""; } else { if (a >=1000){ } return helper ... |
Hi all, Only been programming a few days now. Got to this section about recursion. Completely confused on how each pass throu the recursive method is being stored until its base case is reached and then calculates from the base case back to 5. With an argument of 5 being passed f.fact(5) class factorial{ int fact(int n){ int result; if(n==1) return ... |
I am not sure if you have to use arrays and if you have a particular format you need to follow, so I hope this helps. I have been messing with Iterators with collections and I know this way does what you are asking. Personally I haven't tried Iterator with Arrays but they definitely work with ArrayLists. Java Code: import java.util.ArrayList; ... |
Hello Everybody... Few questions... I am writing this program for school that has to implement both the recursive and dynamic way of editing distance between two strings. I have figured out the dynamic way (levenshtein distance) of editing distance but i am stuck with the recursive... i think it may be a simple little problem but any of your guys help ... |
Hi, the following program should take two parameters (array[],int number) and checks if the given number could be returned by summing or subtracting the objects in from the array. Ex. if number is: 31 and the array {5,22,13,5,7,-4}. Returns true because 22+13-4=31 . My code isn't finish I just want to know if I'm doing too much or should I go ... |
|
hi, trying to understand the recursion topic, specially with boolean results. Somehow I've managed to get along at the beginning of the following method but then it all turned confusing. I got to say that I didn't find a really good explanation source for boolean recursion on the web and since I would like to understand the logic behind it. I ... |
Hey everyone, I've got a question where I'm sort of stuck on writing a java code for a recursive "school method" multiplication where we have input 'n' and two n digit numbers 'a' and 'b' where the output is m=a.b. I also have to use an array representation for representing numbers. Could anyone please help me out? Thanks The basic format ... |
66. Recursion java-forums.org |
|
I need to create a change machine using recursion but the exact directions state "The method will display all combinations of quarters, dimes, nickels, and pennies that equal the desired amount." What that sounds like it's asking is for me to print out all possible coin combinations that could be used to give change. ie $1 = 100 pennies, 95 pennies ... |
first part does a spiral second part is in recursion but its not workin can anyone help // variables used for ends of lines int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0;; int offset = 5; // set initial starting values for line x1 = 200; // find center of width y1 = ... |
hello there, I need some help solving this mystery. I was asked to write a recursive method to draw the Sierpinski's Carpet using DrawingPanel. I have done all the work and studied it many times to figure what is wrong. Every thing is working perfectly it is just that i am having a little different drawing. the picture to the right ... |
hi ozzyman Thank you for your response...but i'm still blur... here my codes import java.util.Scanner; /** * Write a java class named reverseWords using recursion. * It takes a sentence and returns the sentence in reverse order. * For example, given "the cat is brown" as input, * your program should print out "brown is cat the". * Note that space ... |
static void drawSquare(int n, double x, double y, double size){ if(n <= 0){ // n is number of squares // I use predefined class StdDraw StdDraw.setPenColor(StdDraw.GRAY); StdDraw.filledSquare(x, y, size); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.square(x, y, size); } else{ // x0, y0, x1, y1 - coordinates of the squares, produced after each recursive step x0 = x/2; y0 = y/2; x1 = x + x/2; ... |
//define a List because you don't know how many items you need to store in it List powerSet = new ArrayList<>(); //define set to make powerSet from char[] aSet = new char[] {'a', 'b', 'c'}; //loop through each character //define a String to store full set String fullSet = ""; //add blank to List powerSet.add(fullSet); for (int i=0; i |
I am trying to draw a fractal tree using turtle graphics Java Graphics Turtle com.lrdev.turtle The algorithm is supposed to be: Draw the trunk Turn left x degrees Draw the left branch recurse ... draw the trunk ... Backup down the left branch to the trunk Turn right x+x degrees (to counter the left turn you have to turn double) Draw ... |
|
|
You have good logic but you are making a mistake. The reverse string is local to the method, meaning a new one is created each time a recursive call is made. Try changing the System.out.print(...) to a System.out.println(...) to highlight the problem more. You can keep reverse variable but it has to be declared elsewhere. Or you can probably change the ... |
|
Hi. I've got small problem with my recursive function. My task sounds like this: A builder wishes to build a (three-dimensional) pyramid. Each layer is comprised of square blocks. The top layer comprises a single block, the next layer comprises four blocks (2 x 2 blocks). The next expands so that each side is 3 blocks long. The central block is ... |
I don't really understand what you want your code to do exactly, but some aspects of it look a bit dodgy to me. For instance, did you really mean to call this.offspring.iterator() every time? You are going through the exact same loop with every recursion. Did you mean child.offspring.iterator() or something like that? |
the i in the calling method has nothing to do with the i in the called methoc. They just happen to have the same name. You have not used the return value of the method. Review method calls and how to store their return values. If Norm pops his head round the door, he may have a good reference. |
It seems unlikely to me that you could fill a whole book just with recursion. Especially with Java, which isn't a functional language (in the sense of functional programming style). Read some comp sci texts. Actually a couple texts you might like, both use Scheme as the language, but you'll probably hit recursion harder with them (as well other stuff). The ... |
|
I've been given this assignment in computer science class, and I am having trouble getting started. I'd really appreciate it if anyone can point me in the right direction. I had no idea what recursion is, I just know that this problem uses it. So here it is: "Five robbers have stolen 100 bearer bond certificates and they have to divide ... |
I think your chain method is trying to do too much. You should make your program simpler. I would have two methods, one to sort the digits in ascending order and one to sort the digits in descending order. Then your main can call those two methods and have two ints returned. Then simply subtract one from the other. BTW this ... |
Hello, for my assignment I have to use recursion, I have to divide the frame into 4 parts, placing different images in the top right, bottom right, and bottom left portions. The top left part is supposed to be divided into 4 parts and have the same thing done to it, as have the three images again and the top left ... |
|
|
I am trying to get better at recursion and my friend and I are trying to figure out this problem from javabat. Been looking at it for hours but still haven't gotten very far. No this is not an assingment I am not trying to cheat. Any help at all would be great. Below is the problem and a link to ... |
|
ex: I have 5 weeks, setup for each week is $50. holding costs to retain 1 good for one week is $.75 demand for weeks 1-5 are 10, 30, 20,50,10,50. wagner-whitin says look at the last week first. The only possibility is setup in week 5, that would cost 50 dollars. then week 4: possibilities are setup in week 4 and ... |
I ve struggled with what to put in the if statement! Can anyone see my errors between my pseudo-code and implementation above... printPermutations(int currentIndex) if currentIndex has gone past the end of the permutation array print out the permutation else for each index in the given string array if the character at that index is not already used in the permutation ... |
93. recursion forums.oracle.comHi guys I am writing a program called ReverseLines to copy standard input to standard output but with the lines in reverse order. I have a rough idea of what to do and wrote some pseudo code, but I cant figure out how to output the remaining lines...? i ahve checked out the tutorial, which helped but I am still unsure! ... |
|
95. Recursion forums.oracle.com |
You're missing an important part of that stack trace: what is the actual error (i.e. what kind of exception and what is the message)? Also, what value did you pass to Generate(String) initially? I can see you're getting quite frustrated and I suspect you're chipping away at your code trying to get it to work. My advice is to step away ... |
Open your debugger and start stepping, it's probably the only way you're going to really understand what's happening. Remember, each time you re-enter your routine, you get a new environment and this continues until you get to your haulting condition, then each of the environments hits their respective returns and backs back out to the previous environment level until you get ... |
98. Recursion forums.oracle.comMy question is how the compiler knows that calculate(b,e/2) equals b^e/2 but not something else. Is it because the base case I define makes the complier knows to do so? because in recursion, it seems that the problems are solved beginning from the most basic one(the base case). Thank you very much! |
|
|