Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
23 May, 2013, 08:06:55 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: 1 ... 5 6 [7]   Go Down
  Print  
Author Topic: Random simple java programs that actually do something useful. -  (Read 6369 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Yeong
Eternally Young Scarlet Moon
LV12 Extreme Poster (Next: 5000)
************
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 01:44:48
Date Registered: 15 October, 2010, 04:29:49
Location: Arden, NC
Posts: 3694


Topic starter
Total Post Ratings: +260

View Profile
« Reply #90 on: 25 March, 2011, 23:52:00 »
0

Well, this is the java code that lets u convert decimal to binary ~ 36-base number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.*;
public class Convert
{
    public static String convertbase(int n, int base) {
       // special case
       if (n == 0) return "0";

       String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       String s = "";
       while (n > 0) {
          int d = n % base;
          s = digits.charAt(d) + s;
          n = n / base;
       }
       return s;
    }
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the number base 10 to convert:  ");
        int original = keyboard.nextInt();
        System.out.print("Enter the new base: ");
        int base = keyboard.nextInt();
        String answer = convertbase(original,base);
        System.out.println("The number "+original+"[base 10] = "+answer+"[base "+base+"]");
    }
}
Logged

Project Redemption....

My project progresses:HERE
My Pastebin stuffs:HERE
Check your rate: HERE
My Animations: HERE
Spoiler for Images :D:

ノ◕ヮ◕)ノ:・゚ PENGUIN WAVE!!:„ø¤º°¨ ¨°º¤KEEP THE PENGUIN GOING ¸„ø¤º°¨ ¨°º¤øº LETS GO PENGUIN !¤¤º°¨¨°º¤øº¤ø„¸¸ø¤º°¨„ ø¤º°¨¨°º
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 21 May, 2013, 17:58:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Total Post Ratings: +174

View Profile
« Reply #91 on: 25 March, 2011, 23:59:46 »
0


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.*;
public class Convert
{
    public static String convertbase(int n, int base) {
       // special case
       if (n == 0) return "0";

       String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       String s = "";
       while (n > 0) {
          int d = n % base;
          s = digits.charAt(d) + s;
          n = n / base;
       }
       return s;
    }
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the number base 10 to convert:  ");
        int original = keyboard.nextInt();
        System.out.print("Enter the new base: ");
        int base = keyboard.nextInt();
        String answer = convertbase(original,base);
        System.out.println("The number "+original+"[base 10] = "+answer+"[base "+base+"]");
    }
}

This is looking good. It seems like it supports all bases.

Here's how it works for me:

1
2
3
4
5
6
7
8
9
10
david@DavidPC:~/Documentos/Java$ javac Convert.java
david@DavidPC:~/Documentos/Java$ java Convert
Enter the number base 10 to convert:  65535
Enter the new base: 16
The number 65535[base 10] = FFFF[base 16]
david@DavidPC:~/Documentos/Java$ java Convert
Enter the number base 10 to convert:  255
Enter the new base: 2
The number 255[base 10] = 11111111[base 2]
« Last Edit: 26 March, 2011, 00:03:23 by Scout » Logged
Yeong
Eternally Young Scarlet Moon
LV12 Extreme Poster (Next: 5000)
************
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 01:44:48
Date Registered: 15 October, 2010, 04:29:49
Location: Arden, NC
Posts: 3694


Topic starter
Total Post Ratings: +260

View Profile
« Reply #92 on: 26 March, 2011, 00:14:38 »
0

Well, that's what supposed to look like.  Smiley
BTW, don't try incredibly huge number or it will go overflow Big smile
Logged

Project Redemption....

My project progresses:HERE
My Pastebin stuffs:HERE
Check your rate: HERE
My Animations: HERE
Spoiler for Images :D:

ノ◕ヮ◕)ノ:・゚ PENGUIN WAVE!!:„ø¤º°¨ ¨°º¤KEEP THE PENGUIN GOING ¸„ø¤º°¨ ¨°º¤øº LETS GO PENGUIN !¤¤º°¨¨°º¤øº¤ø„¸¸ø¤º°¨„ ø¤º°¨¨°º
nemo
LV9 Veteran (Next: 1337)
*********
Offline Offline

Last Login: 04 April, 2013, 01:12:57
Date Registered: 16 May, 2010, 03:55:30
Posts: 1198

Total Post Ratings: +83

View Profile
« Reply #93 on: 26 March, 2011, 21:49:09 »
0

then to go from any base to another...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner intRead = new Scanner(System.in);
Scanner strRead = new Scanner(System.in);
System.out.print("Enter a number to convert: ");
String src = strRead.nextLine();
System.out.print("Enter the number's base: ");
int oldBase = intRead.nextInt();
System.out.print("Enter the desired base: ");
int newBase = intRead.nextInt();
String ans = Integer.toString(Integer.valueOf(src, oldBase), newBase);
System.out.println(src + " in base " + oldBase + " is " + ans + " in base " + newBase);
}
}
Logged


Pages: 1 ... 5 6 [7]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.182 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.