Chapter 4: A Paradigm Program structure Connecting to the Java world Types Access modifiers Lifetime modifiers.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Classes and Objects CMSC 202. Version 9/122 Programming & Abstraction All programming languages provide some form of abstraction. – Also called information.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Lecture 2: Object Oriented Programming I
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Java Programming Review (Part I) Enterprise Systems Programming.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Writing Classes (Chapter 4)
Java Language and SW Dev’t
Introduction to Object-Oriented Programming
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
The Java Programming Language
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Java Translation Program Structure
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
By Mr. Muhammad Pervez Akhtar
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Object Oriented Programming Lecture 2: BallWorld.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Introduction to Modular Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Data types and variables
Chapter 4 Procedural Methods.
Chapter Three - Implementing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object Oriented Programming
Chapter 7 Procedural Methods.
Anatomy of a Java Program
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Presentation transcript:

Chapter 4: A Paradigm Program structure Connecting to the Java world Types Access modifiers Lifetime modifiers

Example Program import java.lang.*; public class FirstProgram { public static void main (String [] args) { System.out.println(“My first Java!”); }

How to run?  javac FirstProgram.java  ls FirstProgram.* FirstProgram.class FirstProgram.java  java FirstProgram My first Java! 

Class name is IMPORTANT a handle for object creation (later) Filename = ClassName.java compiled file = ClassName.class

Java program = community of objects Objects are instances of classes A source program = Series of class descriptions

Class definition A class header plus a class body public class FirstProgram { … } curly braces! layout NOT significant => find your own style, but be consistent!

Class body a series of members of 2 types: Data fields: internal data of an object Methods: defines behavior (FirstProgram: no data field, 1 method, main method = first method invoked by the interpreter)

Method description Like class: header + body modifiers return-type name (arguments){ sequence-of-statements } public static void main (String[] args) {..} MUST be nested properly inside a class definition, lower case initial (convention!)

Connecting to the JAVA world import java.lang.*; makes this package visible, huge standard JAVA library (API = Applications Programming Interface) System.out.println(“My first Java!”); class System, data member out, behavior println(), one string argument

Types Primitive data types: lowercase! int newVar = 42; int | float | double | boolean | void | … predefined, cannot define ourselves Classes: e.g. String String name = “Fred Smith”; most types, can define ourselves

boolean Result of relational operator: <, <= logical operator: &&, ||, (or &, |) strict left-to-right short-circuit evaluation: SAFE: (x > 0) && ((x/3) > 1.7)

Arrays public class SecondProgram { public static void main (String [] args) { if (args.length > 0) System.out.println(“Hello ” + args[0]); else System.out.println(“Hello everybody!”); }

Arrays cont. length not specified: String[] data member length holds actual size subscript operator: [i], e.g. args[0], zero-based: 0.. n-1 if length=n Strings: + means concatenation: System.out.println(“Hello ” + args[0]);

Three Access modifiers control who can access/use classes, data members, and methods public: everybody, private: only inside the current class def protected: current class def and all subclasses (more in later chapters)

Bank account example public class BankAccount { private int balance = 0; public void deposit (int amount) { balance = balance + amount); } public void withdrawal (int amount) { balance = balance – amount; }

Good practise data members are private (some) methods are public: provide services/interface to customers of the class public class FirstProgram { … public static void main (String[] args) {..

Lifetime modifiers public static void main (String[] args) {.. Static parts are shared by all instances of a class, they come into existence when the class is loaded, even before any instance is created. Access by prefixing with class name: System.out Math.log

Summary Import connects a program to libraries Program = sequence of class defs Class = header + body Body = data members + methods Method = header + body Method header = modifiers, return-type, name, arguments Method body = sequence of statements Access modifiers = public/private/protected static members: shared by all instances