Friday 2 October 2015

Basic puzzle in java programming #1

In this program the below scenario will be implemented.

Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged. Note: use .equals() to compare 2 strings.

notString("hi") ? "not hi"
notString("a") ? "not a"
notString("not bad") ? "not bad"

 /* Program to print
 * "hi" to "not hi"
 *  "String to not String"
 */

package in.blogspot.java2bigdata;

public class addNotToString {
 public String notString(String str) {
    if (str.length() >= 3 && str.substring(0, 3).equals("not")) {
      return str;
    }
    
    return "not " + str;
  }
 public static void main(String[] args){
  addNotToString word = new addNotToString();
  
  System.out.println(word.notString("Hi"));
  System.out.println(word.notString("good"));
  System.out.println(word.notString("not good"));
  System.out.println(word.notString("is not"));
  
 }

 }


click here for star pattern in java programming #1
click here for star pattern in java programming #2

Star pattern in java programming #3

In the program the following star pattern will be printed.

Diamond pattern:


Click here for Star pattern in java programming #1

Click here for Star pattern in java programming #2

This program will accept a number as input. The loops are used to build a diamond pattern of *.

/* 
* Diamond star pattern
**/
package com.blogspot.java2bigdata;

import java.io.*;
import java.lang.*;
import java.util.*;
 
class DiamondPattern{
      static public int ReadInteger()
      {
            try
            {
                  String inpString = "";
                  InputStreamReader input = new InputStreamReader(System.in);
                  BufferedReader reader = new BufferedReader(input);
                  String s = reader.readLine();
                  return Integer.parseInt(s);
            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
            return -1;
      }
 
      public static void main(String[] args)
      {
            System.out.println("Enter the number till you want a star (*) diamond pattern.");
            
            int n = ReadInteger();
           
            System.out.println("\nHere is the Diamond of Stars\n");
 
            for (int i = 1; i <= n; i++)
            {
                  for (int j = 0; j < (n - i); j++)
                        System.out.print(" ");
                  for (int j = 1; j <= i; j++)
                        System.out.print("*");
                  for (int k = 1; k < i; k++)
                        System.out.print("*");
                  System.out.println();
            }
 
            for (int i = n - 1; i >= 1; i--)
            {
                  for (int j = 0; j < (n - i); j++)
                        System.out.print(" ");
                  for (int j = 1; j <= i; j++)
                        System.out.print("*");
                  for (int k = 1; k < i; k++)
                        System.out.print("*");
                  System.out.println();
            }
 
            System.out.println();
      }
}

Output:


Thursday 1 October 2015

Star pattern in java programming #2

In the program the following star pattern will be printed.

 Click here for Star pattern in java programming #1

Output:

* * * * * * * * *
   * * * * * * * *
      * * * * * * *
         * * * * * *
            * * * * *
               * * * *
                  * * *
                     * *
                        *


/* Star pattern
 * 
 * */

package in.blogspot.java2bigdata;

import java.util.Scanner;  
    
public class StarPattern {  
       public static void main(String[] args)  
       {  
            int number; 
            System.out.println("Enter the number till you want a star pattern");  
            Scanner scan = new Scanner(System.in);  
            number = scan.nextInt();  
    
            label:     for(int i=0; i<number; i++)  
                 {  
                      for(int k=i; k>0; k--)  
                     {  
                           System.out.print(" ");  
                      }  
                      for(int j=0; j<number-i; j++)  
                      {  
                           System.out.print("*");  
                      }  
                     //for jump to next line  
                      System.out.println("");  
                 }  
       }  
  } 

At first, we get the number where we want to print the star pattern from the user with the use of Scanner class and store that number in variable number then we make a loop which starts from 0 till number-1.

Inside of that loop there is another loop which is used for giving spaces from left side equal to the line number.

There is one more loop inside the first loop which takes values from 0 to number-1 and used for printing stars and then there is a println statement which is used for going to the next line.