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

Java Packages Examples

Uploaded by

Samsung A12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Java Packages Examples

Uploaded by

Samsung A12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

✅ 1.

Basic User-Defined Package


🧠 Question: Create a greeting package that contains a class to print a welcome message.

// File: greeting/[Link]
package greeting;

public class Welcome {


public void sayHello() {
[Link]("Welcome to Java Packages!");
}
}

// File: [Link]
import [Link];

public class Test {


public static void main(String[] args) {
Welcome w = new Welcome();
[Link]();
}
}

✅ 2. Using Built-in Package [Link]


🧠 Question: Use Scanner class from [Link] to take user input.

import [Link];

public class InputExample {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello " + name);
}
}

✅ 3. Package with Utility Method


🧠 Question: Create a package tools with a Calculator class having an add() method.

// File: tools/[Link]
package tools;

public class Calculator {


public int add(int a, int b) {
return a + b;
}
}

// File: [Link]
import [Link];

public class Main {


public static void main(String[] args) {
Calculator c = new Calculator();
[Link]("Sum = " + [Link](5, 3));
}
}

✅ 4. Multiple Classes in a Package


🧠 Question: Create a mathlib package with two classes: Adder and Multiplier.

// File: mathlib/[Link]
package mathlib;

public class Adder {


public int add(int x, int y) { return x + y; }
}

// File: mathlib/[Link]
package mathlib;

public class Multiplier {


public int multiply(int x, int y) { return x * y; }
}

// File: [Link]
import [Link];
import [Link];

public class App {


public static void main(String[] args) {
Adder a = new Adder();
Multiplier m = new Multiplier();
[Link]("Add: " + [Link](2, 3));
[Link]("Multiply: " + [Link](2, 3));
}
}

✅ 5. Package with Default Access Modifier


🧠 Question: What happens when a class is not marked public in a package?

// File: tools/[Link]
package tools;

class Helper { // Not public


void help() {
[Link]("Helping...");
}
}

// File: [Link]
import [Link]; // ❌ Will give error unless both files are in same
package

✅ 6. Sub-Package Example
🧠 Question: Create a sub-package [Link] with a method to reverse a string.

// File: utils/strings/[Link]
package [Link];

public class StringTool {


public String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
}

// File: [Link]
import [Link];

public class Main {


public static void main(String[] args) {
StringTool st = new StringTool();
[Link]([Link]("hello"));
}
}

✅ 7. Using static import

🧠 Question: Use static import to access [Link]() without Math. prefix.

import static [Link].*;

public class RootCalc {


public static void main(String[] args) {
[Link]("Square root of 16 is " + sqrt(16));
}
}

✅ 8. Access Modifier Restriction


🧠 Question: Try accessing a default class from a different package.
// File: alpha/[Link]
package alpha;

class Hidden {
void show() { [Link]("Hidden class"); }
}

// File: beta/[Link]
import [Link]; // ❌ Won't compile: class Hidden is not public

public class Tester {


public static void main(String[] args) {
Hidden h = new Hidden();
[Link]();
}
}

✅ 9. Reverse Domain Naming


🧠 Question: Use proper package naming like [Link].

// File: com/example/greet/[Link]
package [Link];

public class Greeter {


public void greet() {
[Link]("Greetings from [Link]!");
}
}

// File: [Link]
import [Link];

public class App {


public static void main(String[] args) {
new Greeter().greet();
}
}

✅ 10. Compile Package from Terminal


🧠 Question: How do you compile a package-based file from the terminal?

// File: demo/[Link]
package demo;

public class Message {


public void show() {
[Link]("Hello from demo package");
}
}

Terminal Commands:

javac -d . demo/[Link]
javac -d . [Link]
java Main

You might also like