NB52053

#NADUI__JAVA LAB 2 ASSIGNMENT

Jun 11th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1.                                   /////////////////      LAB 2 OOP HELALI APA  //////////////////
  2.  
  3. // TASK 2
  4.  
  5. ----------------------------------------------------------------------------------------
  6. public class BankAccount {
  7.  
  8.      public String name, ID;
  9.      public double balance;
  10.  
  11.  
  12.     public BankAccount (String name , String ID, double balance){
  13.         this.name=name;
  14.         this.ID=ID;
  15.         this.balance=balance;
  16.  
  17.  
  18. }  // Constructor
  19.    
  20.     // Method One
  21.         public void deposit(double depAmount){
  22.  
  23.             balance = balance + depAmount;
  24.      }
  25.    
  26.     //Method Two
  27.      public void withdraw(double withAmount){
  28.  
  29.                if (balance >= withAmount){
  30.                 balance = balance - withAmount;
  31.           }
  32.          else {
  33.                   System.err.println("Insufficient balance for withdraw");
  34.          }
  35.  
  36.      }
  37.  
  38.     //Method Three
  39.      public double getBalance(){
  40.  
  41.             return balance;
  42.       }
  43.  
  44.     //Method Four
  45.         public void display() {
  46.             System.out.println("Name:" + name + "  ID: " + ID + "   Balance: " + balance);
  47.         }
  48. }
  49.  
  50. public class Bank{
  51.  
  52.     public static void main (String [] args){
  53.  
  54.         BankAccount cus1 = new BankAccount();
  55.  
  56.  
  57.         cus1.name =  "Nadira Anjum";
  58.         cus1.ID  =  "120053";
  59.         cus1.balance = 0;
  60.  
  61.  
  62.         cus1.deposit(1000);
  63.         cus1.withdraw(3000);
  64.  
  65.         System.out.println(cus1.getBalance());
  66.         cus1.display();
  67.  }
  68.  
  69. }
  70.  
  71.  
  72. public class BankAccountArray {
  73.  
  74.     public static void main (String [] args){
  75.  
  76.       BankAccount[] customer = new BankAccount[4];
  77.        
  78.         customer [0] = new BankAccount("Nazrul Islam" ,"1234" ,20000 );
  79.         customer [1] = new BankAccount("Rizvi Mahmud", "2345",  2000);
  80.         customer [2] = new BankAccount("Rizwana Khan", "3456" , 3000);
  81.         customer [3] = new BankAccount( "Anwar Hossain","4567" , 1000 );
  82.        
  83.          for (int i =0; i<customer.length; i++){
  84.  
  85.                  customer[i].display();
  86.          }
  87.      }
  88. }
  89. ----------------------------------------------------END----------------------------------------------
  90.  
  91.  
  92.  
  93. ------------------------------------------------------------------------------------------------------
  94. //TASK 3 (a) OPTION 1
  95.  
  96. package com.company;
  97. import java.util.Scanner;
  98.  
  99. public class Fibo {
  100.  
  101.         public static void main(String[] args) {
  102.  
  103.             int t1 =0, t2 = 1, nextTerm = 0;
  104.  
  105.             Scanner scan = new Scanner(System.in);
  106.             System.out.println("Enter the term");
  107.             int n = scan.nextInt();
  108.  
  109.             for (int i= 1; i<=n ; ++i){
  110.  
  111.                 if (i == 1){
  112.  
  113.                     System.out.printf("  %d  " , t1);
  114.                     continue;
  115.                 }
  116.  
  117.                 if (i == 2){
  118.                     System.out.printf("  %d  " , t2);
  119.                     continue;
  120.  
  121.                 }
  122.                 nextTerm = t1 + t2;
  123.                 t1 = t2;
  124.                 t2 = nextTerm;
  125.                 System.out.printf("  %d  " , nextTerm);
  126.  
  127.             }
  128.         }
  129. }
  130.  
  131.  
  132.  
  133. //TASK 3(b)   OPTION 1
  134.  
  135. package com.company;
  136. import java.util.Scanner;
  137.  
  138. public class EvenOdd {
  139.  
  140.     public static void main(String args[]) {
  141.        
  142.         Scanner input = new Scanner(System.in);
  143.  
  144.         System.out.printf("Enter any number : ");
  145.  
  146.        
  147.         int number = input.nextInt();
  148.  
  149.        
  150.         if ((number % 2) == 0) {
  151.             System.out.printf("number %d is even number %n", number);
  152.  
  153.         } else {
  154.             System.out.printf("number %d is odd number %n", number);
  155.         }
  156.     }
  157. }
  158.  
  159. ---------------------------------------------------------------------------------------------------------------
  160. PROBLEM 3 ... OPTION 2
  161.  
  162. public class Numbers {
  163.  
  164.     int num;
  165.  
  166.     public void showFibonacci() {
  167.         int t1 = 0, t2 = 1, nextTerm = 0;
  168.  
  169.  
  170.         for (int i = 1; i <= num; ++i) {
  171.  
  172.             if (i == 1) {
  173.  
  174.                 System.out.printf("  %d  ", t1);
  175.                 continue;
  176.             }
  177.  
  178.             if (i == 2) {
  179.                 System.out.printf("  %d  ", t2);
  180.                 continue;
  181.  
  182.             }
  183.             nextTerm = t1 + t2;
  184.             t1 = t2;
  185.             t2 = nextTerm;
  186.             System.out.printf("  %d  ", nextTerm);
  187.  
  188.         }
  189.     }
  190.  
  191.     public void showPyramid(){
  192.  
  193.  
  194.  
  195.         int rowCount = 1;
  196.  
  197.         System.out.println("Here Is the Pyramid");
  198.  
  199. if (num <=9 ) {
  200.  
  201.     for (int i = num; i > 0; i--) {
  202.  
  203.         for (int j = 1; j <= i * 2; j++) {
  204.             System.out.print(" ");
  205.         }
  206.  
  207.         for (int j = 1; j <= rowCount; j++) {
  208.             System.out.print(j + " ");
  209.         }
  210.  
  211.         for (int j = rowCount - 1; j >= 1; j--) {
  212.             System.out.print(j + " ");
  213.         }
  214.  
  215.         System.out.println();
  216.  
  217.         rowCount++;
  218.     }
  219. }
  220.  
  221.     else {                          /////// ***** You can avoid this else part for printing *****//////
  222.  
  223.         for (int i = 9; i > 0; i--) {
  224.  
  225.             for (int j = 1; j <= i * 2; j++) {
  226.                 System.out.print(" ");
  227.             }
  228.  
  229.             for (int j = 1; j <= rowCount; j++) {
  230.                 System.out.print(j + " ");
  231.             }
  232.  
  233.             for (int j = rowCount - 1; j >= 1; j--) {
  234.                 System.out.print(j + " ");
  235.             }
  236.  
  237.             System.out.println();
  238.  
  239.             rowCount++;
  240.         }
  241.       }
  242.     }
  243.  
  244.  
  245.     public boolean checkNumber(){
  246.  
  247.         boolean even = true;
  248.         if (num%2!=0){
  249.             even = false;
  250.         }
  251.         return true;
  252.     }
  253.  
  254.     void setNum(int num){
  255.  
  256.  
  257.     }
  258. }
  259.  
  260.  
  261.  
  262.  
  263. import java.util.Scanner;
  264.  
  265. public class NumberTest {
  266.  
  267.     public static void main(String[] args) {
  268.  
  269.         Scanner sc = new Scanner(System.in);
  270.         Numbers test = new Numbers();
  271.         int select = sc.nextInt();
  272.  
  273.         test.num = 5;
  274.  
  275.     switch (select) {
  276.  
  277.              case 1:test.showFibonacci();
  278.              break;
  279.  
  280.              case 2:test.showPyramid();
  281.              break;
  282.  
  283.              case 3: test.checkNumber();
  284.              break;
  285.  
  286.              case 4:test.setNum(0);
  287.         }
  288.  
  289.     }
  290. }
  291. ---------------------------------------------------------------------------E---N----D-------------------------------------------------
Add Comment
Please, Sign In to add comment