Refresher Session - 2021B
Refresher Session - 2021B
Please watch our tutorial on how to use our bot in best way.
https://www.linktr.ee/reccobot
Recco Bot
Filename:1_JAVA Refresher
Please watch our tutorial on how to use our bot in best way.
https://www.linktr.ee/reccobot
Recco Bot
Tutorial-1: Crash Course on Java
Slide Credits: Internet / Chetan Arora
Java Architecture
2
Java Architecture
3
Object Oriented Programming
• The data has the principal role
• Methods belong to the data, without the data, the method does
not have any meaning (Except static methods)
Real world
entities
Objects have states
Lyin
g
Red Broken
Happy
il
l Hooke
Objects have behavior
Hello, Nice to da da
I am meet you …
John
Grrrrrrrr Vroemm
Object Properties
• Identity
• State
• Behavior on
off
myLamp
Methods
Object
boundary
data
OOP Modelling: Objects and Classes
• Class – Category/Bluprint/Contract
• Properties/states data
methods
• Functionality/Services (examines/alters state)
color
speed attributes
power
drive
turn right operations
turn left
stop
Java Syntax
public class Car Car
{
// attribute declarations
private String color;
String color
private int speed; int speed
private int power; int power
// method declarations
public void drive() drive()
{ // …. turnRight()
} turnLeft()
public void turnRight() stop()
{ // ….
}
Class Pencil
Pencil Name
home()
up() methods
down()
write()
Declaring objects
• A class can be used to create objects
• Objects are the instances of that class
Car
String color
int speed
int power new
drive()
turnRight()
turnLeft()
stop()
Defining and Calling Methods on Objects
• Calling methods
• Declaring and defining a class
• Instances of a class
• Defining methods
• The this keyword
Meet samBot
0 1 2 3 4
• Goal: move samBot from his
starting position to his destination 0
by giving him a list of instructions
• samBot only knows instructions 1
“move forward n steps” and “turn
right” 2
• “Turn right.” 0
• “Move forward 1 step.”
1
• “Turn right.”
• “Move forward 3 steps.” 2
“Calling Methods”: Sending Messages in Java
0 1 2 3 4
2
Putting Code Fragment in a Real Program
•public class RobotMover {
Note: public and class are Java “reserved words” aka “keywords” and have
pre-defined meanings in Java; we’ll be using Java keywords a lot in the future
Declaring and Defining a Class (2/3)
• Class definition (aka “body”)
defines properties and capabilities of •public class RobotMover {
• Each class goes in its own file, where name of file matches name of class
}
}
The this keyword (1/2)
public class Robot {
• Now that the Robot class has public void moveForward(int numberOfSteps) {
the method turnAround, we // code that moves robot forward
}
can call the method on any
Robot public void turnLeft() {
this.turnRight();
• There are other ways of this.turnRight();
implementing this method that this.turnRight();
•}
are just as correct
public void turnAround() {
this.turnRight();
this.turnRight();
}
}
public class Robot {
turnAround public void turnRight() {
// code that turns robot right
}
• Instead of calling turnRight,
could call our newly created public void moveForward(int numberOfSteps) {
method, turnLeft // code that moves robot forward
}
• Both of these solutions are
equally correct, in that they will public void turnLeft() {
turn the robot around 180o this.turnRight();
this.turnRight();
• How do they differ? When we this.turnRight();
try each of these }
implementations with samBot,
what will we see in each case? public void turnAround() {
this.turnLeft();
this.turnLeft();
}
}
Using Classes and Objects
Where did all these instances come from?
• We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods
count = title.length()
• Note that a primitive variable contains the value itself, but an object variable contains the address of the object
• An object reference can be thought of as a pointer to the location of the object
• Rather than dealing with arbitrary addresses, we often depict a reference graphically
num1 38
num1 38
Before:
num2 96
num2 = num1;
num1 38
After:
num2 38
Reference Assignment
name2 = name1;
6-64
The static Modifier
6-65
Static Variables
• Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists
public static float price;
• Memory space for a static variable is created when the class is first referenced
• All objects instantiated from the class share its static variables
• Changing the value of a static variable in one object changes it for all others
6-66
Static Methods
class Helper
{
`` private int hello=5;
public static int cube (int num)
{
Sys.out.println(this.hello)
;
return num * num * num;
}
} Because it is declared as static, the method
can be invoked as
value = Helper.cube(5);
6-67
Static Class Members
• The order of the modifiers can be interchanged, but by convention visibility modifiers come first
• Recall that the main method is static – it is invoked by the Java interpreter without creating an object
• Static methods cannot reference instance variables because instance variables don't exist until an object exists
• However, a static method can reference static variables or local variables
6-68
Static Class Members
6-69
class MyClass {
private static int count = 0;
public MyClass () {
count++;
}
public static int getCount () {
return count;
}
}
-------------------------
MyClass obj;
obj1.tryMe();
obj2.tryMe();
6-71
The this reference
• The this reference can be used to distinguish the instance variables of a class from corresponding method parameters
with the same names
• The constructor of the Account class (from Chapter 4) could have been written as follows:
6-72
References
• Rather than dealing with arbitrary addresses, we often depict a reference graphically as a “pointer” to an object
bishop1
6-73
References
• An object reference variable that does not currently point to an object is called a null reference
• The reserved word null can be used to explicitly set a null reference:
name = null;
if (name == null)
System.out.println ("Invalid");
6-75
The null Reference
• An object reference variable declared at the class level (an instance variable) is automatically initialized to null
• The programmer must carefully ensure that an object reference variable refers to a valid object before it is used
• Attempting to follow a null reference causes a NullPointerException to be thrown
• Usually a compiler will check to see if a local variable is being used without being initialized
6-76
Method Overloading
• Method overloading is the process of giving a single method name multiple definitions
• If a method is overloaded, the method name is not sufficient to determine which method is being called
• The signature of each overloaded method must be unique
• The signature includes the number, type, and order of the parameters
6-77
Method Overloading
• The compiler determines which method is being invoked by analyzing the parameters
float tryMe(int x)
{ Invocation
return x + .375;
result = tryMe(25, 4.32)
}
6-78
Method Overloading
println (String s)
println (int i)
println (double d)
and so on...
6-79
Overloading Methods
6-80
Overloading Methods
6-81