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
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference l Parameter passing Classes, Objects, and Methods.
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.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
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.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
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.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
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.
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.
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.
Introduction to Methods
Unit 2: Java Introduction to Programming 2.1 Initial Example.
COMP More About Classes Yi Hong May 22, 2015.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Classes, Objects, and Methods
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
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.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Java Byte Code l The Java compiler generates Java Byte Code. (Most.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CS61B L03 Building Objects (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Walt Savitch University of California, San Diego
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Day Class Definitions and Methods Local & global variables, parameters & arguments,
Procedural programming in Java Methods, parameters and return values.
COMP 150: Introduction to Object-Oriented Programming Lecturer: Dr. AJ Bieszczad 4-1 Chapter 4 l Class and Method Definitions l Information Hiding and.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Chapter 1- Basic Computing and your first program.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
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.
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 Computer Science / Procedural – 67130
Classes, Objects, and Methods
Defining Classes and Methods
Classes, Encapsulation, Methods and Constructors (Continued)
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Introduction to Java Brief history of Java Sample Java Program
Defining Classes and Methods
Defining Classes and Methods
Introduction to Computer Science and Object-Oriented Programming
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.

Home Work 1 Is on the handout. If you do not pass HW 1 by the deadlines, you must drop the course. No exceptions. If you take an interview before the turnin deadline, you do not need to turn in the assignment. Instructions for turning in assignments will be given in a later lecture.

Console Output int number = 7; System.out.println(number + “ things”); Screen output: 7 things

Console Input System.out.println(“Enter an integer:”); int number = SavitchIn.readLineInt(); Sets the value of number to the integer types in at the keyboard. The integer must be on a line by iteslf.

SavitchIn.readLineInt() SavitchIn is a class. readLineInt is a method. () indicates no arguments. Returns a value of type int, namely the integer entered at keyboard. Good error messages if user screws up.

SavitchIn A class written in Java. Is not a standard Java library class. The file SavitchIn.java (or SavitchIn.class) must be in the same directory as your program. File SavitchIn.java is with programs on machine in SUNPAL and is on CD in the Savitch text.

SavitchIn readLineInt() readLineDouble() readLineNonWhiteChar() readLine()

readLineNonWhiteChar() Different from other methods Reads the first nonwhite char on a line and discards the rest of the line. The char need not be the only thing on the line. If line is yes it reads the ‘y’

public class FirstProgram { public static void main(String[] args) { System.out.println("Hello out there."); char answerLetter; answerLetter = SavitchIn.readLineNonwhiteChar(); if (answerLetter == 'y') System.out.println("Nice weather."); System.out.println("Good-bye."); }

public class NameOfProgram { public static void main(String[] args) { Your Code Goes Here. }

Class A type A value of a class type is called an object An object has both data and actions Can have multiple kinds of data Actions called methods (called procedures or functions in some other languages)

String class Data is something like “Hi Mom” Sample methods (actions) “Hi Mon”.length() returns 6 So the following is legal int count = “Hi Mom”.length(); String greeting = “Hello; System.out.println( greeting.length());

Sample Class Definition

public class Sample { public String name; public int count; public void writeOutput() { System.out.println("Name = " + name); System.out.println(“Count = " + count); } //This goes in a file named Sample.java

Instance Variables public class Sample { public String name; public int count; Sample o = new Sample(); o.name = “Sam”;

Using a Class Sample anObject = new Sample(); anObject.name = “Sally”; anObject.count = 7; anObject.writeOutput();

Two kinds of Methods Methods that return a value Void Methods

Methods That Return a Value Must have a return statement of the form return expression ;

public class Sample2 { public String name; public int count; public int doubleCount() { return 2*count; }

void Method public class Sample { public String name; public int count; public void writeOutput() { System.out.println("Name = " + name); System.out.println(“Count = " + count); }

The this parameter public class Sample { public String name; public int count; public void writeOutput() { System.out.println("Name = " + this.name); System.out.println(“Count = " + this.count); }

The this parameter Sample anObject = new Sample(); anObject.name = “Sally”; anObject.count = 7; anObject.writeOutput();

public void writeOutput() { System.out.println("Name = " + this.name); System.out.println(“Count = " + this.count); } //this means anObject

Local Variable A Variable Declared Inside a Method definition

Two Kinds of Variables Instance Variables Local Variables No Global Variables

Parameters Like blanks in a method definiton. Filled in with arguments when the method is called (invoked).

public int projectedPopulation(int years) { double populationAmount = population; int count = years; while ((count > 0) && (populationAmount > 0)) { …. } if (populationAmount > 0) return (int)populationAmount; else return 0; }

All Parameters are Call-by-Value Only the value of the argument is plugged in for the parameter. You cannot define a method that takes an int variable as an argument and changes the value of the argument. More to say about class parameters, but they are strictly speaking call-by-value.

Information Hiding & Encapsulation Design classes & methods so that a programmer who uses them need only know what the methods do, not how they do it. User need never look at the body of a method definition. Also need not know how the data is implemented.

Need not know how the data is implemented A class for amount of money: A single instance variable of type double Or Two instance variables of type int Or One instance variable of type int Or One instance variables of type String

All Instance Variables Should Be Private public class Sample3 { private String name; private int count; public void readName() { name = SavitchIn.readLine(); }

Cannot Access a Private Instance Variable by Name Outside the Class Definition Public static void main(String[] args) { Sample3 s = new Sample3(); s.name = “Sam”; //Illegal s.count = SavitchIn.readLineInt(); //Illegal

Need Accessor and Mutator Methods Accessor methods: read instance variables, usually spelled “get…” Mutator methods: change instance variables, usually spelled “set…”

Mutator Methods Should Always Check That Change Is Sensible

public void setPopulation(int newPopulation) { if (newPopulation >= 0) population = newPopulation; else { System.out.println( "ERROR: using a negative population."); System.exit(0); }

Encapsulation ADT (Abstract Data Type) API (Application Programmer Interface) You should be able to change the implementation of a class and not have to change any application program that uses the class.

javadoc Automatically extracts HTML documentation from classes. Only takes public stuff Only takes comments of the form /** */