<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:dc="https://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[BP Forums - Java]]></title>
		<link>https://bpforums.info/</link>
		<description><![CDATA[BP Forums - https://bpforums.info]]></description>
		<pubDate>Sat, 30 May 2026 14:33:54 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Character ammount]]></title>
			<link>https://bpforums.info/showthread.php?tid=912</link>
			<pubDate>Mon, 25 Apr 2016 14:32:40 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://bpforums.info/member.php?action=profile&uid=1584">.righteous</a>]]></dc:creator>
			<guid isPermaLink="false">https://bpforums.info/showthread.php?tid=912</guid>
			<description><![CDATA[I've just finished watching your tenth "Learning Java" part and I want to limit the character number to five digits only.Â <br />
I've tried this:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>import java.util.*;<br />
public class digitExtractorTester {<br />
&nbsp;&nbsp;&nbsp;&nbsp;static Scanner userInput = new Scanner(System.in);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String rNumber = "12345";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print("Enter a custom, five digit number: ");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String nSequence = userInput.next();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(" ");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (nSequence.chars() != rNumber.chars()){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Your input contains too many or too less digits.&#92;nOnly five digit numbers are allowed.");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;digitExtractor byMath = new digitExtractor(Integer.valueOf(nSequence));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byMath.returnNumberSequenceByMath();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(" ");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;digitExtractor byChar = new digitExtractor(nSequence);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byChar.returnNumberSequenceByChar();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div>But it doesn't seem to be working. Any suggestions?<br />
<hr class="mycode_hr" />
Never mind. I solved my own problem.<br />
If anyone's wondering, I was using <span style="font-style: italic;" class="mycode_i">chars()</span> when I was supposed to be using <span style="font-style: italic;" class="mycode_i">length()</span>. <a href="http://stackoverflow.com/questions/816142/strings-maximum-length-in-java-calling-length-method" target="_blank" rel="noopener" class="mycode_url">(Source of information)</a><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>nSequence.length() != rNumber.length()</code></div></div>]]></description>
			<content:encoded><![CDATA[I've just finished watching your tenth "Learning Java" part and I want to limit the character number to five digits only.Â <br />
I've tried this:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>import java.util.*;<br />
public class digitExtractorTester {<br />
&nbsp;&nbsp;&nbsp;&nbsp;static Scanner userInput = new Scanner(System.in);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String rNumber = "12345";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print("Enter a custom, five digit number: ");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String nSequence = userInput.next();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(" ");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (nSequence.chars() != rNumber.chars()){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Your input contains too many or too less digits.&#92;nOnly five digit numbers are allowed.");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;digitExtractor byMath = new digitExtractor(Integer.valueOf(nSequence));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byMath.returnNumberSequenceByMath();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(" ");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;digitExtractor byChar = new digitExtractor(nSequence);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byChar.returnNumberSequenceByChar();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div>But it doesn't seem to be working. Any suggestions?<br />
<hr class="mycode_hr" />
Never mind. I solved my own problem.<br />
If anyone's wondering, I was using <span style="font-style: italic;" class="mycode_i">chars()</span> when I was supposed to be using <span style="font-style: italic;" class="mycode_i">length()</span>. <a href="http://stackoverflow.com/questions/816142/strings-maximum-length-in-java-calling-length-method" target="_blank" rel="noopener" class="mycode_url">(Source of information)</a><br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>nSequence.length() != rNumber.length()</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Adding backgroundcolor to snake applet]]></title>
			<link>https://bpforums.info/showthread.php?tid=907</link>
			<pubDate>Fri, 28 Aug 2015 16:51:31 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://bpforums.info/member.php?action=profile&uid=1421">Kevin</a>]]></dc:creator>
			<guid isPermaLink="false">https://bpforums.info/showthread.php?tid=907</guid>
			<description><![CDATA[Hi guys,<br />
<br />
first, i'm not the best english speaker and -writer. Therefore i ask you to apologize my language mistakes <img src="https://bpforums.info/images/smilies/smile.png" alt="Smile" title="Smile" class="smilie smilie_1" /><br />
<br />
Problably in your eyes, i have a very simply problem, but i cant add the background function to Brandonio's Snake Project. I want to have a black background in the applet. After this, i want to change the color of the grid to white.<br />
<br />
I try to add two JPanel's to snakeApplet.java. But is it still not working. Every time i started the Applet, the background is white.<br />
<br />
<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>import java.applet.Applet;<br />
import java.awt.BorderLayout;<br />
import java.awt.Color;<br />
import java.awt.Dimension;<br />
import java.awt.Graphics;<br />
<br />
import javax.swing.JPanel;<br />
<br />
public class snakeApplet extends Applet{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;private snakeCanvas c;<br />
&nbsp;&nbsp;&nbsp;&nbsp;JPanel jp1 = new JPanel();<br />
&nbsp;&nbsp;&nbsp;&nbsp;JPanel jp2 = new JPanel();<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;public void init()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jp1.setBackground(Color.BLACK);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(jp1);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jp2.setBackground(Color.BLACK);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(jp2, BorderLayout.SOUTH);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = new snakeCanvas();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.setSize(new Dimension(640, 480));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.setPreferredSize(new Dimension(640, 480));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.setVisible(true);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.setFocusable(true);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.add(c);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setVisible(true);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setSize(new Dimension(640, 480));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;public void paint(Graphics g)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setSize(new Dimension(640, 480));<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div>In addition, i want to add music to the applet. The music should instantly playing if the applet is open.<br />
If the idea of the black background and white grid is not to be a looker, than i would to add a picture in the background of the grid.<br />
But i dont know how i can code it.<br />
I hope someone can help me please <img src="https://bpforums.info/images/smilies/smile.png" alt="Smile" title="Smile" class="smilie smilie_1" /><br />
Yours sincerley<br />
Kevin<br />
<br />
Edit: Kevin is only a nickname <img src="https://bpforums.info/images/smilies/biggrin.png" alt="Big Grin" title="Big Grin" class="smilie smilie_4" />]]></description>
			<content:encoded><![CDATA[Hi guys,<br />
<br />
first, i'm not the best english speaker and -writer. Therefore i ask you to apologize my language mistakes <img src="https://bpforums.info/images/smilies/smile.png" alt="Smile" title="Smile" class="smilie smilie_1" /><br />
<br />
Problably in your eyes, i have a very simply problem, but i cant add the background function to Brandonio's Snake Project. I want to have a black background in the applet. After this, i want to change the color of the grid to white.<br />
<br />
I try to add two JPanel's to snakeApplet.java. But is it still not working. Every time i started the Applet, the background is white.<br />
<br />
<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>import java.applet.Applet;<br />
import java.awt.BorderLayout;<br />
import java.awt.Color;<br />
import java.awt.Dimension;<br />
import java.awt.Graphics;<br />
<br />
import javax.swing.JPanel;<br />
<br />
public class snakeApplet extends Applet{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;private snakeCanvas c;<br />
&nbsp;&nbsp;&nbsp;&nbsp;JPanel jp1 = new JPanel();<br />
&nbsp;&nbsp;&nbsp;&nbsp;JPanel jp2 = new JPanel();<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;public void init()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jp1.setBackground(Color.BLACK);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(jp1);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jp2.setBackground(Color.BLACK);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(jp2, BorderLayout.SOUTH);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = new snakeCanvas();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.setSize(new Dimension(640, 480));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.setPreferredSize(new Dimension(640, 480));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.setVisible(true);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.setFocusable(true);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.add(c);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setVisible(true);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setSize(new Dimension(640, 480));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;public void paint(Graphics g)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setSize(new Dimension(640, 480));<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</code></div></div>In addition, i want to add music to the applet. The music should instantly playing if the applet is open.<br />
If the idea of the black background and white grid is not to be a looker, than i would to add a picture in the background of the grid.<br />
But i dont know how i can code it.<br />
I hope someone can help me please <img src="https://bpforums.info/images/smilies/smile.png" alt="Smile" title="Smile" class="smilie smilie_1" /><br />
Yours sincerley<br />
Kevin<br />
<br />
Edit: Kevin is only a nickname <img src="https://bpforums.info/images/smilies/biggrin.png" alt="Big Grin" title="Big Grin" class="smilie smilie_4" />]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Knuth Numbers recursion]]></title>
			<link>https://bpforums.info/showthread.php?tid=898</link>
			<pubDate>Wed, 22 Apr 2015 07:46:17 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://bpforums.info/member.php?action=profile&uid=1329">Miriam</a>]]></dc:creator>
			<guid isPermaLink="false">https://bpforums.info/showthread.php?tid=898</guid>
			<description><![CDATA[Hello,<br />
<br />
I recently had a final exam on which my teacher asked to write a method for Knuth numbers recursion. I got a stack overflow on my implementation, and I am new on Java, so I am not sure what happened, it might be that I had the wrong formula for Knuth numbers? I would love some feedback.<br />
<br />
Here is the question, and my code<br />
<br />
<span style="font-style: italic;" class="mycode_i">Knuth numbers are given by the recursive formula Kn+1 = 1 + min (2 * Ka, 3 * Kb) where a is the smallest integer larger than n/2.0 and b is the smallest integer larger than n/3.0. The use of doubles in the denominator is necessary to ensure that integer division is not used.<br />
<br />
For example, if n is 7, then a equals 4 and b equals 3 and thus K8 = 1 + min (2* K4, 3 * K3).<br />
<br />
If n is 8, then a equals 5 while b remains 3 and thus K9 = 1 + min (2 * K5, 3 * K3).<br />
If n is 9, then a remains 5 while b equals 4 and thus K10 = 1 + min(2 * K5, 3 * K3).<br />
<br />
Mathematically, â€œthe smallest integer larger than xâ€ is the â€œceilingâ€ function. If n is an int, then n / 2 is calculated using integer division, discarding the remainder; but you want the division to give you a double. Thus you need code that looks like this:<br />
<br />
int a = Math.ceil(n / 2.0);<br />
int b = Math.ceil(n / 3.0);<br />
<br />
Since the equation defining Knuth numbers is a recursive one, we must have a base case; letâ€™s assume that K0 = 1.<br />
<br />
Create and test a class called KnuthNumbers (in a project also called KnuthNumbers) which includes a static method to compute Knuth numbers.</span><br />
<br />
my code:<br />
<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>public class KnuthNumbers<br />
{<br />
Â  Â /**<br />
Â  Â  * Recursive Knuth Numbers 1, 3, 3, 4, 7, 7, 7, 9, 9, 10, 13, ... Â <br />
Â  Â  */<br />
Â  Â public static int knuthNumber(final double n)<br />
Â  Â throws DataFormatException{<br />
Â  Â  Â  Â double a = Math.ceil(n / 2.0);<br />
Â  Â  Â  Â double b = Math.ceil(n / 3.0);<br />
Â  Â  Â  Â // bad input<br />
Â  Â  Â  Â if (n &lt; 1)<br />
Â  Â  Â  Â  Â  Â throw new DataFormatException("n cannot be less then 1");<br />
Â  Â  Â  Â if (n &gt; 12)<br />
Â  Â  Â  Â  Â  Â throw new DataFormatException("n is too large. Max is 12");<br />
Â  Â  Â  Â //base case<br />
Â  Â  Â  Â if (n == 1)<br />
Â  Â  Â  Â  Â  Â return 1;<br />
<br />
Â  Â  Â  Â //recursion<br />
<br />
Â  Â  Â  Â return knuthNumber(a + b);<br />
Â  Â }<br />
}</code></div></div><br />
<br />
<br />
<br />
TEST:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>public class KnuthNumbersTest<br />
{<br />
Â  Â  <br />
<br />
Â  Â @Test<br />
Â  Â public void testKnuthNumber(){<br />
Â  Â  Â  Â long startTime = 0;<br />
Â  Â  Â  Â long endTime = 0;<br />
Â  Â  Â  Â double result = 0;<br />
<br />
Â  Â  Â  Â try {<br />
Â  Â  Â  Â  Â  Â assertEquals(-1, KnuthNumbers.knuthNumber(-3));<br />
Â  Â  Â  Â  Â  Â fail("number should not be negative");<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (DataFormatException e){<br />
Â  Â  Â  Â  Â  Â assertEquals("n cannot be less then 1", e.getMessage());<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (Exception e) {<br />
Â  Â  Â  Â  Â  Â fail("Unexpected exception: " + e.toString());<br />
Â  Â  Â  Â }<br />
<br />
Â  Â  Â  Â try {<br />
Â  Â  Â  Â  Â  Â assertEquals(0, KnuthNumbers.knuthNumber(0));<br />
Â  Â  Â  Â  Â  Â fail("number should not be zero");<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (DataFormatException e){<br />
Â  Â  Â  Â  Â  Â assertEquals("n cannot be less then 1", e.getMessage());<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (Exception e) {<br />
Â  Â  Â  Â  Â  Â fail("Unexpected exception: " + e.toString());<br />
Â  Â  Â  Â }<br />
<br />
Â  Â  Â  Â try {<br />
Â  Â  Â  Â  Â  Â assertEquals(1, KnuthNumbers.knuthNumber(1.0));<br />
Â  Â  Â  Â  Â  Â assertEquals(9, KnuthNumbers.knuthNumber(5.4));<br />
Â  Â  Â  Â  Â  Â assertEquals(13, KnuthNumbers.knuthNumber(7.6));<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (Exception e) {<br />
Â  Â  Â  Â  Â  Â fail ("Unexpected exception: " + e.toString());<br />
Â  Â  Â  Â }<br />
<br />
Â  Â }<br />
<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
I recently had a final exam on which my teacher asked to write a method for Knuth numbers recursion. I got a stack overflow on my implementation, and I am new on Java, so I am not sure what happened, it might be that I had the wrong formula for Knuth numbers? I would love some feedback.<br />
<br />
Here is the question, and my code<br />
<br />
<span style="font-style: italic;" class="mycode_i">Knuth numbers are given by the recursive formula Kn+1 = 1 + min (2 * Ka, 3 * Kb) where a is the smallest integer larger than n/2.0 and b is the smallest integer larger than n/3.0. The use of doubles in the denominator is necessary to ensure that integer division is not used.<br />
<br />
For example, if n is 7, then a equals 4 and b equals 3 and thus K8 = 1 + min (2* K4, 3 * K3).<br />
<br />
If n is 8, then a equals 5 while b remains 3 and thus K9 = 1 + min (2 * K5, 3 * K3).<br />
If n is 9, then a remains 5 while b equals 4 and thus K10 = 1 + min(2 * K5, 3 * K3).<br />
<br />
Mathematically, â€œthe smallest integer larger than xâ€ is the â€œceilingâ€ function. If n is an int, then n / 2 is calculated using integer division, discarding the remainder; but you want the division to give you a double. Thus you need code that looks like this:<br />
<br />
int a = Math.ceil(n / 2.0);<br />
int b = Math.ceil(n / 3.0);<br />
<br />
Since the equation defining Knuth numbers is a recursive one, we must have a base case; letâ€™s assume that K0 = 1.<br />
<br />
Create and test a class called KnuthNumbers (in a project also called KnuthNumbers) which includes a static method to compute Knuth numbers.</span><br />
<br />
my code:<br />
<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>public class KnuthNumbers<br />
{<br />
Â  Â /**<br />
Â  Â  * Recursive Knuth Numbers 1, 3, 3, 4, 7, 7, 7, 9, 9, 10, 13, ... Â <br />
Â  Â  */<br />
Â  Â public static int knuthNumber(final double n)<br />
Â  Â throws DataFormatException{<br />
Â  Â  Â  Â double a = Math.ceil(n / 2.0);<br />
Â  Â  Â  Â double b = Math.ceil(n / 3.0);<br />
Â  Â  Â  Â // bad input<br />
Â  Â  Â  Â if (n &lt; 1)<br />
Â  Â  Â  Â  Â  Â throw new DataFormatException("n cannot be less then 1");<br />
Â  Â  Â  Â if (n &gt; 12)<br />
Â  Â  Â  Â  Â  Â throw new DataFormatException("n is too large. Max is 12");<br />
Â  Â  Â  Â //base case<br />
Â  Â  Â  Â if (n == 1)<br />
Â  Â  Â  Â  Â  Â return 1;<br />
<br />
Â  Â  Â  Â //recursion<br />
<br />
Â  Â  Â  Â return knuthNumber(a + b);<br />
Â  Â }<br />
}</code></div></div><br />
<br />
<br />
<br />
TEST:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>public class KnuthNumbersTest<br />
{<br />
Â  Â  <br />
<br />
Â  Â @Test<br />
Â  Â public void testKnuthNumber(){<br />
Â  Â  Â  Â long startTime = 0;<br />
Â  Â  Â  Â long endTime = 0;<br />
Â  Â  Â  Â double result = 0;<br />
<br />
Â  Â  Â  Â try {<br />
Â  Â  Â  Â  Â  Â assertEquals(-1, KnuthNumbers.knuthNumber(-3));<br />
Â  Â  Â  Â  Â  Â fail("number should not be negative");<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (DataFormatException e){<br />
Â  Â  Â  Â  Â  Â assertEquals("n cannot be less then 1", e.getMessage());<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (Exception e) {<br />
Â  Â  Â  Â  Â  Â fail("Unexpected exception: " + e.toString());<br />
Â  Â  Â  Â }<br />
<br />
Â  Â  Â  Â try {<br />
Â  Â  Â  Â  Â  Â assertEquals(0, KnuthNumbers.knuthNumber(0));<br />
Â  Â  Â  Â  Â  Â fail("number should not be zero");<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (DataFormatException e){<br />
Â  Â  Â  Â  Â  Â assertEquals("n cannot be less then 1", e.getMessage());<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (Exception e) {<br />
Â  Â  Â  Â  Â  Â fail("Unexpected exception: " + e.toString());<br />
Â  Â  Â  Â }<br />
<br />
Â  Â  Â  Â try {<br />
Â  Â  Â  Â  Â  Â assertEquals(1, KnuthNumbers.knuthNumber(1.0));<br />
Â  Â  Â  Â  Â  Â assertEquals(9, KnuthNumbers.knuthNumber(5.4));<br />
Â  Â  Â  Â  Â  Â assertEquals(13, KnuthNumbers.knuthNumber(7.6));<br />
Â  Â  Â  Â }<br />
Â  Â  Â  Â catch (Exception e) {<br />
Â  Â  Â  Â  Â  Â fail ("Unexpected exception: " + e.toString());<br />
Â  Â  Â  Â }<br />
<br />
Â  Â }<br />
<br />
}</code></div></div>]]></content:encoded>
		</item>
	</channel>
</rss>