Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Dancing bug class Class vs object Comparable interface

Similar presentations


Presentation on theme: "Agenda Dancing bug class Class vs object Comparable interface"— Presentation transcript:

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

2 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

3 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

4 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

5 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);

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

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

8 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 }

9 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?

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

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

12 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;

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

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

15 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

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


Download ppt "Agenda Dancing bug class Class vs object Comparable interface"

Similar presentations


Ads by Google