More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 6. Overloading Methods and Constructors  Two or more methods in a class may have the same name as long as their parameter lists are different.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Enhancing classes Visibility modifiers and encapsulation revisited
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 7: More about Methods 1 Chapter 7 More about Methods.
CS1020 Data Structures and Algorithms I Lecture Note #6 Exceptions Handling exceptional events.
Saravanan.G.
Writing Classes (Chapter 4)
Introduction to Objects A way to create our own types.
Java programs are built from classes. There is nothing else, but classes.
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.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 9 - Classes with Class Members Class Variables Class Methods How to Access Class Members When to Use Class Members Class Constants Example Program.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Chapter One Lesson Three DATA TYPES ©
A: A: double “4” A: “34” 4.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
1 Enums (Chapter 4) To enumerate is: to name things one after another in a list Java has a type, called an enum, where a programmer specifies a finite.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Lecture 3 John Woodward.
Implementing Classes Yonglei Tao.
Selected Topics From Chapter 6 Iteration
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Defining Classes and Methods CS140: Introduction to Computing 1 Savitch Chapter 5, 6 9/30/13.
More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.
CS 302 Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Chapter Three - Implementing Classes
Introduction to Computer Programming
Review Operation Bingo
המשך תכנות מונחה עצמים תרגול מס' 9.
בניית מחלקות.
null, true, and false are also reserved.
An Introduction to Java – Part I, language basics
COMPUTER 2430 Object Oriented Programming and Data Structures I
בניית מחלקות.
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
CS 302 Week 9 Jim Williams.
Code Animation Examples
CS2011 Introduction to Programming I Objects and Classes
Building Java Programs
Take out a piece of paper and PEN.
class PrintOnetoTen { public static void main(String args[]) {
COMPUTER 2430 Object Oriented Programming and Data Structures I
Objects with ArrayLists as Attributes
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13

What is displayed? Nothing 4.IDK 2 public class MyTime { private int hour; private int min; public static final int MIN_IN_HOUR = 60; } public static void main(String[] args) { MyTime myTime = new MyTime(); System.out.print(MyTime.hour); }

What is displayed? Nothing 4.IDK 3 public class MyTime { private int hour; private int min; public static final int MIN_IN_HOUR = 60; } public static void main(String[] args) { MyTime myTime = new MyTime(); System.out.print(MyTime. MIN_IN_HOUR ); }

What should the return type be? 1.void 2.int 3.float 4.none of these 4 public class MyTime { private int hour; private int min; public MyTime(){ hour = 0; min = 0; } //compare two MyTime objects public _______equals(){ //TODO comparison logic }

What is should the parameter type be? 1.void 2.int 3.MyTime 4.none of these 5 public class MyTime { private int hour; private int min; public MyTime(){ hour = 0; min = 0; } //compare two MyTime objects public boolean equals(___ obj){ //TODO comparison logic }

What is missing? 1.this. 2.foo. 3.MyTime. 4.none of these 6 public class MyTime { private int hour; private int min; public MyTime(){ hour = 0; min = 0; } public boolean equals(MyTime foo){ if( ___ min == min && ___ hour == hour) return true; else return false; }

How many copies of bc? one per object public class Ball { private int x, y; private int velocityY; private int velocityX; private static final int RADIUS = 10; private static final int GRAVITY = 3; private static int bc = 0; public Ball() { x = 0; y = 0; velocityY = 0; velocityX = 0; }

How many copies of totalDeposits? one per object public class BankAcct{ int acctNum; double balance; private static double interest = 1.9; private static double totalDeposits = 0; BankAcct(int number) { acctNum = number; balance = 0.0; }

How to update totalDeposits? 1.totalDeposits += deposit 2.this.totalDeposits += deposit 3.BankAcct.totalDeposits += deposit 4.IDK 9 public class BankAcct{ int acctNum; double balance; private static double interest = 1.9; //sum of account balances private static double totalDeposits = 0; BankAcct(int number) { acctNum = number; balance = 0.0; } public void deposit(double deposit){ balance += deposit; ____________________; }

Two add() methods ? 1.Error 2.OK if the parameter types differ 3.OK if the parameter names differ 4.IDK 10 class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public void add(int min){ this.min += min; } public void add(int hour){ this.hour += hour; }

Two add() methods ? 1.Error 2.OK if param. type and/or number of param. differ 3.OK if the parameter names differ 4.IDK 11 class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public void add(int m, int h){ min += m; hour += h; } public void add(int hour){ this.hour += hour; }

Two add() methods ? 1.Error 2.OK if the return types differ 3.OK if the parameter names differ 4.OK if param. type and/or number of param. differ 12 class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public void add(int min){ this.min += min; } public int add(int min){ this.min += min; return this.min; } }

private final String color ? 1.Member of Suit objects 2.Method of Suit objects 3.Constructor of Suit objects 4.IDK enum Suit { CLUBS("black"), DIAMONDS("red"), HEARTS("red"), SPADES("black"); private final String color; private Suit(String sColor) { color = sColor; } public String getColor() { return color; } }

private Suit(String sColor) ? 1.Member of Suit objects 2.Method of Suit objects 3.Constructor of Suit objects 4.( ╯ °□°) ╯︵ ┻━┻ 14 enum Suit { CLUBS("black"), DIAMONDS("red"), HEARTS("red"), SPADES("black"); private final String color; private Suit(String sColor) { color = sColor; } public String getColor() { return color; } }