1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Looking inside classes Fields, Constructors & Methods Week 3.
Starting to program with BlueJ and the Beetle - a 2 day course for real beginners Lynda Thomas
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Getting to know Greenfoot
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
How to Create a Java program CS115 Fall George Koutsogiannakis.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
CS 201 Functions Debzani Deb.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
1 CSC 221: Computer Programming I Fall 2004 Objects and classes: a first pass  software objects, classes, object-oriented design  BlueJ IDE, compilation.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Introducing Java.
A First Program Using C#
Microsoft Visual Basic 2005: Reloaded Second Edition
Comments are for people Header comments supply basic information about the artifact.
Introduction to Programming with Java. Overview What are the tools we are using – What is Java? This is the language that you use to write your program.
Introduction to Object-Oriented Programming
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
OBJECTS AND CLASSES CITS1001. Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Chapter 1 - Getting to know Greenfoot
Classes CS 21a: Introduction to Computing I First Semester,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Chapter 1 Object Orientation: Objects and Classes.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Introduction to programming in the Java programming language.
1 CSC 221: Computer Programming I Spring 2008 Objects and classes: a broad view  software objects, classes, object-oriented design  BlueJ IDE, compilation.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Working With Objects Tonga Institute of Higher Education.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Chapter 1: Introduction to Computers and Programming.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Chapter 1 Object Orientation: Objects and Classes.
Week 21 Introduction to Programming Ms. Knudtzon C Period Lecture 3.
3 Introduction to Classes and Objects.
Objects and Classes CITS1001 week 1.
Yanal Alahmad Java Workshop Yanal Alahmad
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Java Programming with BlueJ
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Java part 2
February , 2009 CSE 113 B.
Object Oriented Programming in java
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
References Revisted (Ch 5)
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

1.1: Objects and Classes msklug.weebly.com

Agenda: Attendance Let’s get started What is Java? Work Time

Objects first? We are going to begin talking about programming within the language of Java We will start with the overall structure of Java code then dig in throughout the year

What is Java? Java is a programming language  It is compiled into instructions that the computer can understand  It was developed by Sun Microsystems and is maintained by Oracle  Similar in syntax to C++ and C  We are using Java Development Kit (JDK)  This is an object-oriented programming language

What is BlueJ? BlueJ is Integrated Development Environment (IDE)  It is a program that allows us to develop projects in Java  We can write, compile, run and debug in BlueJ

What are Classes? Metaphorically, classes are the factories for the create objects. If you needed to create a circle, you would go to Circle class. Each circle you create is an object. Sometimes, we call each object we create an “instance” of the class. In BlueJ, a project is a collection of classes. Convention: We start the names of classes with capital letters. We start instances with a lower case letter.

Woah, wait what? Objects are created from classes, the class describes the kind of object Example:  Class: Human  Object/Instance: Ms. Klug

Let’s get started Start BlueJ Open the project “figures” Create a circle object

Class of type “Canvas”

Object bench An instance of object Circle, (notice the lowercase first letter)

Exercise 1.1 Go ahead!

But I don’t see a circle Double click on the instance of an object to see its attributes. Let’s look at the attributes of the circle we created False, not visible

Objects do things Object do things using methods  Let’s look at the methods associated with our circle object Let’s “call/invoke” makeVisible()

Exercise 1.2 You are now ready for this exercise

Parameters Some methods allow for input of a value. This input values are called parameters. The header of a method is called its signature. It provides information needed to invoke that method. Signature Comment

More on the signature of a Method void moveHorizontal(int distance) type name

Exercise 1.3 You are now ready for this exercise.

Review so far: BlueJ Java Object/Instance Class Method Parameters Signature

Data types Parameters have types. The type defines what kinds of values a parameter can take. int signifies whole numbers. String indicates combinations of letters and symbols. Values for strings are always in double quotes. boolean indicates values that are either true or false.

Exercises 1.4, 1.5, 1.6, and 1.7 Go for it!

Multiple Instances Many similar objects can be created from a single class.

State The values of all the attributes of an object is referred to as the object’s state in the object inspector. Java refers to these object attributes as fields. Double click on the instance of an object to see the object’s state.

Exercise 1.8 It’s time!

Different classes have different attributes

What is an object? Objects of the same class have the same fields and methods Similarly, objects of a different class may have different fields and methods Example:  Circle has has a “diameter” field

Exercise 1.9 Use paper to keep track on how you got it done. You’ll need those for a future exercise. What is the minimum number of steps needed?

Exercise 1.10 Select Show Terminal from the View menu. This shows another window that BlueJ uses for text output. Then select Record method calls from the terminal’s Options menu. This function will cause all our method calls (in their textual form) to be written to the terminal. Now create a few objects, call some of their methods, and observe the output in the terminal window.

What did we learn from Exercise 1.10? We can see what creating an object and giving it a name looks like. We see that, to call a method on an object, we write the name of the object, followed by a dot, followed by the name of the method. The command ends with a parameter list—an empty pair of parentheses if there are no parameters. All Java statements end with a semicolon.

Exercise 1.11 Select Show Code Pad from the View menu. This should display a new pane next to the object bench in your main BlueJ window. This pane is the Code Pad. You can type Java code here.

Exercise 1.12 In the Code Pad, type the code shown above to create a person object and call its makeVisible and moveRight methods. Then go on to create some other objects and call their methods. Tip You can recall previously used commands in the Code Pad by using the up arrow.

Exercise 1.13 Open the house project. Create an instance of class Picture and invoke its draw method. Also, try out the setBlackAndWhite and setColor methods.

Exercise 1.14 How do you think the Picture class draws the picture?

Concept: Method calling. Objects can communicate by calling each other’s methods. So… how do we write the class for such an object?

Concept: The source code of a class determines the structure and behavior (the fields and methods) of each of the objects of that class.  The text that defines the details of the class A large part of learning the art of programming is learning how to write these class definitions

Exercise 1.15 Look at the pop-up menu of class Picture again. You will see an option labeled Open Editor. Select it. This will open a text editor displaying the source code of the class.

About compilation… The computer doesn’t understand Java directly The computer uses machine language (also called assembler) Compiling translates the Java code into machine code Java actually creates virtual machines…

You are now ready… Exercises

Time for the lab-classes project I’ll start the exploration, then You play while doing Exercise 1.21

Methods that return values Methods may return information about an object via a return value. Like with parameters (values passed into methods), return values can vary based on type.

Exercise 1.22 Create some student objects. Call the getName method on each object. Explain what is happening.

Objects as parameters Not only can you pass primitive types (int, boolean) into a method, but you can also pass objects (instances of type String, Circle, Student, etc.)

Exercises Do it now! Big idea: in order to enroll a student, you need to create several instances from the Student class. Remember that the student’s name is not the same as the name of the object.

Summary object Java objects model objects from a problem domain. class Objects are created from classes. The class describes the kind of object; the objects represent individual instantiations of the class. method We can communicate with objects by invoking methods on them. Objects usually do something if we invoke a method.

Summary parameter Methods can have parameters to provide additional information for a task. signature The header of a method is called its signature. It provides information needed to invoke that method. type Parameters have types. The type defines what kinds of values a parameter can take.

Summary multiple instances Many similar objects can be created from a single class. state Objects have state. The state is represented by storing values in fields. method calling Objects can communicate by calling each other’s methods.

Summary source code The source code of a class determines the structure and behavior (the fields and methods) of each of the objects of that class. result Methods may return information about an object via a return value.

Last Exercises !

Review Method Signatures  Starts with the word “public” or “private”  Then the type returned by the method or “void” if the method does not return a value  Then the name of the method  Then open parenthesis

Then the inputs to the method  If there is no inputs, then finish the method with a close paren  Each input is specified by a pair of words A type A variable  Variables must start with a letter  Variables can not be the same name as the class, method, or any fields for the object A comma separates the type/variable pair, if there is more than one