Friday, March 11, 2011

JAVA BAT : String 1 : firstTwo

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

First Two Problem: Given a string, return the string made of its first two chars, so the String "Hello" yields "He". If the string is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string "" yields the empty string "". Note that str.length() returns the length of a string.
Examples:
firstTwo("Hello") → "He"
firstTwo("abcdefg") → "ab"
firstTwo("ab") → "ab"

Solution for this JavaBat firstTwo problem:


public String firstTwo(String str) {          

     if(str.length()<2){
         return str;
     }
     else{
         return str.substring(0,2);
     }

}

Tuesday, February 22, 2011

JAVA BAT : String 1 : extraEnd

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

Extra End Problem : Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.
Examples:
extraEnd("Hello") → "lololo"
extraEnd("ab") → "ababab"
extraEnd("Hi") → "HiHiHi"

Solution for this JavaBat extraEnd problem:

public String extraEnd(String str) {

  String lastchars = str.substring(str.length()-2,str.length());
  return lastchars+lastchars+lastchars;

}


Tuesday, February 1, 2011

JAVA BAT : String 1 : Make Out Word

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

Make Out Word Problem:Given an "out" string length 4, such as "<<>>", and a word, return a new string where the word is in the middle of the out string, e.g. "<>". Note: use str.substring(i, j) to extract the String starting at index i and going up to but not including index j.
Examples:
makeOutWord("<<>>", "Yay") → "<<Yay>>"
makeOutWord("<<>>", "WooHoo") → "<<WooHoo>>"
makeOutWord("[[]]", "word") → "[[word]]"



In this problem we have a method named makeOutWord with two string parameter. The first parameter is a length 4 string and the second string parameter will come in the middle of the first string or in other words left half of the first string will come before the second word and the right half of first string will come after the second word. 

Solution for this JavaBat makeOutWord problem:


    public String makeOutWord(String outside, String inside)
    {

      String left = outside.substring( 0, 2 );
      String right = outside.substring( 2, 4 );
      String str = left + inside + right;
      return str;
    }



This code can also be written in this way:


    public String makeOutWord(String outside, String inside)
    {
      String str = outside.substring( 0, 2 ) + inside 
           + outside.substring( 2, 4 );
      return str;
    }


Sunday, January 16, 2011

JAVA BAT : Array 1 : SameFirstLast

In Array 1 section, these are simple Array based problems in which there is no use of loops to solve the problems.

Same First Last Problem :Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are the same.

sameFirstLast({1, 2, 3}) → false
sameFirstLast({1, 2, 3, 1}) → true
sameFirstLast({1, 2, 1}) → true

To solve this problem first we check weather the array given is of length greater then zero or not. To check this we use the method Array.length (For strings the method is String.length() ). Then we will check for the first and last element, are they same.

So the code for this problem: 


           public boolean sameFirstLast ( int[] nums ) {
 
                   if ( nums.length>0 ){
                             if ( nums[0]==nums[nums.length-1] ){
                                         return true;
                             }else{
                                       
return false;
                            }
                   }
                   else{
                            return false;
                   }

          }

Java Bat : Array 1 : FirstLast6

In Array 1 section, these are simple Array based problems in which there is no use of loops to solve the problems.

First Last 6 Problem : Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.

firstLast6({1, 2, 6}) → true
firstLast6({6, 1, 2, 3}) → true
firstLast6({3, 2, 1}) → false

In this problem an array of ints is passed to a method firstLast6 and if 6 appears as either the first or last element in the array the firstLast6 method will return true else it will return false.

To check weather first and last element is 6 or not.


                    nums[0]==6 || nums[nums.length-1]==6


So the Answer for this Java Bat Problem:


           public boolean firstLast6( int[] nums ) {
                   
                        if ( nums[0]==6 || nums[nums.length-1]==6 ){
                                      return true;
                        }
                        else{
                                     return false;

                        }
          }

Saturday, January 15, 2011

JAVA BAT : Array 1 Problems

In Array 1 section, these are simple Array based problems in which there is no use of loops to solve the problems. To solve these problems we should use simple array methods for example:

arr[0], arr[1].......a[n] to access the elements of array.
arr.length to get the number of elements in array.
int[] arr = int[n] to initialize array of n integers (n is length for array)
int[] num = {1,2,3,4};  to initialize array in which element of array will be 1,2,3 and 4

Array 1 Problems:
 firstLast6 sameFirstLast makePi
 commonEnd sum3 rotateLeft3
 reverse3 maxEnd3 sum2
 middleWay makeEnds has23
 no23 makeLast double23
 fix23 start1 biggerTwo
 makeMiddle plusTwo swapEnds
 midThree maxTriple frontPiece
 unlucky1 make2 front11

Thursday, January 13, 2011

JAVA BAT : String 1 : Make Tags

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

Make Tags Problem: The web is built with HTML strings like "Yay" which draws Yay as italic text. In this example, the "i" tag makes and which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word, e.g. "Yay".
Examples:
makeTags("i", "Yay") → "<i>Yay</i>"
makeTags("i", "Hello") → "<i>Hello</i>"
makeTags("cite", "Yay") → "<cite>Yay</cite>"

In this problem two words are passed as parameter to a method makeTags, one is 'tag' and other is 'word'. The makeTags method should return a string formatted in a way that the tag is around the word as like a HTML tag.

To solve this problem first we have to create a HTML tags from the given 'tag' string.  For this we will create a start tag and an end tag.


                    String starttag = "<"+tag+">";
                    String endtag =
"</"+tag+">";
 

Now we have to place these tags around the words.


                    String str = starttag+word+endtag;

So the Answer for this Java Bat Problem:

           public String makeTags(String tag, String word) {
                   
                           String starttag="<"+tag+">"; 
                           String endtag="</"+tag+">";
                           String str= starttag+word+endtag;
                           return str;

          }
 

Tuesday, January 11, 2011

JAVA BAT : String 1 : makeAbba

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(),  str.substring(i, j) etc.

Make ABBA Problem: Given two strings, a and b, return the result of putting them together in the order abba.
Examples:
makeAbba("Hi", "Bye") → "HiByeByeHi"
makeAbba("Yo", "Alice") → "YoAliceAliceYo"
makeAbba("x", "y") → "xyyx"

In this problem a method that has two string as parameter for example string A and B should return a string that is in the order ABBA. For example 'Hi' and 'Bye' are passed as parameter then the result should be 'HiByeByeHi'.

Answer For this Java Bat problem:


                   public String makeAbba(String a, String b) {
                                      String str=a+b+b+a;
                                      return
str;
                   }
 
  
Above Code can also be written in this way:


                   public String helloName(String a, String b) {

                                          
 return a+b+b+a;

                   }

   
 

JAVA BAT : String 1 : Hello Name

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

Hello Name Problem: Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".

Examples :
helloName("Bob") → "Hello Bob!"
helloName("Alice") → "Hello Alice!"
helloName("X") → "Hello X!"

So, in this problem a string is passed to a method helloName and that method should return a string that have a 'Hello ' concatenated at front and '!' concatenated at the end. As Shown in Examples above. 

Answer For this Java Bat problem:


                         public String helloName(String name) {
                                           name= "Hello "+name+"!";
                                         
 return name;
                         }
 
 
Above Code can also be written in this way:


                         public String helloName(String name) {

                                         
 return "Hello "+name+"!";

                         }

   

JAVA BAT : String 1 Problems

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i, j) etc.

JAVA BAT String 1 Problems:

 helloName makeAbba makeTags
 makeOutWord extraEnd firstTwo
 firstHalf withoutEnd comboString
 nonStart left2 right2
 theEnd withouEnd2 middleTwo
 endsLy nTwice twoChar
 middleThree hasBad atFirst
 lastChars conCat lastTwo
 seeColor frontAgain minCat
 extraFront without2 deFront
 startWord withoutX withoutX2

Welcome To Java Bat Answers

Hi, Welcome to Java Bat Answers. Need help with JavaBat, so you are in the right place. This is a blog where you will get all Java Bat Answers. Java Bat is a very good website which has good Java Coding Problems. Java bat has changed its name to coding bat when it introduced python programming. This blog contains answers of Java section of coding bat.