39
CMPT 183 Foundations of Computer Science I Angel Gutierrez Fall 2013 Computer Science is no more about computers than astronomy is about telescopes.-Dijkstra

Objects First With Java - Chapter 1pages.csam.montclair.edu/~gutierre/01-Lab-HelloWorldShapes.pdfcompletely empty them, or pour water from one jug ... • program: series of ... Objects

Embed Size (px)

Citation preview

CMPT 183 Foundations of Computer Science I

Angel Gutierrez

Fall 2013

“Computer Science is no more about

computers than astronomy is about

telescopes.” -Dijkstra

A few questions …

• Who has used a computer today?

• How many computers have you used today?

– When / how / what / why?

• What are and where are computers?

• How many computers have you used today?

So what is Computer Science, anyway?

• Computers are a powerful automation tool, but we

need to tell them what to do…

– How can we communicate with them to take advantage

of this power?

– How do we break down our problems so they can be

automatically solved by computers?

– How can we organize our solutions so that others can

take advantage of them? (and so we don’t always need

to reinvent the wheel either)

Telling computers what to do…

• Computer programming: implement solutions to

solve new problems automatically

• Types of problems we can solve with a computer?

Anything that deals with digital data

• Algorithm: a set of precise, unambiguous steps

that can be used to accomplish some task

(i.e., solve a problem)

Problem Solving Practice

Computer programming =

implementing solutions to

automatically solve problems

Practice Problem 1: Jugs • You have 2 jugs that hold 3 and 5 gallons

• You can completely fill the jugs with water,

completely empty them, or pour water from one jug

into the other

• Assuming the jugs are unmarked, how do you get

exactly 4 gallons of water in the 5 gallon jug?

Practice Problem 2: Lights • In a room there are 3 light bulbs on a wall: 1, 2, 3

• Outside the room are 3 switches: A, B, C

• Each switch controls just one light

• If you are only allowed to go into the room once

(and you can’t see the lights from outside the

room), how can you determine which switch

controls which light?

Practice Problem 3:

Cat survival rate increases w/height of fall • 22 August 1989 Science Times reports on the ability of cats

to survive large falls

• Study used data on 129 falling cats collected by the Animal

Medical Center from June to November 1984

• The cats fell from 2 to 32 stories

• Quote form the article: “Even more surprising, the larger the

fall, the greater the chance of survival. Only one of 22 cats

that plunged above 7 stories died and there was only one

fracture among the 13 that fell more than 9 stories. The cat

that fell 32 stories on concrete, Sabrina, suffered [only] a

mild punctured lung and a chipped tooth …”

• Can you explain this phenomenon?

Problem Solving • The purpose of programming is to solve a problem

• Solving a problem consists of multiple activities:

– Understand the problem

– Design a solution

– Consider alternatives and refine the solution

– Implement the solution

– Test the solution

• These activities may not be linear – they can overlap and interact

Copyright © 2012 Pearson Education, Inc.

From Problem Solving to Programming

• The key to designing a solution is breaking it down into manageable pieces

• When writing software, we design separate pieces that are responsible for certain parts of the solution

• An object-oriented approach lends itself to this kind of solution decomposition

• We will break our solutions into pieces called objects and classes

Copyright © 2012 Pearson Education, Inc.

Object-Oriented Programming

in Java

Programming • program: series of instructions executed by the

computer to complete a task

• software: program + any data it needs to run

• programs may be written in many programming

languages: Java, C, C++, C#, VB, perl, python,

JavaScript, php

• each statement of a program is executed one at a

time, in order, until the program ends

Created by Jerry Alan Fails

Java Program Structure

• In the Java programming language:

– A program is made up of one or more classes

– A class contains one or more methods (and fields)

– A method contains program statements

• Example method: println

– Allows us to print plain text to the screen (console/terminal)

– Methods may have parameters/arguments

– For example, println takes 1 argument: a string of characters to be printed, indicated by “”

– When we want a method to be executed, we invoke/call it by typing its name and arguments

Copyright © 2012 Pearson Education, Inc.

Comments

• Explain the purpose of the code and what steps are used to achieve it

• For human consumption

• Don’t affect how a program works

• In Java, can take three forms:

// this comment runs to the end of the line

/* this comment runs to the terminating

symbol, even across line breaks */

/** this is a javadoc comment */

Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc.

//***********************************************************

// Lincoln.java Author: Lewis/Loftus

//

// Demonstrates the basic structure of a Java application.

//***********************************************************

public class Lincoln

{

//---------------------------------------------------------

// Prints a presidential quote.

//---------------------------------------------------------

public static void main (String[] args)

{

System.out.println ("A quote by Abraham Lincoln:");

System.out.println ("Whatever you are, be a good one.");

}

}

Java Program Structure Example

class header

cla

ss b

od

y

comments

method

signature

method

body

parameter/argument

//***********************************************************

// Lincoln.java Author: Lewis/Loftus

//

// Demonstrates the basic structure of a Java application.

//***********************************************************

public class Lincoln

{

//---------------------------------------------------------

// Prints a presidential quote.

//---------------------------------------------------------

public static void main (String[] args)

{

System.out.println ("A quote by Abraham Lincoln:");

System.out.println ("Whatever you are, be a good one.");

}

}

Copyright © 2012 Pearson Education, Inc.

Output

A quote by Abraham Lincoln:

Whatever you are, be a good one.

White Space

• Spaces, blank lines, and tabs are called white space

• White space is used to separate words and symbols in a program – extra is ignored

• A valid Java program can be formatted many ways

– Programs should be formatted to enhance readability, using consistent indentation

– See Lincoln2.java and Lincoln3.java

Copyright © 2012 Pearson Education, Inc.

Original Lincoln.java

Source Code

Output

Lincoln2.java: different white space & comments

Lincoln3.java: different white space & comments

Basic class structure

Three major components of a class:

– Fields – store data for the object to use

– Constructors – allow the object to be set up properly

when first created

– Methods – implement the behavior of the object

public class ClassName { Fields Constructors Methods }

Object-Oriented Programming (OOP) • Java is an object-oriented programming language

• Objects make it easier to reuse code that others have written

– Java comes with built-in functionality (standard library)

• Objects commonly represent real-world entities

– For instance, an object might represent a particular employee in a company

– Each employee object handles the processing and data management related to that employee

Copyright © 2012 Pearson Education, Inc.

Classes • An object is defined by a class

• A class is the blueprint of an object:

– The class uses methods to define the behaviors of the object

– A class represents a concept, and an object represents the embodiment of that concept

– Multiple objects can be created from the same class

Copyright © 2012 Pearson Education, Inc.

Class = Blueprint

• One blueprint to create several similar, but

different, houses:

Copyright © 2012 Pearson Education, Inc.

Lab 1: Hello World Shape Applet

Your first program!

Development Environments

• There are many programs that support the development of Java software, including:

– Java Development Kit (JDK)

– Eclipse* we’ll be using

– NetBeans

– BlueJ

– jGRASP

• Though the details of these environments differ, the basic compilation and execution process is essentially the same

• Analogy: cell phones

– We all have different types of phones - they can all make calls, text, and (most) access the web

– But each phone performs these tasks a little differently

Copyright © 2012 Pearson Education, Inc.

Eclipse 101

• All your Java programs will be stored in a workspace folder

– this should be on your USB drive or laptop

• Inside the workspace, each program will have its own project folder

Importing Hello World Shape Applet • Download HelloWorldShapeApplet.zip to the Desktop or

Downloads folder from the Course Materials folder in Bb.

• Open Eclipse and go to File > Import > General > Existing

Projects into Workspace

• Select archive file, hit “Browse”, and select the

HelloWorldShapeApplet.zip you just downloaded

• Double-click the project folder, the src folder, and then the

default package. You should now see a class file,

HelloShapeApplet.java.

• To run the applet:

– Right-click HelloShapeApplet.javaclass and select “Run

As > Java Applet”

– Click on the green play button at the top of the screen

You should

see…

Goal of Lab 1

• Edit the paint method in the HelloWorldShapeApplet class

so that the blue circle forms an even border around the

yellow, like this:

• Replace “Hello World” with your name and update both

ovals so your name is centered within them

Submitting Lab 1: • Right-click on your project folder, select

– “Export > General > Archive File”

• Name it “HelloWorld_netid”

• Upload to BB – Tutorial: http://ondemand.blackboard.com/r91/movies/bb91_student_submit_assignment.htm

– Demos (from a different class, for a different assignment):

• Windows: http://msuweb.montclair.edu/~hillem/109/BBSubmissionWindows7.mp4

• Mac: http://msuweb.montclair.edu/~hillem/109/BBSubmissionMac.mp4

4.0

Course Overview &

Syllabus

Why take this course?

• You are a citizen in a computing society!

• http://www.cs.colorado.edu/why/

• http://www.bls.gov/oco/ocos303.htm

• http://www.bls.gov/oco/ocos305.htm

Course Goals

• Deepen understanding of computers

• Learn to create software applications

• Provide resources to support your future learning

• Work hard

• Have fun

Course Expectations

• Read, think, & do exercises (textbook)

• Participate – THINK! (come to every class)

• Practice, practice, practice (labs & projects)

• Understand materials (exams)

• Academic honesty

• Time commitment

• Utilize all resources to learn materials well

Focus of the Course

• Object-Oriented Software Development

– problem solving

– program design, implementation, and testing

– object-oriented concepts • classes

• objects

• encapsulation

• inheritance

• polymorphism

– graphical user interfaces (GUIs)

– the Java programming language

Copyright © 2012 Pearson Education, Inc.

Types of Thinking • Object-Oriented (OO)

– Using objects & classes, including classes written by

others (Java API libraries)

– Writing classes (including methods & fields)

• Procedural/Algorithmic

– How to use syntax (conditionals, loops, storing data in

variables) to solve basic problems

– Writing & testing methods

• Event-driven (GUIs)

– How to create interactive, visual applications

– Example: Hangman

Copyright © 2012 Pearson Education, Inc.

Textbook

Java Software Solutions

Foundations of Program Design

Seventh Edition

+ MyProgrammingLab

John Lewis

William Loftus

Homework • Complete surveys

– Assessment survey:

http://tolstoy.montclair.edu:8080/ceb/jsp/s183A.jsp

– Intro survey: • https://docs.google.com/spreadsheet/viewform?formkey=dEZCRFVY

UWVfbEp4YUk1b2VCeGpjRFE6MA

• Get MyProgrammingLab access

• Read Chapter 1

• Submit Lab 1