Agenda Dancing bug class Class vs object Comparable interface

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Advertisements

GridWorld Case Study The Classes A Summary by Jim Mims.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
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.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Location Class Gridworld. Location Class Encapsulates the coordinates for an actor’s position in a grid – Row and Column number Rows increase going down.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
Introduction to Searching and Sorting
Previous Exam 1.0. Question 1 - a Is the following statement true or false? Briefly explain your answer. A && B is the same as B && A for any Boolean.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Agenda Scope of variables Comparable interface Location class in GridWorld homework.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
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!
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
© A+ Computer Science - Row = 0 Column = 0.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
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.
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.,
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CS884 (Prasad)java11Extn1 Java 1.1 Extensions Nested classes static public/protected/private.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
The GridWorld Case Study Program Section 1 - Overview of the Class Hierarchies Section 2 - The Actor Class Section 3 - The Rock and Flower Classes Section.
Arrays and Lists: Handling Infinite Data CS 21a: Introduction to Computing I First Semester,
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of.
Chomp. How is the game played Human player goes first choose a square, all to the right and down are “eaten” computer takes a turn whoever is forced to.
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
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.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Introduction to Programming
Interface.
Lecture 2: Data Types, Variables, Operators, and Expressions
Agenda About Quiz ChameleonCritter class CrabCritter class homework.
Interfaces.
Introduction to Searching and Sorting
An Introduction to Java – Part II
Introduction to Java Programming
Classes & Objects: Examples
د.سناء الصايغ الفصل الأول البرمجة الشيئية
© A+ Computer Science - GridWorld © A+ Computer Science -
Agenda About Homework for BPJ lesson 36 About practice of last class
class PrintOnetoTen { public static void main(String args[]) {
Arrays and Array Lists CS 21a.
Comparable and Comparator Interfaces
© A+ Computer Science - GridWorld The GridWorld case study provides a graphical environment where visual objects inhabit and interact.
Creating and Modifying Text part 3
© A+ Computer Science - GridWorld © A+ Computer Science -
Agenda Remarks about last homeworks Class methods(static methods)
© A+ Computer Science - GridWorld © A+ Computer Science -
First Semester Review.
CSC 205 – Java Programming II
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Agenda Dancing bug class Class vs object Comparable interface Location class in GridWorld homework

public class DancingBug extends Bug { public int dancecount; private int[] c; public DancingBug(int[] d){ dancecount=0; c = d; } public void act(){ int turncount = c[dancecount]; for(int x=0; x<turncount;x++) turn(); super.act(); if (dancecount<c.length-1){ dancecount++; } else{ dancecount=0; } } Dancing bug

public class DancingBug extends Bug{ private int [] turnArray; private int turns; private int moves; public DancingBug(int[] a){ turnArray = a; turns = 0; moves = 0; } Dancing bug

Dancing bug public void act(){ if (moves == turnArray.length){ if (turns < turnArray[moves]){ turn(); turns++; } else if (canMove()==false){ turn(); } else{ move(); moves++; turns = 0;} }} Dancing bug

Acrobat class Acrobat a = new Acrobat(“James”, “M”, 18); State variables: name sex age Constructor(…) methods: getName() setAge() clap(int x) Acrobat a = new Acrobat(“James”, “M”, 18); String objName= a.getName(); a.setAge(20);

How to compare objects? Circle cir1 = new Circle(3.0); cir1 == cir2 ?????? Circle cir3; cir3 = cir1; cir1 == cir3 ??????

String str1 = “Hello”; String str2 = “Hello”; str 1 == str2 ???? String str3 = new String(“Hello”); str3 == str1?????

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 int compareTo(Object other) { Location otherLoc = (Location) other; if (getRow() < otherLoc.getRow()) return -1; if (getRow() > otherLoc.getRow()) return 1; if (getCol() < otherLoc.getCol()) if (getCol() > otherLoc.getCol()) return 0; }

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

Homework I DiagonalBug A DiagonalBug is a Bug that initially faces northeast, northwest, southeast, or southwest. When a DiagonalBug cannot move, it turns 180 degrees. Create the DiagonalBug class. Write a constructor for the DiagonalBug and set the initial direction in the constructor. A random number should be used to determine which one of the four possible directions each DiagonalBug will face. The act method should be overridden to a make DiagonalBug turn 180 degrees when it cannot move

Do the following Exercises on BPJ Lesson 45 1-5, 7-8, 10-12 Homework II