Question
(1) Binary number is the fundamental data representation in computer science. However, in most mathematical computations, people usually use decimal because its more intuitive and
(1) Binary number is the fundamental data representation in computer science. However, in most mathematical computations, people usually use decimal because its more intuitive and simpler. In the StackApps class, the decToBin() method takes an decimal integer and converts it to a binary number. The digits of the result binary number are stored in a char stack, stackBinary. Make sure you have the digits pushed on to the stack in the correct order. For example, the decimal number 123 is converted to binary number 1111011. Do not create any arrays or use anything from the Character, Integer, Double, and String classes in your implementation, including but not limited to Integer.parseInt, Integer.toString, Integer.toBinaryString, Character.getNumericValue, Character.forDigit. In general, you are only supposed to deal with the digits at the char and int level and make use of stacks. (2) When an integer has more than a certain number of digits, it is represented using scientific notation. The int type itself cannot be used to store numbers with more than 10 digits. In some cases, this may be inconvenient as people usually expect complete numbers and doing big integer computations are inevitable. The subtractBigInteger() method takes two numbers as CharStacks num1 and num2, and subtract the number stored in num2 from the one stored in num1, i.e., num1 - num2. To simply implementation, you can assume that the number stored in num1 is no less than the number stored in num2. For example, when subtracting 181,749 from 314,739,847, the two stacks and the result stack would look like the following: In the subtractBigInteger() method, you are provided with a char stack, stackResult. You will need to perform subtractions between the two operands and save the result on the result stack. Pay attention to the order of the digits (see above figure for reference). You should compute the subtraction between the two big numbers digit by digit (from low to high). Be sure to take care of borrows. Do not create any arrays or import the java.math.BigInteger library or anything from the Character, Integer, Double, and String classes in your implementation! Same as above, you are only supposed to deal with the digits at the char and int level and make use of stacks. Note that you are only supposed to touch the above two methods. You are NOT allowed to create any other methods, instance variables, or make any changes to methods other than the two method or files other than "StackApps.java". Points will be taken off if you fail to follow this rule.
4 public class StackApps{ 6 // convert a decimal number into a binary number and save it in a stack 7 // Do not create any arrays! Do not use any Java libraries to do the convertion. 8 // Doing so will result in points deduction. 9 public String decToBin(int numDec){ 11 CharStack stackBinary = new CharStack(64); // stack used to store the result binary number 13 // TODO: complete this method 16 return stackBinary.toString(); // return a string representation of the stack 17 } 19 // subtract num2 from num1 (both represented as CharStacks), and save the result on a stack 20 // Do not create any arrays! Do not use any Java libraries to do the calculation. 21 // Doing so will result in points deduction. 22 public String subtractBigInteger(CharStack num1, CharStack num2) { 24 CharStack stackResult = new CharStack(64); // stack used to store the result of the addition 26 // TODO: complete this method 29 return stackResult.toString(); // return a string representation of the stack 30 } 31 } 4 public class CharStack { 7 private char[] m_array; 8 private int m_index; 11 public CharStack(int cap) { 13 m_array = new char[cap]; 14 m_index = 0; 15 } 18 public boolean isEmpty() { 20 if (m_index == 0) 21 return true; 22 else 23 return false; 24 } 27 public char top() { 29 if (isEmpty()) 30 throw new RuntimeException("top attempted on an empty stack"); 31 else 32 return m_array[m_index - 1]; 33 } 36 public void push(char c) { 38 m_array[m_index] = c; 39 m_index++; 40 } 43 public char pop() { 45 if (isEmpty()) 46 throw new RuntimeException("pop attempted on an empty stack"); 47 else 48 { 49 char c = m_array[m_index - 1]; 50 m_index--; 52 return c; 53 } 54 } 57 public int size() { 59 return m_index; 60 } 63 @Override 64 public String toString(){ 66 String stackContent = ""; 68 for (int i = m_index - 1; i >= 0; i--) 69 stackContent += m_array[i]; 71 return stackContent; 72 } 73 }
8 public class TestStack{ 10 public static void main(String[] args) {
System.out.println("================ Problem 2 ================"); 27 TestP2(); 28 System.out.println("================ End of Problem 2 ================");
public static void TestP2() { 624 try { 626 ObjectInputStream in = new ObjectInputStream(new FileInputStream("testNumbers.dat")); 627 StackApps myApps = new StackApps(); 628 ArrayList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started