Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 12-09-2008, 06:44 PM   #1
Beta Member
 
loljustplaying's Avatar
 
Join Date: Dec 2008
Posts: 3
Default JAVA(Jcreator) final exam help!

Ok guys. I'm currently in high school and i have this final exam project i have to do.
Somehow, the old thread got deleted and i can't find the code anymore. This is the last part of the project.What i have to do is:

> Write a method
public static int sumDigits(int num)

The sumDigits method should calculate and return the sum of all the digits in the number that is passed to it. For example, if I call sumDigits(1234), the method should return the value 10 ( 1+2+3+4) . sumDigits does not print anything to the screen.
Then, write a simple main method to test sumDigits. It should allow the user to input a number, call the sumDigits method, and print out the result for the user to see in the format:
The sum of the digits in (number) is (answer).

Someone coded this up in C++ code... but unfortunately, i know nothing about C++...

Help coding this would be GREATLY appreciated.
__________________

loljustplaying is offline   Reply With Quote
Old 12-09-2008, 06:58 PM   #2
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: JAVA(Jcreator) final exam help!

I can quickly write something in #English and you can write to Java.

Send input -> function
Attach number to variable
Create variable i (used in loop), create variable total
Create a loop from number to 0 (in terms of i)

total =+ i

return variable to main argument
__________________

JogaBonito1502 is offline   Reply With Quote
Old 12-09-2008, 07:23 PM   #3
Beta Member
 
loljustplaying's Avatar
 
Join Date: Dec 2008
Posts: 3
Default Re: JAVA(Jcreator) final exam help!

um... do u think u can code the whole thing? i know this may sound like a lot of work.. but i'm actually helping my friend.

Thanks so far!
loljustplaying is offline   Reply With Quote
Old 12-09-2008, 07:57 PM   #4
Golden Master
 
freestyler105's Avatar
 
Join Date: Sep 2006
Posts: 7,883
Default Re: JAVA(Jcreator) final exam help!

Someone check this, I don't have JCreator set up on this computer so I haven't tried to compile:

Code:
import chn.util.*;
public class help {
    
    public static void main(String[] args) {

ConsoleIO console=new ConsoleIO();
System.out.println("Enter number:");
int oldnum=console.readInt();
int newnum=sumDigits(oldnum);
System.out.println("The sum of the digits in "+oldnum+" is "+newnum);
    }

public static int sumDigits(int num){
String A= String.valueOf(num);
int length=A.length();
int[] numArray= new int[length];
int count=0;
for(int x=0; x<=length; x++){
numArray[x]=A.charAt(x);
count=count+numArray[x];}
return count;
}
}
__________________
C2D E6600 | 4GB DDR2-800 | 9800GTX+ | Asus P5B-E | 150GB Raptor | 320GB 7200.10 | 750W Xigmatek PSU
freestyler105 is offline   Reply With Quote
Old 12-09-2008, 10:44 PM   #5
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: JAVA(Jcreator) final exam help!

Quote:
Originally Posted by loljustplaying View Post
um... do u think u can code the whole thing? i know this may sound like a lot of work.. but i'm actually helping my friend.

Thanks so far!
I would be able to if I knew Java syntax. I only know C++.
JogaBonito1502 is offline   Reply With Quote
Old 12-12-2008, 10:26 AM   #6
Baseband Member
 
Bozorg's Avatar
 
Join Date: Mar 2006
Posts: 34
Send a message via AIM to Bozorg Send a message via MSN to Bozorg
Default Re: JAVA(Jcreator) final exam help!

I think this is what you want. I changed the above code around to make it work.

Code:
   import java.util.Scanner;
    public class help {
    
       public static void main(String[] args) {
      
         Scanner scanner = new Scanner(System.in);
         System.out.println("Enter number:");
         int oldnum=scanner.nextInt();
         int newnum=sumDigits(oldnum);
         System.out.println("The sum of the digits in "+oldnum+" is "+newnum);
      }
   
       public static int sumDigits(int num){
         String A= String.valueOf(num);
         int length=A.length();
         int[] numArray= new int[length];
         int count=0;
         for(int x=0; x<length; x++){
            numArray[x]= Integer.parseInt(A.substring(x, x+1));
            count=count+numArray[x];
         }    
         return count;
      }
   }
__________________

__________________
IT System Administrator
Network+ Certified
Bozorg is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 04:17 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0