File - AP Computer Science

January 8, 2018 | Author: Anonymous | Category: Engineering & Technology, Computer Science, Java Programming
Share Embed Donate


Short Description

Download File - AP Computer Science...

Description

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 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.

View more...

Comments

Copyright � 2017 NANOPDF Inc.
SUPPORT NANOPDF