decimal-octal problem..


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: decimal-octal problem..

  1. #1
    Join Date
    Oct 2002
    Posts
    4

    decimal-octal problem..

    I have an assignment due in soon and I really dunno where to start with this. I think the modulus function may be the key. Any tips?


    Develop a JAVA program which reads a decimal integer value and calculates and prints it octal equivalent.

  2. #2
    Join Date
    Oct 2002
    Posts
    66
    Ah right. I had to do a similar thing when I was working in Sony of the summer, to come up with a solution to randomly making the most different colours possible for bars on a web bar chart. This was a very difficult task and it meant making a Hex class and then using it in a file to randomly create colours. Also meant that I had to study a colour chart for a few hours to come up with a solution!

    I'll just E-mail my mates there and ask if they can send it to me and I'll post it here and hopefully it will help you. If not then I suppose I'll have to re-write it for you
    Meethoss

  3. #3
    Join Date
    Oct 2002
    Posts
    4
    Would be much appreciated.

  4. #4
    Join Date
    Oct 2002
    Posts
    66
    Here it is:

    Code:
    /**
     * 
    
    Title: Hex Class</p>
     * 
    
    Description: A class to allow the user to input a hex string or decimal colour to be stored, manipulated and output
     * as hex</p>
     * @author Stephen Pickett
     * @version 1.0
     */
    
    public class Hex {
    
        private int store[] = new int [3];
        private final String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
    
        /**
         *
         * @param red an integer representation of the amount of red required (min = 0, max = 255)
         * @param green an integer representation of the amount of green required (min = 0, max = 255)
         * @param blue an integer representation of the amount of blue required (min = 0, max = 255)
         */
        public Hex(int red, int green, int blue) {
            store[0] = red;
            store[1] = green;
            store[2] = blue;
        }
    
        /**
         *
         * @param red a string representation of the amount of red required (min = 00, max = FF)
         * @param green a string representation of the amount of green required (min = 00, max = FF)
         * @param blue a string representation of the amount of blue required (min = 00, max = FF)
         */
        public Hex(String red, String green, String blue) {
            for (int count = 0; count <= 2; count++) {
                String number = "";
                switch (count) {
                    case 0: number = red; break;
                    case 1: number = green; break;
                    case 2: number = blue; break;
                }
                Character temp;
                Character temp1;
                if (number.length() == 2) {
                    temp = new Character(number.charAt(0));
                    temp1 = new Character(number.charAt(1));
                } else {
                    temp = new Character((char) 0);
                    temp1 = new Character(number.charAt(0));
                }
                store[count] = ((convert(temp)*16) + (convert(temp1)*1));
            }
        }
    
        /**
         *
         * @return returns the hex number as a string
         */
        public String toString() {
            String temp = new String();
            for (int i = 0; i <= 2; i++) {
                int num = (int) Math.ceil(store[i]/16);
                temp += hex[num];
                num = (int) Math.ceil(store[i]%16);
                temp += hex[num];
            }
            return temp;
        }
    
        /**
         *
         * @param number the number (as a Character) to be converted to decimal (int) from hex (String)
         * @return the decimal form of the hex
         */
        private int convert(Character number) {
                if (Character.isDigit(number.charValue())){
                        return (int) Integer.parseInt(number.toString());
                } else {
                    String temp = Character.toString(Character.toUpperCase(number.charValue()));
                    if (temp.equals("A")) {
                        return 10;
                    } else if (temp.equals("B")) {
                        return 11;
                    } else if (temp.equals("C")) {
                        return 12;
                    } else if (temp.equals("D")) {
                        return 13;
                    } else if (temp.equals("E")) {
                        return 14;
                    } else if (temp.equals("F")) {
                        return 15;
                    } else {
                        return 0;
                    }
                }
            }
    
        /**
         *
         * @param colour colour to be incremented (0 = red, 1 = green, 2 = blue)
         * @param inc amount by which to increment the colour
         */
        public void increment (int colour, int inc) {
            if ((store[colour] + inc) < 255) {
                store[colour] += inc;
            } else {
                store[colour] = 255;
            }
        }
    
        /**
         *
         * @param colour colour to be decremented (0 = red, 1 = green, 2 = blue)
         * @param inc amount by which to decrement the colour
         */
        public void decrement (int colour, int dec) {
            if ((store[colour] - dec) > 0) {
                store[colour] -= dec;
            } else {
                store[colour] = 0;
            }
        }
    
        /**
         *
         * @param red an integer representation of the amount of red required (min = 0, max = 255)
         * @param green an integer representation of the amount of green required (min = 0, max = 255)
         * @param blue an integer representation of the amount of blue required (min = 0, max = 255)
         */
        public void set(int red, int green, int blue) {
            store[0] = red;
            store[1] = green;
            store[2] = blue;
        }
    
        /**
         *
         * @param red a string representation of the amount of red required (min = 00, max = FF)
         * @param green a string representation of the amount of green required (min = 00, max = FF)
         * @param blue a string representation of the amount of blue required (min = 00, max = FF)
         */
        public void set(String red, String green, String blue) {
            for (int count = 0; count <= 2; count++) {
                String number = "";
                switch (count) {
                    case 0: number = red; break;
                    case 1: number = green; break;
                    case 2: number = blue; break;
                }
                Character temp;
                Character temp1;
                if (number.length() == 2) {
                    temp = new Character(number.charAt(0));
                    temp1 = new Character(number.charAt(1));
                } else {
                    temp = new Character((char) 0);
                    temp1 = new Character(number.charAt(0));
                }
                store[count] = ((convert(temp)*16) + (convert(temp1)*1));
            }
        }
    }
    If you can't translate that into working with octal then let me know and I'll help you. I've got coursewok for Thursday, though, so I'll be pretty busy till then!

    Good luck
    Meethoss

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center