Download presentation
Presentation is loading. Please wait.
1
JAVA An Introduction to Java OOP Basics
Mudassir Hasan
2
Data Types & Operators Data Types: byte, short, int, long, float, double, long , boolean, char Operators: - Arithmetic: +,-,*,/, % ( modulus-remainder ) - Relational: <, >, <=, >=, ==, != - Logical: &&, ||, !
3
Strings public class LearnJava
{ public static void main( String [] args ) { String fun = “Java is fun”; System.out.println(fun); } }
4
Strings (cont’d.) Date columbusDay = new Date(10, 12, 2009);
System.out.println(“Columbus day was on “ + columbusDay.getDay() ); Columbus day was on 12 Stores values for columbusDay. int age = 20; // String Concatenation String youth = "He is " + age + " years old."; System.out.println(youth); He is 20 years old.
5
Modifiers Access Modifiers:
Public – access methods of the same class and of other classes Private – access methods of the same class only Protected – access methods in the same class and also in subclasses
6
Constructors Initializes the fields of the new object
It is called when an object is instantiated using the new keyword For example: Auto suv = new Auto( );
7
Constructors Default Constructor public class Auto {
private int drivenMiles; private int modelYear; public Auto ( ) // same name as the class // no parameters passed, auto initialized drivenMiles to 0. modelYear = 2009; }
8
Constructors (cont’d.)
Overloaded Constructors public Auto(int adriven Miles, int amodelYear ) // passes parameter, allows user to set their own values
9
Class Methods Accessor Methods, also called get Methods Get radius
public int getRadius( ) { return radius; }
10
Class Methods Mutator Methods, also called set Methods
Allows the user to set its own values in the client class public Class public void setRadius( int newRadius ) { radius = newRadius; } Client Class Circle circ = new Circle( 4 );
11
Arrays public class ArrayElements {
public static void main(String [] args) double cells = new double [3] cells[0] = 1; cells[1] = 2; cells[2] = 3; } First index is 0. Last index is [array.length – 1]
12
Swapping Array int temp = array[2]; array[2] = array[4];
array[4] = temp; Value 23 45 7 33 78 Index 1 2 3 4 5
13
Class public class Circle { private int point; private int radius; public Circle( int newPoint, int newRadius ) { point = newPoint; radius = newRadius; } public int getLocation() { return point; } public void setLocation( int newPoint) { point = newPoint; }
14
Class (cont’d.) public int getRadius() { return radius; } public void setRadius( int newRadius ) { radius = newRadius; } public double getPerimeter() { return 2 * Math.PI * radius; } public double getArea() { return Math.PI * radius * radius; } public String toString() { return "The point is " + point "\nThe radius is " + radius "\nPerimeter of the circle is " + getPerimeter() "\nArea of the circle is " + getArea();
15
Client Class public class ClientCircle { public static void main( String [] args ) { Circle circ = new Circle( 4, 5 ); System.out.println( circ.toString() ); } } The point is 4 The radius is 5 Perimeter of the circle is Area of the circle is
16
Bibliography Anderson, Julie and Franceschi, Herve. Java Illuminated.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.