Java Problem

Nov 2, 2009
709
29
0
එහේ.
Ane pls if can you help to make design of the program in Pseudocode and in Java for this question.


In Mathematics, different number systems are in use. The most commonly used number
system is the decimal number system, which has a base of 10. Other number systems in
use are the binary number system (base 2), octal number system (base 8) and the
hexadecimal number system (base 16). They are all positional number systems, where
the decimal value of the number can be obtained by summing the multiplication of each
digit by its corresponding place value. For example, in binary system the decimal
equivalent of the number 1010 can be obtained by the sum:
1 x 23 + 0 x 22 + 1 x 21 + 0 x 20 = 1 x 8 + 0 x 4 + 1 x 2 + 0 x 1 = 12


Your task:

Design a program that would read a number and the base of the number system and print
the
equivalent decimal value.

Assume that the base of the number system is always 10 or less. That is, all the digits of
the number system are a subset of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Examples of
such number systems are those with base 2 (binary), base 8 (octal), base 4, base 5 etc.

oyata ona mokak hari base ekaka thiyena number ekak thawa base ekakata harawannada?
 

¤--bACarDi--¤

Well-known member
  • Jan 9, 2009
    12,130
    288
    83
    124.43.xxx.xxx
    i got clue like this
    1. dispaly the relevant msg
    2.read the value what u insert
    3. get the length of the string
    4. create the in aray length int
    5 Assign the value for each location

    according to this shoud solve it


    hello im not a java expert & languages are mixed up now :baffled:

    but did some, i dnt have java so can't compile it. there may be many syntax errors fix them & just implement converting methods lol u DO THE MATH :lol:



    Code:
    #import Java.util.*
    
    class NumberBaseConverter 
    {
    	public static void main (String [] args)
    	{	
    		int number, base , decimal;
    		Scanner sc = new Scaner();
    		System.out.println("Eneter No: ");
    		number = sc.nextInt();
    		System.out.println("Base of the Entered Number: ");	
    		base = sc.nextInt();
    
    	
    		switch (base)
    		{
    		case 2:
    			decimal = binaryToDecimal(number);		
    			break;
    		case 8:
    			decimal = octalToDecimal(number);		
    			break;
    		case 16:
    			decimal = hexaToDecimal(number);		
    			break;
    		default:
    			System.out.println("Invalid Input, get a life :P ");	
    			break;
    		}
    
    		
    		System.out.println("Entered Number in decimal = " + decilmal );	
    
    
    	}
    
    
    	public static int binaryToDecimal (int number)
    	{
    		
    		return ;
    	
    	}
    
    	public static int octalToDecimal (int number)
    	{
    		
    		return ;
    	
    	}
    
    	public static int hexaToDecimal (int number)
    	{
    		
    		return ;
    	
    	}
    
    
    
    }
     

    sachii

    Member
    Sep 17, 2008
    8,708
    268
    0
    Ĭǹ Ẏṑũř Ȟĕâřŧ
    hello im not a java expert & languages are mixed up now :baffled:

    but did some, i dnt have java so can't compile it. there may be many syntax errors fix them & just implement converting methods lol u DO THE MATH :lol:



    Code:
    #import Java.util.*
    
    class NumberBaseConverter 
    {
        public static void main (String [] args)
        {    
            int number, base , decimal;
            Scanner sc = new Scaner();
            System.out.println("Eneter No: ");
            number = sc.nextInt();
            System.out.println("Base of the Entered Number: ");    
            base = sc.nextInt();
    
        
            switch (base)
            {
            case 2:
                decimal = binaryToDecimal(number);        
                break;
            case 8:
                decimal = octalToDecimal(number);        
                break;
            case 16:
                decimal = hexaToDecimal(number);        
                break;
            default:
                System.out.println("Invalid Input, get a life :P ");    
                break;
            }
    
            
            System.out.println("Entered Number in decimal = " + decilmal );    
    
    
        }
    
    
        public static int binaryToDecimal (int number)
        {
            
            return ;
        
        }
    
        public static int octalToDecimal (int number)
        {
            
            return ;
        
        }
    
        public static int hexaToDecimal (int number)
        {
            
            return ;
        
        }
    
    
    
    }

    ha ha big thanks for the help :)
    me there is a for loop ne then it easy according that instruct
     

    Topology

    Member
    Feb 24, 2011
    2,395
    225
    0
    Out of Elakiri.
    pseudo code

    Code:
    START
    	NUMERIC base,numbertemp,temp,i,numInt
    	NUMERIC temp2=0
    	CHAR[100] number
    	DISPLAY "enter the base number"
    	ACCEPT  base
    	
    	IF base LESS THAN 10 THEN
    		DISPLAY "enter the number"
    		ACCEPT number
    		NUMERIC i= length(number)-1
    
    		FOR EACH num IN number
    			numInt = (NUMERIC)num
    			temp = base ^ i
    			temp2 = temp2 + (temp * numInt )
    			i = i-1;
    		END FOR EACH
    	END IF
    
    	DISPLAY "the answer is for " + base + " base is " + temp2 
    END
     
    Last edited:

    sachii

    Member
    Sep 17, 2008
    8,708
    268
    0
    Ĭǹ Ẏṑũř Ȟĕâřŧ
    Code:
    import java util;
    class Test {
                scanner sc = new scanner(system.in);
        s.out("Entere nuber");
        string x= sc.nextInt();
        enter nubmer
        s.out ("enter BASE");
        int base = sc.next.Int();
        int t = array () = new
        for (int i =0 ; i< numberlengt ; i++);
    see this. just and idea i got one of my friend help out ask to do like this can u check this
     
    Jul 8, 2007
    1,138
    156
    0
    Melbourne
    Code:
    import java.util.*;
    
    /**
     *
     * @author Chinthy
     */
    public class Base
    {
    
    
        public static void main(String args[])
        {
            int number;
            double val = 0;
            int length;
            int base;
    
            System.out.println("Enter value: ");
            Scanner scan1  = new Scanner(System.in);
            number = scan1.nextInt();
            length = String.valueOf(number).trim().length();
    
            System.out.println("Enter base value: ");
            Scanner scan2 = new Scanner(System.in);
            base  = scan2.nextInt();
    
            while(number>0)
            {
                double [] rem = new double[length];
                for(int i =0;i<length;i++)
                {
                    
                    rem[i] = number % 10;
                    number = number / 10;
    
                    val = val + (rem[i]*(Math.pow(base, i)));
                }
    
            }
    
            System.out.println("Value: "+val);
    
    
    
    
    
        }
    
    
    
    }

    Checked two three values. Harida danne na dan haduwe... i think this should work.:D:D:D
     
    Last edited:

    sachii

    Member
    Sep 17, 2008
    8,708
    268
    0
    Ĭǹ Ẏṑũř Ȟĕâřŧ
    pseudo code

    Code:
    START
        NUMERIC base,numbertemp,temp,i,numInt
        NUMERIC temp2=0
        CHAR[100] number
        DISPLAY "enter the base number"
        ACCEPT  base
        
        IF base LESS THAN 10 THEN
            DISPLAY "enter the number"
            ACCEPT number
            NUMERIC i= length(number)-1
    
            FOR EACH num IN number
                numInt = (NUMERIC)num
                temp = base ^ i
                temp2 = temp2 + (temp * numInt )
                i = i-1;
            END FOR EACH
        END IF
    
        DISPLAY "the answer is for " + base + " base is " + temp2 
    END
    thank u very much :)
     

    sachii

    Member
    Sep 17, 2008
    8,708
    268
    0
    Ĭǹ Ẏṑũř Ȟĕâřŧ
    Code:
    import java.util.*;
    
    /**
     *
     * @author Chinthy
     */
    public class Base
    {
    
    
        public static void main(String args[])
        {
            int number;
            double val = 0;
            int length;
            int base;
    
            System.out.println("Enter value: ");
            Scanner scan1  = new Scanner(System.in);
            number = scan1.nextInt();
            length = String.valueOf(number).trim().length();
    
            System.out.println("Enter base value: ");
            Scanner scan2 = new Scanner(System.in);
            base  = scan2.nextInt();
    
            while(number>0)
            {
                for(int i =0;i<length;i++)
                {
                    double [] rem = new double[length];
                    rem[i] = number % 10;
                    number = number / 10;
    
                    val = val + (rem[i]*(Math.pow(base, i)));
                }
    
            }
    
            System.out.println("Value: "+val);
    
    
    
    
    
        }
    
    
    
    }
    Checked two three values. Harida danne na dan haduwe... i think this should work.:D:D:D
    ye this is ok :)
    told to do this way thanks brother :)
     
    Jul 8, 2007
    1,138
    156
    0
    Melbourne
    ye this is ok :)
    told to do this way thanks brother :)

    Did a slight adjustment. That rem[] array declaration should come before the for loop. Mehema tibbath hari coz the val variable is carried onto the next loop and the new value is preserved. But this is better so it doesn't create a new array each time the loop runs.
     

    e06020

    Member
    Oct 28, 2009
    22
    2
    3
    ye i got clue but not enough to solve

    This is the Answer, but i have not include conditional statements to check weather the inputs are correct, for correct inputs this works fine

    here is the code :

    public class Convertor {

    public Convertor() {
    }
    public static void main(String args []){
    Convertor conv = new Convertor();
    int decimalValue = conv.getDecimal("121",3);
    System.out.println("Decimal value = " + decimalValue);
    }

    public int getDecimal(String num , int base){
    char [] numbers = num.toCharArray();
    int sum = 0 ;
    for (int i = numbers.length-1; i >= 0; i--) {
    int val = Integer.parseInt(numbers+"");
    int order = (numbers.length-1) - i;
    int ordermul = (int) Math.pow((double)base, order);
    sum = sum +ordermul*val;
    }

    return sum;
    }
    }
     

    sachii

    Member
    Sep 17, 2008
    8,708
    268
    0
    Ĭǹ Ẏṑũř Ȟĕâřŧ
    Did a slight adjustment. That rem[] array declaration should come before the for loop. Mehema tibbath hari coz the val variable is carried onto the next loop and the new value is preserved. But this is better so it doesn't create a new array each time the loop runs.
    ye im doing this now thanks :)

    This is the Answer, but i have not include conditional statements to check weather the inputs are correct, for correct inputs this works fine

    here is the code :

    public class Convertor {

    public Convertor() {
    }
    public static void main(String args []){
    Convertor conv = new Convertor();
    int decimalValue = conv.getDecimal("121",3);
    System.out.println("Decimal value = " + decimalValue);
    }

    public int getDecimal(String num , int base){
    char [] numbers = num.toCharArray();
    int sum = 0 ;
    for (int i = numbers.length-1; i >= 0; i--) {
    int val = Integer.parseInt(numbers+"");
    int order = (numbers.length-1) - i;
    int ordermul = (int) Math.pow((double)base, order);
    sum = sum +ordermul*val;
    }

    return sum;
    }
    }


    thanks dear big help
     

    madsugath

    Member
    Nov 30, 2008
    505
    18
    0
    Gampaha
    java mathaka netha.. :( uni eke 1st year kalee.. eva itin exam walata vtraneee.... :rofl:.. C# nam puluwan... bumpsssssssssssssssssssssss.......