Week 1 of OOP in JAVA

Create a program to compute factorial.


import java.util.Scanner;

public class Factorial {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int num, f = 1;
    System.out.print("Enter the number : ");
    num = scanner.nextInt();
    for (int i = num; i > 1; i--) {
      f *= i;
    }
    System.out.print("The factorial of " + num + " is " + f);
    scanner.close();
  }
}


Create a class named student with data members name and age. Create an object of that class and display the student details using a member function.


class Student{
  String name;
  int roll;
  void display(){
    System.out.println("Name : " + name + "\nRoll No : " + roll);
  }
}

public class Student_Main {
  public static void main(String[] args){
    Student s1 = new Student();
    s1.name = "Sunny";
    s1.roll = 12;

    s1.display();
  }
}


Create a class named Room having 2 variables length and breadth. Write 3 methods within the class, one is to take the input, second is to display the values and third is to calculate the area. Create an object of that class in main function.


class Room{
 double length,breadth,area;
 void getData(double l,double b){
  length = l;
  breadth = b;
 }
 void printData(){
  System.out.println("Length = " + length + "\nBreadth = " + breadth);
 }
 void area(){
  System.out.println("The area of the room is " + length*breadth);
 }
}
public class Room_Main {
  public static void main(String[] args){
    Room r1 = new Room();
    r1.getData(54.3,66.1);
    r1.printData();
    r1.area();
  }
}


Create a class named Calculator which has methods for addition and subtraction of 2 numbers and display the results.


class Calculator{
  void add(int a, int b){
    System.out.println("The sum is : " + (a+b));
  }
  void subtract(int a, int b){
    System.out.println("The difference is : " + (a-b));
  }
}

public class Calc{
  public static void main(String[] args) {
    Calculator calc = new Calculator();
    calc.add(5,3);
    calc.subtract(7, 4);
  }
}


Design a class to represent a bank account. Include details as following :
(i) Name of Depositor
(ii) Type of Account
(iii) Account Number
(iv) Balance
Also design the following methods :
(i) Assign Initial Value
(ii) To Deposit Money
(iii) To Withdraw Money After Checking Balance
(iv) Display Name and Balance


class Customer{
  String name;
  char accType;
  String accNo;
  double balance = 0;
  void assignInitial(double money){
    if(money >= 0)
      balance = money;
    else
      System.out.println("Money cannot be negative");
  }
  void deposit(double money){
    if(money >= 0){
      balance += money;
      System.out.println(money + " is deposited");
    }
  }
  void withdraw(double money){
    if(money > balance)
       System.out.println("You do not have sufficient funds");
    else{
      balance -= money;
      System.out.println(money + " is withdrawn");
    }
  }
  void details(){
    System.out.println("Account Holder : " + name + "\nCurrent Balance : " + balance);
  }
}
public class Bank {
  public static void main(String[] args) {
    Customer c1 = new Customer();
    c1.name = "Arun Singh";
    c1.accType = 'S';
    c1.accNo = "SBI0142001";
    c1.assignInitial(25000);
    c1.deposit(14000);
    c1.withdraw(500000);
    c1.withdraw(5000);
    c1.details();
  }
}


Comments

Popular Posts