Java Module 2 Chapter 7
Java Module 2 Chapter 7
Write a Java program to develop the stack data structure. Use an array of ten integers. Ten is the
maximum size of the stack. Design the following methods with suitable parameters: push(), pop(),
peek(), and display().
• push() – inserts an integer at the top of the stack, without altering any other elements of the
existing stack only if there is room in the stack. Check if room exists before pushing.
• Pop() - pulls an integer out from the top of the stack and displays the same.
• Peek() - returns the integer from the top of the stack without removing it from the stack itself.
• Display() displays all integers in the stack, in the order they were inserted. This can be used
whenever you want to confirm the elements that are present in the stack
Use the correct access modifiers for all data members and methods. Write another class to test the
stack thoroughly.
Closer Look @ Methods & Classes
● Methods can be overloaded
● One of the ways Java supports polymorphism.
class Overload {
void test() {
System.out.println("No parameters");
}
void test(int a) {
System.out.println("a: " + a);
}
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
public class OverloadTest {
public static void main(String args[]) {
Overload overload = new Overload();
double result;
overload.test();
overload.test(10);
overload.test(10, 20);
result = overload.test(123.25);
System.out.println("Result of overload.test(123.25): " +
result);
}
}
Java’s automatic type conversions can play a role in overload resolution
class Overload {
void test() {
System.out.println("No parameters");
}
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
void test(double a) {
System.out.println("Inside test(double) a: " + a);
}
}
class OverloadTest { // which exact method gets called?
public static void main(String args[]) {
Overload overload = new Overload();
int i = 88;
overload.test();
overload.test(10, 20);
overload.test(i);
overload.test(123.2);
}
}
Overloading Constructors
class Box {
double width;
double height;
double depth;
Box(double length) {
width = height = depth = length;
}
double volume () {
return width * height * depth;
}
}
public class BoxTest {
public static void main(String args[]) {
Box box1 = new Box(10, 20, 15);
Box box2 = new Box();
Box box3 = new Box(7);
double volume = box1.volume();
System.out.println("Volume of box1 is " + volume);
volume = box2.volume();
System.out.println("Volume of box2 is " + volume);
volume = box3.volume();
System.out.println("Volume of cube is " + volume);
}
}
// Objects may be passed to methods.
public class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
boolean equals(Test o) {
if (o.a == a && o.b == b) return true;
else return false;
}
}
class PassingObjects {
public static void main(String args[]) {
Test object1 = new Test(100, 22);
Test object2 = new Test(100, 22);
Test object3 = new Test(-1, -1);
class Test {
i *= 2;
j /= 2;
class CallByValue {
test.method(a, b);
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void method(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByReference {
public static void main(String args[]) {
Test test = new Test(15, 20);
System.out.println(“test.a and test.b before call: " +
test.a + " " + test.b);
test.method(test);
System.out.println(“test.a and test.b after call: " +
test.a + " " + test.b);
}
}
Returning Objects
class Test {
int a;
Test(int i) {
a = i;
}
Test incrementByTen() {
Test temp = new Test(a+10);
return temp;
}
}
class ReturnObject {
public static void main(String args[]) {
Test object1 = new Test(2);
Test object2;
object2 = object1.incrementByTen();
System.out.println("object1.a: " + object1.a);
System.out.println("object2.a: " + object2.a);
object2 = object2.incrementByTen();
System.out.println(“object2.a after second increase: “ + object2.a);
}
}
Recursion
process of defining something in terms of itself.
class Length {