Question

In: Computer Science

Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia

Assignment 4 is at the bottom

Objective

Your program should prompt the user to enter a number of no greater than 8 digits. If the user enters a number greater than 8 digits or a value less than 0, it should re-prompt the user to enter a number again*. You do not need to check if the digits are valid octal numbers (0-7), as this is guaranteed.

Instructions

  • Note that the conversion logic should be in a separate method (not the main() method).
  • main() method will take the input from the user and pass it to the conversion() method.
  • The conversion() method will convert the input octal to decimal and print the output.
    • No return value is required.
    • a sample structure is shown below.
  • Use a sentinel while loop to solve the problem.
  • Ideally, you should copy your assignment 4 code here and modify it to serve the new purpose. This will save you time.
  • USE ONLY INTEGER VARIABLES FOR INPUTS AND OUTPUTS.
  • USE ONLY THE TECHNIQUES TAUGHT IN CLASS

main(){

int oct = user input;

conversion(oct);

}

void conversion(int o){

// logic goes here.

print(decimal);

}

Goals

  • more experience in WHILE loop.
  • experience in Java Methods
  • logical thinking

Sample Runs

Sample Program Run (user input is underlined)

Enter up to an 8-digit octal number and I will convert it for you: 77777777

16777215

Sample Program Run (user input is underlined)

Enter up to an 8-digit octal number and I will convert it for you: 775002

260610

Sample Program Run (user input is underlined)

Enter up to an 8-digit octal number and I will convert it for you: 0

0

Sample Program Run (user input is underlined)

Enter up to an 8-digit octal number and I will convert it for you: 55

45

Sample Program Run (user input is underlined)

Enter up to an 8-digit octal number and I will convert it for you: 777777777

Enter up to an 8-digit octal number and I will convert it for you: 777777777

Enter up to an 8-digit octal number and I will convert it for you: 700000000

Enter up to an 8-digit octal number and I will convert it for you: 77

63

Assignment 4:

// Main.java : Java program to convert decimal number to octal

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

int inputNum, convertedNum;

Scanner scan = new Scanner(System.in);

// Input of integer number in decimal

System.out.print("Please enter a number between 0 and 2097151 to convert: ");

inputNum = scan.nextInt();

// validate the number

if(inputNum < 0 || inputNum > 2097151)

System.out.println("UNABLE TO CONVERT");

else

{

int weight=0; // weight of the digit, used for converting to octal

convertedNum = 0; // variable to store the number in octal

int temp = inputNum;

int digit;

// loop that continues till we convert the entire number to octal

while(temp > 0)

{

digit = (temp%8); // get the remainder when the number is divided by 8

// multiply the digit with its weight and add it to convertedNum

convertedNum += digit*Math.pow(10, weight);

temp = temp/8; // remove the last digit from the number

weight++; // increment the weight

}

// display the integer in octal

System.out.printf("Your integer number " + inputNum + " is %07d in octal",convertedNum);

}

scan.close();

}

}

Solutions

Expert Solution

import java.util.Scanner;

public class OctalToDecimal {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter up to an 8-digit octal number and I will convert it for you: ");
       int num = sc.nextInt();
       conversion(num);
   }

   private static void conversion(int n) {
       int num = n;
       int dec_value = 0;

       // starting from 1 as 8^0 is 1
       int base = 1;
       int temp = num;
       while (temp > 0) {
           // fetching last digit using %
           int last_digit = temp % 10;
           // removing last digit
           temp = temp / 10;

           dec_value += last_digit * base;
           // increseing base to next like 8^1,8^2..
           base = base * 8;
       }
       System.out.println(dec_value);
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

<terminated> Octal ToDecimal [Java Application] C:\Soft\PegaEclipse-win64-4.5.2.2\PegaEclipse-win64-4.5 Enter up to an 8-digit octal number and I will convert it for you: 775002 260610


Related Solutions

Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a java program to convert a positive integer value to a roman number. Your program...
Write a java program to convert a positive integer value to a roman number. Your program should print a description, prompt them for how many numbers they need to convert and then loop that many times. Each time the program needs to prompt for a number and output the equivalent roman numeral. If the user enters a number less than 1 then output an error message. See sample output below: **************************************************** * Welcome to the Roman Numeral Converter!          * *...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of...
Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of each of these lists to generate a total sum Print the lists, and the total sum of each value C++ contains some built-in functions (such as itoa and std::hex) which make this assignment trivial. You may NOT use these in your programs. You code must perform the conversion through your own algorithm. The input values are: 5 9 24 2 39 83 60 8...
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a java program which reads a list of N integers and prints the number of prime numbers in the list. Input: The first line contains an integer N, the number of elements in the list. N numbers are given in the following lines. Output:...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT