Results 1 to 3 of 3

Thread: Dr. Java program

  1. #1
    Join Date
    Nov 2011
    Posts
    2

    ohmy Dr. Java program

    Here is my program... It works it just doesn't do everything I want it to do... I also would like to convert integers into Binary numbers... I have the method but I don't know how to put it in the main method??? I'm very confused... Plz help!!!
    import java.util.*;

    public class Binary1
    {
    public static final Scanner CONSOLE = new Scanner(System.in);

    public static void main(String[] args)
    {
    System.out.println("Lab 8 written by Steven Jones");
    System.out.println("");

    String[] noOrYes = new String[] {"no", "yes"};
    boolean keepGoing = true;

    while (keepGoing)
    {
    String bitString = promptForBitString("Please enter in a binary number: ");
    int[] array = bitStringToBitArray(bitString);
    int val = convertBitArrayToInt(array);
    System.out.printf("The binary number \'%s\' = %d in decimal.%n", bitString, val);
    System.out.println("");


    int i = promptForChoice("Do you want to continue?", noOrYes);
    if (i == 0)
    {
    keepGoing = false;
    }
    }
    }

    public static int convertBitArrayToInt(int[] array)
    {
    int sum = 0;
    int placeValue = 1;
    for (int i=0; i < array.length; ++i)
    {
    if (array[i] == 1)
    {
    sum += placeValue;
    }
    placeValue *= 2;
    }
    return sum;
    }
    public static int[] convertIntToBitArray(int value) {
    // assume value is a positive integer
    //first declare an array of 31 element
    int[ ] res = new int[31]; //initially all element has the value of 0

    int index =0; //initialize index as 0

    while (value == 0){
    if (value%2 == 1)
    res[index] = 1;
    else
    res[index] = 0;

    value = value/2; //divide the value by 2 to get the next bit
    index ++;
    }
    return res;
    }

    // when you prompt for a non-negative int, you will do something similar
    // e.g.,: public static int promptForNonNegative(String prompt)
    // - Will use nextInt() instead of next()
    // - Will check (input >= 0) instead of isBitString(input)\
    // - 'input' will be an int!
    // - The error message will also be different

    public static String promptForBitString(String prompt)
    {
    // Donald Knuth's n-1/2 loop
    for (;
    {
    System.out.print(prompt);
    String input = CONSOLE.next();
    if (isBitString(input)) {
    return input;
    }
    System.err.printf("\"%s\" contains characters other than 0 or 1. Please re-enter.%n", input);
    }
    }

    // should return true if the String only contains 0s and 1s
    public static boolean isBitString(String s)
    {
    for (int i = 0; i < s.length(); i++)
    {
    char c = s.charAt(i);
    if (c != '0' && c != '1')
    {
    return false;
    }
    }
    return true;
    }

    // Prompts the user with the given string 'prompt' and restricts the user's
    // input selection to be only one of the strings in the 'choices' array.
    // Returns the index of the choice made by the user.
    public static int promptForChoice(String prompt, String[] choices)
    {
    boolean selectionValid = false;
    int selection = -1;
    while (!selectionValid)
    {
    // prompt the user and get their selection
    String choicesList = separate(choices, ", ");
    System.out.printf("%s [%s]: ", prompt, choicesList);
    String choiceVerbatim = CONSOLE.next();
    String choice = choiceVerbatim.toUpperCase();

    // check the selection to see if it's valid
    for (int i=0; i<choices.length; ++i)
    {
    String candidate = choices[i].toUpperCase();
    if (candidate.startsWith(choice))
    {
    selection = i;
    selectionValid = true;
    break;
    }
    }
    if (!selectionValid)
    {
    System.err.printf("\"%s\" is not a valid selection, please try again.%n",
    choiceVerbatim);
    }
    }
    return selection;
    }

    // Returns a string containing all of the given strings (in 'list')
    // separated by the given 'separator'.
    public static String separate(String[] list, String separator)
    {
    // accumulation pattern
    String result = "";
    for (int i=0; i<list.length; ++i)
    {
    result += list[i];
    if (i != list.length - 1)
    {
    result += separator;
    }
    }
    return result;
    }

    // converts a bit String into an int array
    public static int[] bitStringToBitArray(String bitString)
    {
    int bLength = bitString.length();
    int[] bitArray = new int[bLength];
    int bIndex = bLength - 1;
    for (int i = 0; i < bLength; i++)
    {
    char c = bitString.charAt(bIndex);
    if (c == '0')
    {
    bitArray[i] = 0;
    }
    else if (c == '1')
    {
    bitArray[i] = 1;
    }
    else
    {
    throw new IllegalArgumentException
    (bitString + " should only contain 0s and 1s");
    }
    bIndex--;
    }
    return bitArray;
    }

    // converts an int array into a bit String
    public static String bitArrayToBitString(int[] bitArray) {
    int bLength = bitArray.length;
    String bitString = "";
    for (int i = 0; i < bLength; i++)
    {
    if (bitArray[i] == 0 || bitArray[i] == 1)
    {
    bitString = bitArray[i] + bitString;
    }
    else
    {
    throw new IllegalArgumentException
    (Arrays.toString(bitArray) + " should only contain 0s and 1s");
    }
    }
    // eliminate leading 0s
    int firstOne = bitString.indexOf('1');
    if (firstOne == -1)
    {
    // true if bitString is all 0s
    bitString = "0";
    }
    else
    {
    bitString = bitString.substring(firstOne, bLength);
    }
    return bitString;
    }
    }

  2. #2
    Join Date
    Jan 2006
    Posts
    605

    Re: Dr. Java program

    Check the below method that takes an integer type value and converts it into octal type data:

    Code:
    public class DataConversion{
      public static void main(String[] args) {
      System.out.println("Data types conversion example!");
      int in = 50;
      System.out.println("Integer: " + in);
      //integer to binary
      String by = Integer.toBinaryString(in);
      System.out.println("Byte: " + by);
      //integer to hexadecimal
      String hex = Integer.toHexString(in);
      System.out.println("Hexa decimal: " + hex);
      //integer to octal
      String oct = Integer.toOctalString(in);
      System.out.println("Octal: " + oct);
      }
    }

  3. #3
    Join Date
    Nov 2011
    Posts
    2

    Re: Dr. Java program

    This is the task I'm trying to achieve with the program... I did everything but output converting integers into binary form.

    Your program should have a method to convert decimal numbers to binary numbers and a method to convert binary numbers to decimal numbers. The binary numbers will be represented as int arrays. Methods to convert between arrays and Strings are provided.

    Your program should ask the user to enter a nonnegative int. Print a warning message if the value is less than 0, else convert the int to a binary number. Both the int and the binary number should be printed.

    Also, your program should ask the user to enter a binary number. Your program should read this in as a String. Print a warning message if the String contains characters other than 1 and 0; you should have a method to determine if the String only contains 0s and 1s. Convert the binary number into an int. Both the the binary number and the int should be printed.

    Your program should have a while loop so that the user can continue to enter another int and binary number. Ask the user whether he or she wants to continue. You might use your matchesChoice method from Lab 7 to ensure that the user answers yes or no.

Similar Threads

  1. How to run java applet program using cmd
    By Rao's in forum Software Development
    Replies: 3
    Last Post: 07-01-2012, 03:35 PM
  2. Tagging java program
    By MonDo021 in forum Software Development
    Replies: 1
    Last Post: 01-04-2011, 11:36 PM
  3. GUI image program java
    By Caden Fernandes in forum Software Development
    Replies: 3
    Last Post: 12-11-2009, 11:48 AM
  4. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM
  5. How do i excute java program from asp.net
    By softte in forum Software Development
    Replies: 2
    Last Post: 09-05-2009, 02:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,675,089,901.87093 seconds with 17 queries