You are on page 1of 8

Object Oriented Programming

through JAVA lab manual


Name: SHAIK SAMEER

ID: B161753

Class: E3 CSE AB2-012

Git repository: https://github.com/mrsameer/javaoops

WEEK-5

1. Write a Java Program to implement Wrapper classes and their methods.

/*
1. Write a Java Program to implement Wrapper classes and their methods.
*/
package labmanual.week5;

public class Q1 {
public static void main(String[] args) {
// create primitive types
int a = 5;
double b = 5.54;

// convert primitive types info wrapper objects (boxing)


Integer aObj = Integer.valueOf(a);
Double bObj = Double.valueOf(b);

if (aObj instanceof Integer)


System.out.println("An Integer object is created");
if (bObj instanceof Double)
System.out.println("A Double object is created");

// convert objects into primitive types (unboxing)


int aint = aObj.intValue();
double bdoub = bObj.doubleValue();

System.out.println("The value of aint " + aint);


System.out.println("The value of bdub " + bdoub);
}
}

Object Oriented Programming through JAVA lab manual 1


2. Write an application that prompts the user for the radius of a circle and uses a
method
called circleArea to calculate the area of the circle and uses a method
circlePerimeter to
calculate the perimeter of the circle.

/*
2. Write an application that prompts the user for the radius of a circle and uses a method
called circleArea to calculate the area of the circle and uses a method circlePerimeter to
calculate the perimeter of the circle.
*/
package labmanual.week5;

import java.util.Scanner;

public class Q2 {
public double circleArea(double radius) {
return Math.PI * radius * radius;
}

public double circlePerimeter(double radius) {


return 2 * Math.PI * radius;
}

private static Scanner sc = new Scanner(System.in);


public static void main(String[] args) {
Q2 q2 = new Q2();
int option;
Double radius;
while (true) {
System.out.print("1. Get area and perimeter\n2.Quit\n");
option = sc.nextInt();
switch (option) {
case 1:
System.out.print("Enter the radius: ");
radius = sc.nextDouble();
System.out.println("The area of the circle is: " + q2.circleArea(radius));
System.out.println("The perimeter of the circle is: " + q2.circlePerimeter(radius));
break;
case 2:
System.exit(0);
default:
System.out.println("Select correct choice");
}
}
}
}

Object Oriented Programming through JAVA lab manual 2


3. Write a JAVA program for the following
a. Call by value
RGUKT-B.Tech-CSE-OOPS-CS3702-E2-SEM-2-AY:2019-2020.
b. Call by object
Page 3OOPS using JAVA CSE-E3

/*
3. Write a JAVA program for the following a. Call by Value b. Call by Object
*/
package labmanual.week5;

public class Q3 {

int data = 100;

public void change(int data) {


data = data + 50;
}

public void change(Q3 q3) {


q3.data = q3.data +50;
}

public static void main(String[] args) {


Q3 q3 = new Q3();
// call by value demonstration
System.out.println("Before calling method, data = " + q3.data);
q3.change(100);
System.out.println("After calling method, data = " + q3.data);

// call by reference demonstration


System.out.println("Before calling method, data = " + q3.data);
q3.change(q3);
System.out.println("After calling method, data = " + q3.data);
}
}

Object Oriented Programming through JAVA lab manual 3


4. Create a class Account with an instance variable balance (double). It should
contain a
constructor that initializes the balance, ensure that the initial balance is
greater than 0.0.
Acct details: Acct_Name, Acct_acctno, Acct_Bal, Acct_Address.
Create two methods namely credit and debit, getBalance. The Credit adds the
amount
(passed as parameter) to balance and does not return any data. Debit method
withdraws
money from an Account. GetBalance displays the amount. Ensure that the debit
amount
does not exceed the Account’s balance. In that case the balance should be left
unchanged
and the method should print a message indicating
“Debit amount exceeded account
balance”.

/*
Create a class Account with an instance variable balance (double). It should contain a
constructor that initializes the balance, ensure that the initial balance is greater than 0.0.
Acct details: Acct_Name, Acct_acctno, Acct_Bal, Acct_Address.
Create two methods namely credit and debit, getBalance. The Credit adds the amount
(passed as parameter) to balance and does not return any data. Debit method withdraws
money from an Account. GetBalance displays the amount. Ensure that the debit amount
does not exceed the Account’s balance. In that case the balance should be left unchanged
and the method should print a message indicating
“Debit amount exceeded account
balance”
*/
package labmanual.week5;

import java.util.Scanner;

public class Account {


private Double balance;
private String accountName;
private long acccountNo;
private String accountAddress;

public Account(Double balance, String accountName, long acccountNo, String accountAddress) {


if (balance > 0.0)
this.balance = balance;
this.accountName = accountName;
this.acccountNo = acccountNo;
this.accountAddress = accountAddress;
}

public Double getBalance() {


return balance;
}

private void setBalance(Double balance) {


this.balance = balance;
}

Object Oriented Programming through JAVA lab manual 4


public String getAccountName() {
return accountName;
}

public void setAccountName(String accountName) {


this.accountName = accountName;
}

public long getAcccountNo() {


return acccountNo;
}

public void setAcccountNo(long acccountNo) {


this.acccountNo = acccountNo;
}

public String getAccountAddress() {


return accountAddress;
}

public void setAccountAddress(String accountAddress) {


this.accountAddress = accountAddress;
}

// credit (add) amount to the account


public void credit(double amount) {
this.setBalance(balance + amount);
}

// debit or withdraw (subtract an amount from the balance)


public boolean debit(double amount) {
if (balance >= amount)
this.setBalance(balance-amount);
else
return false;
return true;
}
}

class AccountDemo {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Account account1 = new Account(1000.0, "sameer sbi account", 1, "hyderabad");
Account account2 = new Account(12000.0, "roy andhra bank account", 34, "new york");

// display initial balance of each object.


System.out.printf("account1 balance: %.2f\n", account1.getBalance());
System.out.printf("account2 balance: %.2f\n", account2.getBalance());

double depositAmount; // deposit amount read from the user


double withDrawAmount; // withdraw amount read from the user

System.out.print("Enter the deposit amount for account1: "); // prompt


depositAmount = sc.nextDouble();
account1.credit(depositAmount);
System.out.printf("Deposited %.2f rupees into account1\n", depositAmount);

// display current balances


System.out.printf("account1 balance: %.2f\n", account1.getBalance());
System.out.printf("account2 balance: %.2f\n", account2.getBalance());

System.out.print("Enter the deposit amount for account2: "); // prompt


depositAmount = sc.nextDouble();
account2.credit(depositAmount);
System.out.printf("Deposited %.2f rupees into account2\n", depositAmount);

// display current balances


System.out.printf("account1 balance: %.2f\n", account1.getBalance());
System.out.printf("account2 balance: %.2f\n", account2.getBalance());

System.out.print("Enter withdraw amount for account1: "); // prompt


withDrawAmount = sc.nextDouble();
if (account1.debit(withDrawAmount) == true)
System.out.println("Succesfully withdrawn");
else
System.out.println("Insufficient funds");

// display current balances

Object Oriented Programming through JAVA lab manual 5


System.out.printf("account1 balance: %.2f\n", account1.getBalance());
System.out.printf("account2 balance: %.2f\n", account2.getBalance());

System.out.print("Enter withdraw amount for account2: "); // prompt


withDrawAmount = sc.nextDouble();
if (account2.debit(withDrawAmount) == true)
System.out.println("Succesfully withdrawn");
else
System.out.println("Insufficient funds");

// display current balances


System.out.printf("account1 balance: %.2f\n", account1.getBalance());
System.out.printf("account2 balance: %.2f\n", account2.getBalance());
}
}

5. Write Java program for the following


a. Example for this operator and the use of this keyword.
b. Example for super keyword.
c. Example for static variables and methods.

/*
5. Write Java program for the following
a. Example for this operator and the use of this keyword.
b. Example for super keyword.
c. Example for static variables and methods.
*/
package labmanual.week5;

// constructor in a class without this keyword


class Student1 {
int rollno;
String name;
float fee;

public Student1(int rollno, String name, float fee) {


rollno = rollno;
name = name;

Object Oriented Programming through JAVA lab manual 6


fee = fee;
}

public void display() {


System.out.println("roll no: " + rollno + " name: " + name + " fee: " + fee);
}
}

// constructor in a class with this keyword


class Student2 {
int rollno;
String name;
float fee;

public Student2(int rollno, String name, float fee) {


/*
In the above example, parameters (formal arguments) and instance variables are same. So, we are using
this keyword to distinguish local variable and instance variable.
*/
this.rollno = rollno;
this.name = name;
this.fee = fee;
}

public void display() {


System.out.println("roll no: " + rollno + " name: " + name + " fee: " + fee);
}
}

class Person {
int id;
String name;

public Person(int id, String name) {


this.id = id;
this.name = name;
}
}

class Emp extends Person {


float salary;

public Emp(int id, String name, float salary) {


super(id, name); // reusing parent constructor
this.salary = salary;
}
public void display() {
System.out.println("Id: " + id + " name: " + name + " salary: " + salary);
}
}

class StaticDemo {
static int x = 19; // static variable alongside initialization
static int y; // declaration of static variable

// static method
static void func(int z) {
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
}

// static block
static {
System.out.println("Running static block");
y = x + 100;
}
}
public class Q5 {
public static void main(String[] args) {
// demonstration of this keyword
new Student1(111, "ankit", 5000f).display(); // displays incorrect information as it fails to assign values
new Student2(111, "ankit", 5000f).display(); // displays correct information

//demonstration of super keyword


new Emp(1, "ankit", 45000f).display();

// demonstration of static variables and methods


StaticDemo.func(100);

Object Oriented Programming through JAVA lab manual 7


}
}

Object Oriented Programming through JAVA lab manual 8

You might also like