KEMBAR78
Java Basics - Part2 | PDF
19Z305OBJECT
ORIENTED
PROGRAMMING
Unit 2
Overview
PART
-
1
• Characteristics
of Java
• Data types,
Variables,
Operators
• Control
Statements,
Arrays
PART
-
2
• Classes
• Methods,
Constructors
• Inheritance
• Abstract class
12/11/2020 Vani Kandhasamy,PSG Tech 2
Methods
 Method definition:
TYPE NAME (PARAMETER LIST) {
STATEMENTS
return VALUE;
}
 Method call:
NAME (ARGUMENT LIST);
12/11/2020 Vani Kandhasamy,PSG Tech 3
Output:
4
9
16
12/11/2020 Vani Kandhasamy,PSG Tech 4
12/11/2020 Vani Kandhasamy,PSG Tech 5
Classes
Part 1Chapter 6
12/11/2020 Vani Kandhasamy,PSG Tech 6
Class
Class <class name> {
}
Data Members
Methods
How to represent Box as Class?
12/11/2020 Vani Kandhasamy,PSG Tech 7
Class
Class Box {
}
Class <class name> {
}
Data Members
Methods
double width;
double length;
double height;
double volume() {
return width*length*height;
}
Box as Class
12/11/2020 Vani Kandhasamy,PSG Tech 8
Class Box {
}
Box mybox1; // declare reference to object
mybox1 = new Box(); // allocate a Box object
(or)
Box mybox2 = new Box();
double width;
double length;
double height;
double volume() {
return width*length*height;
}
Objects
12/11/2020 Vani Kandhasamy,PSG Tech 9
Statement Effect
Box mybox;
mybox
mybox = new Box()
mybox width
height
length
12/11/2020 Vani Kandhasamy,PSG Tech 10
Statement Effect
Box mybox1 = new Box();
Box mybox2 = mybox1;
mybox2
width
height
length
mybox1
width
height
length
mybox1
12/11/2020 Vani Kandhasamy,PSG Tech 11
Class Box {
}
class BoxDemo1 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.length = 15;
// assign values to mybox2's instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.length = 9;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
double width;
double length;
double height;
double volume() {
return width*length*height;
}
Objects
12/11/2020 Vani Kandhasamy,PSG Tech 12
Statement Effect
mybox1.width = 10;
mybox1.height = 20;
mybox1.length = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.length = 9;
mybox2
width = 3
height = 6
length = 9
mybox1
width =10
height =20
length = 15
12/11/2020 Vani Kandhasamy,PSG Tech 13
class BoxDemo1 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.length = 15;
// assign values to mybox2's instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.length = 9;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
12/11/2020 Vani Kandhasamy,PSG Tech 14
Methods &
Constructors
Part 1Chapter 6 & 7
12/11/2020 Vani Kandhasamy,PSG Tech 15
Practice7
Open repl.it
Update Box() class – add methods
for initializing member/instance
variables
12/11/2020 Vani Kandhasamy,PSG Tech 16
class Box {
// sets dimensions of box
void setDim(double w, double h, double l) {
width = w;
height = h;
length = l;
}
}
class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// initialize each box
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
}
}
Methods -
a fix for the
encapsulation violation
12/11/2020 Vani Kandhasamy,PSG Tech 17
class Box {
// sets dimensions of box
void setDim(double w, double h, double l) {
width = w;
height = h;
length = l;
}
}
class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// initialize each box
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
}
}
Automatic
Initialization:
Constructor
12/11/2020 Vani Kandhasamy,PSG Tech 18
Constructor
12/11/2020 Vani Kandhasamy,PSG Tech 19
class Box {
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 20;
length = 15;
}
// This is the constructor for Box.
Box(double w, double h, double l) {
width = w;
height = h;
length = l;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box(3,6,9);
}
}
DefaultConstructor
Parameterized Constructor
12/11/2020 Vani Kandhasamy,PSG Tech 20
class Box {
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 20;
length = 15;
}
// This is the constructor for Box.
Box(double w, double h, double l) {
width = w;
height = h;
length = l;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box(3,6,9);
}
}
12/11/2020 Vani Kandhasamy,PSG Tech 21
class Box {
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 20;
length = 15;
}
// This is the constructor for Box.
Box(double width, double height, double length) {
width = width;
height = height;
length = length;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box(3,6,9);
}
}
Output:
3000
0
12/11/2020 Vani Kandhasamy,PSG Tech 22
class Box {
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 20;
length = 15;
}
// This is the constructor for Box.
Box(double width, double height, double length) {
this.width = width;
this.height = height;
this.length = length;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box(3,6,9);
}
}
Keyword: this
12/11/2020 Vani Kandhasamy,PSG Tech 23
Statement Effect
Box mybox1 = new Box(10,15,20);
Box mybox2 =new Box(10,15,20); mybox2
width=10
height=15
length=20
mybox1
width=10
height=15
length=20
12/11/2020 Vani Kandhasamy,PSG Tech 24
Statement Effect
Box mybox1 = new Box(10,15,20);
Mybox1.width = 5 mybox1
width=5
height=15
length=20
mybox1
width=10
height=15
length=20
12/11/2020 Vani Kandhasamy,PSG Tech 25
Statement Effect
Box mybox1 = new Box(10,15,20);
Mybox1.width = 5 mybox1
width=5
height=15
length=20
mybox1
width=10
height=15
length=20
Its still possible to
access Data
members outside
the class –
violating OOP
12/11/2020 Vani Kandhasamy,PSG Tech 26
Access
Specifiers
12/11/2020 Vani Kandhasamy,PSG Tech 27
class Box {
// This is the constructor for Box.
private double width;
private double height;
private double length;
// This is the constructor for Box.
Box(double width, double height, double length) {
this.width = width;
this.height = height;
this.length = length;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox2 = new Box(3,6,9);
mybox2.width = 10; // ERROR!!!
}
}
Access Specifiers -
a fix for the abstraction
violation
12/11/2020 Vani Kandhasamy,PSG Tech 28
Static
members
public class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
b = a * 4;
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
public static void main(String args[]) {
meth(42);
}
}
a, b - StaticVariables
meth - Static Method
12/11/2020 Vani Kandhasamy,PSG Tech 29
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
Class name is used to
access static methods
12/11/2020 Vani Kandhasamy,PSG Tech 30
Method
Overloading
 Polymorphism
 Same method name but type and/or number of their
parameters differs
 Different return types alone insufficient to overload
12/11/2020 Vani Kandhasamy,PSG Tech 31
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
12/11/2020 Vani Kandhasamy,PSG Tech 32
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
int a = 88;
// call all versions of test()
ob.test();
ob.test(a);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
12/11/2020 Vani Kandhasamy,PSG Tech 33
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
int a = 88;
// call all versions of test()
ob.test();
ob.test(a);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
12/11/2020 Vani Kandhasamy,PSG Tech 34
Constructor
Overloading
class Box {
// This is the constructor for cube.
Box(double side) {
width = height = length = side;
}
// This constructor is used when all dimensions specified
Box(double w, double h, double l) {
width = w;
height = h;
length = l;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mycube1 = new Box(4);
Box mybox2 = new Box(3,6,9);
}
}
Differs only by
#parameters
12/11/2020 Vani Kandhasamy,PSG Tech 35
Argument
Passing
Address of
argument
Value of
argument
12/11/2020 Vani Kandhasamy,PSG Tech 36
class Test {
void meth(int a, int b) {
a *= 2;
b /= 2;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.println("a and b before call: " +
a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " +
a + " " + b);
}
}
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}
12/11/2020 Vani Kandhasamy,PSG Tech 37
Constructor +
Object
class Box {
// This is the constructor for cube.
Box(double side) {
width = height = length = side;
}
// This constructor is used when all dimensions specified
Box(Box ob) {
width = ob.width;
height = ob.height;
length = ob.length;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mycube1 = new Box(4);
Box mycube2 = new Box(mycube1);
}
}
12/11/2020 Vani Kandhasamy,PSG Tech 38
Returning
Objects
class Test {
int a;
Test(int i) {
a = i;
}
Test incrByTen() {
Test temp = new Test(a+10);
return temp;
}
}
class RetOb {
public static void main(String args[]) {
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: "
+ ob2.a);
}
}
Returning Objects
12/11/2020 Vani Kandhasamy,PSG Tech 39
Practice9
Read the problem sheet2 provided
in the class room
Open repl.it
Complete the code stub
IntegerSetTest.java
12/11/2020 Vani Kandhasamy,PSG Tech 40
References
 "Java:The Complete Reference” by Schildt H - Part 1 - Chapter 6 & 7
 https://www.javatpoint.com/java-tutorial
12/11/2020 Vani Kandhasamy,PSG Tech 41

Java Basics - Part2

  • 1.
  • 2.
    Overview PART - 1 • Characteristics of Java •Data types, Variables, Operators • Control Statements, Arrays PART - 2 • Classes • Methods, Constructors • Inheritance • Abstract class 12/11/2020 Vani Kandhasamy,PSG Tech 2
  • 3.
    Methods  Method definition: TYPENAME (PARAMETER LIST) { STATEMENTS return VALUE; }  Method call: NAME (ARGUMENT LIST); 12/11/2020 Vani Kandhasamy,PSG Tech 3
  • 4.
  • 5.
  • 6.
    Classes Part 1Chapter 6 12/11/2020Vani Kandhasamy,PSG Tech 6
  • 7.
    Class Class <class name>{ } Data Members Methods How to represent Box as Class? 12/11/2020 Vani Kandhasamy,PSG Tech 7
  • 8.
    Class Class Box { } Class<class name> { } Data Members Methods double width; double length; double height; double volume() { return width*length*height; } Box as Class 12/11/2020 Vani Kandhasamy,PSG Tech 8
  • 9.
    Class Box { } Boxmybox1; // declare reference to object mybox1 = new Box(); // allocate a Box object (or) Box mybox2 = new Box(); double width; double length; double height; double volume() { return width*length*height; } Objects 12/11/2020 Vani Kandhasamy,PSG Tech 9
  • 10.
    Statement Effect Box mybox; mybox mybox= new Box() mybox width height length 12/11/2020 Vani Kandhasamy,PSG Tech 10
  • 11.
    Statement Effect Box mybox1= new Box(); Box mybox2 = mybox1; mybox2 width height length mybox1 width height length mybox1 12/11/2020 Vani Kandhasamy,PSG Tech 11
  • 12.
    Class Box { } classBoxDemo1 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.length = 15; // assign values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.length = 9; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } double width; double length; double height; double volume() { return width*length*height; } Objects 12/11/2020 Vani Kandhasamy,PSG Tech 12
  • 13.
    Statement Effect mybox1.width =10; mybox1.height = 20; mybox1.length = 15; mybox2.width = 3; mybox2.height = 6; mybox2.length = 9; mybox2 width = 3 height = 6 length = 9 mybox1 width =10 height =20 length = 15 12/11/2020 Vani Kandhasamy,PSG Tech 13
  • 14.
    class BoxDemo1 { publicstatic void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.length = 15; // assign values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.length = 9; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } 12/11/2020 Vani Kandhasamy,PSG Tech 14
  • 15.
    Methods & Constructors Part 1Chapter6 & 7 12/11/2020 Vani Kandhasamy,PSG Tech 15
  • 16.
    Practice7 Open repl.it Update Box()class – add methods for initializing member/instance variables 12/11/2020 Vani Kandhasamy,PSG Tech 16
  • 17.
    class Box { //sets dimensions of box void setDim(double w, double h, double l) { width = w; height = h; length = l; } } class BoxDemo2 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); // initialize each box mybox1.setDim(10, 20, 15); mybox2.setDim(3, 6, 9); } } Methods - a fix for the encapsulation violation 12/11/2020 Vani Kandhasamy,PSG Tech 17
  • 18.
    class Box { //sets dimensions of box void setDim(double w, double h, double l) { width = w; height = h; length = l; } } class BoxDemo2 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); // initialize each box mybox1.setDim(10, 20, 15); mybox2.setDim(3, 6, 9); } } Automatic Initialization: Constructor 12/11/2020 Vani Kandhasamy,PSG Tech 18
  • 19.
  • 20.
    class Box { //This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 20; length = 15; } // This is the constructor for Box. Box(double w, double h, double l) { width = w; height = h; length = l; } } class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(3,6,9); } } DefaultConstructor Parameterized Constructor 12/11/2020 Vani Kandhasamy,PSG Tech 20
  • 21.
    class Box { //This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 20; length = 15; } // This is the constructor for Box. Box(double w, double h, double l) { width = w; height = h; length = l; } } class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(3,6,9); } } 12/11/2020 Vani Kandhasamy,PSG Tech 21
  • 22.
    class Box { //This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 20; length = 15; } // This is the constructor for Box. Box(double width, double height, double length) { width = width; height = height; length = length; } } class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(3,6,9); } } Output: 3000 0 12/11/2020 Vani Kandhasamy,PSG Tech 22
  • 23.
    class Box { //This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 20; length = 15; } // This is the constructor for Box. Box(double width, double height, double length) { this.width = width; this.height = height; this.length = length; } } class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(3,6,9); } } Keyword: this 12/11/2020 Vani Kandhasamy,PSG Tech 23
  • 24.
    Statement Effect Box mybox1= new Box(10,15,20); Box mybox2 =new Box(10,15,20); mybox2 width=10 height=15 length=20 mybox1 width=10 height=15 length=20 12/11/2020 Vani Kandhasamy,PSG Tech 24
  • 25.
    Statement Effect Box mybox1= new Box(10,15,20); Mybox1.width = 5 mybox1 width=5 height=15 length=20 mybox1 width=10 height=15 length=20 12/11/2020 Vani Kandhasamy,PSG Tech 25
  • 26.
    Statement Effect Box mybox1= new Box(10,15,20); Mybox1.width = 5 mybox1 width=5 height=15 length=20 mybox1 width=10 height=15 length=20 Its still possible to access Data members outside the class – violating OOP 12/11/2020 Vani Kandhasamy,PSG Tech 26
  • 27.
  • 28.
    class Box { //This is the constructor for Box. private double width; private double height; private double length; // This is the constructor for Box. Box(double width, double height, double length) { this.width = width; this.height = height; this.length = length; } } class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox2 = new Box(3,6,9); mybox2.width = 10; // ERROR!!! } } Access Specifiers - a fix for the abstraction violation 12/11/2020 Vani Kandhasamy,PSG Tech 28
  • 29.
    Static members public class UseStatic{ static int a = 3; static int b; static void meth(int x) { b = a * 4; System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } public static void main(String args[]) { meth(42); } } a, b - StaticVariables meth - Static Method 12/11/2020 Vani Kandhasamy,PSG Tech 29
  • 30.
    class StaticDemo { staticint a = 42; static int b = 99; static void callme() { System.out.println("a = " + a); } } class StaticByName { public static void main(String args[]) { StaticDemo.callme(); System.out.println("b = " + StaticDemo.b); } } Class name is used to access static methods 12/11/2020 Vani Kandhasamy,PSG Tech 30
  • 31.
    Method Overloading  Polymorphism  Samemethod name but type and/or number of their parameters differs  Different return types alone insufficient to overload 12/11/2020 Vani Kandhasamy,PSG Tech 31
  • 32.
    class OverloadDemo { voidtest() { System.out.println("No parameters"); } // Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a; } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; // call all versions of test() ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.25); System.out.println("Result of ob.test(123.25): " + result); } } 12/11/2020 Vani Kandhasamy,PSG Tech 32
  • 33.
    class OverloadDemo { voidtest() { System.out.println("No parameters"); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a; } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; int a = 88; // call all versions of test() ob.test(); ob.test(a); ob.test(10, 20); result = ob.test(123.25); System.out.println("Result of ob.test(123.25): " + result); } } 12/11/2020 Vani Kandhasamy,PSG Tech 33
  • 34.
    class OverloadDemo { voidtest() { System.out.println("No parameters"); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a; } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; int a = 88; // call all versions of test() ob.test(); ob.test(a); ob.test(10, 20); result = ob.test(123.25); System.out.println("Result of ob.test(123.25): " + result); } } 12/11/2020 Vani Kandhasamy,PSG Tech 34
  • 35.
    Constructor Overloading class Box { //This is the constructor for cube. Box(double side) { width = height = length = side; } // This constructor is used when all dimensions specified Box(double w, double h, double l) { width = w; height = h; length = l; } } class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mycube1 = new Box(4); Box mybox2 = new Box(3,6,9); } } Differs only by #parameters 12/11/2020 Vani Kandhasamy,PSG Tech 35
  • 36.
  • 37.
    class Test { voidmeth(int a, int b) { a *= 2; b /= 2; } } class CallByValue { public static void main(String args[]) { Test ob = new Test(); int a = 15, b = 20; System.out.println("a and b before call: " + a + " " + b); ob.meth(a, b); System.out.println("a and b after call: " + a + " " + b); } } class Test { int a, b; Test(int i, int j) { a = i; b = j; } // pass an object void meth(Test o) { o.a *= 2; o.b /= 2; } } class CallByRef { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b); ob.meth(ob); System.out.println("ob.a and ob.b after call: " + ob.a + " " + ob.b); } } 12/11/2020 Vani Kandhasamy,PSG Tech 37
  • 38.
    Constructor + Object class Box{ // This is the constructor for cube. Box(double side) { width = height = length = side; } // This constructor is used when all dimensions specified Box(Box ob) { width = ob.width; height = ob.height; length = ob.length; } } class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mycube1 = new Box(4); Box mycube2 = new Box(mycube1); } } 12/11/2020 Vani Kandhasamy,PSG Tech 38
  • 39.
    Returning Objects class Test { inta; Test(int i) { a = i; } Test incrByTen() { Test temp = new Test(a+10); return temp; } } class RetOb { public static void main(String args[]) { Test ob1 = new Test(2); Test ob2; ob2 = ob1.incrByTen(); System.out.println("ob1.a: " + ob1.a); System.out.println("ob2.a: " + ob2.a); ob2 = ob2.incrByTen(); System.out.println("ob2.a after second increase: " + ob2.a); } } Returning Objects 12/11/2020 Vani Kandhasamy,PSG Tech 39
  • 40.
    Practice9 Read the problemsheet2 provided in the class room Open repl.it Complete the code stub IntegerSetTest.java 12/11/2020 Vani Kandhasamy,PSG Tech 40
  • 41.
    References  "Java:The CompleteReference” by Schildt H - Part 1 - Chapter 6 & 7  https://www.javatpoint.com/java-tutorial 12/11/2020 Vani Kandhasamy,PSG Tech 41