urgently needed

the user enters a number, the program adds the individual digits of the number together and gives output,if the output is more than one digit, it adds it again and gives the output, the process continues until there is single digit left
[243 byte] By [imranmehmooda] at [2007-10-3 2:53:36]
# 1
Sit down, put your feet up and relax. I'll do your work for you right now. Be back soon with the code.
floundera at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 2
this algorithm has been BEATEN TO DEATH on these forums -apparently every teacher assigns this.ive seen many extremely short and efficient recursive methods for this.so does us the favor and do a search.
TuringPesta at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 3
apparently this is how someone at harvard would do it:www.people.fas.harvard.edu/~jjackson/CSCIE50bSection/Code/Recursion/DigitSum.java
TuringPesta at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 4
Ahh, I just remembered. This is called the Digital (or Prime Digital) Rootof a number. http://en.wikipedia.org/wiki/Digital_root http://mathworld.wolfram.com/DigitalRoot.html http://forum.java.sun.com/thread.jspa?threadID=707803&messageID=4099742
TuringPesta at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 5
I love it when they post homework assignments and don't even bother adding a question. Just the assignment. Not even "how would I do this?" at the end.
paulcwa at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 6
Hi inthemoodI have almost finished your homework. Won't be long now.
floundera at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 7

> and don't even bother adding a question

Its very disrespectful. It seems to be happening more frequently too.

Sometimes I think Sun should just cave and add a "homework"

section and stockpile the 10 or so assignments we always see

and all the threads answering them.

It might even give teachers pause to come up with different problems.

TuringPesta at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 8

import static java.lang.Integer.parseInt;

import static java.lang.Integer.bitCount;

import static java.lang.Integer.toHexString;

import static java.lang.Integer.toBinaryString;

import javax.swing.JOptionPane;

import javax.swing.JFileChooser;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

public class DigitalRootCalc {

public static void main(String[] args) {

//obtain argument

String s = JOptionPane.showInputDialog("Input number");

//parse the argument, assume hexadecimal

int number = parseInt(s, 16);

int original = number;

//loop until number contains no more than one digit

while (highestOneBit(number) | 0x1 == 0x1) {

//add up the digits of the number, and assign it to number

number = bitCount(number);

}

//display result

JOptionPane.showMessageDialog(null, "Argument: " + toHexString(original) + "\nDigital Root: " + toBinaryString(number));

//ask whether to save to disk

int option = JOptionPane.showConfirmDialog(null, "Save result to disk?");

//check to see if user chose "yes"

if (option == JOptionPane.YES_OPTION) {

//initialize file chooser

JFileChooser fc = new JFileChooser();

//let user choose a file to save to

option = fc.showSaveDialog(null);

//check to see if user chose "save" instead of "cancel"

if (option == JFileChooser.APPROVE_OPTION) {

//watch out for IOException

try {

//obtain chosen file

File f = chooser.getSelectedFile();

//create FileWriter to write to file

FireWriter fw = new FileWriter(f);

//write results to file

fw.write("Argument: " + toHexString(original) + "\nDigital Root: " + toBinaryString(number));

}

//catch IOException

catch (IOException ex) {

//report exception

ex.printStackTrace();

//abort program

System.exit(1);

}

}

}

}

myjavasticka at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 9

>catch (IOException ex) {

>//report exception

>ex.printStackTrace();

>//abort program

>System.exit(1);

> }

You know, you could at least close the streams first before killing the JVM and taking all other programs possibly running inside with it.

I once saw code like that in Websphere. Very funny.

CeciNEstPasUnProgrammeura at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 10
i guess it can be made more tricky. perform an integer division by 9 (n%9) the reminder should be the answer. in the case where the reminder is ZERO then the answer is 9. and this should apply for all the cases.
Bhasker_Reddya at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 11
> I once saw code like that in Websphere. Very funny.I once saw such code too but not in Websphere.
el_doradoa at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 12
> > I once saw code like that in Websphere. Very> funny.> > I once saw such code too but not in Websphere.Where's the fun then?
CeciNEstPasUnProgrammeura at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 13
> > > I once saw code like that in Websphere. Very> > funny.> > > > I once saw such code too but not in Websphere.> > Where's the fun then?It was on JBoss.
el_doradoa at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...
# 14
> It was on JBoss.*lol*
CeciNEstPasUnProgrammeura at 2007-7-14 20:42:38 > top of Java-index,Java Essentials,New To Java...