Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference l Parameter passing Classes, Objects, and Methods.
Chapter 51 Feb 08, Chapter 52  Methods in a class are invoked using objects A a1 = new A(); a1.func1();  Calling object and the dot can be omitted.
Classes, Objects, and Methods
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
CSE 11 HW 2, 3 and 4 Posted in public and on web Deadlines HW 2 turnin: Wed Jan 22 interview: Saturday January 25.class files in public directory for HW.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Classes, Objects, and Methods
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
10-Nov-15 Java Object Oriented Programming What is it?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
SEEM Java – Basic Introduction, Classes and Objects.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
CSE 1201 Object Oriented Programming ArrayList 1.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
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.
More About Objects and Methods
Chapter 7 User-Defined Methods.
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Class Structure 15-Jun-18.
More About Objects and Methods
CMSC 202 Static Methods.
CSC240 Computer Science III
Defining Classes II.
Class Structure 16-Nov-18.
Classes, Objects, and Methods
Class Structure 28-Nov-18.
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
More About Objects and Methods
Class Structure 25-Feb-19.
Classes and Objects Static Methods
Java Programming Language
Chapter 4 Constructors Section 4.4
Classes and Objects Object Creation
More About Objects and Methods Chapter 5 Programming with Methods
Presentation transcript:

Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any other purpose without the written permission of Walter Savitch.

CSE 11 Home Work 2 Posted in public and on web Deadlines turnin: Wed Jan 22 (Do not turn it in before January 19th) interview: Saturday January 25.class files in public directory Do not need to turnin the HW if you take an interview before the turnin deadline

turnin –c cs11w FileName.java Instructions in public. You must be logged onto sunpal as yourself. The file you are turning in must be in the same directory as you. Only has the last file you turned in. If you do an interview before the turnin deadline, then you do not turn in the assignment at all.

Inner Class Class defined within another class. Can be private and then local to the outer class. For now, we use it only to make a hw assignment into a single class for the turnin program. (Can make inner classes either public or private for this.)

Static Variables Kind of instance variables. One variables used for all objects. All objects read and write the one variable.

public class Sample { private static int count; public static final double PI = ;

Static Methods Can be invoked using class name instead of a calling object. They have no this. Anything of the form this.variable or this.method is not allowed Even if the “this.” is only implicit

Static Methods All the methods in SavithcIn Methods in the class Math Methods in Wrapper Classes

Wrapper Classes Two Personalities Personality 1: A class to stand in for a primitive type. Personality 2: A class with lots of useful static methods

The Wrapper Class Integer (Personality 1) Integer classyInt = new Integer(42); classyInt.intValue() returns 42

The Wrapper Class Integer (Personality 2) Integer.toString(42) returns “42” Integer.parseInt(“42”) returns 42

Overloading Giving two (or more) definitions to the same method name (in the same class). The two definitions must differ in the number of parameters or the type of some parameter position.

public static double average(double first, double second) { return ((first + second)/2.0); } public static double average(double first, double second, double third) { return ((first + second + third)/3.0); } public static char average(char first, char second) { return (char)(((int)first + (int)second)/2); }

Constructors Species klingonSpecies = new Species(); Method with same name as the classes. Reserves memory and initilaizes the object of the class. Creates objects.

public class Species { private String name; private int population; private double growthRate; public void writeOutput( ) { System.out.println("Name = " + name); System.out.println("Population = " + population); System.out.println("Growth rate = " + growthRate + "%"); }

public class Species { private String name; private int population; private double growthRate; public void writeOutput( ) … public Species( ) { name = “No name yet”; population = 0; growthRate = 0.0; }

public Species( String theName, int thePopulation, double theGrowthRate) { name = theName; population = thePopulation; growthRate = theGrowthRate; }

public Species( String name, int population, double growthRate) { this.name = name; this.population = population; this.growthRate = growthRate; }

Constructor Method with same name as the class. Has no returned type, not even void. Is typically overloaded. Should initialize the object. Will do whatever you program it to do.

public Species( String name, int population, double growthRate) { System.out.println(“3 arg constructor called”); //Legal. Good for debugging, but //not good for final constructor. this.name = name; this.population = population; this.growthRate = growthRate; }

Use of Constructors Species s1 = new Species(); s1.writeOutput(); Species s2 = new Species(“Red Bear”, 10, 5.7); s2.writeOutput();

Name = No name yet Population = 0 Growth rate = 0.0% 3 arg constructor called. Name = Red Bear Population = 10 Growth rate = 5.7%

Default Constructor Constructor with no parameters If you define No Constructors At All for a class, then Java automatically provides a default constructor. If you provide any constructors at all, then Java provides no other constructors. “Always” define a defualt constructor.

Legal but NOT A GOOD DEFINITION. public Species( String name, int population, double growthRate) { this.name = name; this.population = population; this.growthRate = growthRate; }

Better version: public Species( String name, int population, double growthRate) { this.name = name; this.growthRate = growthRate; if (population >= 0) this.population = population; else Error messag and exit }

Package A named collection of class. A libray of classes. Can be stored in one directory and used in any directory.

Adding a class to a package package myLibrary; public class Sample {

Using a package import myLibrary; public class MyProgram {

Package Names and Directories Package name is a relative path name from a class path directory to the package directory. Uses dots rather than /, for example myPackages.CSE11.speciesStuff Class path is an environment variables of the operating system. How you set it depends on the operating system. Typically looks like: path; path; path; Be sure to include a dot for the current directory in your class path. Class path discussed in discussion section.