Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 3 -Write a program Submit a program called: DecTern.java Create a progra

ID: 3708695 • Letter: P

Question

Problem 3 -Write a program Submit a program called: DecTern.java Create a program that converts between decimal (base 10) and ternary (base 3). The ternary base system is composed of the following unique symbols: 0, 1, 2. You may find it helpful to look at class code and the previous HW You should, at a minimum, create the following methods 1) A method that intakes a base 10 number and returns a base 3 number. 2) A method that intakes a base 3 number and returns a base 10 number. 3) A method that gets user input. If you do not create these methods, you will not receive any credit for this problem. You are also welcome to create any helper methods. The program should have the following functionality: The user should be able to enter a number in base 10 or base 3. There should be some sort of indication of the base the number is in so that you know which type of conversion you will be doing. 1) The program should then convert the number to the opposite base. For example, if the user entereda number in base 10, the program should convert the number into base 3. 2) xi 3) The program should continue prompting for input and converting until the user enters the word "e No sample run.

Explanation / Answer

Solution >>>>>>>>>>>>

import java.util.Scanner;

public class DecTern {

//conversion from base 10 to base 3

public static long convertToBase3(int num) {

long ret = 0, factor = 1;

while (num > 0) {

ret += num % 3 * factor;

num /= 3;

factor *= 10;

}

return ret;

}

//conversion from base 3 to base 10

public static long convertToBase10(int num) {

int decimal = 0, p = 0;

while (num != 0) {

decimal += ((num % 10) * Math.pow(3, p));

num = num / 10;

p++;

}

return decimal;

}

public static int getUserInput() {

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = in.nextInt();

return n;

}

public static void main(String[] agrs) {

String str = "";

int number;

long base10Num, base3Num;

while (!str.equalsIgnoreCase("Exit")) {

System.out.println("1. Convert Base 10 to Base 3");

System.out.println("2. Convert Base 3 to Base 10");

System.out.println("Type "exit" to stop");

System.out.println("Enter choice...");

Scanner in = new Scanner(System.in);

str = in.nextLine();

switch (str) {

case "1":

System.out.println("Conversion from Base 10 to Base 3");

number = getUserInput();

base3Num = convertToBase3(number);

System.out.println("Converted Base 3 number is : " + base3Num+" ");

break;

case "2":

System.out.println("Conversion from Base 3 to Base 10");

number = getUserInput();

base10Num = convertToBase10(number);

System.out.println("Converted Base 10 number is : " + base10Num+" ");

break;

case "exit":

System.out.println("Thank you");

break;

default:

System.out.println("invalid input ! ");

break;

}

}

}

}

Output >>>>>>>>>>>>>

1. Convert Base 10 to Base 3
2. Convert Base 3 to Base 10
Type "exit" to stop
Enter choice...
1
Conversion from Base 10 to Base 3
Enter a number: 3
Converted Base 3 number is : 10

1. Convert Base 10 to Base 3
2. Convert Base 3 to Base 10
Type "exit" to stop
Enter choice...
2
Conversion from Base 3 to Base 10
Enter a number: 10
Converted Base 10 number is : 3

1. Convert Base 10 to Base 3
2. Convert Base 3 to Base 10
Type "exit" to stop
Enter choice...
exit
Thank you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote