Agenda Scope of variables Comparable interface Location class in GridWorld homework.

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

INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Location Class Gridworld. Location Class Encapsulates the coordinates for an actor’s position in a grid – Row and Column number Rows increase going down.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
Liang Chapter 6 Variable scope and the keyword this.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
COMP More About Classes Yi Hong May 22, 2015.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Java Arrays  Java has a static array capable of multi-dimensions.  Java has a dynamic array, which is also capable of multi-dimensions.  The ArrayList.
10-Nov-15 Java Object Oriented Programming What is it?
Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Interfaces.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Nested Classes CompSci 230 S Software Construction.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Topics Instance variables, set and get methods Encapsulation
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CompSci 230 S Software Construction
Examples of Classes & Objects
AKA the birth, life, and death of variables.
Java Course Review.
Interface.
Lecture 5: Some more Java!
CompSci 230 S Programming Techniques
Implementing Classes Yonglei Tao.
Namespaces, Scopes, Access privileges
CS 302 Week 11 Jim Williams, PhD.
Interfaces.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Classes Variables That Are Not of a Built-in Type Are Objects
Classes & Objects: Examples
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
class PrintOnetoTen { public static void main(String args[]) {
Namespaces, Scopes, Access privileges
Scope of variables class scopeofvars {
Method of Classes Chapter 7, page 155 Lecture /4/6.
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Agenda Dancing bug class Class vs object Comparable interface
Corresponds with Chapter 5
Announcements Lab 5 due Wednesday at noon.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Agenda Scope of variables Comparable interface Location class in GridWorld homework

scope The scope of a variable is the part of the program over which the variable name can be referenced(used). You cannot refer to a variable before its declaration.

You can declare variables in several different places: 1.In a class body as state variables. Variables declared here are referred to as class-level variables. 2.As parameters of a method or constructor. 3.In a method's body or a constructor's body. 4.Within a statement block, such as inside a while or for block.

Variables As state variables public class Circle { private double radius; //constructor public Circle(double r) {.. } public void setRadius(…) { … } public double area() { … } public double getRadius() { … } } Class-level variables are accessible from anywhere in the class.

Variables as method parameters public void setRadius(double newValue) { radius = newValue; } Variables declared as method parameters can be accessed from within the method body. They are local variables, only visible within the body block

Varailes in a method's body or a constructor's body. Assume there is a BankAccount class: public void deposit(double amt){ double totalAmt; totalAmt = balance + amt; } amt and totalAmt are local variables, not visible outside the body block

Variables inside a for loop for (int i = 1; i <= 10; i++) { System.out.println(i); } System.out.println(i); <= invalid the scope of the counter i is inside the for loop

How about a nested loop? for (int x = 0; x < 5; x++) { for (int y = 0; y < 3; y++) { System.out.println(x); System.out.println(y); } } a nested block can access variables declared in the outer block

public class ScopeExample { public static void main(String[] args) { int var1; for (int var2 = 0; var2 < 5; var2++) { method1(); } } public static void method1() { int var3; var1++; for (int var4 = 0; var4 < 2; var4++) { var3 + var4 = 1; } } }

For this scope topic, refer to the following link: __Language/VariableScope.htm

Comparable interface The purpose of this interface is to compare objects. It has only one method public interface Comparable { int compareTo(Object otherObject); //returns a neg number if a<b //return a pos number if a>b //returns 0 if a=b }

String class comparison String x= "xyz"; String y= "abc"; System.out.println(x.compareTo(y)); System.out.println(y.compareTo(x)); What’s the output?

How to test? public static void main(String[] args) { BankAccount aAcct = new BankAccount(100.0); BankAccount bAcct = new BankAccount(200.0); System.out.println(aAcct.compareTo(bAcct)); }

public class BankAccount implements Comparable{ private double balance; public BankAccount(double amt){ balance = amt;} //other methods… public int compareTo(Object otherObj){ BankAccount otherAcct = (BankAccount) otherObj; Int retValue; If(balance < otherAcct.balance) retValue = -1; If(balance > otherAcct.balance) retValue = 1; If(balance == otherAcct.balance) retValue = 0; return retValue; }

Location Class public class Location implements Comparable { private int row; // row location in grid private int col; // column location in grid public static final int LEFT = -90; public static final int RIGHT = 90; public static final int HALF_LEFT = -45; ….. public static final int NORTH = 0; …..

public Location(int r, int c) { row = r; col = c; } public int getRow() { return row; } public int getCol() { return col; }

public int compareTo(Object other) { Location otherLoc = (Location) other; if (getRow() < otherLoc.getRow()) return -1; if (getRow() > otherLoc.getRow()) return 1; if (getCol() < otherLoc.getCol()) return -1; if (getCol() > otherLoc.getCol()) return 1; return 0; }

List Interface 3 classes that implements the List interface: LinkedList, ArrayList, Vector 3 classes are available by importing java.util.*;

Practice on the board Class: Student Variables: lastName, firstName, grades Constructor with 2 parameters to initialize the state variables lastName and firstName; set grades to 0 methods: getName- return the full name(lastName + firstName) setGrade- take a parameter and set the grades

Create a UEmployee class that contains member variables for the university employee name and salary. The UEmployee class should contain member methods for returning the employee name and salary. Create Faculty and Staff classes that inherit the UEmployee class. The Faculty class should include members for storing and returning the department name. The Staff class should include members for storing and returning the job title.