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

Question 1

The third document provides specifications for a Salary class with private data member basic, and input and display methods to get basic pay from the user

Uploaded by

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

Question 1

The third document provides specifications for a Salary class with private data member basic, and input and display methods to get basic pay from the user

Uploaded by

GOPAL Dey.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Question 1

Write a program by using a class with the following specifications:


Class name — Prime
Data members — private int n
Member functions:
void input() — to input a number
void checkprime() — to check and display whether the number is prime or not
Use a main function to create an object and call member methods of the class.

import java.util.Scanner;
public class Prime
{
private int n;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
n = in.nextInt();
}

public void checkprime() {


boolean isPrime = true;
if (n == 0 || n == 1)
isPrime = false;
else {

for (int i = 2; i <= n / 2; i++) {


if (n % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime)
System.out.println("Prime Number");
else
System.out.println("Not a Prime Number");
}

public static void main(String args[]) {


Prime obj = new Prime();
obj.input();
obj.checkprime();
}
}

-----------------------------------------------------------------------------------
--------------------

Question 2

Write a program by using a class with the following specifications:


Class name — Factorial

Data members — private int n

Member functions:

void input() — to input a number


void fact() — to find and print the factorial of the number

Use a main function to create an object and call member methods of the class.

import java.util.Scanner;

public class Factorial


{
private int n;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
n = in.nextInt();
}

public void fact() {


int f = 1;
for (int i = 1; i <= n; i++)
f *= i;
System.out.println("Factorial of " + n
+ " = " + f);
}

public static void main(String args[]) {


Factorial obj = new Factorial();
obj.input();
obj.fact();
}
}

---------------------------------------------------------------------

Question 3

Write a program by using a class with the following specifications:

Class name — Salary

Data members — private int basic

Member functions:

void input() — to input basic pay


void display() — to find and print the following:
da = 30% of basic
hra = 10% of basic
gross = basic + da + hra
Use a main function to create an object and call member methods of the class.

import java.util.Scanner;

public class Salary


{
private int basic;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter the basic pay: ");
basic = in.nextInt();
}

public void display() {


double da = basic * 0.3;
double hra = basic * 0.1;
double gross = basic + da + hra;
System.out.println("da=" + da);
System.out.println("hra=" + hra);
System.out.println("gross=" + gross);
}

public static void main(String args[]) {


Salary obj = new Salary();
obj.input();
obj.display();
}
}

-----------------------------------------------------------------

Example 1 – Encapsulation in Java

In the following example, Human.java class has variables height, weight and bmi
which are private. For each variable, there is a setter and getter.
package com.tutorialkart.java;
/**
* @author tutorialkart
*/
class Human{
private float weight;
private float height;
private float bmi;
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getBmi() {
return bmi;
}
public void setBmi(float bmi) {
this.bmi = bmi;
}
}

public class EncapsulationExample {


public static void main(String[] args) {
Human h1 = new Human();
// using setters of Human
h1.setHeight(1.65f);
h1.setWeight(68);
h1.setBmi(calculateBmi(h1));

// using getters of Human


System.out.println("Person has "+h1.getWeight()+" kgs and is
"+h1.getHeight()+" meters in height, which results in BMI of "+h1.getBmi());
}

public static float calculateBmi(Human h1){


return h1.getWeight()/(h1.getHeight()*h1.getHeight());
}
}

--------------------------------------------------------------------------

//A Account class which is a fully encapsulated class.


//It has a private data member and getter and setter methods.
class Account {
//private data members
private long acc_no;
private String name,email;
private float amount;
//public getter and setter methods
public long getAcc_no() {
return acc_no;
}
public void setAcc_no(long acc_no) {
this.acc_no = acc_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public float getAmount() {
return amount;
}
public void setAmount(float amount) {
this.amount = amount;
}

//A Java class to test the encapsulated class Account.


public class TestEncapsulation {
public static void main(String[] args) {
//creating instance of Account class
Account acc=new Account();
//setting values through setter methods
acc.setAcc_no(7560504000L);
acc.setName("Sonoo Jaiswal");
acc.setEmail("[email protected]");
acc.setAmount(500000f);
//getting values through getter methods
System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+acc.getEmail()+"
"+acc.getAmount());
}
}

You might also like