Object-Oriented Programming

This is a cheatsheet to brush up before attending interview.

Object: State+ Behavior.

Let’s take an example of real world object.  I see a Table Fan  infront of me 🙂

It has a state: On or Off, currentSpeed, Timer

These states can be changed through behaviors like turnOn, turnOff etc.

Software object also has state stored in “field” and behavior exposed using “methods”.

Data Encapsulation: The internal state of the objects are hidden. Methods are the only communication medium to interact with the state of an object.

Information hiding: The internal implementation details of an object remain hidden from outside world due to interactions enabled through object methods.

Class: It’s a blueprint from which objects are created.

In real world we see many objects being similar. They may share same blueprint.


Class TableFan{
   String state = "Off";
   int speed =0;
   static final MAX_SPEED=4;

   void turnOn(){
     state = "On";
   }

   void turnOff(){
     state = "Off";
   }

   void increaseSpeed(){
      if(speed<MAX_SPEED){
         speed = speed+1;
   }

   void decreaseSpeed(){
      if(speed>0)
         speed = speed-1;
   }

}

Inheritance: Different objects have certain amount of similar capabilities. OO-programming allows classes to inherit and refine common state and behavior from other classes. Java supports each class to have one direct superclass and a class can have multiple subclass.

Eg:


Class Circle extends Shape{
// Inherits from Shape class.
// Modify Shape class methods if necessary.
// Adds fields/methods particular to Circle class
}
Advertisements

Poached Egg on a toast

Easy, quick and healthy breakfast!!

Ingredients:

  • Fresh egg
  • Water
  • Salt
  • Pepper
  • Bread
  • Butter

Preparation:

Step 1: Take a wide container and fill it about 2/3 full with water. Bring the water to simmer on a medium heat.

Photo 6-3-16, 8 56 46 AM

Step 2: Take a fresh egg, crack it open and empty it into a small cup.

 

Step 3: Pour the egg into a hot simmering water. Water should not be boiling heavily.

FullSizeRender

Step 4: Turn off the heat and wait for 4-5 minutes for the egg to poach.

Wait until egg white is firm and yolk is still runny. Remove the egg using slotted spoon and place it on a paper towel/kitchen towel to pat it dry.

Step 5: Make a bread toast and spread butter on it. Place the poached egg on the bread toast.

Photo 6-3-16, 9 22 33 AM

Step 6: Sprinkle salt and pepper on the poached egg and serve it hot.

Photo 6-3-16, 9 25 04 AM.jpg

Enjoy and leave the comments after you try it out 🙂

 

Healthy Sandwich

Ingredients:

  1. red onion  – Medium size 1/2
  2. avocado – 1
  3. cucumber – 1
  4. tomato – 1
  5. bell pepper – 1
  6. salt
  7. olive oil
  8. black pepper
  9. Baby Spinach/Basil leaves
  10. Garlic – 2-3 cloves
  11. green chilli(optional) – 1-2

Step 1:

Wash the vegetables and cut them into thin slices as shown.

Step 2:

Spray olive oil. Sprinkle salt and pepper as per taste.

IMG_4794.JPG

Step3:

Grind spinach or basil leaves along with few garlic cloves and green chilli. Green chilli is optional based on your taste.

IMG_4802.JPG

Step 4:

Take 2 slices of fresh wheat bread. Apply the spinach paste on one of the slice.

IMG_4806.JPG

Arrange vegetables on it.

IMG_4827.JPG

You can sprinkle pepper and salt as per taste.

Step 5:

Sandwich is ready to have 🙂 You can grill it too using sandwich grill.

IMG_4829.JPG.jpegIMG_4822.JPG

 

 

Exporting data from Amazon RDS MySQL database to .csv or viewable in Excel sheet.

Amazon RDS do not allow to export table content to a csv file using mysql command.

select * from desired_table
INTO outfile "/path/to/csv file/file.csv"
fields terminated by ',';

gives an error: “Error Code:1045 Access denied for user ***@***.rds.amazonaws.com using password=YES”

As it said access denied I tried to grant access using:
grant all privileges
on YOUR_DATABASE.*
to 'username'@'***.rds.amazonaws.com'
identified by 'your_password';
>flush privileges;

But this did not work too.

There’s no issue from your end. MySQL creates the file on database server. It does not give permission to the client to create a file.

So a simple way to export the data to csv is by using unix sed command.
mysql -u username -p database_name -h ***.rds.amazonaws.com --port=3306 --batch -e "select * from desired_table"  |  sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > filename.csv