Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In CS4B we will be creating various views for this assignment, in CS4A you have to make sure you Model works to specs. In class

In CS4B we will be creating various views for this assignment, in CS4A you have to make sure you Model works to specs. In class we will be discussing MVC (Model-View-Controller) design pattern. MVC is a popular design pattern used in Windows 8, and Web Application Frameworks such as Apache Struts. For this assignment HPCalculatorModel is the model it contains both the data a Stack and also the business logic of an HP (Hewlett Packard) RPN (Reverse Polish Notation) calculator. The view provides what the user sees (i.e., HPCalculatorView) and the controller is the interface, (i.e., HPCalculatorController).

The HPCalculator Zip file f consists of the HPCalculatorModel.java and Op.java classes. For submit you will need to make Op a friendly class and also implement all Op Objects that are defined in HPCalculator Model. Our HP calculator will be modeled after the typical HP RPN calculator such as HP15C, (it shows stack). The only difference between our Calculator and the PH15C is that our stack is pop, empty elements are set to 0.0, in the HP15C once a stack is set to a value the pointer is rotated down, but the values remains. If unfamiliar with RPN try out these RPN calculators also: simple RPN(shows stack), older HP-35 RPN (click image), more advance RPN w stack display.

Specifically what you need to do....

Finish Writing the Model, complete all current op codes, and also change the help system so that it prints out 6 columns instead of just 4 columns.

Radians are the default for trigonometric functions.

Verify that your model works correctly by using the main function.

You main function needs to support a command line interface to the HPCalculatorModel. For submit you are just reading a line, then writing the line.

In lab you will be testing a full set of jUnit tests.

submit HPCalculatorModel.java you will have to make the Op class a friendly class of HPCalculatorModel.

In CS4B we will add GUI interfaces (views) to your calculator.

For submit you will read one line for standard input and then send that line to your push method, when an input line throws an exception from the model, print out the keyword Error: and the exception message, then begin processing the next line.

Turns out company is cutting costs by not hiring new employees, so no new Model employee. Instead, they are forcing you to work weekends. This weekend you get a request from your boss Friday night, that he needs you to write a full jUnit component test of the HPModel by Monday morning. However, to make you feel good, they have hired a programmer to write the controller portion of HP Calc MVC. Management is in awe of the new controller employee.

In this assignment you need to jUnit test HPCalculator Model and Op. You need to verify that each operation in the model works. We will discuss jUnit in class, but you may refer to jUnit JavaDoc | jUnit PDF Chapter, also Canvas has PDF copies of view graphs reviewed in class. Note goal of jUnit test is to call methods in HPCalculatorModel with known actual arguments and verify the correct return value. jUnit testing is a methodology for achieving code refactoring, in code refactoring we need a methodology to be able to change part of a Class, and quickly verify entire system integrity. jUnit testing does specific testing on each component (aka methods) of a class. If all methods have a quick way of verifying integrity chances are the entire system is still correct.

The employee who quit did leave behind the following jUnit testing Strings, - you also have this jUnit data they have been verified...

 String[] s = { "3+", "k3 +", "2 1-", "5.2 3*", "7.4E2/", "6 2/", "6 2%", "2.5E3.5%", "360r", "p2*c", "2E3^", "k3.67E3.2^", "k3e", "k2el", "3.34e2*l", "1.4T", "1000L", "2.3TL", "k2.3S", "o", "16q", "2.3Qq", "100q", "k3E2R", "k3 2+SRR", "C", "3.0p*", "ps", "pc", "pt", "pd", "K.321", "E3+" }; String[] expResult = { "3.0", "6.0", "1.0", Double.toString( 5.2*3 ), "3.7", "3.0", "0.0", "-1.0", Double.toString( Math.toRadians( 360.0 ) ), "1.0", "8.0", Double.toString( Math.pow( 3.67, 3.2) ), Double.toString( Math.exp( 3.0 ) ), "2.0", Double.toString( Math.log( Math.exp( 3.34 )*2.0) ), Double.toString( Math.pow( 10.0, 1.4) ), "3.0", "2.3", /* 2.0 and 2.3 may have round off */ "-2.3", Double.toString( 1.0/-2.3 ), "4.0", "2.3", "10.0", "3.0", "-5.0", "0", Double.toString( 3.0*Math.PI ), Double.toString( Math.sin( Math.PI) ), /* got roundoff not 0.0 */ "-1.0", Double.toString( Math.tan( Math.PI ) ), "180.0", ".321", "3.321" };

Use the above two Strings in your jUnit testPush_String, these strings are only partially complete you need to test all opcodes except for special ones like ?, H, P, which we have separate testing methods for. You also need to verify correct stack operation, to do this create a test method testStackContents, (nota bene, by default jUnit testing only creates test methods that parallel methods in the target class), but you can also add additional methods to the test file. testStackContents should use these or similar testing String arrays. Nota Bene, you can create a stack that is different, but the end result is critical, as a result submit doesn't test the code P. ...

 /** for round off its better to go with Double.toString( )... */ String[] s ={ "3E2E1E4.5P", "kP", "1 2 3 KP", "3.2E3.5CP", /* C sets display to 0 no change on stack */ "3 2 1 4.5P", "KP", /* big K kills stack and resets Display to 0 */ "3TP", "2P", /* Unary operators push so 3T will push a 1000.0 */ "K5pSP", /* 5 gets pushed -PI pushed, display set null */ "K2 3pP", "K", "pcP", "CP" }; /* note you do have assertDouble with required precision, but I'm matching double.toString when I have roundoffs */ String[] expResult = { "StringBuffer:4.5 Stack:3.0,2.0,1.0,", "StringBuffer:4.5 Stack:", "StringBuffer: Stack:", "StringBuffer:0 Stack:3.2,", "StringBuffer:4.5 Stack:3.2,3.0,2.0,1.0,", /* 3.2 from previous push */ "StringBuffer: Stack:", "StringBuffer: Stack:1000.0,", "StringBuffer:2 Stack:1000.0,", "StringBuffer:" + " Stack:5.0," + -1.0*Math.PI + ",", /* 5 push PI print stack */ "StringBuffer:" + " Stack:2.0,3.0," + Math.PI + ",", /* not PI will push the 3 */ "", //K kills stack and return empty string "StringBuffer: Stack:-1.0,", //pcP PI pushes display 0, cosine PI which is -1.0 "StringBuffer:0 Stack:-1.0," //CP clear display set to 0, previous stack };

Finally we even got the test Help expected result string

String expResult = "Code Description Code Description Code Description " + " + Addition - Subtraction * Multiplication " + " % y % x ieeeRemainder / x / y ^ y^x " + " e e^x l ln x L Log Base 10 " + " T 10^x S change sign o 1.0 divide x " + " q square root x Q x*x Square R rotate x y " + " C Clear Display E Enter p PI 3.14 " + " s sine x c cosine x t tangent x " + " d to degrees r radians k kill stack " + " K Kill All D Display P printStack " + " ? Help O Op codes X Exit "; 

Hints...

Don't fix help using jUnit testing, first analyze the descriptions and make sure the file HPCalculatorModel.java has the same descriptions. Your Version of help has only 4 columns.

You should also test white spaces...

 /** White space pushes at most once, E pushes each time you enter E,
* Once you are in a push state (either E or white space) white spaces are ignored * when you pop a stack element it is gone, * all missing elements are default 0.0 * therefore * 3 space E will push 3 twice, once for space once for E * 3 E space will push only once (once in a push state white spaces are ignored) * K3E+ K kill stack and display, push 3, 3+3=6 display is 6 stack is popped * @throws Exception */ @Test public void testPush_Whitespace() throws Exception { System.out.println("push whitespace"); String[] s = { "K3+", //use default stack is 0 3 "K3 E++", //should push twice 9 "K3 E +++", //still 9 2nd white space ignored "K3E ++", //should push once 6 "K3 ++", /*multiple spaces become one white space push 3 once ignore repeated spaces result is 0+3+3+6 note 3 is on stack and also on display (refer to HP15C link) */ "K3EE++", /*Kill Stack and display 3 push twice =3+3+3=9 */ "K3E P++", /*Kill Stack and Display 3Push (ignore spaces) 0+3+3=6 */ "K6 5EEP+++", /*Kill6push5pushpush 6+5+5+5=21 */ "K6 5E E P++++ ", /* 6+5+5+5(display) = 21 spaces ignored inside of E */ "K6E5EEE++++", /* 6+5+5+5+5=26.0 each E pushes the 5 note 15C files stack with 6 on way down, we set pop stack to 0*/ "K11 E++" /* space pushes 11, E pushes it again */ }; String[] expResult = { "3.0", "9.0", "9.0", "6.0", "6.0", "9.0", "6.0", "21.0", "21.0", "26.0", "33.0" }; HPCalculatorModel instance = new HPCalculatorModel(); for( int i=0; i < s.length ; ++i ) { String result = instance.push(s[i]); assertEquals( s[i], expResult[i], result); } }

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

Step: 3

blur-text-image

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

PostgreSQL 10 High Performance Expert Techniques For Query Optimization High Availability And Efficient Database Maintenance

Authors: Ibrar Ahmed ,Gregory Smith ,Enrico Pirozzi

3rd Edition

1788474481, 978-1788474481

More Books

Students also viewed these Databases questions

Question

What reward will you give yourself when you achieve this?

Answered: 1 week ago

Question

1. What are your creative strengths?

Answered: 1 week ago

Question

What metaphors might describe how we work together?

Answered: 1 week ago