0% found this document useful (0 votes)
11 views10 pages

Programs From Access Specifiers To Encapsulation

The document covers various Java programming concepts including access specifiers, encapsulation, and dynamic input handling. It provides code examples demonstrating the use of public, private, protected, and default access levels, as well as encapsulation through getter and setter methods. Additionally, it includes programs for reading user input dynamically and performing basic arithmetic operations.

Uploaded by

bghosh337
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)
11 views10 pages

Programs From Access Specifiers To Encapsulation

The document covers various Java programming concepts including access specifiers, encapsulation, and dynamic input handling. It provides code examples demonstrating the use of public, private, protected, and default access levels, as well as encapsulation through getter and setter methods. Additionally, it includes programs for reading user input dynamically and performing basic arithmetic operations.

Uploaded by

bghosh337
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/ 10

Access specifiers

1)
package one;

public class Demo1 {

public static int num1 =100;


private static double num2=20.25;
static char c ='z';
protected static String s ="abcd";

public static void main(String[] args) {


System.out.println(num1);
System.out.println(num2);
System.out.println(c);
System.out.println(s);
}
}

Output:
100
20.25
z
abcd

package one;

public class Sample1 {

public static void main(String[] args) {


System.out.println(Demo1.num1);
// System.out.println(Demo1.num2);
System.out.println(Demo1.c);
System.out.println(Demo1.s);
}
}

Output
100
z
abcd
package two;

import one.Demo1;

public class MainClass extends Demo1 {

public static void main(String[] args) {


System.out.println(Demo1.num1);
// System.out.println(Demo1.num2);
// System.out.println(Demo1.c);
System.out.println(Demo1.s);
}
}

Output
100
abcd

2)
public class Person {

private String name;


private long contactNum;

public String getName() {


return name;
}
public void setName(String name) {
this.name = name;
}
public long getContactNum() {
return contactNum;
}
public void setContactNum(long contactNum) {
this.contactNum = contactNum;
}

}
public class MainClass {

public static void main(String[] args) {

Person p1 = new Person();

p1.setName("John");

System.out.println(p1.getName());

System.out.println("=========================");

p1.setContactNum(93573467l);

System.out.println(p1.getContactNum());

}
}

Output:
John
=========================
93573467
Program on dynamic read
1)
import java.util.Scanner;

public class Demo1 {

public static void main(String[] args) {

System.out.println("start");
System.out.println("enter the int data");

Scanner sc = new Scanner(System.in);


int num1= sc.nextInt();

System.out.println(num1);
System.out.println("=================");
System.out.println("eneter the double data");

double num2 = sc.nextDouble();


System.out.println(num2);

System.out.println("=================");

int[] a1 = new int[5];

for(int i=0 ; i<a1.length ;i++)


{
System.out.println("enter the int data");
a1[i] =sc.nextInt();

System.out.println(a1[i]);
}

System.out.println("end");
}

Output:
start
enter the int data
10
10
=================
enter the double data
20.14
20.14
=================
enter the int data
10
10
enter the int data
20
20
enter the int data
30
30
enter the int data
40
40
enter the int data
50
50
end

2)
import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");


double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();

System.out.print("Choose an operation (+, -, *, /): ");


char operator = scanner.next().charAt(0);

double result = 0;

// Perform the operation based on the operator


switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
// Check for division by zero
if (num2 == 0) {
System.out.println("Error! Division by zero.");
return;/*The return statement is used inside an if block to exit the method
immediately when a specific condition is met*/
}
result = num1 / num2;
break;
default:
System.out.println("Invalid operator!");
break;
}

System.out.println("Result: " + result);


}

Output:
Enter the first number: 30
Enter the second number: 60
Choose an operation (+, -, *, /): *
Result: 1800.0
program on encapsulation

1)
public class Product {

private double price;

public double getPrice() {


return price;
}

public void setPrice(double price) {


//validation
if(price > 0)
{//coping data from local to object
this.price = price;
}
}
}

import java.util.Scanner;

public class ProductDriver {

public static void main(String[] args) {


Product p1 = new Product();

Scanner sc = new Scanner(System.in);

System.out.println("enter the price for "


+ "the product of double type");

double price = sc.nextDouble();

p1.setPrice(price);

System.out.println(p1.getPrice());

}
}

Output
enter the price for the product of double type
2000
2000.0
2)

public class Student


{

private int age;


private double marks;

public int getAge() {


return age;
}

public void setAge(int age) {


if(age >= 20 && age <= 50)
{
this.age = age;

}
else {
System.out.println("age criteria doesn't match");
}
}

public double getMarks() {


return marks;

public void setMarks(double marks) {

if((marks >= 35 && marks <=100) )


{
this.marks = marks;
}
else {
System.out.println("your marks is not sufficient");
}
}

public void isEligible()


{
if((age >= 20 && age <= 50) && (marks >= 35 && marks <=100))
{
System.out.println("eligible");
}
}
}
public class StudentDriver {

public static void main(String[] args) {

Student s1 = new Student();


s1.setAge(19);
System.out.println("Student age is "+s1.getAge());

s1.setMarks(80);
System.out.println("Student marks is "+s1.getMarks());

s1.isEligible();
}
}

Output:

Student age is 20
Student marks is 80.0
eligible

You might also like