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

RESULT ON THE SCREEN: ? 12 ? 7 OUTPUT = 12 The Memory class Create a class calle

ID: 3584277 • Letter: R

Question

RESULT ON THE SCREEN: ? 12 ? 7 OUTPUT = 12 The Memory class Create a class called Memory so that: The size of the array of the Memory object is provided as a parameter in the constructor. Assume that values are integers. Accessor and mutator methods arc provided to read or write at a specific memory address. Name these methods readFrom and writeTo respectively and use the following method signatures: void writeTo(int location, int value) int readFrom (int location) A method loadMemory initializes the array with the values provided in the above picture (it will correspond to a default program downloaded into memory). Remember that integers do not have leading 0's; as a result, the value 0909 can be represented as integer 909. The leading 0 is shown in the diagram to indicate the width of the value only with the first two digits (right hand side) being the opcode and the last two digits (left hand side) being the operation. Do not reset the size of the memory array in this method, but assume that it has been correctly sized when instantiated. A method called size which returns the total number of memory addresses. The Terminal class Create a class Terminal that provides: A method for returning the value entered from the keyboard. The method should first print the question mark, wait for a value, and then return it. Use the following method signature: int read() A method to prints on the screen a value given as parameter. The method should print the word OUTPUT followed by the equal sign and the value provided as parameter. Use the following method signature: void print(int value)

Explanation / Answer

The PHP community is large and diverse, composed of innumerable libraries, frameworks, and components. It is common for PHP developers to choose several of these and combine them into a single project. It is important that PHP code adhere (as close as possible) to a common code style to make it easy for developers to mix and match various libraries for their projects. The Framework Interop Group (formerly known as the ‘PHP Standards Group’) has proposed and approved a series of style recommendations, known as PSR-0, PSR-1 and PSR-2. Don’t let the funny names confuse you, these recommendations are merely a set of rules that some projects like Drupal, Zend, CakePHP, phpBB, AWS SDK, FuelPHP, Lithium, etc are starting to adopt. You can use them for your own projects, or continue to use your own personal style. Ideally you should write PHP code that adheres to one or more of these standards so that other developers can easily read and work with your code, and applications that implement the components can have consistency even when working with lots of third-party code. The first few recommendations are designed to be a super-set of the previous recommendation. Read about PSR-0 Read about PSR-1 Read about PSR-2 You can use PHP_CodeSniffer to check code against these recommendations. Use Fabien Potencier’s PHP Coding Standards Fixer to automatically modify your code syntax so that it conforms with these standards, saving you from fixing each problem by hand. English is preferred for all symbol names and code infrastructure. Comments may be written in any language easily readable by all current and future parties who may be working on the codebase. Back to Top Language Highlights Programming Paradigms PHP is a flexible, dynamic language that supports a variety of programming techniques. It has evolved dramatically over the years, notably adding a solid object-oriented model in PHP 5.0 (2004), anonymous functions and namespaces in PHP 5.3 (2009), and traits in PHP 5.4 (2012). Object-oriented Programming PHP has a very complete set of object-oriented programming features including support for classes, abstract classes, interfaces, inheritance, constructors, cloning, exceptions, and more. Read about Object-oriented PHP Read about Traits Functional Programming PHP supports first-class function, meaning that a function can be assigned to a variable. Both user defined and built-in functions can be referenced by a variable and invoked dynamically. Functions can be passed as arguments to other functions (feature called Higher-order functions) and function can return other functions. Recursion, a feature that allows a function to call itself is supported by the language, but most of the PHP code focus on iteration. New anonymous functions (with support for closures) are present since PHP 5.3 (2009). PHP 5.4 added the ability to bind closures to an object’s scope and also improved support for callables such that they can be used interchangeably with anonymous functions in almost all cases. Continue reading on Functional Programming in PHP Read about Anonymous Functions Read about the Closure class More details in the Closures RFC Read about Callables Read about dynamically invoking functions with call_user_func_array Meta Programming PHP supports various forms of meta programming through mechanisms like the Reflection API and Magic Methods. There are many Magic Methods available like __get(), __set(), __clone(), __toString(), __invoke(), etc. that allow developers to hook into class behavior. Ruby developers often say that PHP is lacking method_missing, but it is available as __call() and __callStatic(). Read about Magic Methods Read about Reflection Namespaces As mentioned above, the PHP community has a lot of developers creating lots of code. This means that one library’s PHP code may use the same class name as another library. When both libraries are used in the same namespace, they collide and cause trouble. Namespaces solve this problem. As described in the PHP reference manual, namespaces may be compared to operating system directories that namespace files; two files with the same name may co-exist in separate directories. Likewise, two PHP classes with the same name may co-exist in separate PHP namespaces. It’s as simple as that. It is important for you to namespace your code so that it may be used by other developers without fear of colliding with other libraries. One recommended way to use namespaces is outlined in PSR-0, which aims to provide a standard file, class and namespace convention to allow plug-and-play code. Read about Namespaces Read about PSR-0 Standard PHP Library The Standard PHP Library (SPL) is packaged with PHP and provides a collection of classes and interfaces. It is made up primarily of commonly needed datastructure classes (stack, queue, heap, and so on), and iterators which can traverse over these datastructures or your own classes which implement SPL interfaces. Read about the SPL Command Line Interface PHP was created primarily to write web applications, but it’s also useful for scripting command line interface (CLI) programs. Command line PHP programs can help you automate common tasks like testing, deployment, and application administrativia. CLI PHP programs are powerful because you can use your app’s code directly without having to create and secure a web GUI for it. Just be sure not to put your CLI PHP scripts in your public web root! Try running PHP from your command line: