Skip to main content

Posts

Showing posts from May, 2012

Knapsack Algorithm Shortcut Method

The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items. The main goal of Knapsack  is: You have a knapsack that has capacity (weight) W. You have several items I1,…,In. Each item Ij has a weight wj and a benefit bj.You want to place a certain number of copies of each item Ij in the knapsack so that: The knapsack weight capacity is not exceeded and The total benefit is maximal. There is a Shortcut Method  for Knapsack Algorithm , see the video:  

Java Program for finding Palindrome

Just follow the program to find Palindrome  in java: import java.io.BufferedReader; import java.io.InputStreamReader; public class Palindrome {     public static void main(String[] args) throws Exception {    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));      String s1 = new String();          s1 =br.readLine();          s1 = s1.toLowerCase();     String s2 = new StringBuffer(s1).reverse().toString();          if(s1.equals(s2)){         System.out.println("Palindrome");     }else          System.out.println("Not Palindrome");      } }

Read or input form user in Java using BufferedReader

 BufferedReader use to read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In Java  BufferedReader is a efficient method to read an input. Here given a short example to read a string from user and print it: package palindrome; import java.io.BufferedReader; import java.io.InputStreamReader; public class  inputExample   {     public static void main(String[] args) throws Exception {    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));     String s = new String();       s =br.readLine();     System.out.println(s);     } }

Difference between Scanner vs. BufferedReader

BufferedReader Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. Where Scanner is a simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and does not do any special parsing. In currently latest JDK6 release/build, the Scanner has a littler buffer (1KB char buffer) as opposed to the BufferedReader (8KB byte buffer), but it's more than enough.As to the choice, use the Scanner if you want to parse the file, use th...

Difference Between Function and Method

Each language has its own lexicon of terms with special meaning. In C Programming Language, the word Function means a program routine. In Java, the term Function does not have any special meaning. Whereas Method means one of the routines that forms the implementation of a class. First let we know what is Function? A function is a piece of reuseable code that is called by name. In C or C++ we used to say function to a reusable code. It can be passed data to operate on (arguments / parameters) and can optionally return a result. Function is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code. So What is Method?? A method  is a piece of code that is called by name that is associated with an object. A method is almost identical to a function but it's purpose is generally to operate on the data contained within the component (class). Method is a function that is a member of a class. ...