1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class l class myClassName extends SuperClass { –// your.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Access to Names Namespaces, Scopes, Access privileges.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Enhancing classes Visibility modifiers and encapsulation revisited
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP Introduction to Programming Adrian Ilie July 13, 2005.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
The Java Programming Language
From C++ to Java A whirlwind tour of Java for C++ programmers.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
CS 261 – Data Structures Introduction to C Programming.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CS884 (Prasad)java11Extn1 Java 1.1 Extensions Nested classes static public/protected/private.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Simple Arrays Arrays of primitives and Strings Sections 7.1, 7.2.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Department of Computer Engineering Methods Computer Programming for International Engineers.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Methods.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Functions + Overloading + Scope
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Examples of Classes & Objects
Introduction to Modular Programming
Some Eclipse shortcuts
Methods Chapter 6.
Namespaces, Scopes, Access privileges
Chapter 4 Procedural Methods.
Classes & Objects: Examples
Group Status Project Status.
Objects and Classes Creating Objects and Object Reference Variables
Chapter 7 Procedural Methods.
Namespaces, Scopes, Access privileges
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Object Oriented Programming
Introduction to Object-Oriented Concepts in Java
Presentation transcript:

1 Creating Classes & Applications Chapter Six

2 Defining Classes l class MyClassName { // new class l class myClassName extends SuperClass { –// your class is a subclass –class MyRunClass implements Runnable {.

3 Defining Instance Variables l class Bicycle extends PersonPowerVechicle { // this was an idea from earlier chapter l String bikeType; l int chainGear; rearCogs; l int currentGearFront; current GearRear; } –the { } surround four instance variables

4 Constants - What’s Your “Pi” l final float pi = ; // NO changes l final boolean debug = false; l final int maxsize = 4000; –final is the keyword - no changes are allowed to pi, debug or maxsize l final String star = “*”;

5 Class Variables l Use static l Examples: –static int sum; –static final int maxObjects = 10; l Class variables “global…” good throughout class

6 Creating Methods - Basic Parts l public private protected package (default) not discussed today “all modifiers” l Name the method l Type it (ie. int etc.) l list parameters (arguments) coord(10,10) l body l “method’s signiture” = all of the above

7 Example - method definition l returntype methodname (type1 arg1,...) } l int[] makeRange (int lower, int upper) {...} l Example The RangeClass class l Whats important? l Why is the output The array [ ]

8 class RangeClass { l int[] makeRange(int lower, int upper) { l for(int i = 0; i < arr.length; i++) { –arr[i] = lower++; } return arr; } l public static void main(String arg[]) { l int theArray[]; l RangeClass theRange = new l RangeClass( ); l //continued next slide

9 Continued... l theArray = theRangemakeRange(1,10); l System.out.print (“The array: [ “); l for (int i = 0; i < the Array.length; i++) { –System.out.print(theArray[i] + “ “); } l } l Book output is correct l [ ]

10 The this Keyword - refers to the current object l t = this.x // the x instance variable for this object... t has just been assigned the current value of object x...which had better have already been defined, assigned etc. l return this; // return current object l this,myMethod(this) // call mymethod, defined in this class, and pass it the current object... You should know what is “CURRENT”

11 scope, rules of (not the mouthwash) l When can a variable be referenced (used)? l Example l class ScopeTest { l int test = 10; l void printTest ( ) { l int test = 20; l System.out.println(“Test = “ + test); } }

12 Passing arguements -parameters to Methods l The PassByReference class l Pass by value (a copy - original stays as is) l Pass by reference (address passed anything goes) and usually does... l Next Slide shows a listing to demonstrate

13 class PassByReference { l int onetozero( int arg[]) { l int count = 0; l for(int i=0; i<arg.length;i++) { –if (arg[i] = = 1) { »count++; »arg[i] = 0; } } »return count; –} l }

14 l public static void (String arg[]) { l int arr[] = { 1, 3, 4, 5, 1, 1, 7}; //any nums l PassByReference test = new PassByReference( ); l int numOnes;

15 l System.out.print(“Values of the array: [ “); l for (int i = 0; i < arr.length; i++) { –System.out.print(arr[i] + “ “); } // num + space l System.out.println (“ ] “); l numOnes = test.onetozero(arr); l System.out.println(“Number of Ones = “ + numOnes); System.out.print(“New values of the array: [ “);

16 l for (int i = 0; i < arr.length; i++) { –System.out.print(arr[i] + “ “); –} l System.out.println(“ ]” + “ “); } l } l OUTPUT: [ ] l Number of Ones = 3 l “New” [ ]

17 Class and Instance Variables and Now....Methods l Class methods are available to any instance of the class itself - and can be available to other classes. l Some class methods can be used anywhere l Java Math library as an example: l float root = Math.sqrt(453.0); l System.out.print(“Largest x vs. y = “ + Math.max(x,y)); // returns Biggest of x or y

18 l int count = Integer.parseInt(“42”, 10) l Example of a wrapper “like a gum wrapper” l Not much between your gum and the protective wrapper but it does keep it clean.....sort of.

19 Java Applications l public static void main ( String args[] ) {...} l What does all that stuff mean? –public available to other classes & objects –static = this is a class method –void means the main( ) does NOT return anything –main( ) takes one parameter : an array of strings –body {...} would normally follow

20 Java Applications are stand alone programs...

21 Passing Arguments to Java Programs l java Myprogram arguementOne 2 three –The space between argumentOne, the 2, and three is important.... l java myprogram Java is cool l or java myprogram “ Java is waycool”

22 EchoArgs l class EchoArgs { l public static void main(String args[]) { –for( int i = 0; i < args.length; i++) { »System.out.println »(“Argument “ + i + “ : “ + args[i]);} } } l java EchoArgs jump l jump l java EchoArgs “foo bar” zap teaddle 5 l foo bar zap teaddle 5

23 Last application of Chapter.....Wake up it’s almost time to go home.... class SumAverage { public static void main (String args[]) { int Sum = 0;

24 l for (int i =0; i < args.length; i++) { –sum += Aargs[i]; } l System.out.println(“Sum is: “ + sum); l System.out.printlm(“Average is: “ + (float) sum / Args.length); l }

25 l ERROR: so sum += Integer.parseInt(args[i]); and all is well l java SumAverage = = Sum 6 Avg 2