OOP 1-2

Day 1

1.       P39

Can you answer these questions?

Q1. Consider the following activities of an ATM application-

  •  
    • Authorization of User
    • Display of menu
    • Cash withdrawal
    • Cash deposit
    • Accepting Pin Number
    • Verification of balance amount
    • Printing of status/transaction on paper

Using structured programming approach , identify the key modules and the related activities? What is the focus of the structured programming approach?

Hint:

The key modules may be :

  1. Login

          Accepting pin number

          Authorization of the user

  1. Display of menu
  2. Cash withdrawal

          Verification of balance amount

          Printing status/transaction on paper

  1. Cash Deposit

The focus of the approach is on the functionalities/activities of the ATM application

2.       P40 Q2. In the ATM machine, the customer chooses the operations using a touch screen. The customer need not know the internal working of the ATM machine . Which OO concept(s) can be used in this scenario?

Q3. Consider the following statement: “Vehicles can be of two types viz. Water vehicles and Land vehicles “ . Which OO concept may be used to represent this scenario?

Hint:

Q2. Abstraction – as some essential details which are needed for the operation of ATM is known to the customer and the internal details are not known

     Encapsulation- as the ATM machine encapsulates or hides the internal working – say how the cash gets stored and how the bank balance is known etc.

                Refer to the footer notes of Slide # 25.

Q3. Inheritance – Generalization/Specialization

3.       P45

 

4.        P56

Can you answer these questions?

Q1. Consider Bank Account as a class. What are the attributes and methods  possible with this class? What is this approach called ? What is the focus of this approach?

Q2. In Q1, what are the attributes/activities that would be made public/private from the perspective of designing the class

 

Q3. Consider an online banking system. A customer can use this application to transfer funds, check his balance and also print the monthly statement. A bank officer can use this system to key in requests for pin number, change details of customers and to print the monthly report

Identify the actors and use cases in this scenario

Hint:

Q1. Account Number, Type of account and amount balance may be the attributes . GetAccountNumber, OpenAccount, CloseAccount, DisplayBalance may be the activities on the attributes. This approach is the OO approach where the focus is on the data and the activities on the data

Q2. The Account Number and Type of Account may be made private. The GetAccountNumber, OpenAccount, CloseAccount, DisplayBalance methods may be made public

                The attributes can be made private and the activities may be exposed ie. made public

Q3. Actors- Customer, Bank Officer, UseCases – Transfer funds, Check Balance,Print statement, Key-in request, Change customer details,PrintReport

5.       2-P13

6.       2-P25

Pointers

References

Printing a pointer will print the address stored in it

Printing a reference will NOT print the address of the object referred by it

Pointer arithmetic , like incrementing a pointer is valid in the case of a pointer

Arithmetic operators CANNOT be used on references

A pointer has to be de-referenced using the * operator to get the value pointed by it

A reference is automatically de-referenced to give the data referred by it and no special operator is required for this

 

7.       2-P66

Can you answer these questions?

Q1. Match the following:

 

 

1.  Java Source Code

A.   .class file

2.   Byte Code

B.   Platform dependent

3.   Java Virtual Machine

C.   Write Once, Run Anywhere

 

Q2. From the below given snippet, identify the number of 

       reference variables and objects

                        MyClass refObj = new MyClass();

                        MyClass tempObj = null;

                        MyClass objOne = refObj;

Q3. Consider the below given code snippet:

class MyClass{

        public int myVariable;

        public void myMethod(){

                        int temp=10;

                        ………..

        }

        public static void main(String args[]){

                        MyClass obj=new MyClass();

                        …………..

        }

}

 

Where the following stored in memory?

myVariable, temp, obj,

 

ANS:

Q1.  Ans

        1 – C, 2 - A, 3 – B

Q2. Ans

        No. of Reference Variables = 3

        No. of Objects = 1

Q3.  Ans

        myVariable        – Heap

        temp     – Stack

        obj         -  Stack

Q4. What will be the output of the following code snippet and Why?

class MyClass{

        public int varOne=1000;

        public float varTwo=20.00f;

        public double varThree=0.0;

        public void myMethod(){

                        varThree=(double)(varTwo + varOne*(7/100));

                        System.out.println("varThree:"+varThree);

        }

        public static void main(String args[]){

                        MyClass obj=new MyClass();

                        obj.myMethod();

        }

}

Q4.  Ans

        varThree:20.0

        Since 7/100 results in 0, although there is type casting, there is loss of data

Q5. What will be the output of the following code snippet and Why?

class MyClass{

        public int varOne=1000;

        public float varTwo=20.00f;

        public double varThree=0.0;

        public void myMethod(){

                        varThree=(double)(varTwo + varOne*((double)7/100));

                        System.out.println("varThree:"+varThree);

        }

        public static void main(String args[]){

                        MyClass obj=new MyClass();

                        obj.myMethod();

        }

}

Q5.  Ans

        varThree:90.0

Q6. What will be the output of the following code snippet and Why?

class MyClass{

        public int varOne=1000;

        public float varTwo=20.00f;

        public double varThree=0.0;

        public void myMethod(){

                        varThree=(double)(varTwo + varOne*(double) (7/100));

                        System.out.println("varThree:"+varThree);

        }

        public static void main(String args[]){

                        MyClass obj=new MyClass();

                        obj.myMethod();

        }

}

Q6.  Ans

        varThree:20.0

Q7. Debug the following code snippet, so that the output is varOne:100 ?

class MyClass{

        public int varOne=1000;

        public void myMethod(){

                        int temp;

                        varOne= temp + 10.0;

                        System.out.println(“varOne:"+varOne);

        }

        public static void main(String args[]){

                        MyClass obj=new MyClass();

                        obj.myMethod();

        }

}

Note: there are 2 errors in the code

Q8. What will be the output of the following code snippet?

class MyClass{

        public boolean myMethod(int temp){

                        if(temp>100){

                                        return true;

                        }

                        else{

                                        return false;

                        }

        }

        public static void main(String args[]){

                        MyClass obj=new MyClass();

                        if(obj.myMethod(110)){

                                        System.out.println("Data is valid");

                        }

                        else{

                                        System.out.println("Data is invalid");

                        }

        }

}

Q8. Ans

        Data is valid

Q9. What will be the output of the following code snippet?

public class MyClass{

        public static void main(String args[]){

                        int age=25;

                        float ticketFare=0.0f;

                        if(age>10){

                                        ticketFare = 20f;

                        }

                        if(age>20){

                                        ticketFare = 40f;

                        }

                        if(age>30){

                                        ticketFare = 60f;

                        }

                        System.out.println("Your Ticket Fare is " + ticketFare);

        }

}

Q9. Ans

        Your Ticket Fare is 40.0

Q10. What will be the output of the following code snippet?

class Customer{

        private int pincode;

}

class Demo{

        public static void main(String args[]){

                        Customer cust = new Customer();

                        cust.pincode=570020;

                        System.out.println("PIN:"+cust.pincode);

        }

}

Q10. Ans

        compilation error due to accessing private member

Q11. What will be the output of the following code snippet?

class Customer{

        public int pincode;

}

class Demo{

        public static void main(String args[]){

                        Customer cust = new Customer();

                        cust.pincode=570020;

                        System.out.println("PIN:"+cust.pincode);

        }

}

Q11. Ans

        PIN: 570020

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值