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

Computer Program2

This document contains code for two Java programs. The first program implements importing classes from a user-defined package and creating packages. It contains code to create a Calculator class in a package and call its methods from another class in the same package. The second program explains the use of different access specifiers (public, protected, default, private) through classes with fields and methods using each access specifier.

Uploaded by

aditya.cse121118
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)
13 views

Computer Program2

This document contains code for two Java programs. The first program implements importing classes from a user-defined package and creating packages. It contains code to create a Calculator class in a package and call its methods from another class in the same package. The second program explains the use of different access specifiers (public, protected, default, private) through classes with fields and methods using each access specifier.

Uploaded by

aditya.cse121118
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/ 4

Assignment - 6

Name – Sourasish Mondal


University Roll No. – 11500221053
Stream – CSE Section – B Group – 2
Subject – Object Oriented Programming
Subject Code – PCC-CS593
Problem Statement:

1. Java program to implement the concept of importing classes from


user defined package and creating packages.

Code:

Folder Directory:

cs593/
├── src/
│ └── assignment6/
│ ├── souraapp/
│ │ ├── Calculator
│ │ └── MainApp
│ └── Main
├── out
└── cs593.iml

Calculator.java

package assignment6.souraapp;

public class Calculator {


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

public static int subtract(int a, int b){


return a - b;
}
}
MainApp.java

package assignment6.souraapp;

public class MainApp {


public static void main(String[] args){
int number1 = 23;
int number2 = 34;
int sum = Calculator.add(number1, number2);
int subtract = Calculator.subtract(number1, number2);

System.out.println("The sum is: " + sum);


System.out.println("The difference is: " + subtract);
}
}

Main.java

package assignment6;

import assignment6.souraapp.MainApp;
public class Main {
public static void main(String[] args) {
MainApp.main(args);
}
}

Output:
2. Write a java program to explain the use of access specifiers Public,
Protected, Default, Private

Code:

PublicClass.java

package AccessModifier;

// class with public access


public class PublicClass {
public String publicString = "This is a string inside the
PUBLIC class";
public void publicMethod() {
System.out.println("Public method can be accessed from
anywhere!");
}
}
// class with protected access
class ProtectClass{
protected String protectedString = "This is a string inside
the PROTECTED class";
protected void protectedMethod(){
System.out.println("Protected method can be accessed
within the same package or by subclasses.");
}
}
// class with default access
class DefaultClass{
String defaultString = "This is a inside the DEFAULT class";
void defaultMethod(){
System.out.println("Default methods can be accessed within
the same package");
}
}
// class with private access
class privateClass{
private String privateString = "This is a string inside the
PRIVATE class";
private void privateMethod(){
System.out.println("Private methods can be accessed within
the same class");
}
}
accessClass.java

package AccessModifier;

public class accessClass {


public static void main(String[] args) {
PublicClass publicObj = new PublicClass();
System.out.println(publicObj.publicString);
publicObj.publicMethod();

ProtectClass protectedObj = new ProtectClass();


System.out.println(protectedObj.protectedString);
protectedObj.protectedMethod();

DefaultClass defaultObj = new DefaultClass();


System.out.println(defaultObj.defaultString);
defaultObj.defaultMethod();

privateClass privateObj = new privateClass();


// System.out.println(privateObj.privateString);
// privateObj.privateMethod();

}
}

Output:

For private class:

You might also like