0% found this document useful (0 votes)
0 views5 pages

Oop Homework Compilation

This document is a comprehensive guide on Object-Oriented Programming in Java, covering key concepts such as print statements, primitive data types, static and non-static blocks, and object relationships like association, aggregation, and composition. It also explains command line arguments, regular expressions, the use of Object class as parameters, protected access modifiers, and the differences between deep and shallow copy constructors. The guide provides code examples for better understanding of each topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

Oop Homework Compilation

This document is a comprehensive guide on Object-Oriented Programming in Java, covering key concepts such as print statements, primitive data types, static and non-static blocks, and object relationships like association, aggregation, and composition. It also explains command line arguments, regular expressions, the use of Object class as parameters, protected access modifiers, and the differences between deep and shallow copy constructors. The guide provides code examples for better understanding of each topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Object-Oriented Programming Homework Compilation – Java

(Expanded Guide)

1. Print Statements in Java

a. System.out.print()

• Outputs text to the console without moving to a new line.


• Useful for inline output.

System.out.print("A");
System.out.print("B"); // Output: AB

b. System.out.println()

• Outputs text and moves the cursor to the next line.


• Default method for line-by-line output.

System.out.println("Hello");
System.out.println("World");

c. System.out.printf()

• Formatted printing (like in C).


• Useful for structured display.

System.out.printf("Name: %s, Age: %d", "Ali", 20);

2. Java Primitive Data Types

Type Size Range / Usage

byte 1 byte -128 to 127 – memory-efficient small integers

short 2 bytes –32,768 to 32,767 – less commonly used

int 4 bytes Most common – general integers

long 8 bytes Larger range – must use suffix L

float 4 bytes Decimal – less precise, use suffix f

double 8 bytes Decimal – default type for floating-point

char 2 bytes Stores a single character (Unicode)

boolean 1 bit true or false – used in conditions

1
3. Static and Non-static Blocks

a. Static Block

• Runs once when class is loaded.


• Used for static variable initialization.

static {
System.out.println("Static block");
}

b. Non-static (Instance) Block

• Runs every time an object is created.


• Executes before constructor.

{
System.out.println("Instance block");
}

4. Mutable Object Inside Immutable Class

If you place a mutable object like a List in an immutable class without copying it, it breaks immutability.

class Student {
private final List<String> subjects;

public Student(List<String> subjects) {


this.subjects = new ArrayList<>(subjects); // defensive copy
}

public List<String> getSubjects() {


return new ArrayList<>(subjects);
}
}

5. Association, Aggregation, Composition

a. Association

• Loose relationship between classes.


• Example: Doctor and Patient.

2
b. Aggregation (Has-A)

• Weak ownership.
• Objects can exist independently.

class Department {}
class University {
Department dept; // Aggregation
}

c. Composition

• Strong ownership.
• Lifespan of part depends on whole.

class Engine {}
class Car {
private final Engine engine = new Engine();
}

Relationship Ownership Lifespan tied Example

Association No No Teacher–Class

Aggregation Weak No Dept–Univ

Composition Strong Yes Car–Engine

6. Command Line Arguments

• Command line arguments allow users to pass data to the main() method at program start.
• main(String[] args) receives these arguments.

public class Example {


public static void main(String[] args) {
System.out.println("Hello " + args[0]);
}
}

Run in terminal:

java Example Ali


// Output: Hello Ali

• args.length tells how many arguments were passed.


• Useful for configurations, file paths, etc.

3
7. Regex (Regular Expression)

• A regex is a pattern for matching character combinations.


• Found in java.util.regex.Pattern and Matcher .

a. Basic Syntax:

Symbol Meaning Example

. Any character a.c matches abc , a@c

* 0 or more a* matches "", "a", "aaa"

+ 1 or more a+ matches "a", "aaa"

\d Digit [0-9] \d+ matches 123

\w Word character \w+ matches abc123

[] Character set [abc] matches a or b or c

b. Java Usage:

String input = "abc123";


boolean matches = input.matches("[a-z]+\\d+"); // true

8. Can You Pass Object Class as Parameter?

Yes, you can. Every class in Java extends Object (directly or indirectly), so any class instance can be
passed where Object is expected.

public void printObject(Object obj) {


System.out.println(obj.toString());
}

However:

• Not type-safe: You lose compile-time checking.


• Requires casting to specific type inside method.

9. Protected Access Modifier

• Accessible:
• Inside the same class
• In the same package
• In subclasses (even outside the package)

4
class Parent {
protected int number = 42;
}

class Child extends Parent {


void show() {
System.out.println(number); // allowed
}
}

• Use when you want subclasses to have access but still restrict full external usage.

10. Deep vs Shallow Copy Constructors

a. Shallow Copy

• Copies object references – not actual object.


• Changes to one affect the other.

class Student {
StringBuilder name;

Student(Student s) {
this.name = s.name; // shallow copy
}
}

b. Deep Copy

• Copies entire object and nested objects.


• Independent copies.

class Student {
StringBuilder name;

Student(Student s) {
this.name = new StringBuilder(s.name); // deep copy
}
}

c. Why Avoid Shallow Copy?

• Leads to bugs and unintended modifications.


• Deep copy ensures data encapsulation and object safety.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy