CS 170 Java Programming 1. Week 5: Procedures and Functions

Size: px
Start display at page:

Download "CS 170 Java Programming 1. Week 5: Procedures and Functions"

Transcription

1 CS 170 Java Programming 1 Week 5: Procedures and Functions

2 What s the Plan? Topic 1: More on graphical objects Creating your own custom Turtle types Introducing media, pictures and sounds Topic 2: Decomposition: writing your own procedures Learn to eliminate redundancy Topic 3: Passing Parameters Learn how to define formal parameters Topic 4: Writing functions Learn to write and test your own user-defined functions

3 Your "IC" or "Lab" Document Use Word or OpenOffice to create a new document Save the file as IC05.doc (Office compatible) Place on your network U: drive or on a thumb drive Put your name and today's date at the top of the sheet Title it "CS 170 Lab Exercises Week 5" Start DrJava and we'll do a little bit with Turtles Exercise 5.1: Create and display a Turtle named sam Shoot a screen-shot and paste it into your IC document

4 Drawing a Square Exercise 5.2: ask sam to draw a square; the algorithm: Ask sam to turn right Ask sam to go forward 50 units Ask sam to turn right again Ask sam to go forward 50 more units Ask sam to turn right again Ask sam to go forward 50 more units Ask sam to turn right one last time Ask sam to go forward one final 50 units Snap a picture when finished.

5 Your Very Own Turtle Type Much easier if we could just tell sam to draw a square Turtle class doesn't have any drawsquare() method Solution? Create our own smarter Turtle class This is known as Inheritance Lets us create new types by building on existing types Exercise 5.2: Create a new file in Definitions Pane Create a class named MyTurtle Add phrase extendsbasicturtle to header Create a new MyTurtle object named yertle

6 Adding a drawsquare Method The syntax for a new "turtle command" looks like this: public void drawsquare() { // put the instructions here } Place this inside the MyTurtle class So, what do we put for the "instructions"? yertle.turnleft() doesn't work We don't have a MyTurtle object Inside class, current active object is named this

7 The drawsquare Instructions Exercise 5.3: Add the drawsquare() method Create a new MyTurtle object and have it draw this Snap a screen-shot and paste it into your IC document

8 A Bigger Square? Improve drawsquare() by using a variable public void drawsquare() { int width = 50; this.turnright(); this.forward(width); //.. and so on } Better because we only have to change one line MyTurtle still only draws one size square

9 Adding a Parameter Move variable declaration to parameter list public void drawsquare(int width) { this.turnright(); this.forward(width); //.. and so on } You supply the size when you ask your turtle to draw Means that your turtle can draw any size square Parameters make methods more flexible

10 Try It Yourself Exercise 5.4: complete improved drawsquare() Start by typing this into the Interactions Pane MyTurtle tom = new MyTurtle(); tom.forward(50); tom.drawsquare(30); tom.turn(30); tom.drawsquare(50); Add commands until your picture looks like this Snap a pic.

11 Topic 2 Programs and Procedures Adding Procedures to Eliminate Redundancy (Gaddis 4.1)

12 Introducing Procedures Exercise 5.5: open Abacus.java in your Week 5 folder Single main that draws an Abacus on screen Snap a picture Now, copy file by using Save As Abacus2.java Make sure you rename class after saving Both class and file must have the same name Want to learn how to use multiple procedures "Procedures" are methods that "carry out an action" First task is learning the syntax

13 Procedures Procedures let us divide a complex tasks into simpler pieces by decomposing the complex task To define a simple procedure you write this: public static void onestep() { // actions needed for one step } Almost like drawsquare(), except for static keyword Needed when a procedure is called from main()

14 Defining printtophalf Exercise 5.6: create a procedure printtophalf() Make sure it appears inside the Abacus2 class Cut the statements out of the main() method that draw the top half of the abacus. Paste them into the body of the printtophalf() method. Compile, run and snap a screen-shot.

15 Calling Procedures Your abacus is missing the top half. Why? Because you defined the procedure but didn't call it To call a procedure you type its name and a semicolon: printtophalf(); Place at the point where you want this performed Exercise 5.6: call the printtophalf() method from the main() method. Compile, run and snap a picture.

16 Completing Abacus2 Exercise 5.7: Do the printbottomhalf() procedure on your own. Run and shoot a screenshot. Procedures can be used for more than just printing values or returning information about an object. Provide a powerful way to solve problems Put multiple steps into a single, simple method Use simple method as a building block for larger methods Procedural decomposition or divide and conquer

17 Introducing Decomposition Decomposition allows us to complete more complex jobs in a simpler way, eliminating redundancy

18 Applying Decomposition Early on, the easiest way to apply decomposition is to first write the redundant code first and then add procedures Largest procedures first Notice the patterns Then divide into smaller procedures As you gain more experience, you'll often work the other way around (called bottom up design) Good programmers alternate between these Open Abacus, save as Abacus3, rename class

19 Locate Redundant Code Look at the redundant or repeated lines in code

20 Stepwise Refinement Exercise 5.8: define a printbeads() procedure Copy code to print one line of beads Call printbeads() from main() method Now we still have redundant calls to printbeads() Replace two calls to printbeads() with a printtwobeads() method (calls printbeads()) Again? Add a printfourbeads() method that calls printtwobeads() Compile and run. Shoot a screen-shot of your code

21 Abacus3

22 Topic 3 Writing Functions with Parameters Writing Functions to Calculate Values (Gaddis )

23 Writing Functions We are going to start moving away from IPO programs and start moving towards writing functions Think of these as "mini" IPO programs Here are the major differences: No user input. Input comes only through the parameter list No visible output: output is returned from the function Can still have a main() method with regular input and output to test the function; you won't do I/O in the function Let's start with a String function

24 The helloname Example Given a Stringname, e.g. "Bob", return a greeting of the form "Hello Bob!". helloname("bob") "Hello Bob!" helloname("alice") "Hello Alice!" helloname("x") "Hello X!" First, let's write this as an IPO program HelloName.java Create a String variable, read the user's name Print the result by using concatenation Now, open StringFunctions.java

25 Function Syntax Functions (value-producing methods) are procedures: That have a return type other than void And end with a return expression This is the function output: public static rettype funcname(args) { // statements return rettype-expression; }

26 Let's Create a Function Write the a "first-draft" skeleton for the helloname() function by asking these questions: 1. What is the access? (always use public) 2. What kind of thing does the function return? 3. What is the name of the function? 4. Skip parameters for now. Just put in () 5. Add braces to surround the function body. 5. Create a variable for the result, and initialize 6. Return the result Now, let's look at the input

27 Function Inputs Instead of reading input, a function uses parameters A formal parameter variable is a local variable that: is declared inside the argument list is given a value when the function is called helloname() with a formal parameter (or argument) public static String helloname(string name) { return result; }

28 Testing Your Function Informally test by using the Interactions Pane Run test suit by using CheckResults Compares expected to actual output

29 Completing helloname Exercise 5.9: complete the method by using concatenation to combine the inputs with the desired greeting. Run tests and shoot a screen shot.

30 Introducing JavaBat Site developed by Prof. Nick Parlante at Stanford Can use instead of DrJava for functions exercises Go to May create account if you want to save your work

31 A JavaBat Example Locate the makeabba problem in String1 section String problems that don't require loops this week

32 Solving the Problem On proficiency quizzes, you'll write skeleton yourself Always start by completing the supplied skeleton 1. Create a variable, same type as return type I usually name it result Initialize it to an "empty" value (0, 0.0, false, or "") 2. Add the return expression with result 3. Run the program Check the expected output, then write code to fix Exercise 5.10: complete problem and shoot a screen-shot

33 Finishing Up To complete your IC "lab" document: Complete Exercise 5.11 in the Writing Procedures lesson, Exercise 5.12 in the Passing Parameters lesson, Exercise 5.13 in the Writing Functions lesson, Exercises in the Syntax Errors lesson and Exercise 5.16 in the Style Rules lesson. Submit to Blackboard when done Reading for next week: Gaddis: read Chapter 4, MediaComp: Chapter 3

34 Homework and Quiz Homework, Quiz and Assignments Deadline is Monday at noon Eight homework problems (1 extra-credit) 2 procedures and using String functions 5 small String function problems Upload to Assignment Dropbox. Make sure to press Submit Proficiency Quiz 4: Simple String functions Practice on assignments You will have ½ hour, assigned seating

Week 2: Data and Output

Week 2: Data and Output CS 170 Java Programming 1 Week 2: Data and Output Learning to speak Java Types, Values and Variables Output Objects and Methods What s the Plan? Topic I: A little review IPO, hardware, software and Java

More information

CS 170 Java Programming 1. Week 12: Creating Your Own Types

CS 170 Java Programming 1. Week 12: Creating Your Own Types CS 170 Java Programming 1 Week 12: Creating Your Own Types What s the Plan? Topic 1: A Little Review Work with loops to process arrays Write functions to process 2D Arrays in various ways Topic 2: Creating

More information

Week 3: Objects, Input and Processing

Week 3: Objects, Input and Processing CS 170 Java Programming 1 Week 3: Objects, Input and Processing Learning to Create Objects Learning to Accept Input Learning to Process Data What s the Plan? Topic I: Working with Java Objects Learning

More information

CS 170 Java Programming 1. Week 9: Learning about Loops

CS 170 Java Programming 1. Week 9: Learning about Loops CS 170 Java Programming 1 Week 9: Learning about Loops What s the Plan? Topic 1: A Little Review ACM GUI Apps, Buttons, Text and Events Topic 2: Learning about Loops Different kinds of loops Using loops

More information

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging CS 170 Java Programming 1 Week 13: Classes, Testing, Debugging What s the Plan? Short lecture for makeup exams Topic 1: A Little Review How to create your own user-defined classes Defining instance variables,

More information

CS 170 Java Programming 1. Week 10: Loops and Arrays

CS 170 Java Programming 1. Week 10: Loops and Arrays CS 170 Java Programming 1 Week 10: Loops and Arrays What s the Plan? Topic 1: A Little Review Use a counted loop to create graphical objects Write programs that use events and animation Topic 2: Advanced

More information

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions CS 170 Java Programming 1 Week 15: Interfaces and Exceptions Your "IC" or "Lab" Document Use Word or OpenOffice to create a new document Save the file as IC15.doc (Office 97-2003 compatible) Place on your

More information

CS 170 Java Programming 1. Week 7: More on Logic

CS 170 Java Programming 1. Week 7: More on Logic CS 170 Java Programming 1 Week 7: More on Logic What s the Plan? Topic 1: A Little Review Use relational operators to compare values Write functions using if and else to make decisions Topic 2: New Logical

More information

Express Yourself. Writing Your Own Classes

Express Yourself. Writing Your Own Classes Java Programming 1 Lecture 5 Defining Classes Creating your Own Classes Express Yourself Use OpenOffice Writer to create a new document Save the file as LastFirst_ic05 Replace LastFirst with your actual

More information

Structured Programming

Structured Programming CS 170 Java Programming 1 Objects and Variables A Little More History, Variables and Assignment, Objects, Classes, and Methods Structured Programming Ideas about how programs should be organized Functionally

More information

Instructions PLEASE READ (notice bold and underlined phrases)

Instructions PLEASE READ (notice bold and underlined phrases) Lab Exercises wk11 Practice with Strings Required Reading None Instructions PLEASE READ (notice bold and underlined phrases) Lab Exercise has three parts: A. Lab Demo Review of Strings B. Exercises Work

More information

Express Yourself. What is Eclipse?

Express Yourself. What is Eclipse? CS 170 Java Programming 1 Eclipse and the for Loop A Professional Integrated Development Environment Introducing Iteration Express Yourself Use OpenOffice or Word to create a new document Save the file

More information

Slide 1 CS 170 Java Programming 1

Slide 1 CS 170 Java Programming 1 CS 170 Java Programming 1 Objects and Methods Performing Actions and Using Object Methods Slide 1 CS 170 Java Programming 1 Objects and Methods Duration: 00:01:14 Hi Folks. This is the CS 170, Java Programming

More information

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style

More information

Slide 1 CS 170 Java Programming 1 The while Loop Duration: 00:00:60 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 The while Loop Duration: 00:00:60 Advance mode: Auto CS 170 Java Programming 1 The while Loop Writing Counted Loops and Loops that Check a Condition Slide 1 CS 170 Java Programming 1 The while Loop Duration: 00:00:60 Hi Folks. Welcome to the CS 170, Java

More information

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto CS 170 Java Programming 1 Working with Rows and Columns Slide 1 CS 170 Java Programming 1 Duration: 00:00:39 Create a multidimensional array with multiple brackets int[ ] d1 = new int[5]; int[ ][ ] d2;

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG 1 Notice Reading Assignment Chapter 1: Introduction to Java Programming Homework 1 It is due this coming Sunday

More information

Slide 1 CS 170 Java Programming 1 Testing Karel

Slide 1 CS 170 Java Programming 1 Testing Karel CS 170 Java Programming 1 Testing Karel Introducing Unit Tests to Karel's World Slide 1 CS 170 Java Programming 1 Testing Karel Hi Everybody. This is the CS 170, Java Programming 1 lecture, Testing Karel.

More information

Spring CS Homework 3 p. 1. CS Homework 3

Spring CS Homework 3 p. 1. CS Homework 3 Spring 2018 - CS 111 - Homework 3 p. 1 Deadline 11:59 pm on Friday, February 9, 2018 Purpose CS 111 - Homework 3 To try out another testing function, check-within, to get more practice using the design

More information

Express Yourself. The Great Divide

Express Yourself. The Great Divide CS 170 Java Programming 1 Numbers Working with Integers and Real Numbers Open Microsoft Word and create a new document Save the file as LastFirst_ic07.doc Replace LastFirst with your actual name Put your

More information

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged. QUIZ Lesson 4 Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged. QUIZ Lesson 4 Exercise 4: Write an if statement that

More information

CS Homework 11 p. 1. CS Homework 11

CS Homework 11 p. 1. CS Homework 11 CS 111 - Homework 11 p. 1 Deadline 11:59 pm on Monday, May 2, 2016 How to submit Each time you would like to submit your work: CS 111 - Homework 11 If your files are not already on nrs-labs, be sure to

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 9 Exercise

More information

STUDENT LESSON A1 Introduction to Object-Oriented Programming (OOP)

STUDENT LESSON A1 Introduction to Object-Oriented Programming (OOP) STUDENT LESSON A1 Introduction to Object-Oriented Programming (OOP) Java Curriculum for AP Computer Science, Student Lesson A1 1 STUDENT LESSON A1 Introduction to Object-Oriented Programming (OOP) INTRODUCTION:

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG 1 Notice Prepare the Weekly Quiz The weekly quiz is for the knowledge we learned in the previous week (both the

More information

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit. Com S 227 Fall 2017 Miniassignment 1 50 points Due Date: Monday, October 16, 11:59 pm (midnight) Late deadline (25% penalty): Tuesday, October 17, 11:59 pm General information This assignment is to be

More information

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords Introduction to Java Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords Program Errors Syntax Runtime Logic Procedural Decomposition Methods Flow of Control

More information

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER   COURSE WEBSITE COURSE WEBSITE http://www.steveguynes.com/bcis3630/bcis3630/default.html Instructor: Dr. Guynes Office: BLB 312H Phone: (940) 565-3110 Office Hours: By Email Email: steve.guynes@unt.edu TEXTBOOK: Starting

More information

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3 Programming in C main Level 2 Level 2 Level 2 Level 3 Level 3 1 Programmer-Defined Functions Modularize with building blocks of programs Divide and Conquer Construct a program from smaller pieces or components

More information

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017 CS 111 - Homework 9 p. 1 Deadline 11:59 pm on Friday, April 7, 2017 Purpose CS 111 - Homework 9 To give you an excuse to look at some newly-posted C++ templates that you might find to be useful, and to

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG 1 Notice Class Website http://www.cs.umb.edu/~jane/cs114/ Reading Assignment Chapter 1: Introduction to Java Programming

More information

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit. Com S 227 Spring 2018 Miniassignment 1 40 points Due Date: Thursday, March 8, 11:59 pm (midnight) Late deadline (25% penalty): Friday, March 9, 11:59 pm General information This assignment is to be done

More information

Spring CS Homework 12 p. 1. CS Homework 12

Spring CS Homework 12 p. 1. CS Homework 12 Spring 2018 - CS 111 - Homework 12 p. 1 Deadline 11:59 pm on Friday, May 4, 2018 Purpose CS 111 - Homework 12 To practice with sentinel- and question-controlled loops, file input and file output, and writing

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

More information

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file? QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The

More information

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

More on Arrays CS 16: Solving Problems with Computers I Lecture #13 More on Arrays CS 16: Solving Problems with Computers I Lecture #13 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #12 due today No homework assigned today!! Lab #7 is due on Monday,

More information

Chapter 4C Homework Functions III Individual Assignment 30 Points Questions 6 Points Script 24 Points

Chapter 4C Homework Functions III Individual Assignment 30 Points Questions 6 Points Script 24 Points PCS1-Ch-4C-Functions-3-HW.docx CSCI 1320 Initials P a g e 1 If this lab is an Individual assignment, you must do all coded programs on your own. You may ask others for help on the language syntax, but

More information

M.CS201 Programming language

M.CS201 Programming language Power Engineering School M.CS201 Programming language Lecture 4 Lecturer: Prof. Dr. T.Uranchimeg Agenda How a Function Works Function Prototype Structured Programming Local Variables Return value 2 Function

More information

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 152: Data Structures with Java Hello World with the IntelliJ IDE CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building

More information

Com S 127 Lab 2. For the first two parts of the lab, start up Wing 101 and use the Python shell window to try everything out.

Com S 127 Lab 2. For the first two parts of the lab, start up Wing 101 and use the Python shell window to try everything out. Com S 127 Lab 2 Checkpoint 0 Please open the CS 127 Blackboard page and click on Groups in the menu at left. Sign up for the group corresponding to the lab section you are attending. Also, if you haven't

More information

CS Homework 11 p. 1. CS Homework 11

CS Homework 11 p. 1. CS Homework 11 CS 111 - Homework 11 p. 1 Deadline 11:59 pm on Friday, December 12, 2014 How to submit Each time you would like to submit your work: CS 111 - Homework 11 IF they are not already on nrs-labs, then transfer/save

More information

Lesson 5C MyClass Methods. By John B. Owen All rights reserved 2011, revised 2014

Lesson 5C MyClass Methods. By John B. Owen All rights reserved 2011, revised 2014 Lesson 5C MyClass Methods By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Defining your own class Defining and calling a static method Method structure String return

More information

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto CS 170 Java Programming 1 Real Numbers Understanding Java's Floating Point Primitive Types Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Floating-point types Can hold a fractional amount

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

More information

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Define and discuss abstract classes Define and discuss abstract methods Introduce polymorphism Much of the information

More information

Homework 09. Collecting Beepers

Homework 09. Collecting Beepers Homework 09 Collecting Beepers Goal In this lab assignment, you will be writing a simple Java program to create a robot object called karel. Your robot will start off in a world containing a series of

More information

Control Flow: Loop Statements

Control Flow: Loop Statements Control Flow: Loop Statements A loop repeatedly executes a of sub-statements, called the loop body. Python provides two kinds of loop statements: a for-loop and a while-loop. This exercise gives you practice

More information

Instructions PLEASE READ (notice bold and underlined phrases)

Instructions PLEASE READ (notice bold and underlined phrases) Assignment 3 Dynamic Arrays and Inheritance Note: This is a Preview! More Questions are coming in Version 1.0 Required Reading Problem Solving with C++ Chapter 7 Arrays Chapter 9 Pointers and Dynamic Arrays

More information

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto CS 170 Java Programming 1 More on Strings Working with the String class Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 What are Strings in Java? Immutable sequences of 0 n characters

More information

CSE 230 Intermediate Programming in C and C++ Functions

CSE 230 Intermediate Programming in C and C++ Functions CSE 230 Intermediate Programming in C and C++ Functions Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse230/ Concept of Functions

More information

Computer Science II Lecture 1 Introduction and Background

Computer Science II Lecture 1 Introduction and Background Computer Science II Lecture 1 Introduction and Background Discussion of Syllabus Instructor, TAs, office hours Course web site, http://www.cs.rpi.edu/courses/fall04/cs2, will be up soon Course emphasis,

More information

Tips from the experts: How to waste a lot of time on this assignment

Tips from the experts: How to waste a lot of time on this assignment Com S 227 Spring 2018 Assignment 1 100 points Due Date: Friday, September 14, 11:59 pm (midnight) Late deadline (25% penalty): Monday, September 17, 11:59 pm General information This assignment is to be

More information

Computer Science AP 2017 Summer Assignment Mrs. McFarland

Computer Science AP 2017 Summer Assignment Mrs. McFarland Computer Science AP 2017 Summer Assignment Mrs. McFarland Read Chapter 1 from the book Think Java: How to Think Like a Computer Scientist by Allen B. Downey. I have included Chapter 1 in this pdf. If you

More information

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with. 1 Outline TOPIC 3 INTRODUCTION TO PROGRAMMING Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015 COMP-202: Foundations of Programming Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015 Assignment Due Date Assignment 1 is now due on Tuesday, Jan 20 th, 11:59pm. Quiz 1 is

More information

Lecture 1. Course Overview Types & Expressions

Lecture 1. Course Overview Types & Expressions Lecture 1 Course Overview Types & Expressions CS 1110 Spring 2012: Walker White Outcomes: Basics of (Java) procedural programming Usage of assignments, conditionals, and loops. Ability to write recursive

More information

CISC 1600 Lecture 3.1 Introduction to Processing

CISC 1600 Lecture 3.1 Introduction to Processing CISC 1600 Lecture 3.1 Introduction to Processing Topics: Example sketches Drawing functions in Processing Colors in Processing General Processing syntax Processing is for sketching Designed to allow artists

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

In the first class, you'll learn how to create a simple single-view app, following a 3-step process:

In the first class, you'll learn how to create a simple single-view app, following a 3-step process: Class 1 In the first class, you'll learn how to create a simple single-view app, following a 3-step process: 1. Design the app's user interface (UI) in Xcode's storyboard. 2. Open the assistant editor,

More information

CS Homework 10 p. 1. CS Homework 10

CS Homework 10 p. 1. CS Homework 10 CS 111 - Homework 10 p. 1 Deadline 11:59 pm on Friday, December 2, 2016 How to submit Each time you would like to submit your work: CS 111 - Homework 10 If your files are not already on nrs-labs, be sure

More information

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit. Com S 227 Fall 2018 Miniassignment 1 40 points Due Date: Friday, October 12, 11:59 pm (midnight) Late deadline (25% penalty): Monday, October 15, 11:59 pm General information This assignment is to be done

More information

Curriculum Map Grade(s): Subject: AP Computer Science

Curriculum Map Grade(s): Subject: AP Computer Science Curriculum Map Grade(s): 11-12 Subject: AP Computer Science (Semester 1 - Weeks 1-18) Unit / Weeks Content Skills Assessments Standards Lesson 1 - Background Chapter 1 of Textbook (Weeks 1-3) - 1.1 History

More information

Lesson Share TEACHER'S NOTES LESSON SHARE. ing by Olya Sergeeva. Overview. Preparation. Procedure

Lesson Share TEACHER'S NOTES LESSON SHARE.  ing by Olya Sergeeva. Overview. Preparation. Procedure Lesson Share TEACHER'S NOTES Age: Adults Level: Intermediate + Time: 1 hour 40 minutes Objective: to practise writing work-related emails Key skills: writing Materials: one copy of the worksheet per student;

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level

More information

Spring CS Homework 6 p. 1. CS Homework 6

Spring CS Homework 6 p. 1. CS Homework 6 Spring 2018 - CS 111 - Homework 6 p. 1 Deadline 11:59 pm on Friday, March 9, 2018 Purpose CS 111 - Homework 6 To practice with some C++ basics, including following design recipe steps for designing and

More information

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright Java Basics Object Orientated Programming in Java Benjamin Kenwright Outline Essential Java Concepts Syntax, Grammar, Formatting, Introduce Object-Orientated Concepts Encapsulation, Abstract Data, OO Languages,

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

CS Homework 2 p. 1. CS Homework 2

CS Homework 2 p. 1. CS Homework 2 CS 111 - Homework 2 p. 1 Deadline 11:59 pm on Friday, February 2, 2018 Purpose CS 111 - Homework 2 To practice defining and using named constants and check-expect expressions, and to practice using the

More information

Tips from the experts: How to waste a lot of time on this assignment

Tips from the experts: How to waste a lot of time on this assignment Com S 227 Spring 2018 Assignment 1 80 points Due Date: Friday, February 2, 11:59 pm (midnight) Late deadline (25% penalty): Monday, February 5, 11:59 pm General information This assignment is to be done

More information

CS 051 Homework Laboratory #2

CS 051 Homework Laboratory #2 CS 051 Homework Laboratory #2 Dirty Laundry Objective: To gain experience using conditionals. The Scenario. One thing many students have to figure out for the first time when they come to college is how

More information

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers Today s Lesson plan (3) 10 min Looking back What did we learn last week? Blocks of: Theory Exercises

More information

Tips from the experts: How to waste a lot of time on this assignment

Tips from the experts: How to waste a lot of time on this assignment Com S 227 Spring 2017 Assignment 1 80 points Due Date: Thursday, February 2, 11:59 pm (midnight) Late deadline (25% penalty): Friday, February 3, 11:59 pm General information This assignment is to be done

More information

During the first 2 weeks of class, all students in the course will take an in-lab programming exam. This is the Exam in Programming Proficiency.

During the first 2 weeks of class, all students in the course will take an in-lab programming exam. This is the Exam in Programming Proficiency. Description of CPSC 301: This is a 2-unit credit/no credit course. It is a course taught entirely in lab, and has two required 2-hour 50-minute lab sessions per week. It will review, reinforce, and expand

More information

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions. Handout 2 Functions, Lists, For Loops and Tuples [ ] Functions -- parameters/arguments, "calling" functions, return values, etc. Please make sure you understand this example: def square(x): return x *

More information

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with. 1 Outline 2 TOPIC 3 INTRODUCTION TO PROGRAMMING Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto CS 170 Java Programming 1 Expressions Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 What is an expression? Expression Vocabulary Any combination of operators and operands which, when

More information

ICS 61 Game Systems and Design Introduction to Scratch

ICS 61 Game Systems and Design Introduction to Scratch ICS 61, Winter, 2015 Introduction to Scratch p. 1 ICS 61 Game Systems and Design Introduction to Scratch 1. Make sure your computer has a browser open at the address http://scratch.mit.edu/projects/editor/.

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

To gain experience using GUI components and listeners.

To gain experience using GUI components and listeners. Lab 5 Handout 7 CSCI 134: Fall, 2017 TextPlay Objective To gain experience using GUI components and listeners. Note 1: You may work with a partner on this lab. If you do, turn in only one lab with both

More information

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want

More information

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7) Array Basics: Outline Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants

More information

CS 61B Discussion Quiz 1. Questions

CS 61B Discussion Quiz 1. Questions Name: SID: CS 61B Discussion Quiz 1 Write your name and SID above. Detach this page from your discussion handout, and turn it in when your TA instructs you to do so. These quizzes are used as attendance.

More information

In this course, you need to use Pearson etext. Go to "Pearson etext and Video Notes".

In this course, you need to use Pearson etext. Go to Pearson etext and Video Notes. **Disclaimer** This syllabus is to be used as a guideline only. The information provided is a summary of topics to be covered in the class. Information contained in this document such as assignments, grading

More information

QUIZ: What value is stored in a after this

QUIZ: What value is stored in a after this QUIZ: What value is stored in a after this statement is executed? Why? a = 23/7; QUIZ evaluates to 16. Lesson 4 Statements, Expressions, Operators Statement = complete instruction that directs the computer

More information

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014 Lesson 6A Loops By John B. Owen All rights reserved 2011, revised 2014 Topic List Objectives Loop structure 4 parts Three loop styles Example of a while loop Example of a do while loop Comparison while

More information

http://xkcd.com/224/ CS 152: Programming Language Paradigms Prof. Tom Austin San José State University What are some programming languages? Taken from http://pypl.github.io/pypl.html January 2016 Why are

More information

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Programming Fundamentals - Day Instructor: Jason Yoon Website: Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

Course: Honors AP Computer Science Instructor: Mr. Jason A. Townsend

Course: Honors AP Computer Science Instructor: Mr. Jason A. Townsend Course: Honors AP Computer Science Instructor: Mr. Jason A. Townsend Email: jtownsend@pkwy.k12.mo.us Course Description: The material for this course is the equivalent of one to two semesters of an entry

More information

CS Fall Homework 11 p. 1. CS Homework 11

CS Fall Homework 11 p. 1. CS Homework 11 CS 111 - Fall 2018 - Homework 11 p. 1 Deadline 11:59 pm on MONDAY, December 3, 2018 Purpose To practice with loops, arrays, and more! How to submit Submit your THREE.cpp FILES: CS 111 - Homework 11 hw11.cpp

More information

Lab 1: Introduction to Java

Lab 1: Introduction to Java Lab 1: Introduction to Java Welcome to the first CS15 lab! In the reading, we went over objects, methods, parameters and how to put all of these things together into Java classes. It's perfectly okay if

More information

Recommended Group Brainstorm (NO computers during this time)

Recommended Group Brainstorm (NO computers during this time) Recommended Group Brainstorm (NO computers during this time) Good programmers think before they begin coding. Part I of this assignment involves brainstorming with a group of peers with no computers to

More information

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4)

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4) CIS 110: Introduction to Computer Programming Lecture 2 Decomposition and Static Methods ( 1.4) Outline Structure and redundancy in algorithms Static methods Procedural decomposition 9/16/2011 CIS 110

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

CSCI 200 Lab 3 Using and implementing sets

CSCI 200 Lab 3 Using and implementing sets CSCI 200 Lab 3 Using and implementing sets In this lab, you will write a program that manages a set of workers, using the Worker hierarchy you developed in Lab 2. You will also implement your own version

More information

Instructions. Quiz #2. Name: Solutions Student Number: Signature:

Instructions. Quiz #2. Name: Solutions Student Number: Signature: Quiz #2 Name: Solutions Student Number: Signature: Instructions 1. Fill in your Name, Student Number, and signature above. 2. This is a closed book Quiz. No electronic or paper aids permitted. 3. Do not

More information

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects CS256 Computer Science I Kevin Sahr, PhD Lecture 11: Objects 1 Programs as Models remember: we write programs to solve realworld problems programs act as models of the real-world problem to be solved one

More information