I have a requirement where I have a class as Page which itself contains ArrayList<Page>.Here ArrayList<Page> is nothing but the pages which are accessible from the base Page.I know the depth level ( reading from file) which means how many level I need to go to identify more pages.BUT the problem is how to set the base Page class. I need to set the base Page class but for that I need the objects for the subsequent pages and hence an iterative type of implementation.
I recently wrote a simple recursive program that chooses K objects out of N (I was asked to use the variables N choose the R, however) total objects. Here is the code:
int n = 0; int r = 0; //the total number of objects defaults to 0 String nChoice = JOptionPane.showInputDialog(null, "How many objects are there to choose from?"); String rChoice = JOptionPane.showInputDialog(null, "How many object are to be chosen from this group?"); try { n = Integer.parseInt(nChoice);
[Code] ....
It works fine, however in my class we were given two different formula to implement into our code. I used the one above, obviously. However, the second formula we were given was:
C(n,R) = n! -------(R!(n-R)!)
I had to get the spacing right.
How do I read this formula? How could it be implemented? What are the benefits (if there are any) from using one method over the other? Which method of calculating N choose K (or, in my case, N choose R) would be more widely accepted?
I'm trying to implement a non-recursive version of the insertion method, but I'm having a bit of trouble. From what I can tell, it's only returning the last two node..
public void insert(Key key, Value val) { root = insert(key, val, root); } private Node insert(Key key, Value val, Node x) { if(x == null) { x = new Node(key, val, 1);
As one of the methods of my IntTree tree I have to implement a method that multiplies the level number with the sum of the nodes on the current level. So far I have this, and it doesn't work. What I am wondering is am I on the right track at all with the second return statement?
public int depthSum(){ return depthSum(overallRoot); } private int depthSum(IntTreeNode root) { if(root==null) return 0; int level = 0;
I am not sure how to add all the possibilities of elements in an array and find the greatest sum. I want to do this recursively. I don't need any code. How I would do it.
I was told to write a method that adds up the sequence of the formula (n/2n+1) eg. 1/3 + 2/5 + 3/7 etc. simple enough i suppose. my method is below
public static double Series(int n){ if (n==0)return 0; else return (n/(n*2+1)) + Series(n - 1); }
However for some reason or another it returns 0 for any number that is put in. I've written it dozens of different ways with no change and i feel like something fairly obvious is being missed on my part. I am honestly intrigued and interested as to why this is happening. i assume it has something to do with the way i put the actual formula in cause if i put anything else in like simply n the recursion would work as expected.
I'm trying to understand the concept behind this recursive method called rangeSum. This method sums a range of array elements with recursion. I tried summing the elements of 2 through 5, and I tried writing down what actually happens when java executes the program, but what I get when I try to figure it out by paper is different from what netbeans gives me. Here is a snapshot of my scratch work as well as my source code. Netbeans gives me "The sum of elements 2 through 5 is 18" when I try running it but it I get 12 when I do the recursion on paper. I know that the source code is correct because it's out of the book but what am I doing wrong when I try figuring it out by hand?
XML Code:
package recursivecall; import java.util.Scanner; /** * Author: <<Conrado Sanchez>> Date: Task: */ public class RecursiveCall {
I wrote this tail recursive function that mirrors the iterative version, except that the loop in the iterative version is replaced by an if statement here and then a recursive call. Is this truly recursive? I have seen the fibo(n-1) + fibo(n - 2) version, but is this also an acceptable recursive solution? Why is it never solved this way?
public class FiboRecursive { public static int fibo (int n) { int sum = 0; int n1 = 1; int n2 = 1; if (n == 1 || n == 2) { sum = 1;
SO for my project, we have to create a program where we input two four letter words, and using a list of words our teacher provided us and only changing one letter at a time, make the words match.For example, you input BALD and CALL and it would output BALD BALL CALLWe have to use recursion to do this, and I'm totally lost as to where to even begin.
a)Write a method that recursively displays any given character the specified number of times on one line.For example, the call: displayRowOf Characters(,5);
Produce a line: *****Write another method that uses a for-loop to perform the same process.
B is something like this ? for (i=1; i<=n; i++) i= '*' * n; System.out.print(i);
I have this assignment to write a Merge Sort algorithm using recursion. To start I have a very tough time picturing what is happening when it comes to recursion, but I do understand how merge sorting works. At the moment I feel as though a very good portion of my code is correct, but I am having trouble with the recursion in the main method [ mergeSort(Queue<T> queue) ].
I have another 4 or so hours to pass in my assignment finished or not, and at this point I can honestly say I have no clue how to make my code work. I tried working through the problem on paper with a simple queue of size 3, but even that is a struggle. On paper my code works perfectly fine, so there is definitely something I am missing.
Below is what I have along with my JUnit test.
Java Code:
private Queue<T> output = new Queue<T>(); private Queue<T> output1 = new Queue<T>(); private Queue<T> output2 = new Queue<T>(); public Queue<T> mergeSort(Queue<T> queue) { // TODO 1 if(queue.size() <= 1) { return queue;
I can often write a recursive backtracking solution, but don't know how to cache the answers into an appropriate array.
For example:
Java Code:
public static int max(int[] costs, int index, int total, int shares) { if(index >= costs.length) { return total; } int buy = max(costs, index + 1, total - costs[index], shares + 1); // buy one stock int sell = max(costs, index + 1, total + shares * costs[index], 0); // sell all stocks return Math.max(total, Math.max(buy, sell)); // compares between buy, sell, and doing nothing } mh_sh_highlight_all('java');
This is a dynamic programming exercise, but I have no idea what dimensions the dp array should be (I was thinking maybe dp[index][total][shares], but that seemed like overkill). Is this just because my understanding of recursion isn't solid enough or am I missing something else?
I am currently working on a project where I need to return data from a database over RMI to a client who requests it. Some of the fields in the Data Object can not be seen by the client so I need to create another object to send over the network instead. The method I use is this...
public static SerializableObject createSerializableObjectFromDataObject(DataObject dataObject){ SerializableObject serializableObject = new SerializableObject(); serializableObject.setField(dataObject.getField()); serializableObject.setAnotherField(dataObject.getAnotherField()); return serializableObject; }
Is there a better way of doing this? I am creating many subclasses DataObject which all require this static method to be implemented and I can't push it into the superclass because each one needs custom behaviour.
Here are two codes that I am using but I have one that just doesn't work for some reason and the other does. Encode doesn't work. I don't need the Character.isAlphabetic for encode but not sure what I can use with 'if' to set the String encodedString.
I have been playing around with a snippet I wrote to get the Label on a drive (below). It works fine for me (though I will take any constructive criticism). My question is whether the is a way to set the drive label, purely with Java. I know I could call command line, or even resort to using the Windows API
I tried using this since the jSpinner's minimum value is 1.. (I want it to reset the jSpinner after updating the database):
jSpinner1.setValue(new Integer(1));
But gives me an error:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.Vector.elementData(Vector.java:730) at java.util.Vector.elementAt(Vector.java:473) at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649) at shop.jSpinner1StateChanged(shop.java:267) at shop.access$100(shop.java:22) at shop$3.stateChanged(shop.java:145) at javax.swing.JSpinner.fireStateChanged(JSpinner.java:457)
JFrame{ JPanel(That MenuBar at the top) JPanel(That panel at center with table){ JScrollPane{ JTable } } }
I want to add my custom image to that grey space right there. I guess it is a JScrollPane, because I added that orange background on JPanel that contains it. But when I add this code to my custom class of JScrollPane:
@Override public void paintComponent(Graphics g) { super.paintComponent(g); if(background == null){ background = new ImageIcon(ClassLoader.getSystemResource("tableBg.png")).getImage(); } g.drawImage(background, 0, 0, null); }
The result is the same, no changes.
Then I read some documentation. I found this method:
scrollPane.getViewport().setBackground(Color c);
It works, but it accepts only color and I want to add image. Is there any way to do that? Do I need to subclass JViewport and use paintComponent ? If yes, then how to make getViewport() method return my custom subclassed version?