File - AP Computer Science
Download
Report
Transcript File - AP Computer Science
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:
http://www.java2s.com/Tutorial/Java/
0020__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()
public int getCol()
{
{
return row;
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.