Lecture05 Annotated
Lecture05 Annotated
}
Scope of a variable: Instance
variables have class scope
• The instance variables of a class have class
scope. Class scope begins at the opening
left brace and ends at the closing right brace
of the class definition.
• Class scope allows any method in the class
to directly access any instance variable.
• In effect, instance variables are "global
variables" within a class
Scope of a variable: Local
variables have block scope
• A block is a compound statement and
consists of all the statements between an
opening and closing brace.
• Local variables defined within a block have
block scope; not visible outside the block.
• A local variable can have the same name as
an instance variable. In this case the
instance variable is hidden from the method
by the local (hence the "this" keyword).
public class Point {
private double x,y; // instance variables
public Point() {
x = 1; // same variables as instance variables
y = 1;}
public Point (double x, double y){
this.x = x; // this.x refers to instance variable
this.y = y;
this.x++;
System.out.println("this.x = " + this.x);
System.out.println("x = " + x); // parameter variable
}
public Point (double x, double y, boolean dummy) {
x++;
System.out.println("x = " + x);
}
} Point p1= new Point();
Point p2= new Point(2,3);
Point p3=new Point(2,3,false);
public class A {
Find output
private A next;
private int v;
public A(A n, int i) {
v = i;
next = n;
}
public int getT() {
int t = v;
if (next != null) {
t = t + next.getT();
}
return t;
}
public static void main(String[] args) {
A a = new A(null, 3);
A b = new A(a, 1);
A c = new A(b, 2);
System.out.println(a.getT());
System.out.println(b.getT());
System.out.println(c.getT()); }}
public class A {
Given the following
private A next; program output,
private int v; Fill in the blanks
public A(A n, int i) {
v = i;
next = n;
}
public int getT() {
int t = v;
if (next != null) {
t = t + next.getT();
}
return t;
}
public static void main(String[] args) {
A a = new A(____, ____);
A ___ = new A(____, 1);
A ___ = new A(____, 2);
System.out.println(a.getT()); 5
System.out.println(b.getT()); 8
System.out.println(c.getT()); }} 6
Arrays as parameters
• Passing entire array = pass by reference =
same behavior as passing any object
void aProc() {
int[] xyz = new int[12];
theProc(xyz);
System.out.println(xyz[3]);
}
void theProc(int[] mno)
{
mno[3] = 15;
}
Arrays as parameters
• Passing individual array element= pass by
value= same behavior as passing a primitive
void aProc() {
int[] xyz = new int[12];
theProc(xyz[3]);
System.out.println(xyz[3]);
}
void theProc(int mno)
{
mno = 15;
}
public class ArrayTest { Given the following
public static void main(String[] args) {
int[] test = new int[2];
program output,
test[0] = 10; Fill in the blanks
test[1] = 5;
System.out.println(test[0] + "," + test[1]); 10, 5
fiddle(test, test[1]);
System.out.println(test[0] + "," + test[1]); 19, 15
}
static void fiddle(int[] test, int element) {
___ = 15;
___ = 19;
____ = 12;
System.out.println(___+ "," + ___ + ","
+ element); 15, 19,12
test = new int[2];
test[0] = ___;
test[1] = ___;
System.out.println(test[0] + "," + test[1]); 20, 21
}}
Two Dimensional (2D) Arrays
2D Arrays - initializing
int[][] A = { { 1, 0, 12, -1 },
{ 7, -3, 2, 5 },
{ -5, -2, 2, 9 }
};
or
int[][] A = new int[3][4];
A.length: the number of rows of A.
A[0].length: the number of columns in A
A[1].length : the number of columns in A
A[2].length : …..
Two Dimensional (2D) Arrays
public class TwoDArray
{
public static void main (String[] args)
{
int[][] table = new int[5][10];
for (int row=0; row < table.length; row++)
for (int col=0; col < table[row].length; col++)
table[row][col] = row * 10 + col;
}
Accessors (getters)
class Triangle {
//Instance variable
private double side1;
private double side2;
private double side3;
public double GetSide1() {
return side1;
}
public double GetSide2() {
return side2;
}
public double GetSide3() {
return side3;}
}
Mutator (setter)
class Triangle {
//Instance variable
private double side1;
private double side2;
private double side3;
public double GetSide1() {
return side1;
}
public double GetSide2() {
return side2;
}
public double GetSide3() {
return side3;}
public void setSides (double side1, double side2, double side3){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;}
}
getPerimeter()
class Triangle {
//Instance variable
private double side1;
private double side2;
private double side3;
public double GetSide1() {
return side1;
}
public double GetSide2() {
return side2;
}
public double GetSide3() {
return side3;}
public void setSides (double side1, double side2, double side3){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;}
public double getPerimeter(){
return side1+side2+side3;
}
}
TriangleTest
import java.util.Scanner
class TriangleTest{
public static void main (String[] args) {
double side1, side2, side3;
Scanner scan = new Scanner (System.in);
System.out.println(“Your Side1, Side2 and Side3:”);
side1= scan.nextDouble();
side2 = scan.nextDouble();
side3 = scan.nextDouble();
Triangle tri = new Triangle();
tri.setSides(side1, side2, side3);
System.out.println(tri.getPerimeter());
}
}