0% found this document useful (0 votes)
4 views

Week 5

This a tells Java programming basic oop concepts

Uploaded by

golatosami09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 5

This a tells Java programming basic oop concepts

Uploaded by

golatosami09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

LAB HANDOUTS WEEK.

5 / Lab 7

Object Oriented Programming (SE24 – 325 )

Course Instructor Mirhazar Khan

Class and Object, Defining Class, Instance Variables, Declaring Objects, Assigning Object Reference Variables,
Topics Covered Methods and Calling Methods, Constructor, The this Keyword, Math Class and its Methods

This is how you an create Students Object from class Student into Main class.

class Student { // Class declaration with the class name 'Student'


String name; // Instance variable of type String to hold the student's name
int age; // Instance variable of type int to hold the student's age

void studentinfo() { // Method declaration with the method name 'studentinfo'


System.out.println("Hi, I'm a x,y,z Student"); // Print statement to display student info
In Java, a class defines the structure (data) }
and actions (methods) for creating objects. }
Think of it as a blueprint—while the object is
Class and Object an actual instance built from that blueprint. Example of a Simple Class:
Each object of a class has its own set of public class Main {
data (instance variables) but shares the same public static void main(String[] args) {
behavior (methods) defined in the class. Student student1 = new Student(); // Creating an object of the Student class
student1.name = "Alice"; // Setting the name of student1
student1.age = 20; // Setting the age of student1
student1.studentinfo(); // Calling the studentinfo method on student1
}
}

Class names are nouns and start with a


capital letter. Inside, you define data (
public class ClassName {
instance variables) and actions (methods)
// Instance variables
that objects created from this class can
// Methods
Defining a Class Defining a Class
perform.
}

Instance variables store data specific to each


class Car {
object created from a class. Each object gets
String model;
Instance Variables its own copy of instance variables, which Example:
int year;
means changing one object’s data doesn’t
}
affect another’s.

This command allocates memory for myCar To create an object in Java, declare it with
and initializes it based on the Car class.
Car myCar = new Car();
the class name and use the new keyword:
Declaring Objects

When you assign one object reference to Car car1 = new Car();
Assigning Object Reference Variables another, both references point to the same Car car2 = car1; // car2 now references the
object in memory. same object as car1

class Circle {
double radius; public returnType methodName(parameter
Methods define the actions that objects of a
list) {
class can perform. In a class, methods are
double calculateArea() { // Method body
usually named with verbs to represent
Methods
return Math.PI * radius * radius; }
actions.
}
}

Circle circle = new Circle();


Once an object is created, you can call its
Calling Methods methods using dot notation:
circle.radius = 5.0;
System.out.println("Area: " + circle.calculateArea());

class Person {
String name;

When you create an object with new, the // Constructor A constructor initializes new objects of a
constructor is called to initialize the object’s Person(String name) { Example of a Constructor: class. The constructor has the same name as Constructor
data. this.name = name; the class and no return type.
}
}

class Student {
String name;

Student(String name) {
The this Keyword Example:
this.name = name; // 'this.name' refers
to the instance variable
}
}

public class MathExample {


public static void main(String[] args) {
System.out.println("PI: " + Math.PI);
The Math class provides constants and
System.out.println("Square root of
methods for mathematical operations like
16: " + Math.sqrt(16)); Example of Math Class Usage:
finding square roots, power values, and
Math Class
System.out.println("2 to the power of
trigonometric functions.
3: " + Math.pow(2, 3));
}
}

Example of a Calculator demonstrating Class, Object, Method & Method Calling

class SimpleCalculator { // Class declaration with the class name 'SimpleCalculator'

// Method to add two numbers


int add(int num1, int num2) {
return num1 + num2; // Return the sum of num1 and num2
}

// Method to subtract one number from another


int subtract(int num1, int num2) {
return num1 - num2; // Return the difference of num1 and num2
}

// Method to multiply two numbers


int multiply(int num1, int num2) {
return num1 * num2; // Return the product of num1 and num2
}

// Method to divide one number by another


double divide(double num1, double num2) {
if (num2 != 0) { // Check if the divisor is not zero
return num1 / num2; // Return the result of the division
Program Example } else {
System.out.println("Error: Division by zero is not allowed."); // Print an error message
return 0; // Return 0 to indicate an invalid result
}
}
}

// Example usage
public class CalculatorDemo {
public static void main(String[] args) {
SimpleCalculator calculator = new SimpleCalculator(); // Create a new SimpleCalculator object

// Perform calculations and print the results


System.out.println("Addition: " + calculator.add(5, 3)); // Add 5 and 3
System.out.println("Subtraction: " + calculator.subtract(10, 4)); // Subtract 4 from 10
System.out.println("Multiplication: " + calculator.multiply(7, 6)); // Multiply 7 and 6
System.out.println("Division: " + calculator.divide(20, 4)); // Divide 20 by 4
System.out.println("Division by zero: " + calculator.divide(5, 0)); // Attempt to divide by zero
}
}

You might also like