Part I. The Picture class



Similar documents
1 Description of The Simpletron

3.1. RATIONAL EXPRESSIONS

Determine If An Equation Represents a Function

Computer Science 281 Binary and Hexadecimal Review

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

9.4. The Scalar Product. Introduction. Prerequisites. Learning Style. Learning Outcomes

Integers (pages )

for the Bill Hanlon

Understanding class definitions

Pocantico Hills School District Grade 1 Math Curriculum Draft

1.2 Linear Equations and Rational Equations

=

Scheduling. Getting Started. Scheduling 79

Section 1.1 Linear Equations: Slope and Equations of Lines

Pre Calculus Math 40S: Explained!

Section 1.5 Exponents, Square Roots, and the Order of Operations

Paper 1. Mathematics test. Calculator not allowed. First name. Last name. School KEY STAGE TIER

Mathematics Success Grade 6

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

YOU MUST BE ABLE TO DO THE FOLLOWING PROBLEMS WITHOUT A CALCULATOR!

Examples of Tasks from CCSS Edition Course 3, Unit 5

Let s put together a Manual Processor

JobTestPrep's Numeracy Review Decimals & Percentages

1 One Dimensional Horizontal Motion Position vs. time Velocity vs. time

If A is divided by B the result is 2/3. If B is divided by C the result is 4/7. What is the result if A is divided by C?

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

Section 4.1 Rules of Exponents

Math Workshop October 2010 Fractions and Repeating Decimals

Financial Mathematics

Figure 1.1 Vector A and Vector F

Part 1 Foundations of object orientation

6.4 Normal Distribution

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

Numerator Denominator

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

In this Chapter you ll learn:

IV. ALGEBRAIC CONCEPTS

Notes on Assembly Language

Graphing Linear Equations

Accuplacer Arithmetic Study Guide

Stupid Divisibility Tricks

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

Multiplying and Dividing Signed Numbers. Finding the Product of Two Signed Numbers. (a) (3)( 4) ( 4) ( 4) ( 4) 12 (b) (4)( 5) ( 5) ( 5) ( 5) ( 5) 20

OA3-10 Patterns in Addition Tables

MATH 60 NOTEBOOK CERTIFICATIONS

Lecture 8 : Coordinate Geometry. The coordinate plane The points on a line can be referenced if we choose an origin and a unit of 20

16-bit ALU, Register File and Memory Write Interface

The Function Game: Can You Guess the Secret?

EC 362 Problem Set #2

Solve addition and subtraction word problems, and add and subtract within 10, e.g., by using objects or drawings to represent the problem.

Number Sense and Operations

Mathematics Navigator. Misconceptions and Errors

Session 7 Fractions and Decimals

Basic Formulas in Excel. Why use cell names in formulas instead of actual numbers?

0.8 Rational Expressions and Equations

Arithmetic 1 Progress Ladder

Chapter 5 Instructor's Manual

Order of Operations More Essential Practice

Vocabulary Cards and Word Walls Revised: June 29, 2011

Indicator 2: Use a variety of algebraic concepts and methods to solve equations and inequalities.

Paper 1. Calculator not allowed. Mathematics test. First name. Last name. School. Remember KEY STAGE 3 TIER 6 8

Lies My Calculator and Computer Told Me

The Point-Slope Form

To Evaluate an Algebraic Expression

Elements of a graph. Click on the links below to jump directly to the relevant section

Sunny Hills Math Club Decimal Numbers Lesson 4

FIRST GRADE MATH Summer 2011

Year 9 set 1 Mathematics notes, to accompany the 9H book.

CNCTRAIN OVERVIEW CNC Simulation Systems

Custom Linetypes (.LIN)

Mathematical goals. Starting points. Materials required. Time needed

The Euclidean Algorithm

Udacity cs101: Building a Search Engine. Extracting a Link

EE6-5 Solving Equations with Balances Pages 77 78

7.4A/7.4B STUDENT ACTIVITY #1

Colour by Numbers Image Representation

Graphing - Slope-Intercept Form

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

2. Select Point B and rotate it by 15 degrees. A new Point B' appears. 3. Drag each of the three points in turn.

Unit 6 Number and Operations in Base Ten: Decimals

MATHEMATICS. Y5 Multiplication and Division 5330 Square numbers, prime numbers, factors and multiples. Equipment. MathSphere

Toothpick Squares: An Introduction to Formulas

MACHINE INSTRUCTIONS AND PROGRAMS

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Rules of Exponents. Math at Work: Motorcycle Customization OUTLINE CHAPTER

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Named Memory Slots. Properties. CHAPTER 16 Programming Your App s Memory

Section 1.4 Place Value Systems of Numeration in Other Bases

WSMA Decimal Numbers Lesson 4

Negative Integral Exponents. If x is nonzero, the reciprocal of x is written as 1 x. For example, the reciprocal of 23 is written as 2

Base Conversion written by Cathy Saxton

MATHEMATICS: REPEATING AND GROWING PATTERNS First Grade. Kelsey McMahan. Winter 2012 Creative Learning Experiences

DESCRIPTIVE STATISTICS. The purpose of statistics is to condense raw data to make it easier to answer specific questions; test hypotheses.

Using Linear Programming in Real-Life Problems

Trigonometric Functions and Triangles

Introduction to Java

Prime Factorization 0.1. Overcoming Math Anxiety

CALCULATIONS & STATISTICS

2.2 Derivative as a Function

Transcription:

CS 161 LAB 5 This lab will have two parts. In the first part, we will create a class to automate the drawing of the robot from the second lab. For the second part, we will begin a ClunkyCalculator class that will mimic a simple calculator using BlueJ s ability to manually call methods. We will do half of the Calculator class this week and finish it as part of next week s lab. To begin, make a Lab5 folder in your CS161 folder. The two projects you make will go in this folder. Part I As part of the second lab, you were to save the final settings for the robot picture. Find those settings as we will have the computer draw the robot for us. From your Lab2 folder, copy the shapes2 project and paste it into your Lab5 folder. Now open this project in BlueJ. The Picture class Once BlueJ has open this project, select the new class option. Call this class Picture, and when the editor opens with the skeleton class, put your name and the date in the appropriate locations and change the documentation at the beginning of the class to state that it will automatically draw the robot picture from the second lab. The class instance variables Since this class will draw the robot picture, it needs a class instance variable for each of the components. This means the class must have two objects of type Square, three objects of type Rect, two objects of type Triangle, and two objects of type Circle. Rather that naming the objects square1, square2, etc, as BlueJ does, name each object for its role in the picture: Name one of the Square objects head and the other body; name the Rect objects neck, leftarm, and rightarm; name the Triangle objects leftleg and rightleg; and the Circle objects leftwheel and rightwheel. The Constructor The constructor for this class is not very complex. It needs to initialize the nine objects using the new operator. So, for example, one line in the constructor is the following: head = new Square(); Remember that the types of these objects have already been declared at the beginning of the class. DO NOT put the type before the object name; i.e. do NOT write: Square head = new Square(); If you do, you will make the object head a local variable. This means that the class instance variable head would be undefined outside of the constructor. CS 161 Page 1 of 8 Lab 5

CS 161 Page 2 of 8 Lab 5

The Method drawhead We will make a separate method to draw each of the parts of the robot. While this means there will be nine methods, plus a tenth method that will put it all together, each method will be rather short, simple, and will perform only one action. The drawhead method is public, void, and takes no parameters. Be sure to add the appropriate documentation to the beginning of this method that explains what action the method performs. The first action it takes is to make head visible. If you have forgotten the names of the methods in the Square class, you might want to open the editor for the Square class and switch to the Documentation view. For example, a portion of the Documentation view of the Square class looks like the following: This not only tells you the names of the methods in each class, but what information, if any, you must give to a method to have it perform its actions. Also remember that each method call is an external method call. It must be prefixed with an object name using the dot notation. So, to make head visible, the statement is: head.makevisible(); After head is made visible, it must be resized. Call the appropriate method, passing in the size that you saved from Lab2. CS 161 Page 3 of 8 Lab 5

Next, head must be moved to the proper X-Y location. To move the objects to their proper location, use the methods slowmovehorizontal and slowmovevertical. To use these methods, however, you will need to do some calculations. The various methods to move an object in the Square class add their parameter to either the current X-coordinate or the current Y- coordinate. What you most likely saved from Lab2 was the final X and Y-coordinates. To get the proper value to pass to the move methods, you must use the editor to look at the code for the Square class to find the initial X and Y positions and subtract these numbers from the final X and Y positions. It is the difference between the starting and ending coordinates that represents the distance each object must move, and it is this distance that must be passed to move methods. Of course, if you really wanted to, there is no reason why you could not add accessor methods for the X and Y coordinates to the various classes in this project. Then, using the accessor methods, you could get the initial positions and use subtraction statements with your final positions, saving the results in local variables. These variables could be used as actual parameters in the methods that move the objects. This is just a thought, and it is certainly not required for this lab. To review, the steps the drawhead method as well as the other drawing methods must take are the following. 1. Make the object visible. 2. Resize the object to the size you saved from Lab2. 3. Move the object horizontally and vertically to the coordinates that you saved from Lab2. Once you have completed the drawhead method, compile the Picture class, make an object of the class and call the drawhead method. Does it work correctly? If not, fix it before going on to another method. The Other draw Methods There are eight other draw methods to create, one for each part of the robot s body. Call these methods drawbody, drawneck, drawrightarm, and so on. Each of these methods follows the same pattern as the drawhead method. After you finish each method, test the method and correct any errors in the method before you start the next method. In addition, add the appropriate documentation at the beginning of each method. The makepicture method The last method to add is a makepicture method. This method just contains nine statements. Each statement is an internal method call to one of the draw methods, for example: drawhead(); Thus, calling the makepicture method will cause the robot to be drawn. CS 161 Page 4 of 8 Lab 5

When the makepicture method works correctly and draws the robot picture, make a printout of the class. Be sure that each method has the appropriate documentation. Then demonstrate the makepicture method for your lab instructor. Part II: The ClunkyCalculator Now for the second project. The purpose of this project is to make some methods that use the basic mathematical and relational operators, practice using local variables, and gain more experience with the conditional if-else statement. One design of computer CPU s is known as an accumulator machine (or a one-address machine). This type of CPU has a special register called the accumulator. Any arithmetic operation, such as adding two values, uses the contents of the accumulator as one of the operands and the result of the operation is stored back in the accumulator. If you are interested, the CPU that was in the original Apple II computer the MOS Technology 6502 processor was an accumulator machine. This is the type of calculator we are going to create in this project. Using BlueJ, create a new project in your Lab5 folder, and call the project clunky-calculator. Then create a new class, which will be called Calculator. This class will have a single field (class instance variable), which is an int and is called accumulator. The Constructor This class has one constructor. The constructor takes no parameters, and it simply assigns accumulator an initial value of zero. The Method clear This is a mutator method that has two statements in its body. The first statement assigns the accumulator a zero and then the second statement is similar to: System.out.println( Accumulator = + accumulator ); The Method loadaccumulator This is another mutator method, but one that takes a parameter. Its actions are to assign the accumulator the value of its parameter, and then it prints the contents of the accumulator to the terminal window using a similar statement to that in the previous method. The Arithmetic Methods Next, we will create five methods to perform the arithmetic operations of add ( + ), subtract ( - ), multiply ( * ), divide ( / ), and mod ( % ). These methods will be quite similar, so you CS 161 Page 5 of 8 Lab 5

might find it easier to write the first method, then use copy and paste for the other methods because there will only be slight changes. The Method add This method is a void method, and it takes a single parameter. In the body of the method, a local variable is declared and this variable is assigned the value of accumulator. The reason for this local variable is that the value of accumulator will be changed, but we want to record its value before the change so that it can be printed out. After the contents of accumulator is saved in the local variable, add accumulator with the parameter and assign the result back to accumulator. Then a statement is printed to the terminal window that shows the original value of the accumulator (saved in the local variable), a plus sign, the value of the parameter, an equal sign, and the new value of the accumulator. Thus, if the accumulator originally contained the value 3 and the add method was called with a parameter of 5, the statement that appears in the terminal window should be: 3 + 5 = 8 The other 4 methods are similar to the add method. The difference, of course, is the operation each method performs, and the operator that appears in the statement printed to the terminal window. For example, I started with an accumulator containing 0. Then I called add with a parameter of 20, subtract with a parameter of 5, multiply with a parameter of 3, divide with a parameter of 9, and mod with a parameter of 2. Note that I am naming each method with the name of the operation it is performing. My terminal window then contained the following. Notice that in the display the starting value of the accumulator is always the leftmost digit, the parameter to the method is the next digit, and the resulting value of the accumulator is the rightmost digit. The Method compute Now we will add one more method. This method is called compute. It is a void method and takes no parameters. Its purpose is to allow us to test our calculator by placing a sequence of method calls inside this method, compile our class, create an object of the class, and call the compute method. The Final Test CS 161 Page 6 of 8 Lab 5

When all your methods are working correctly, place the following sequence of method calls in your compute method. loadaccumulator( 15 ); add( 20 ); subtract( 30 ); multiply( 8 ); divide( 4 ); mod( 4 ); subtract( 5 ); clear(); Now, compile your class, create an object of the class, and call the compute method. If you get the following terminal window: Show this to your lab instructor, give her a printout of your class, and you are done for this week. Next week we will add more methods to this class. The methods that we will add will implement the relational operators. Note, as an aid, I have attached a partial copy of my documentation for this class. CS 161 Page 7 of 8 Lab 5

Note: This lab was originally written by Dr. Broeg. CS 161 Page 8 of 8 Lab 5