DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.

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

13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Objects & Methods Defining Classes. Slide 2 Reference Variables Revisited Remember: Object variables are references (aka pointers) Point to “null” by.
Introduction to Programming G51PRG University of Nottingham Revision 1
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 2 Review Questions
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter Four Defining Your Own Classes continued.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Methods
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CIT 590 Intro to Programming First lecture on Java.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 2: Java Fundamentals
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Introduction to Object-oriented Programming.
Chapter 4 Introduction to Classes, Objects, Methods and strings
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
Primitive and Reference Data Values
CompSci 230 Software Construction
Chapter 4 Defining Instantiable Classes
Primitive and Reference Data Values
Defining Your Own Classes Part 1
Chapter 2 Java Programming Basics
An Introduction to Java – Part I, language basics
Java Classes and Objects 3rd Lecture
Introduction to Object-Oriented Programming with Java--Wu
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Programs and Classes A program is made up from classes
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Defining Classes and Methods
Defining Classes and Methods
In this class, we will cover:
Classes, Objects and Methods
DON’T PANIC!! Lots of new notions coming in these slides
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later Do worry if none of it makes any sense You should get the general picture now Now brace yourself … stop me if you get confused … ask questions … throw money …

Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static parts define what objects in the class look like. Every class is automatically in existence when the program runs.

Classes and Objects An object is an instance of a class, and is created using the new operator. The non-static part of the class defines what each object looks like. Many instances (objects) can be created from a class … no limit except reality An object contains information and functionality of a thing, e.g., Account, Vehicle, Employee, etc.

Classes and Objects Components Classes (and thus also objects) are composed of methods and data values Data values store information Methods do things, and also have their own local data

Visibility Modifiers: public and private The modifiers public and private designate the accessibility of objects and class data values and methods If a component is declared private, nothing outside the class can access it. If a component is declared public, anything outside the class can access it. Make things private whenever you can That helps with encapsulation

Class and Instance Data Values Class data (indicated by the static modifier) is used to maintain information shared by all instances or aggregate information about the instances. Make class data private whenever you can Instance data is used to maintain information specific to individual instances. Make instance data private always

Primitive and Reference Data Values primitive variables contain values Reference variables point at objects byte short int double long float boolean String Applet MessageBox HiLo InputBox etc. char primitive reference Data Type

Variable and Constant Data Values There are two types of data values: Account SV129 minimum balance current balance account prefix 6427 opening balance A constant whose value must remain fixed over time. A variable whose value can change over time. Constants are indicated by the final modifier Non-final public static data can give you warts

Methods Methods have code (to do stuff) and data A method defined for a class is called a class method (indicated by the static modifier) and a method defined for an object is called an instance method. Every program has one static method called main. Thats where the program starts.

Method Data = Local Variables A local variable is a variable that is declared within a method. Local variables are accessible only in the method in which they are declared.

Calling Methods To instruct a class or an object to do something, we call one of its methods Values passed to a method are called arguments or parameters of the message. The (formal) parameters of a method are local variables that receive the parameters Methods can return one data value

Calling a Method Call deposit with the argument chk-008 deposit deposit

Getting an Answer This call has no argument. chk-008 getMonthlyFee monthly fee The method returns the value monthly fee.

Program Components A Java file is composed of comments, import statements, and class declarations.

//Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Program Component: Comment Comment

Program Component: Import Statement //Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Import Statement

Program Component: Class Declaration //Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Class Declaration

Program Component: Method Declaration //Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Method Declaration

Method Declaration Elements public static void main (String[ ] args){ MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Modifier Return Type Method Name Parameter Method Body

Statements Method bodies contain statements Simple statements end with a ; Compound statements are enclosed in {}s public static void main (String[] args) { int someData = 0; if (someData == 27) { System.out.println(Cosmic rays!); someData = 0; }

Sample Method public double fromDollar( double dollar ) { double amount, fee; fee = exchangeRate - feeRate; amount= dollar * fee; return amount; } Parameter Local Variables

Files and Classes A Java program file ends with.java There must be one public class per file It must have the same name as the file One public class (i.e., one file) must have the main method

Simple Java Programs Simple Java programs can be written in just the one file, containing One public class (with the main method) Other class methods and final data values as required Such programs do not create any objects, but simply run class methods (starting with the main method) and use primitive data.

Template for Simple Java Applications center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Comment Import Statements Class Name Method Body

DONT PANIC!! Well write some programs without creating objects, i.e., youll have to think about only classes (and their methods and data values) Well write some programs that create objects from pre-existing classes, i.e., youll use objects before you have write code to define them. Then well write programs that define and create their own objects