Term 1: Lesson 30 1/2 - Coding Activity For the following activity, you will submit your entire program to the code runner, from your first import statement to your last curly bracket.
Please download the Lesson 30 1/2 Activity template (Links to an external site.)Links to an external site. and use it to write your code. Additionally, you can download the Lesson 30 1/2 Coding Activity (PDF) (Links to an external site.)Links to an external site. to print or use as an offline reference. If you need a refresher on how to use this Code Runner, watch the "Submitting code" video in the Course Introduction. NOTICE: Code runner requires a complete program. Please download the template and p df (Links to an external site.)Links to an external site. and use it to write your program in Dr Java. When you have finished your program, copy its entire contents, from the first import to the last curly bracket, into the code runner below. Octal, or base eight, numbers work the same way as base 2. The base is eight, and we use the digits 0 - 7. So, for example, in octal the number 241 represents:
82
|
|
81
|
|
80
|
|
2 * 82
|
+
|
4 * 81
|
+
|
1 * 80
|
|
2 * 64
|
+
|
4 * 8
|
+
|
1 * 1
|
|
128
|
+
|
32
|
+
|
1
|
= 161 in base ten
|
For this program you will input an int to represent the octal number and translate to the base ten number. The octal number must be 8 digits or less. Your program should also check that all the digits are 0 - 7, then translate the number to base ten. Sample Run 1: Enter a number in base 8: 123 83 Sample Run 2: Enter a number in base 8: 1287 ERROR: Incorrect Octal Format Sample Run 3: Enter a number in base 8: 1111111111 ERROR: Incorrect Octal Format I have this so far: import java .util. Scanner; import java .until .Arrays; class Lesson_1011_Activity { static int octal To Decimal(int n) { In t num = n; int dec_ value = 0; // Initializing base value to 1, i .e 8^0 i n t base = 1; int temp = num; while (temp > 0) { // Extracting last digit int last_ digit = temp % 10; temp = temp / 10; // Multiplying last digit with appropriate // base value and adding it to dec _value dec _value += last_ digit * base;
base = base * 8; } return dec_ value; } static boole an validate Number(int n) { int rem;
int count = 0; while(n>0){ rem = n%10; if(rem >7){ return false; } n = n/10; count++; if(count>8){ return false; } } return true; } public static void main(String[] args){ int num = 67;
Scanner sc = new Scanner(System .in); System. out. print("Enter a number in base 8 : "); num = sc. Next Int (); if(validate Number(num)){ System .out. print l n (octal To Decimal (num)); }else{ System .out .print l n ("ERROR: Incorrect Octal Format"); } sc. close(); } } When this is run against a set of values from the website, none of them work as this message comes up: Your code has been evaluated against a set of test data.
You had 0 out of 5 tests pass correctly. Double check your results in dr Java and if you can't see what's wrong, don't hesitate to start a post on Piazza. Test 1, Sample Run 1: Failed. Test 2, Sample Run 2: Failed. Test 3, Sample Run 3: Failed. Test 4, Random Octal Number: Failed. Test 5, Random Octal Number: Failed.