Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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 results = new ArrayList(); 629 ArrayList numbers = new ArrayList(); 630 results = (ArrayList)in.readObject(); 631 numbers = (ArrayList)in.readObject(); 633 String r; 634 String eMsg; 635 String currentLine; 636 int numPassedTests = 0; 637 int numTotalTests = 0; 639 for (int i = 0; i < 5; i++) 640 { 641 numTotalTests++; 642 currentLine = numbers.get(i); 644 r = ""; 645 eMsg = "N/A"; 646 try { 648 r = myApps.decToBin(Integer.valueOf(currentLine)); 649 } 650 catch (RuntimeException e) 651 { 652 eMsg = "RuntimeException - \"" + e.getMessage() + "\""; 653 } 655 System.out.print("Test " + numTotalTests + ": decToBin(" + currentLine + ") ==> "); 657 if (r.equals(results.get(i))){ 659 System.out.println("[Passed]"); 660 numPassedTests++; 661 } 662 else 663 System.out.println("[Failed]"); 665 System.out.println(" Expected: " + results.get(i)); 666 if (eMsg.equals("N/A")) 667 System.out.println(" Yours: " + r + " "); 668 else 669 System.out.println(" Yours: " + eMsg + " "); 670 } 672 for (int i = 5; i < numbers.size(); i++){ 674 numTotalTests++; 675 currentLine = numbers.get(i); 676 String[] operands = currentLine.split(" "); 677 CharStack num1 = new CharStack(64); // stack used to store number 1 678 CharStack num2 = new CharStack(64); // stack used to store number 2 680 // push digits of number 1 onto stack 681 for (int j = 0; j < operands[0].length(); j++) 682 num1.push(operands[0].charAt(j)); 684 // push digits of number 2 onto stack 685 for (int j = 0; j < operands[1].length(); j++) 686 num2.push(operands[1].charAt(j)); 688 r = ""; 689 eMsg = "N/A"; 690 try{ 692 r = myApps.subtractBigInteger(num1, num2); 693 } 694 catch (RuntimeException e) 695 { 696 eMsg = "RuntimeException - \"" + e.getMessage() + "\""; 697 } 699 System.out.print("Test " + numTotalTests + ": (" + operands[0] + " - " + operands[1] + ") ==> "); 701 if (r.equals(results.get(i))) 702 { 703 System.out.println("[Passed]"); 704 numPassedTests++; 705 } 706 else 707 System.out.println("[Failed]"); 709 System.out.println(" Expected: " + results.get(i)); 710 if (eMsg.equals("N/A")) 711 System.out.println(" Yours: " + r + " "); 712 else 713 System.out.println(" Yours: " + eMsg + " "); 714 } 716 System.out.println("Total test cases: " + numTotalTests + " Correct: " + numPassedTests + " Wrong: " + (numTotalTests - numPassedTests)); 717 } 718 catch (Exception e) 719 { 720 System.out.println("Error occurred: " + e.getMessage()); 721 } 722 } 723 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 8th International Symposium Sstd 2003 Santorini Island Greece July 2003 Proceedings Lncs 2750

Authors: Thanasis Hadzilacos ,Yannis Manolopoulos ,John F. Roddick ,Yannis Theodoridis

2003rd Edition

3540405356, 978-3540405351

More Books

Students also viewed these Databases questions

Question

Simplify the expression without a calculator. (827)1/27

Answered: 1 week ago

Question

what attributes makes you a good candidate for graduate school

Answered: 1 week ago