COMP 110 Classes Luv Kohli October 1, 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

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference l Parameter passing Classes, Objects, and Methods.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Classes, Objects, and Methods
COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
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 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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
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.
COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
COMP Mid-Term Review Yi Hong May 27, 2015.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Day 5. Task: Implement your vending machine code into the program with JOptionPane I/O (modify it) it to me.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
COMP 150: Introduction to Object-Oriented Programming Lecturer: Dr. AJ Bieszczad 4-1 Chapter 4 l Class and Method Definitions l Information Hiding and.
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.
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
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.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
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.
Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods.
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.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
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 about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
CE221 – Data Structures & Algorithms Lab Introduction
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.
Building Java Programs
Yanal Alahmad Java Workshop Yanal Alahmad
COMP 110 Review for midterm exam
User-Defined Functions
Defining Classes and Methods
Building Java Programs
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Self study.
Building Java Programs
Building Java Programs
Building Java Programs
Object Oriented Programming in java
Defining Classes and Methods
Defining Classes and Methods
Announcements Assignment 2 and Lab 4 due Wednesday.
Building Java Programs
Building Java Programs
Presentation transcript:

COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014

Announcements Lab 4 due 2

Questions? 3

Today in COMP 110 Briefly go over Program 2 Classes 4

Program Operand 1Operand 2Operator

Program Assume the String above is in the variable calculation. int firstSpace = calculation.indexOf(‘ ’); int lastSpace = calculation.lastIndexOf(‘ ’); double operand1 = Double.parseDouble(calculation.substring(0, firstSpace)); double operand2 = Double.parseDouble(calculation.substring(lastSpace + 1)); char operator = calculation.charAt(firstSpace + 1);

Program 2 Now you can determine which calculation to perform using a switch or if/else double result = 0.0; switch (operator) { case ‘+’: result = operand1 + operand2; break; case ‘-’: result = operand1 – operand2; break; // and so on } 7

Exponent ^ double result = 0.0; switch (operator) { case ‘^’: result = 1.0; for (int count = 0; count < (int) operand2; count++) result *= operand1; break; case ‘-’: result = operand1 – operand2; break; // and so on } 8

== generally bad for floating-point Floating-point numbers are imprecise ◦ After doing many computations, value may not be exactly the same ◦ 5.0 * 3.0 might be different from Okay for this assignment for the divide by 0 extra credit 9

num++ vs. ++num num++ is NOT num + 1 num++ does num = num + 1 So does ++num, BUT, there is a difference int num1 = 5; System.out.println(num1++); // outputs num1 (5), then increments num1 int num2 = 5; System.out.println(++num2); // increments num2, then outputs num2 (6) 10

Class A class is the definition of a kind of object ◦ A blueprint for constructing specific objects 11 Class Name: Automobile Data: amount of fuel speed license plate Methods (actions): accelerate: How: Press on gas pedal. decelerate: How: Press on brake pedal.

Objects, Instantiation 12 Object Name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK” Object Name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR” Object Name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF” Instantiations, or instances, of the class Automobile

UML (Universal Modeling Language) 13 Automobile - fuel: double - speed: double - license: String + accelerate(double pedalPressure): void + decelerate(double pedalPressure): void Class name Data Methods (actions)

Objects Important: classes do not have data; individual objects have data Classes specify what kind of data objects have 14

Class files and separate compilation Each Java class definition goes in its own, SEPARATE.java file ClassName  save the file as ClassName.java Student.java includes the class Student 15

Class files and separate compilation What happens when you compile a.java file? ◦.java file gets compiled into a.class file  Contains Java bytecode  Same filename except for.class instead of.java  (note to self: show this) You can compile a Java class before you have a program that uses it 16

class Student 17 Class Name: Student - Name - Year - GPA - Major - Credits - GPA sum + getName + getMajor + printData + increaseYear How: increase year by 1 + calcGpa How: average grades

class Student 18 Class Name: Student - name: String - year: int - gpa: double - major: String - credits: int - gpaSum: double + getName(): String + getMajor(): String + printData(): void + increaseYear(): void + calcGpa(double grade): void

Defining a class public class Student { public String name; public int classYear; public double GPA; public String major; //... public String getMajor() { return major; } public void increaseYear() { classYear++; } 19 Class name Data (instance variables) Methods

Creating an object Create an object jack of class Student Student jack = new Student(); Scanner keyboard = new Scanner(System.in); Create an object keyboard of class Scanner 20 Create an object Return memory address of object Assign memory address of object to variable

Instance variables Data defined in the class are called instance variables public String name; public int classYear; public double GPA; public String major; 21 public: no restrictions on how these instance variables are used (more details later – public is actually a bad idea here) type: int, double, String… variables

Using instance variables: inside the class definition 22 public class Student { public String name; public int classYear; public double GPA; public String major; //... public String getMajor() { return major; } public void increaseYear() { classYear++; }

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 apu = new Student(); apu.name = “Apu Nahasapeemapetilon”; apu.major = “Biology”; System.out.println(apu.name + “ is majoring in ” + apu.major); } jack.name and apu.name are two different instance variables because they belong to different objects 23

Methods Two kinds of methods ◦ Methods that return a value  Examples: String’s.substring() method, String’s.indexOf() method, etc. ◦ Methods that return nothing  Example: System.out.println() 24

Methods public String getMajor() { return major; } public void increaseYear() { classYear++; } 25 returns a String returns nothing return type

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() { classYear++; } 26

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); } 27

Calling methods that return nothing object, followed by dot, then method name, then () Use them as Java statements Student jack = new Student(); jack.classYear = 1; jack.increaseYear(); System.out.println(“Jack’s class year is ” + jack.classYear); 28

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; } 29

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 type specified by the return type in the method heading 30

Methods that return a value public String getClassYear() { if (classYear == 1) return “Freshman”; else if (classYear == 2) return “Sophomore”; else if... } 31

Calling methods that return a value object, followed by dot, then method name, then () (same as before) Use them as a value of the type specified by the method’s return type Student jack = new Student(); 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); 32

return statement Can also be used in methods that return nothing Terminates the method Syntax: ◦ return; public void increaseYear() { if (classYear >= 4) return; classYear++; } 33

Friday More about classes 34