COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Chapter 7: User-Defined Functions II
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
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
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
COMP More About Classes Yi Hong May 22, 2015.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
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
Writing Classes (Chapter 4)
COMP 110 Spring Announcements Computers in class on Friday: Lab Office Hours: Monday 12-2 New students see me after class Administrative Changes.
COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Classes CS 21a: Introduction to Computing I First Semester,
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
SEEM Java – Basic Introduction, Classes and Objects.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
User-Defined Functions
Class Structure 16-Nov-18.
Classes, Objects, and Methods
Defining Classes and Methods
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Class Structure 25-Feb-19.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Defining Classes and Methods
In this class, we will cover:
Classes, Objects and Methods
Announcements Assignment 2 and Lab 4 due Wednesday.
Presentation transcript:

COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring Announcements Program 2 due Wednesday by midnight Lab 4 Part 2 is now extra credit

COMP 110: Spring Lab 2

COMP 110: Spring Questions?

COMP 110: Spring Today in COMP 110 Classes & Methods Read Section 5.1 in text

COMP 110: Spring Classes Java programs consists of objects of various class types interacting with one another You have already been using classes System, String, Scanner, Math

COMP 110: Spring Classes A class is a definition of a kind of object It describes a general template or blueprint for creating objects of that class A class “Car” is a definition of what a “Car” is and what it can do

COMP 110: Spring Classes & Objects Objects of the class Car might have the following attributes Make Model Year Owner Location All objects of the class Car have these attributes, but with potentially different values

COMP 110: Spring Classes & Objects Class Car Make Model Year Owner Location focus object Make = Ford Model = Focus Year = 2001 Owner = Samantha Smart Location = School camry object Make = Toyota Model = Camry Year = 2000 Owner = John Doe Location = Garage

COMP 110: Spring Classes & Objects Important: classes do not have data; individual objects have data Classes specify what kind of data objects have 10 Class Car Make Model Year Owner Location camry object Make = Toyota Model = Camry Year = 2000 Owner = John Doe Location = Garage

COMP 110: Spring Classes & Objects Both the “camry” and “ford” objects satisfy the definition of the class “Car” They are said to be instances of the “Car” class When we create an object of a certain class, we are said to instantiate it

COMP 110: Spring Methods Classes also define the actions that objects of its type can perform These are called methods All objects of the same class have the same methods The class “Car” might have the following methods accelerate() brake() sell() start()

COMP 110: Spring String Methods string.length() string.charAt(index) string.substring(index1, index2) string.toLowerCase() string.equals(A_String)

COMP 110: Spring UML A UML class diagram can be useful in describing classes UML stands for Universal Modeling Language

COMP 110: Spring UML Car - make: String - model: String - year: int - owner: String - location: String + accelerate(double pedalPressure): void + brake(double pedalPressure): void + sell(String newOwner): void + start(): void Class name Attributes Methods (actions) A UML class diagram for the class “Car”

COMP 110: Spring Defining Classes You have already been defining your own classes! Every program you have written defines a class //define the class TipCalculator public class TipCalculator { … }

COMP 110: Spring Class Files & Separate Compilation Each Java class definition goes in its own, SEPARATE.java file ClassName  ClassName.java The class “Car” must be saved in a file called “Car.java”

COMP 110: Spring Class Files & Separate Compilation What happens when you compile a.java file?.java file gets compiled into a.class file.class file contains Java bytecode Car.java is compiled into Car.class You can compile a Java class before you have a program that uses it

COMP 110: Spring The Student Class Student - name: String - year: int - gpa: double - major: String - credits: int - gpaSum: double + getName(): String + getMajor(): String + printData(): void + increaseYear(): void + calculateGPA(double grade): void

COMP 110: Spring Defining the Class Student public class Student { public String name; public int year; public double GPA; public String major; //... public String getMajor() { return major; } public void increaseYear() { year++; } // … } Class name Data (instance variables) Methods

COMP 110: Spring Instance Variables The data members of a class are called instance variables public String name; public int year; public double gpa; public String major; public No restrictions on how these instance variables are used (more details later)

COMP 110: Spring Creating Objects Declaring/Initializing Primitives int i = 0; double area = 10.5; Declaring/Creating Objects Scanner keyboard = new Scanner(System.in);

COMP 110: Spring Creating Objects Create an object called “jack” of class “Student” Student jack = new Student(); Create an object Assign memory address of object to variable

COMP 110: Spring Using Instance Variables: Inside the Class Definition public class Student { public String name; public int year; public double GPA; public String major; //... public String getMajor() { return major; } public void increaseYear() { year++; } // … }

COMP 110: Spring Using public Instance Variables Outside a Class public static void main(String[] args) { Student jack = new Student(); jack.name = "Jack Smith"; jack.major = "Computer Science"; System.out.println(jack.name + " is majoring in " + jack.major); Student sam = new Student(); sam.name = "Samantha Smart"; sam.major = "Biology"; System.out.println(sam.name + " is majoring in " + sam.major); } “jack.name” and “sam.name” are two different variables because they belong to different objects

COMP 110: Spring Methods Two kinds of methods Methods that return a value Examples – string.substring() – string.charAt() Methods that return nothing Example –System.out.println()

COMP 110: Spring Methods public String getMajor() { return major; } public void increaseYear() { year++; } returns a String returns nothing return type

COMP 110: Spring Defining Methods that Return Nothing Method heading: keywords public: no restriction on how to use the method (more details later) void: the method returns nothing Method body: statements executed when the method is called (invoked) Must be inside a pair of braces public void increaseYear() { year++; }

COMP 110: Spring Method printData As usual, inside a block (defined by braces), you can have multiple statements public void printData() { System.out.println("Name: " + name); System.out.println("Major: " + major); System.out.println("GPA: " + gpa); }

COMP 110: Spring Methods All method definitions must appear inside the definition of the class to which they belong! public class Student { // … public String getMajor() { return major; } public void increaseYear() { year++; } // … }

COMP 110: Spring Methods When you use a method such as keyboard.nextInt() you are said to call or invoke the method

COMP 110: Spring Calling Methods that Return Nothing Syntax object.method(); Use them like Java statements Student jack = new Student(); jack.year = 1; jack.increaseYear(); //year = 2 jack.increaseYear(); //year = 3 System.out.println("Jack’s class year is " + jack.year); Jack’s class year is 3 Output

COMP 110: Spring Defining Methods that Return a Value Method heading: keywords public: no restriction on how to use the method (more details later) Type: the type of value the method returns Method body: statements executed Must be inside a pair of braces Must have a return statement public String getMajor() { return major; }

COMP 110: Spring The Return Statement A method that returns a value must have at least one return statement Terminates the method, and returns a value to the caller Syntax: return Expression; Expression can be any expression that produces a value of the return type 34

COMP 110: Spring Methods that Return a Value public String getClassYear() { if(year == 1) return "Freshman"; else if(year == 2) return "Sophomore"; else if... }

COMP 110: Spring Calling Methods that Return a Value Syntax object.method(); Use as a variable of the method’s return type Student jack = new Student(); jack.name = "Jack Smith"; jack.major = "Computer Science"; String m = jack.getMajor(); System.out.println("Jack’s full name is " + jack.getName()); System.out.println("Jack’s major is " + m); 36

COMP 110: Spring The Return Statement Can also be used in methods that return nothing Terminates the method Syntax: return; public void increaseYear() { if(year >= 4) return; //exit the method year++; } 37

COMP 110: Spring The Return Statement Typically, you want to write your methods to contain only one return statement It will make your programs easier to read

COMP 110: Spring The Main Method The main method is a method like any other public static void main(String[] args) { // … } The main method is not invoked by you, but invoked automatically by the system when you run a program

COMP 110: Spring The Main Method Classes that have a main method can be run as a program Not all classes have a main method Some are used inside the main method of other classes

COMP 110: Spring Local Variables Variables declared inside a method are called local variables As opposed to instance variables, which are declared in the class definition public class Student { public double gpa; //an instance variable public static void main(String[] args) { int size; //a local variable // … }

COMP 110: Spring Local/Instance variables Instance variables Declared in a class Can be used anywhere in the class that declares the variable, including inside the class’ methods Local variables Declared in a method Can only be used inside the method that declares the variable

COMP 110: Spring Programming Demo Counter class A class used to count things Define a class that records a count as a non- negative whole number Include methods for the following Reset counter to 0 Increment the count Decrement the count Get the current count Display the current count

COMP 110: Spring Counter Class No methods should allow the count to become negative Write a program to test the class

COMP 110: Spring Programming Demo Programming

COMP 110: Spring Wednesday More about Classes