Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 28 September 2014

Utopian Tree Program in java

Problem Statement:

        The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during the monsoon, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter.
        Now, a new Utopian tree sapling is planted at the onset of the monsoon. Its height is 1 meter. Can you find the height of the tree after N growth cycles?

Input Format
The first line contains an integer, T, the number of test cases.
T lines follow. Each line contains an integer, N, that denotes the number of cycles for that test case.

Constraints
1 <= T <= 10
0 <= N <= 60

Output Format
For each test case, print the height of the Utopian tree after N cycles.

Sample Input #00:
2
0
1
Sample Output #00:
1
2

Explanation #00:
        There are 2 test cases. When N = 0, the height of the tree remains unchanged. When N = 1, the tree doubles its height as it's planted just before the onset of monsoon.

Sample Input: #01:
2
3
4
Sample Output: #01:
6
7
Explanation: #01:

There are 2 testcases.
N = 3:
the height of the tree at the end of the 1st cycle = 2
the height of the tree at the end of the 2nd cycle = 3
the height of the tree at the end of the 3rd cycle = 6
N = 4:
the height of the tree at the end of the 4th cycle = 7

Saturday, 15 March 2014

3 dot (…) method or Varargs in Java


To see explanation please click here

Varargs was added in Java 5 and the syntax includes three dots ()

         These functionality enables to write methods/functions which takes variable length of arguments. For example the popular print() method in Java. We can call print() method with multiple arguments.Some time we have a scenario that one method can take variable number of argument  and now with varargs from language makes it much easier. In this Java tutorial we will see How variable arguments makes it easy to write convenient method which can accept any number of arguments,  perfect candidates are sum() and average() kind of method

Following is the syntax of vararg method.


Syntax:- 

            return_type MyMethod(type...Variable_name)


Notice the dots ... in above code. That marks the last argument of the method as variable argument. Also the vararg must be the last argument in the method.
It means that zero or more String objects (or an array of them) may be passed as the parameter(s) for that function.


Important Note 1: 

The parameter(s) passed in this way is always an array - even if there's just one. Make sure you treat it that way in the method body.

Important Note 2: 

 The parameter that gets the ... must be the last in the method signature. So, myMethod(int i, String... strings) is okay, but myMethod(String... strings, int i) is not okay.


Program:-

Program
Program for 3 dot (...) method













 

Output
Output of Above program
            In above code, the add() method is taking variable arguments and is being called from main method with number of arguments. In its every call we use different arguments. This can be done with the help of method overloading up to some extent. But 3 dot (…) method is best solution for it. By using this type of method you can implement addition, subtraction, multiplication, division,etc.
To see the explanation of above program please see video. It helps you to understand the concept easily.


You are always welcome to ask any doubts if you have. Thanks.

Tuesday, 11 March 2014

How to Compile & Run Java Program (.java File) using Command Prompt

There are total FOUR steps in execution of Java Program. These are as follows:-

1. Download jdk:-

 First you have to download jdk i.e. Java Development Kit. You can download this By using link.
Select your Operating System i.e OS type. Then start downloading. 


2. Installing jdk:-

 Install jdk by using the default setting.

3. Write Java Program:-  

Write Java Program which you want to execute. You can use any text editor to write Java Program. Then save that program with .java extension at your desired place. (Eg. Suppose you write java program to print Hello. And save that program with any File_name.java where File_name is name of class in java in which your main( ) method exist.) You must follow all rules while naming .java file. Because file name can't start with digit, it should not be keyword, it mus start with Letter, We can use _,etc.
In the below example I've save my Sample.java file at D:\nik.
Save .java File


4. Compile & Run .java File:-

To compile Sample.java we have to start command prompt in windows(terminal in ubantu).Then go to the path where we save program i.e. D:\nik in my case.
Then set path for java environment variable. For this Perform following steps
i. Go to C:\
ii.Go to Program Files
iii. Go to Java
 iv. Go to your jdk version (in my case it is jdk1.7.0_03)
v. Go to bin
Then copy path i.e.  C:\Program Files\Java\jdk1.7.0_03\bin
 then use following command in command prompt:
D:\nik>set path="C:\Program Files\Java\jdk1.7.0_03\bin";
then compile program by using javac File_name.java; 
D:\nik>javac Simple.java
If you compile successfully then .class file is created at D:\nik folder. 
Then execute that program by using java File_name; 
D:\nik>java Simple;
You can see OutPut. Please see following screen shot of my command prompt.
 
Screen Shot of Command Prompt



 Please give feedback. Ask doubts. Thank You.