Object Oriented Programming with Java

Similar documents
Add Subtract Multiply Divide

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Introduction to Programming Using Java (98-388)

Programming with Java

Language Reference Manual

Operators. Java operators are classified into three categories:

Course Outline. Introduction to java

The Java language has a wide variety of modifiers, including the following:

Index COPYRIGHTED MATERIAL

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4


Building Java Programs

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Building Java Programs

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

COP 3330 Final Exam Review

Lecture 3 Tao Wang 1

COMP 202 Java in one week

STUDENT LESSON A12 Iterations

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Fundamental of Programming (C)

COMP 202. Java in one week

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Program Elements -- Introduction

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

1 Lexical Considerations

BASIC ELEMENTS OF A COMPUTER PROGRAM

Building Java Programs Chapter 2

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Java+- Language Reference Manual

5. Selection: If and Switch Controls

Mr. Monroe s Guide to Mastering Java Syntax

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Basics of Java Programming

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Computer Science & Engineering 150A Problem Solving Using Computers

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

C: How to Program. Week /Mar/05

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Full file at

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CS 106 Introduction to Computer Science I

COMP 202 Java in one week

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Repetition Structures

Full file at

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Building Java Programs


Lecture 7: Classes and Objects CS2301

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

JAVA OPERATORS GENERAL

Chapter 2 - Introduction to C Programming

Chapter 2: Basic Elements of C++

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction to Java & Fundamental Data Types

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Declaration and Memory

2.5 Another Application: Adding Integers

Java Notes. 10th ICSE. Saravanan Ganesh

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Following is the general form of a typical decision making structure found in most of the programming languages:

Introduction to Programming

STRUCTURING OF PROGRAM

Fundamentals of Programming

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Lexical Considerations

Introduction to Computer Programming

Introduction to Programming using C++

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

Chapter 2: Using Data

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CT 229 Java Syntax Continued

COMP Primitive and Class Types. Yi Hong May 14, 2015

Flow Control. CSC215 Lecture

PLT Fall Shoo. Language Reference Manual

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Transcription:

Object Oriented Programming with Java What is Object Oriented Programming? Object Oriented Programming consists of creating outline structures that are easily reused over and over again. There are four elements of Object Oriented Programming: Abstraction Generalization for Relationship Inheritance Encapsulation Encoding of Data Inheritance Relationships between parent-child objects Polymorphism Flexibility of interchangeable parts / reusability Imagine that there is an object known as Automobile. An automobile has properties such as a year, model, make, color, etc. An automobile also has actions, such as drive forward, turn right, turn left, etc. The properties are known as Attributes, and the actions are known as Methods. We can also create multiple instances of the object Automobile, one for each student in a classroom. Each student owns a car, and no two students have the same vehicle; however, each student s car has a model and make and each car has the same action abilities. We can use a UML Class Diagram as a development tool to design abstract ideas of objects; we will refer to objects as classes. Class Automobile Attributes: Year :: number Model :: text Make :: text Color :: text Methods: Drive(distance, speed, direction) Turn(speed, direction) PrintLocation() The UML Class Diagram is a table that breaks a class into two categories: Attributes and Methods. The example UML Class diagram above follows the Automobile model discussed earlier. It contains the attributes Year, Model, Make, and Color as well as three methods: Drive, Turn, and PrintLocation. The each attribute has a data type next to it to describe the format of information stored for that attribute. The Year attribute will have a number, and the Model, Make and Color attributes will all have text for a data type. Object Oriented Programming with Java Provided by the Academic Center for Excellence 1 Reviewed June 2008

The Methods are actions that an Automobile object can perform. Methods often expect data before performing a function. For example, an Automobile cannot drive forward without knowing the Speed, Distance, and Direction of the movement. PrintLocation does not require previous information; it will be used simply to output the current location of the car. Data Types vs. Classes We mentioned data types in the UML diagram; data types are the set of possible values that could exist for an instance of data. For example, the data type of a student s age would be a number, and the data type of their name would be text. Below is a table of common data types within various programming languages. The Type column contains variable types used in Java. Type Description Example Usage char A single ASCII character Letter Grade of a student String A series of ASCII Student s Name characters int A short integer Age of a student long A long integer Student ID number float A decimal number Balance of Student Account double A precise decimal Grade Point Average of Student boolean A true or false value Commuting/Non-commuting Student We can create a class of anything we want; however, we still have to create the values within a class based on the principles of these basic data types. Additionally we can create classes of previously created classes and objects. This process is actually very beneficial to abstract critical thinking. We could create a Student class that includes our previously created Automobile class. For example: each student has a name(string), an age(int), an ID number(long), a GPA(double), and a car(automobile). Variables: Scope, regulations and class over-loading The Automobile class consists of attributes and methods. Attributes are known as variables. Algebraically speaking, variables represent an unknown and more importantly, they are used to perform calculations regardless of the individual value. In programming, variables are actually memory locations; the memory is used to store values assigned to a variable. Variables, classes, functions, etc. have a few rules regarding usage and assignment. Variable names are case-sensitive: o studentage is a different variable instance than STUDENTage Variable names cannot be pre-existing reserved commands: o Cannot name a variable char, String, int or any other data type Variable names cannot begin with a number, contain symbols, or spaces other than an underscore ( _ ): o Cannot name a variable: 08Students, #phonenumber, student age Provided by the Academic Center for Excellence 2 Object Oriented Programming with Java

The scope of a variable refers to the life-time and locality of a value. For example, if we create five students that each have a name, GPA, class schedule, etc. it is unnecessary for studenta to have access to studentb s GPA. We would make the GPA a Private variable to isolate its availability to within the instance of a student. The process of making variables Private vs. Public can help prevent unnecessary repetition, recursion, and memory allocation. Usually, all attributes are created as Private and all methods are Public. This process then calls for two additional methods that allow a program to read and write to the Private variable from outside the class. Class overloading is the process of calling a function and giving it different parameters. Parameters are the required information for a function/class to operate. The Automobile class created on the first page requires distance, speed, and direction to call the function Drive. We can set up the Drive function to have a default direction, Forward, and only require a distance and speed. This is an example of polymorphism, adding to the overall flexibility of the class. When the Drive function is called, the class will automatically decide which version of Drive to use based on the number of parameters that were given to it. A common error that occurs while overloading classes and functions is Data Type Mismatch. This error is caused by a function receiving a different data type value than what it was expecting. For example, we cannot perform mathematic operations on Strings and text. Command Syntax Syntax is the proper way to call or use a function. The following are a few important general Java formatting syntax requirements and tips: Complete statements of code should always have a semi-colon(;) at the end Grouped code segments such as statements within a loop or class, should be enclosed within curly braces and Order of Operations (Order of Precedent) is a non-configurable guideline for mathematical procedures Parameters sent to a function are enclosed in parentheses and should have the same data types as what is expected Creating constructors is a good way to control class creation; constructors are a default setting for a created class. Provided by the Academic Center for Excellence 3 Object Oriented Programming with Java

The following are examples of proper syntax and description of common functions & commands within the Java language. Declarations: Main Operational Class: This is used to create a main executable class to operate a program. It operates as the main code routine. Public class Example () Public static void main (String args[])... Insert main code here Variable Declarations/Assignments: The following creates integer variables x, y, and z, as well as two String variables. These variables are local or private, and cannot be seen outside of the class. String variable test is actually assigned a value during declaration. We can declare multiple variables in the same line of code as long as they are of the same data type; they should be separated by a comma. Public class example () Private int x; Private int y,z; Private String test= This is a String Variable ; Private String Name; Documentation adding comments and reminders is important in designing and redesigning a program. Documentation also proves useful when debugging and trying different features. Single-Line Comment- used for documenting the rest of a line of text. // Comment Block Comment used for larger comments such as describing a process or documenting authorship. /* Comment Line 1 Comment Line 2 Comment Line 3 */ Provided by the Academic Center for Excellence 4 Object Oriented Programming with Java

Import used to include previously designed classes/functions within a class. Imports are the very first lines of a program. Import Java.Util.Scanner; // Used to import class Scanner Import Java.Awt; // imports a graphical class Conditional Operands and Arithmetic Symbols Expression Description Expression Description n1 + n2 Addition n1 == n2 Equality n1 n2 Subtraction n1!= n2 Does not equal n1 * n2 Multiplication n1 > n2 Greater than n1 / n2 Division, integer quotient after division n1 >= n2 Greater than or equal to n1 % n2 Modulus, integer remainder after division n1 < n2 Less than n1 = n2 Assignment n1 <= n2 Less than or equal to Loops & repetition: Looping and repetition is used to reuse sections of code that are required multiple times. Looping is very useful for reviewing conditions, counting, and handling user-entry error. The only major caution with looping is to avoid infinite loops; infinite variable creation and storage can cause serious computer problems. Finite Loop For/Next Loop best used for counting; the exit condition is checked before executing the statements within the loop. For (variable; condition; increment) statements in loop While/Do Loop best used for indefinite loops where an unknown quantity of code executions is required; the exit condition is checked before executing the statements within the loop. While ( condition ) Statements in loop Provided by the Academic Center for Excellence 5 Object Oriented Programming with Java

Conditional Statements: Conditional statements are used to make decisions within a program or application. There are multiple conditional statement types; which one to use depends on the situation. A simple yes/no question would best be decided by an If/Else statement. If a decision needs to be made regarding different values such as outputting a response based on what grade a high school student is in, a Switch-Case statement would be more efficient. A process known as nesting can also be used by embedding If/Else statements within If/Else statements. If/Else Statement If ( condition ) Statements if condition is true Else Statements if condition is false Switch-Case Statement use break to exit switch-case as it will continue to test cases. Switch ( variable ) Case (value1): Statement if variable = value1; Break; Case (value2): Statement if variable = value2; Break; Default: Statement if variable is anything else; More Information: For more information regarding specific Java functions, visit the online tutorials and documentation from the creators of Java technology. Tutorials: http://java.sun.com/docs/books/tutorial/index.html API Documentation: http://java.sun.com/j2se/1.5.0/docs/api/ Provided by the Academic Center for Excellence 6 Object Oriented Programming with Java