Self-review Questions
2.1 Explain the meaning of piece.forward();. Why is the semicolon present?
The object referred to by piece is instructed to move forward. Actually, to be more precise: the method forward is called for the object referenced by piece. The actual movement made by the piece depends on how it interprets an invocation of the method forward—in the examples given in the chapter a piece will move one square forward, but different kinds of piece object could move differently. The dot associates the method call (forward) with the object that will undertake the method (the object referred to by piece), while the parentheses (()) cause the method call to take place.
The semicolon is a bit of syntax to mark the end of the method call statement. It can also be seen as a separator, separating this statement from the next. One reason for having the semicolon present is to simplify the implementation of the Java compiler by precisely marking the end of a statement. The semicolon also improves the readability of the code for people and can help remove ambiguities that otherwise might occur in more complicated sections of code. The semicolon in Java is directly analogous to the use of full stop in natural language, it marks the end of a sequence of syntactic features.
2.2 How many different interpretations of the following statement sequence are there? piece.forward ( ) ; piece.forward ( ) ; piece.right ( ) ; piece.forward ( ) ; piece.left ( ) ;
The result of executing these statements depends on the behaviour of the methods called.
Hence, the number of interpretations depends on the number of different versions of the methods—at the extreme, the number of interpretations is infinite (for example, a new version of forward can always be created that moves one square more than the previous version).
In practice, of course, the behaviour of each method will be fixed for a given program, so there will only be a single interpretation of the statement sequence. This makes the statement sequence deterministic, meaning that it will always perform the same movement actions when executed. The location of where the piece ends up will depend on its starting position.
2.3 Write down a sequence of statements that describes making a telephone call. Can it be done without using control statements?
Self-review Questions
One of the simplest sequences might be:
1. Pick up handset.
2. Dial number
3. Speak
4. Hang-up
This requires no control statements but is not robust nor particularly realistic. For example, it assumes the number being called is not engaged and that the conversation is limited to speaking a message and then hanging up. As soon as we want to manage errors or exceptions, not to mention actual human interaction (!) then control flow features are required.
2.4 Does the pseudocode below (from Section 2.5, Page 23) describe a general-purpose solution to starting from and moving to any square on a chessboard? while ( not at destination ) if ( destination is to the left ) piece moves left else if ( destination is to the right ) piece moves right; piece moves forward
The way to approach this question is to attempt to find a start position that does not lead to a solution which will immediate show that the algorithm is not a solution to the problem. Pencil and papers sketches can be used to try out ideas, while working with a small-sized board, say
5×5, would help speed up the search.
Following this approach, you would hopefully notice that the algorithm has a serious flaw—the piece can move forward, left or right but not backwards. Hence, for example, if the destination square is behind the starting square the forward direction of travel is away from the destination and the destination cannot be reached. Moreover, once the piece reaches the edge of the board the algorithm fails, as the
Introduction COMP 202 – Introduction to Computing 1 COMP 202 – Introduction to Computing 1 COMP-202 Unit 3: Conditional Programming CONTENTS: Boolean Expressions The if and if-else Statements • Suppose we want to write a program which asks the user to enter two numbers and then displays only the larger of the two • This will involve executing certain statements in some circumstances, and different statements in other circumstances • Problem: So far, all the programs we have written…
Windows? They are created using something called Programming. Programming, at its simplest form, is lines of code (or text) you type into a computer, to tell it to do the actions you specify. Computer programming is used to create almost everything now. From Games, to the stuff you see on the screen of your phone, and even the whole Windows from Microsoft. Programming is a growing concept, and beginning to become part of our everyday lives. Programming can be useful in many ways, you can learn: To dive deeper into the Computer…
Corequisites N/A Co-badged status This unit is not co-badged. Unit Description This unit is an introductory computer science unit, providing a practical introduction to basic computing and programming concepts. Students gain an understanding of, and practical experience in, computer programming; practical experience in implementing informal prose descriptions of problem solutions using an imperative language; an understanding of, and practical experience in, designing, coding, testing and…
Part 3 Introduction to Object Orientated Programming Chapter 8 Learning About Classes and Objects In this chapter, you will learn how to: • Define and describe benefits of designing programs using object orientated design techniques. • Analyze the differences between structured programming techniques and object oriented programming techniques. • Define and apply the components of class objects to a business problem. • Demonstrate…
about the organization’s financial position. - Problems limiting employee flexibility in benefits plan. The analyst as a business problem solver: • Has computer technology knowledge and programming expertise • Understands business problems • Uses logical methods for solving problems • Has fundamental curiosity • Wants to make things better • Is more of a business problem solver than a technical programmer Analyst’s Approach to problem solving: Research and understand the problem…
address space available not taking into account the actual size of the physical main memory available. A process can run even on a system which has less main memory than the amount required by the process. This negates one of the most fundamental restrictions in programming. The use of virtual memory also frees up the programmer. The programmer no longer needs to worry about the size constraints of the physical memory on every computer their program is going to be used. With that being said they can…
IT 210 – Checkpoint - Object-Oriented Data and Processes – Due Day #5 Declare Blade Control Bar as Real Declare Lawnmower as Real Declare Ignition Key as Real Declare Self Propel Drive as Real Open Self Propel Mower Operations Hold Blade control bar against the handle Insert Ignition Key as Real Turn Ignition Key Put Both Hands on Blade Control bar Walk with Self Propel Lawnmower End Program The objects involved in this task include the person that is going…
computer science as my career. My conversance to computer science began after I secured admission in the Reva Institute of Technology and Management in Bengaluru. The excellent facilities, course and faculty have given me a firm background of the fundamentals of computer science. Having been introduced to the diverse aspects of computer science during the course of my undergraduate studies, I have found computer science most intellectually satisfying and stimulating. Personal satisfaction also plays…
College Algebra 2, or 1 ½ to 2 years of high school algebra, or equivalent preparation Course Description This course develops skills in linear mathematics and probability. Topics include matrices, inverses, input-output analysis, linear programming, sets, counting, probability, the mathematics of finance, and statistics. Applications will be developed in business, economics, and the sciences. Course Outcomes Mathematics Course Outcomes: The outcomes of the College of Professional Studies’…