COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.

Slides:



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

COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
1 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
COMP More About Classes Yi Hong May 22, 2015.
COMP 110 Computer Basics Luv Kohli August 25, 2008 MWF 2-2:50 pm Sitterson
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
COMP Mid-Term Review Yi Hong May 27, 2015.
COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
CSI 3125, Preliminaries, page 1 Compiling the Program.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Methods.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Java Methods and Applications CSIS 3701: Advanced Object Oriented Programming.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Examples of Classes & Objects
Department of Computer Science
COMP 110 Review for midterm exam
Using local variable without initialization is an error.
Writing Methods.
CSC 113 Tutorial QUIZ I.
COMP 110 Information hiding and encapsulation
Java: Getting Started Basic Class Structure
Classes & Objects: Examples
Variables and Their scope
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
CS2011 Introduction to Programming I Methods (II)
class PrintOnetoTen { public static void main(String args[]) {
February , 2009 CSE 113 B.
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Announcements Assignment 2 and Lab 4 due Wednesday.
Methods/Functions.
Presentation transcript:

COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014

Announcements Midterm exam currently scheduled for Monday, October 20 2

Change exam date? Options: ◦ Monday, October 13 ◦ Wednesday, October 22 ◦ Keep it on Monday, October 20 3

Questions? 4

Classes, Objects, and Methods Class: a definition of a kind of object Object: an instance of a class ◦ Contains instance variables (data) and methods Methods ◦ Methods that return a value ◦ Methods that return nothing 5

Today in COMP 110 Local variables and instance variables Brief introduction to methods with parameters In-class exercise 6

Local/Instance variables Instance variables ◦ Declared in a class ◦ Confined to the class  Can be used anywhere in the class that declares the variable, including inside the class’ methods Local variables ◦ Declared in a method ◦ Confined to the method  Can only be used inside the method that declares the variable 7

Simple example public class Student { public String name; public int classYear; //... public void printInfo() { String info = name + “: ” + classYear; System.out.println(info); } public void increaseYear() { classYear++; } public void decreaseYear() { classYear--; } 8 classYear and name are instance variables can be used in any method in this class info is a local variable declared inside method printInfo() can only be used inside method printInfo()

Simple example public class Student { public String name; public int classYear; //... public void printInfo() { String info = name + “: ” + classYear; System.out.println(info); } public void increaseYear() { classYear++; info = “My info string”; // ERROR!!! } public void decreaseYear() { classYear--; } 9 The compiler will not recognize the variable info inside of method increaseYear()

More about local variables public static void main(String[] args) { Student jack = new Student(); jack.name = “Jack Smith”; jack.major = “Computer Science”; String info = “Hello there!”; System.out.println(info); System.out.println(jack.name + “ is majoring in ” + jack.major); Student apu = new Student(); apu.name = “Apu Nahasapeemapetilon”; apu.major = “Biology”; System.out.println(apu.name + “ is majoring in ” + apu.major); } 10 Variable info in main method not affected by variable info in printInfo method in class Student

Methods with parameters Compute the square of this number ◦5◦5 ◦ 10 ◦7◦7 I could give you any number, and you could tell me the square of it We can do the same thing with methods 11

Methods with parameters Parameters are used to hold the value that you pass to the method Parameters can be used as (local) variables inside the method public int square(int number) { return number * number; } 12 Parameters go inside parentheses of method header

Calling a method with parameters public class Student { public String name; public int classYear; //... public void setName(String studentName) { name = studentName; } public void setClassYear(int year) { classYear = year; } 13

Calling a method with parameters public static void main(String[] args) { Student jack = new Student(); jack.setName(“Jack Smith”); jack.setClassYear(3); } More details next week… 14 Arguments

In-class exercise 15

Monday Even more about classes 16