File - AP Computer Science

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; } }
public class DancingBug extends Bug{
private int [] turnArray;
private int turns;
private int moves;
public DancingBug(int[] a){
turnArray = a;
turns = 0;
moves = 0;
}
public void act(){
if (moves == turnArray.length){
moves = 0;}
if (turns < turnArray[moves]){
turn();
turns++; }
else if (canMove()==false){
turn();
}
else{
move();
moves++;
turns = 0;}
}}
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);
• Circle cir2 = 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())
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.*;
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