This assignment is due on 07/17/17 at 11:59:59am. Part1: Binary Print Write a re
ID: 3913995 • Letter: T
Question
This assignment is due on 07/17/17 at 11:59:59am.
Part1: Binary Print
Write a recursive method that prints the binary form of a given non-negative integer, with the following specification:
The method prints the value of n as a BINARY number. If n is zero, then a single zero is printed; otherwise no leading zeros are printed in the output. Examples:
Hint: How to convert an int to its binary form? A very intuitive way is to divide the number by 2 in each recursion. In each recursive call, you print either '0' (unless it is a leading zero) if 2 divides n, or '1' otherwise.
Part2: Evaluate Arithmetic Expressions
Requirements:
Implement a concrete ArrayStack class that extends the Stack interface as we discussed in the class (any other different Stack class implementation, even if it is implemented by yourself, will not receive any credit).
Write a method that evaluates an arithmatic expression, which is given by a string.
Your implementation is required to use the Stack interface we discussed in the class.
Write a necessary test program to verify the result. For example,
You may want to test all possible corner conditions to make sure your implement is inclusive.
Your implementation only considers +, - and * operators and does not need to consider parentheses.
Bonus: extra 10 points will be awarded if you implementation supports parentheses. For examples:
Submission:
Each student submits one copy of the source code: NPrint.java, Evaluate.java, Stack.java and ArrayStack.java. Please also provide the corresponding test programs.
Create a folder and name it as your Linux user account, e.g., hwang_hw4, by the following command:
Copy all your source code to the above folder (clean your source code folder and remove all class files). Linux copy command is "cp filename destination_folder".
Compile a README file (in text format only, do not use Microsoft, use gedit instead) and provide the following:
Your name and CSUID
Compiling instruction
A sample test run (again in text)
Existing bug (things not finished)
Log in grail, go to the parent director of the folder you created, and run (suppose the your folder is "hwang_hw4")
If there is no error message, your submission is successful.
Please Note that: The program should be according to the assignment given and should use the following notes.
Explanation / Answer
public class NPrint {
public static void binaryPrint(int n) {
if (n>=1)
{
// recursively calling the mehtod
binaryPrint(n/2);
// printing the remainder
System.out.print(n%2);
}
if (n==0)
System.out.print(0);
}
public static void main(String[] args) {
NPrint.binaryPrint(0);
System.out.println();
NPrint.binaryPrint(4);
System.out.println();
NPrint.binaryPrint(27);
}
}
/*SAMPLE OUTPUT
0
0100
011011
*/
// Note: One question at a time please -- Policy of Chegg