Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CSC1351 9/6/2011 Where are we?. Polymorphism // Use dynamic lookup to allow different data types to be manipulated // with a uniform interface. public.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Fundamental Programming Structures in Java: Strings.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Arrays and Objects CIS 362. The familiar Employee class.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Interfaces and Inner Classes
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
3 Introduction to Classes and Objects.
Java: Base Types All information has a type or class designation
Yanal Alahmad Java Workshop Yanal Alahmad
Intro To Classes Review
Internet and Java Foundations, Programming and Practice
Chapter 3: Using Methods, Classes, and Objects
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Classes Variables That Are Not of a Built-in Type Are Objects
Interface.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
Arrays of Objects // The following does NOT create memory for
Outline Anatomy of a Class Encapsulation Anatomy of a Method
JAVA An Introduction to Java OOP Basics
Chapter 4 Defining Classes I
CIS 199 Final Review.
Chapter 7 Objects and Classes
CS 240 – Advanced Programming Concepts
Presentation transcript:

Announcements Final Exam:TBD

public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE; i++) intArray[i] = i; System.out.println( intArray[0]); changeZero(intArray); System.out.println( intArray[0]); } static void changeZero(int [] passedArray) { passedArray[0] = 1000; }

Initializing Arrays char [] cArray3 = {'a', 'b', 'c'}; int [] iArray = {1, 2, 3, 4}; double [] dArray = {3.4, 5.67e4};

Two Dimensional Arrays char [][] cArray = new char[10][20]; int [][] iArray = new int[100][50]; cArray[0][0] = ‘a’; cArray[0][1] = ‘b’; cArray[9][19] = ‘x’; iArray[0][0] = 99; iArray[1][5] = 135; iArray[99][49] = 0;

Math Class Contains Typical Math Methods In Package java.lang (i.e., automatically imported) Access using Math.math_thing Math.PI ; Math.E (log e = 1) double pow (double base, double exp) double sqrt(double number) int round(double number) int abs( int number) min(number1,number2) and max(number1, number2) double random(); //random number between 0.0 and 1.0

Tokenizing/Parsing Taking Apart a String and Separating It into Smaller Strings (i.e., “tokens”) Usually, Tokens are Items Separated by Whitespace Tokens Can Be Defined with Different Separators Parsing Takes Tokens and Applies Some Manipulation of Those Tokens

StringTokenizer Class import java.util.*; … String bigString = “This is a sentence.”; StringTokenizer st = new StringTokenizer(bigString); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } // Displays This, is, a, and sentence. one // per line

Classes A Class Is an Object Type A Class Represents a “Thing” (e.g., employee, wrench, list, store, shopping cart, etc.) Service Class – Class used by Other Programs Programmers Define Classes with Data Members (or Fields) and Methods Must Be Created in ClassName.java File Client Program – Program using a class

Access Modifiers Used to Identify What May Use Methods public – any method may use (Usually methods) private – only methods in same class may use (usually data members)

Designing a Class Decide How It Will Be Used Decide on Interface (i.e., public representation, public methods) Decide on Implementation (i.e., private data and data manipulation)

Example Class firstName lastName salary Class Employee

Constructor Class Method of Same Name Example: class Employee Method Employee() Called When Variable (invoking object) Declared (instantiated) of Class Type Initializes Instantiated Object May Have Multiple Constructors Each with Different Parameters

class Employee // Employee.java { private String firstName; private String lastName; private double salary; public Employee() { // set attributes of invoking object firstName = "NoFirstName"; lastName = "NoLastName"; salary = 0.0; }

Accessor Methods Public Methods for Getting Attributes of Class

class Employee { private String firstName; private String lastName; private double salary; public Employee() { firstName = "NoFirstName"; lastName = "NoLastName"; salary = 0.0; } public String GetFirstName() { return firstName; } public String GetLastName() { return lastName; } public double GetSalary() { return salary; }

Client Program A Program that Uses a Class Is a Client of that Class Class Objects are Declared and Instantiated Using the new keyword. new ClassName(parameters) Calls Constructor for Class Class Public Methods Called Using Dot (e.g., variable.GetFirstName(); ) variable is the invoking object

class useEmployee //useEmployee.java { public static void main(String [ ] args) { Employee emp1 = new Employee(); System.out.println("Name is" + emp1.GetFirstName() + " " + emp1.GetLastName()); } firstName lastName salary emp1 0.0 “NoFirstName” “NoLastName”

Mutator Methods Class Methods Used to Modify a Class Object are Called Mutator Methods Allows Restricted Manipulation of Class Object Data

public boolean SetSalary(double passedSalary) { if (passedSalary <= MAXSALARY) { salary = passedSalary; return true; } else { return false; }

class useEmployee { public static void main(String [ ] args) { Employee emp1 = new Employee(); System.out.println("Name is" + emp1.GetFirstName() + " " + emp1.GetLastName()); if (emp1.SetSalary(55000)) { System.out.println("Salary set to 55000"); } else { System.out.println("Salary not set"); }

Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per object) static method: no invoking object (invoke with className.method()) Example: Math class methods

Static Variable Example public class MyClass { … private final int STARTID = 1; //the following are one per object private int IDNum = -1; //the following are one per class private static int nextIDNum = STARTID; public MyClass() { … IDNum = nextIDNum; nextIDNum++; }

Static Method Example public class MyClass { … private final int STARTID = 1; //the following are one per object private int IDNum = -1; //the following are one per class private static int nextIDNum = STARTID; … public static int GetNextID() { // Called by MyClass.GetNextID() return nextIDNum; }

Other Class Methods boolean equals( objType comObj) –compares invoking object with passed object –Returns true if both are “equal”, false if not void display() –Displays attributes String toString() –Converts all attributes to String and returns String