Question

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();

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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();
       int decNum = conversion(num);
       System.out.println(decNum);
   }

   private static int 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;
       }
       return 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

Add a comment
Know the answer?
Add Answer to:
Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Description For this program, you are going to convert decimal (integer) numbers into their octal number...

    Description For this program, you are going to convert decimal (integer) numbers into their octal number (integer) equivalents. Make sure that you create a new Project and Java class file for this assignment. Your Repl.It file should be named “Main.java”. You can read about octal-to-decimal number conversions from wikepedia or another website Instructions 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...

  • Java: can also use “if else” statements Write a program that can convert an integer between...

    Java: can also use “if else” statements Write a program that can convert an integer between 0 and 15 into hex number (including 0 and 15). The user enters an integer from the console and the program displays the corresponding hex number. If the user enters an integer out of range, the program displays a warning message about the invalid input. Table. Conversion between Decimal and Hexadecimal Decimal Hexadecimal 0 0 1 1 2 2 3 3 4 4 5...

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers....

    (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digits I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately....

  • 1. Palindrome Write a program that prompts the user for a positive integer number and output whet...

    Language is C 1. Palindrome Write a program that prompts the user for a positive integer number and output whether the number is a palindrome or not. A palindrome number is a number that is the same when reversed. For example, 12321 is a palindrome; 1234 is not a palindromoe. You will write a function named as palindrome. The function will have two arguments, number and isPalindrome. Both arguments are pass-by-reference. The argument number is a pointer to a variable...

  • a) The following program is supposed to convert a binary number to its decimal equivalent. Modify...

    a) The following program is supposed to convert a binary number to its decimal equivalent. Modify the program so that it does the job.(15 points) b) How would you create the executable file of the program (file name: b2d.c) in Linux? (5 points) #include <stdio.h> int main (void) int binary: printf("Enter a binary number:\n"); scanf("%d", &binary): int nofDigit = 0, remain = binary: while (remain > 0) - remain = remain/10; nofDigit++; int digit, dval = 0, bx = binary:...

  • C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl;...

    C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl; #include <cmath> #include <cstring> class nonNumber : public exception { public: /* write definition for the constructor */ -- Message is “An invalid input was entered” }; int castInput( char *s ) { char *temp = s; int result = 0, negative = 1; // check for minus sign if ( temp[ 0 ] == '-' ) negative = -1; for ( int i...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT