Object Declaration. <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier)

Similar documents
Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Computational Expression

Java Bytecode (binary file)

Basics of Java Programming

CS 231 Data Structures and Algorithms, Fall 2016

INTRODUCTION 1 AND REVIEW

Computer Components. Software{ User Programs. Operating System. Hardware

An Introduction to Processing

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Datatypes, Variables, and Operations

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Key Differences Between Python and Java

Lecture 02, Fall 2018 Friday September 7


BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Section 2.2 Your First Program in Java: Printing a Line of Text

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

d-file Language Reference Manual

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

Introduction to Java

Java Primer 1: Types, Classes and Operators

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

Chapter 2: Programming Concepts

c) And last but not least, there are javadoc comments. See Weiss.

3. Java - Language Constructs I

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

Identifiers and Variables

Computer Components. Software{ User Programs. Operating System. Hardware

Array. Prepared By - Rifat Shahriyar

Full file at

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Creating Java Programs with Greenfoot

Program Fundamentals

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

CEN 414 Java Programming

Basics of Java Programming. Hendrik Speleers

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

last time in cs recitations. computer commands. today s topics.

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Data types Expressions Variables Assignment. COMP1400 Week 2

Identifiers. Identifiers are the words a programmer uses in a program Some identifiers are already defined. Some are made up by the programmer:

PROGRAMMING FUNDAMENTALS

Java Notes. 10th ICSE. Saravanan Ganesh

TABLE OF CONTENTS 2 CHAPTER 1 3 CHAPTER 2 4 CHAPTER 3 5 CHAPTER 4. Algorithm Design & Problem Solving. Data Representation.

STRUCTURING OF PROGRAM

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

Fundamentals of Programming Session 25

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

The New C Standard (Excerpted material)

CLASSES AND OBJECTS IN JAVA

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Introduction to C++ Homework Assignment. Fundamental Concepts. rvalues and lvalues. Programming Standards. A Basic Program.

Fall 2017 CISC124 9/16/2017

Java Identifiers, Data Types & Variables

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Software and Programming 1

Java Fall 2018 Margaret Reid-Miller

Java objects: a first view [Reading: chapters 1 & 2] CSC 142 B 1

Introduction to Programming Using Java (98-388)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Data Types, Literals, Operators

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Fundamentals of Programming Session 23

DaMPL. Language Reference Manual. Henrique Grando

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 2 Working with Data Types and Operators

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

6.096 Introduction to C++ January (IAP) 2009

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Assignment Marking Criteria

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

A variable is a name that represents a value. For

COMP163. Introduction to Computer Programming. Introduction and Overview of the Hardware

Chapter 1. Introduction to Computers and Java Objects. Background information. » important regardless of programming language. Introduction to Java

SSOL Language Reference Manual

Lecture 18 Tao Wang 1

Getting started with Java

Lecture Set 2: Starting Java

Index COPYRIGHTED MATERIAL

Working with JavaScript

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

Computer Science is...

Binghamton University. CS-140 Fall Data Types in Java

Assembler Programming. Lecture 10

Introduction to Java. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Lecture Set 2: Starting Java

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

7. C++ Class and Object

Java: introduction to object-oriented features

Programming. Syntax and Semantics

today cs3157-fall2002-sklar-lect05 1

Technical Questions. Q 1) What are the key features in C programming language?

CMPS 134: Computer Science I Fall 2011 Test #1 October 5 Name Dr. McCloskey

Transcription:

Object Declaration Every object used must be declared Syntax: <class name> <object name>; <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier) Identifier: any sequence of letters, digits, underscores, and dollar signs with the following limitations: Must begin with a letter Cannot contain any spaces or other white space Cannot be a Java reserved word (aka keyword ) Java Convention on Identifiers: First letter lowercase First letter of subsequent words uppercase Reserved Word: an Identifier that is used for a specific purpose and cannot be used for any other purpose. Example of some of Java s Reserved Words: public private protected import class new static void byte short int long float double boolean final return while if do for

Object Creation No objects are actually created by a declaration (with declaration, only an Identifier used to refer to an object is created) Use the new command. Syntax: <object name> = new <class name> (<arguments>) <object name>: the name of the declared object <class name>: the name of the class to which the object belongs <arguments>: sequence of values passed to the method Examples of Object Declaration: Student dave; Noisemaker clapper; Ship battleship; Examples of Object Creation: dave = new Student (4.0, 1234); clapper = new Noisemaker (); battleship = new Battleship (numpegs, xpos, ypos, dir);

Message Sending Once an object has been created, messages can be sent to it Syntax: <object name>.<method name> (<arguments>); <object name>: name of a declared object. : the dot notation gives relation to the items on either side of the dot <method name>: name of a method of the object <arguments>: sequence of values passed to the method Examples of Message Sending: dave.setgpa (2.5); clapper.makenoise (decibellevel); battleship.inserthit ();

Three (3) main parts: 1. Comments 2. Import statements 3. Class declarations Comments Program Components Uses: 1. State the purpose of the program 2. Explain the meaning of code 3. Give other explanations to help programmers understand the program Syntax: /* ANY text between slash-asterisk and asterisk-slash */ OR // ANY text following two slashes to the end of the line All programs should contain a Header Comment containing the following information: 1. Program Title 2. Author 3. Course (including section number) 4. Date Written (or Due Date) 5. Description of Program Comments are NOT required to run a program. However, they are indispensable in writing easy to understand code. (You will lose points if your programs do NOT contain adequate comments.) Excessive comments can hurt more than help in understanding code.

Three (3) main parts: 1. Comments 2. Import statements 3. Class declarations Import Statements Program Components Classes are grouped into Packages To use a class from a Package, the class must be imported into the program. Syntax: import <package name>.<class name>; import: a reserved word indicating a class is to be imported <package name>: the name of the package to which the class belongs <class name>: the name of the class to be imported With subclasses, use multiple dot notations For example: import java.awt.image.colormodel; To import more than one class from a package, use the asterisk notation. Syntax: import <package name>.*; When the asterisk notation is used, ALL of the classes (or subclasses) of a particular package (or super class) will be imported. Java Convention: all package names are lowercase.

Three (3) main parts: 1. Comments 2. Import statements 3. Class declarations Class Declaration Syntax: Program Components class <class name> { <class member declarations> } class: a reserved word indication the declaration of a class <class name>: the name of a class (any valid identifier) Java convention: class names start with a capital letter and each subsequent word in the class name also has a capital letter <class member declarations>: a sequence of class member declarations class member: a data value or a method A program can (and usually does) have more than one class, but only one class will be designated the main class. Typically, the application and the main class have the same name The main class must define a method called main. This method is executed FIRST when the Java application is executed.

Method Declaration Syntax: <modifiers> <return type> <method name> (<arguments>) { <method body> } <modifiers>: sequence of terms designating different kinds of methods <return type>: type of data value returned by a method <method name>: name of the method (any valid identifier) <arguments>: sequence of values passed to a method <method body>: sequence of instructions public static void main (String args []) modifier modifier return type method name parameter

Edit-Compile-Run Cycle Three (3) steps: 1. Step 1: Type in the program using an editor and save it 2. Step 2: Compile the source file 3. Step 3: Execute the bytecode file using an Interpreter Step 1: Type in the program using an editor and save it Examples of editors: Code Warrior JavaWorks vi emacs Save the entered code with the following filename syntax: <name of main class>.java The resultant is a source file written in a high level language (HLL) Examples of high level languages; Java C C++ Pascal BASIC Fortran Machines (i.e., computers) can only understand machine language (written in binary). Machine language is a low level language (LLL).

Edit-Compile-Run Cycle Three (3) steps: 1. Step 1: Type in the program using an editor and save it 2. Step 2: Compile the source file 3. Step 3: Execute the bytecode file using an Interpreter Step 2: Compile the source file A compiler translates the HLL into a LLL called bytecode (Code Warrior contains a compiler) The bytecode file that is generated is titled as follows: <name of source file>.class The whole source file is compiled at once Compilers can detect Compilation Errors (aka Syntax Errors ) Compilation Errors: errors resulting from the source code containing text that does not obey the rules of the language Examples of Compilation Errors: Mismatched parantheses ( ( ) ) ) Missing punctuation (e.g., no semi-colon at the end of statements) Misspelled reserved words The compiler will NOT generate a bytecode file if compilation errors exist in the source file. Most good compilers give detailed error messages when identifying the compilation errors

Edit-Compile-Run Cycle Three (3) steps: 1. Step 1: Type in the program using an editor and save it 2. Step 2: Compile the source file 3. Step 3: Execute the bytecode file using an Interpreter Step 3: Execute the bytecode file using an Interpreter The interpreter executes instructions one line at a time The interpreter can detect Execution Errors (aka Run-Time Errors ) Execution Errors: errors occurring during the execution of the instructions Examples of Execution Errors: Dividing by zero Using undeclared objects/data values Null pointers Editor Compiler Interpreter Running Program source file bytecode file