Sunday 3 April 2011

How to Convert Character into a String in java

import java.io.*;
import java.lang.*;
/*
This program takes a character at the console and converts it into a string format using by the toString() method. This method converts the character into an integer type data. The character class wraps a value of the primitive type char in an object. The toString method returns a string object representing this character's values
*/


public class CharToString{
public static void main(String args[])throws IOException{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Character:");
String s = read.readLine();
char c = s.charAt(0);
System.out.println("Character value is " + c);
String s1 = Character.toString(c);
System.out.println("String value is " + s1);
}
}

Output of this program.

C:\corejava>java CharToString
Enter a Character:
R
Character value is R
String value is R
C:\corejava>

How to Compare String in java

  1. /*
  2. Java String compare example.
  3. */
  4. public class JavaStringCompareExample{
  5. public static void main(String args[]){
  6. /*
  7. Java String class defines following methods to compare Java String object.
  8. 1) int compareTo( String anotherString )
  9. compare two string based upon the unicode value of each character in the String.
  10. Returns negative int if first string is less than another
  11. Returns positive int if first string is grater than another
  12. Returns 0 if both strings are same.
  13. 2) int compareTo( Object obj )
  14. Behaves exactly like compareTo ( String anotherString) if the argument object
  15. is of type String, otherwise throws ClassCastException.
  16. 3) int compareToIgnoreCase( String anotherString )
  17. Compares two strings ignoring the character case of the given String.
  18. */
  19. String str = "Hello World";
  20. String anotherString = "hello world";
  21. Object objStr = str;
  22. /* compare two strings, case sensitive */
  23. System.out.println( str.compareTo(anotherString) );
  24. /* compare two strings, ignores character case */
  25. System.out.println( str.compareToIgnoreCase(anotherString) );
  26. /* compare string with object */
  27. System.out.println( str.compareTo(objStr) );
  28. }
  29. }
  30. /*
  31. OUTPUT of the above given Java String compare Example would be :
  32. -32
  33. 0
  34. 0
  35. */

How to Split a String in java Exmple explained

  1. *
  2. Java String split example.
  3. */
  4. public class JavaStringSplitExample{
  5. public static void main(String args[]){
  6. /*
  7. Java String class defines following methods to split Java String object.
  8. String[] split( String regularExpression )
  9. Splits the string according to given regular expression.
  10. String[] split( String reularExpression, int limit )
  11. Splits the string according to given regular expression. The number of resultant
  12. substrings by splitting the string is controlled by limit argument.
  13. */
  14. /* String to split. */
  15. String str = "one-two-three";
  16. String[] temp;
  17. /* delimiter */
  18. String delimiter = "-";
  19. /* given string will be split by the argument delimiter provided. */
  20. temp = str.split(delimiter);
  21. /* print substrings */
  22. for(int i =0; i < temp.length ; i++)
  23. System.out.println(temp[i]);
  24. /*
  25. IMPORTANT : Some special characters need to be escaped while providing them as
  26. delimiters like "." and "|".
  27. */
  28. System.out.println("");
  29. str = "one.two.three";
  30. delimiter = "\\.";
  31. temp = str.split(delimiter);
  32. for(int i =0; i < temp.length ; i++)
  33. System.out.println(temp[i]);
  34. /*
  35. Using second argument in the String.split() method, we can control the maximum
  36. number of substrings generated by splitting a string.
  37. */
  38. System.out.println("");
  39. temp = str.split(delimiter,2);
  40. for(int i =0; i < temp.length ; i++)
  41. System.out.println(temp[i]);
  42. }
  43. }
  44. /*
  45. OUTPUT of the above given Java String split Example would be :
  46. one
  47. two
  48. three
  49. one
  50. two
  51. three
  52. one
  53. two.three
  54. */

How to Convert String to Character in java Array Example

  1. /*
  2. Convert String to Character Array Example
  3. */
  4. public class StringToCharacterArrayExample {
  5. public static void main(String args[]){
  6. //declare the original String object
  7. String strOrig = "Hello World";
  8. //declare the char array
  9. char[] stringArray;
  10. //convert string into array using toCharArray() method of string class
  11. stringArray = strOrig.toCharArray();
  12. //display the array
  13. for(int index=0; index < stringArray.length; index++)
  14. System.out.print(stringArray[index]);
  15. }
  16. }

  1. /*
  2. Output of the program would be :
  3. Hello World
  4. */

What is the difference between Reference and Primitive types.

What are Primitive Types?

A primitive data type is a type where the VALUE of the variable is stored to the variable name.

What are Reference Types?

A reference type is a type where a REFERENCE of the object is stored. ‘’okay, so what’s a REFERENCE? A REFERENCE is simply a location in the computer’s memory. Just think of it as an address.’’

What’s the difference?

Look at things this way… A primitive data type is like a basket of eggs. When you place 10 eggs into a basket, nothing much that you do external of the basket can affect the number of eggs in the basket. (Here’s a bunch of code that will probably scare the heck out of you if you feel that you’re not born to be a programmer. Here’s a piece of advice. Read the code, one line at a time. If you don’t understand it, read it again. Anything behind the “//” is friendly, I’m just trying to explain how you should think about each line of code. It really isn’t difficult, I promise.)

int x=10;   //10 eggs int basket=x;   //The VALUE of x is stored into the basket x=9;    //x is changed, but the basket is remains the same. System.out.println(“x=”+x);  //see! It’s 9! System.out.println(“basket=”+basket); //see! It still prints 10! 

A reference data type is simply an address. When you use an object, the value isn’t stored, but instead an address of the object is stored. Take a home address for example, 16 Jefferson Park Avenue. It’s not possible for me to put a whole house into my basket, instead, I scribble the address down on a scrap of paper, “16 Jefferson Park Ave”. From now onwards, I whenever I want to do something to 16 Jefferson Park Ave, I simply reference the address. If I wanted to know the number of people in the house, I would need to go to the house, and count the number of people in there. This is necessary since all I have is the address, it would be impossible to determine information about the house just by looking at my own basket like before.

House h = new House(10); House jeff=h; h.setNumber(9); System.out.println(“h=”+h.getNumber()); System.out.println(“jeff=”+jeff.getNumber()); 

If you’re still stuck, try to think about the different results produced by the code. Try to resolve it within yourself why the results of both tests are different. It really is quite simple.

What do I need to know?

Now that you understand the concept (or at least I hope you do), you just need to know when something is a primitive data type, and when it’s a reference. Luckily for you, I have a complete list of primitive data types here. Anything that doesn’t fall into these data types is a reference data type. Here they are.

byte, short, int, long, float, double, boolean, char


Yes. This means String is a Reference Data Type! Okay that’s all I have for now, hope it was helpful!

Source Reference: smu.edu.sg/tsc/Reference_and_Primitive_types

Write a program in C / C++ that calculate the summation of consecutive positive odd integers from 1 to n

Here is the code

#include using namespace std; int main() { int n; int sum = 0; int number; cout << "Enter the value for n: "; cin >> n; for (number=1; number<=(2*n); number+2) { sum = sum + number; } cout << "The sum of "; cout << n; cout << " is "; cout << sum << endl; return 0; }

Write a program in C to find the sum of odd numbers and even numbers from 1 to n

#include
#include

void main()
{
int n,i;
int addeven=0;
int addodd=1;

printf("\nEnter a number\n");
scanf("%d",&n);
fflush(stdin);

for(i=2;i<=n;i++)
{
if(i%2==0)
addeven+=i;
else
addodd+=i;
}

printf("\nThe sum of odd numbers is %d \nThe sum of even numbers is %d\n",addeven,addodd);
}


write a program to find the sum of first n odd integers

/* write a program to add first seven series using for loop
Name: Mohammad Tahir
Date: 4 April 2011 */

#include
#include
void main()
{
int sum=0;
int i,n;
printf("Enter the term: ");
scanf("%d",&n);
for(i=1; i<=n; i=i+2)
{
sum=sum+i;
}
printf("the sum of odd numbers is %d",sum);
getch();
}

Tuesday 29 March 2011

Advanced Algorithm Analysis and Design ( Click to Download Power Point Lectures) Lecture 1 (External Memory Algorithms and Data Structures: B-tree)

Advanced Algorithm Analysis and Design

( Click to Download Power Point Lectures)

Lecture 1 (External Memory Algorithms and Data Structures: B-tree)

Slides:

[power point] [pdf]

Lecture 2 (External Memory Algorithms and Data Structures: Sorting)

Slides:

[power point] [pdf]

Lecture 3 (Text Searching Algorithms)

Slides:

[power point] [pdf]

Lecture 4 (Text Searching Data Structures)

Slides:

[power point] [pdf]

Lecture 5 (Greedy Algorithms)

Slides:

[power point] [pdf]

Lecture 6 (Dynamic Programming)

Slides:

[power point] [pdf]

Lecture 7 (Advanced Graph Algorithms: All-Pairs Shortest Paths)

Slides:

[power point] [pdf]

Lecture 8 (Advanced Graph Algorithms: Maximum Flow)

Slides:

[power point] [pdf]

Lecture 9 (Computational Geometry Algorithms)

Slides:

[power point] [pdf]

Lecture 10 (Computational Geometry Algorithms)

Slides:

[power point] [pdf]

Lecture 11 (Computational Geometry Data Structures: Range Searching)

Slides:

[power point] [pdf]

Lecture 12 (Amortized Analysis)

Slides:

[power point] [pdf]

Lecture 13 (Coping with NP-completeness: Approximation Algorithms)

Slides:

[power point] [pdf]

Lecture 14 (Coping with NP-completeness: Backtracking and Branch-and-Bound)

Slides:

[power point] [pdf]

Advanced Data structures and Algorithms Click to Download PPT Size 00. Prelims.ppt 355K 01. Intro Algs.ppt 847K 02. Generics.ppt 292K 03. Sort

Advanced Data structures and Algorithms

Click to Download PPT
Size
355K
847K
292K
953K
1.8M
1.1M
1.8M
1.3M
568K
858K
952K
681K
2.1M
2.1M
484K
1.9M
0

ADVANCED ALGORITHMS lectures tutorial


Course Description

Algorithm design and analysis is a fundamental and important part of computer science. This course introduces students to advanced techniques for the design and analysis of algorithms, and explores a variety of applications.

The topics and applications that we plan to cover include hashing, bloom filters, scheduling, network design, online load balancing, algorithms in machine learning, boosting (in the context of learning), Markov chains and the MCMC method, byzantine agreement, internet algorithms, and nearest neighbor algorithms. Enroute, we will encounter various useful ideas, including randomization, probabilistic analysis, amortized analysis, competitive analysis, eigenvalues, linear and semi-definite programming, high dimensional geometry, and random walks.

Download Lectures in PDF

1. 09/05 W [PDF] Intro, greedy algorithms: scheduling, MST. (K&T §4, §5)
2. 09/07 F [PDF] Set cover, Divide & Conquer, Dynamic programming. (K&T §5, §6, §11.3)
3. 09/10 M [PDF] Dynamic programming. (K&T §6)
4. 09/12 W [PDF] Network flow. (K&T §7)
5. 09/14 F [PDF] Network flow applications, matchings. (K&T §7)
6. 09/17 M [PDF] Randomized algorithms, Karger's min-cut algorithm. (K&T §13)
         Here are some lecture notes by Avrim Blum on how to speed-up Karger's algorithm.
7. 09/19 W [PDF] Randomized load balancing and hashing. (K&T §13.10, §13.6, M&R §8.4, §8.5)
8. 09/21 F [PDF] Bloom filters, NP-completeness. (M&R §8.4, §8.5, M&U §5.5)
         See also, this survey on the applications of bloom filters by Broder & Mitzenmacher.
9. 10/01 M [PDF] NP-completeness contd., Approximation algorithms. (K&T §8, Vaz. §1)
10. 10/03 W [PDF] Approximation via local search.
11. 10/08 M [PDF] Linear programming, LP rounding. (Vaz. §14)
12. 10/10 W [PDF] Randomized rounding, concentration bounds. (M&R §3.2, §4.1, §4.2)
13. 10/15 M [PDF] Randomized rounding (contd.), LP duality. (M&R §4.2, Vaz. §12)
14. 10/17 W [PDF] LP duality, Primal-dual algorithms. (Vaz. §12, 15)
15. 10/19 F [PDF] Primal-dual algorithms. (Vaz. §15)
16. 10/22 M [PDF] Semi-definite Programming. (Vaz. §26)
17. 10/24 W [PDF] SDP (contd.), Streaming algorithms.
18. 10/26 F [PDF] Streaming algorithms (contd.).
         See this survey by Muthu Muthukrishnan for some motivation behind, and math used in, streaming algorithms.
19. 10/29 M [PDF] Online algorithms & competitive analysis. (B&E-Y §1)
         Here is a nice presentation by Pat Riley & Elly Winner about different approaches to evaluating online algorithms.
20. 10/31 W [PDF] Caching/Paging, k-server problem. (B&E-Y §3, §4, §10)
21. 11/05 M [PDF] Caching lower bound based on Yao's principle, Work function algorithm. (B&E-Y §8.4, §10, §12)
         For a complete analysis of the work function and other k-server algorithms, see these detailed lecture notes (lectures 5-9) by Yair Bartal.
22. 11/07 W [PDF] Work function (contd.), Online learning: regret minimization & the weighted majority algorithm.
23. 11/12 M [PDF] Mistake bound model, winnow & perceptron algorithms.
24. 11/14 W [PDF] MB model contd., PAC model. (K&V §1, §2)
25. 11/16 F [PDF] PAC model, Occam's razor. (K&V §1, §2)
26. 11/19 M [PDF] Boosting in the PAC framework. (K&V §4)
27. 11/26 M [PDF] Random Walks & Markov chains. Cover time, hitting time. (M&R §6)
28. 12/07 F [PDF] Random Walks & Markov chains: the resistance method, and mixing time. (M&R §6)