Find pairs of elements in Array whose sum

Find pairs of elements in Array whose sum


Four Principles of OOP

 

·         Inheritance
2.) Polymorphism
3.) Encapsulation
4.) Abstraction

 

Inheritance

 

·         The concept that when a class of objects is defined, any subclass that is defined can inherit the properties and functionalities of the class

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another.

 

Polymorphism

 

·         The concept of presenting an interface for differing data types
Declare a uniform interface that isn't type aware
Ability of an object to take on many forms

 

Encapsulation

 

·         There's a lot of details that are common to adding to a database, but you don't want to be concerned with coding them every time you add. Encapsulating those details such that they are accessed through a simpler method (say, Add(T type)) is one way to achieve good code-reuse

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as as single unit.

 

Abstraction

 

·         Abstraction is the act of representing essential features without including the background details or explanations.
the abstraction principle is used to reduce complexity and allow efficient design and implementation of complex software systems.
Abstraction is a process process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words user will have the information on what the object does instead of how it does it

 

Static & Dynamic Binding

 

·         Static - binding occurs at compile time
Dynamic - binding occurs at runtime

 

MVC

 

·         Model - part of the application that handles logic for the data we are presenting
View - part of the application that handles display of data
Controller - part of the application that handles user interaction
This process allows you to break up complex applications & focus on one aspect at a time

 

CRUD

 

·         -Create
-Read
-Update
-Delete

 

Create Table

 

·         CREATE TABLE person(
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER);

 

Insert

 

·         INSERT INTO person(id,name,age) VALUES(1,"dylan", 21);
INSERT OR REPLACE INTO..

 

Select

 

·         SELECT * FROM person;
SELECT name, age FROM person;
SELECT name, age FROM person WHERE dead=0;

 

Select from multiple tables

 

·         SELECT pet.id, pet.name, FROM pet, person WHERE pet.id = person.id;

 

Select (conditions)

 

·         SELECT DISTINCT genre FROM movies;
SELECT * FROM movies WHERE name LIKE 'a%';
SELECT * FROM movies ORDER BY rating DESC LIMIT 3;
SELECT * FROM movies ORDER BY rating ASC LIMIT 3;
SELECT SUM(movies) FROM movies;
SELECT MIN/MAX(movies) FROM movies;

 

Alter

 

·         ALTER TABLE thing ADD COLUMN name TEXT;
ALTER TABLE thing DROP COLUMN name;
ALTER TABLE person RENAME TO persons;

 

Delete

 

·         DELETE FROM pet WHERE dead=1;

 

BigO Notation

 

·         allows us to say how the size of inputs affect the runtime of a program

 

BigO

 

·         O(1) - constant
O(log n) - logarithmic
O(n) - linear
O(n log n) -
O(n^2) - quadratic
O(2^n) - exponential
O(n!) - factorial

 

Left Join

 

·         SELECT * FROM table1 LEFT JOIN table2 on table1.id=table2.id;

 

Right Join

 

·         SELECT * FROM table1 RIGHT JOIN table2 on table1.id=table2.id;

 

Inner Join

 

·         SELECT * FROM table1 INNER JOIN table2 on table1.id=table2.id;

 

Outer Join

 

·         SELECT * FROM table1 FULL OUTER JOIN table2 on table1.id=table2.id;

 

FizzBuzz

 

·         public int fizzBuzz(int n){
if ((n%3==0) && (n%5==0)){
System.out.print("FizzBuzz");
}else if (n%3==0){
System.out.print("Fizz");
}else if (n%5==0){
System.out.print("Buzz");
}else{
System.out.print(n);
}
}

 

Find pairs of elements in Array whose sum==a given number

 

·         public void findPairs(int input[], int num){
for(i=0; i<input.length; i++){
for(j=i+1; j<input.length; j++){
if(input[i]+input[j]==num){
return input[i], input[j];
}
}

 

Fibonacci

 

·         public int fibonacci(int n){
if(n==0){
return 0;
}else if(n==1){
return 1;
}else{
return fibonacci(n-1) + fibonacci(n-2);
}
}

 

 

Find two max numbers in an array

 

·         public void maxNumbers(int input[]){
int maxOne, maxTwo;
for(int i =0; i<input.length; i++){
if(maxOne < input[n]){
maxTwo = maxOne;
maxOne = input[n];
}else if(maxTwo < n){
maxTwo = input[n];
}
return maxOne, maxTwo;
}
------------------------------------------------
for(int n : input){
if(max<n){
maxTwo= maxOne;
maxOne = n;
}else if (maxTwo<n){
maxTwo = n;
}

 

 

Reverse a number

 

·         public int reverseNum(int n){
int result = 0;
int rem = 0;
while(n>0){
rem = n%10;
n = n/10;
result = result * 10 + rem;
return result;
}
}

 

Find common elements between two arrays

 

·         public void commonElements(int arr1[], int arr2[]){
HashSet<String> set = new Hashset<String>();
for(int i =0; i<arr1.length; i++){
for(int j =0; j<arr2.length; j++){
if(arr1[i]==arr2[j]){
set.add(arr1[i]);
}
}
return set;
}
}

 

Reverse a string

 

·         public class StringReverse{
String reverse = "";
public String reverseString(String str){
if(str.length==1){
return str;
}else{
reverse += str.charAt(str.length-1) + reverseString(str.substring(0, str.length-1));
}
return reverse;
}
}

 

 

Duplicate in array

 

·         public void duplicateCase(int input[]){
Hashset<String> set = new Hashset<String>();
for(int n: input){
if(!set.add(n)){
System.out.print(n);
}
}
}

 

Factorial

 

·         public int factorial(int n){
if(n==1){
return 1;
}else{
return n * factorial(n-1);
}
}

 

Abstract Class

 

·         A common use of abstract classes is to provide an outline of a class similar like an interface does. But unlike an interface it can already provide functionality, i.e. some parts of the class are implemented and some parts are just outlined with a method declaration. ("abstract")

http://stackoverflow.com/questions/1320745/abstract-class-in-java

A class must be declared abstract when it has one or more abstract methods. A method is declared abstract when it has a method heading, but no body - which means that an abstract method has no implementation code inside curly braces like normal methods do.

 

Abstract class vs interface

 

·         class that implements interface must implement all the method headings listed in interface definition

http://www.javatpoint.com/difference-between-abstract-class-and-interface

 

Stack

 

·         http://www.tutorialspoint.com/java/java_stack_class.htm

 

 

Answer Detail

Get This Answer

Invite Tutor