Java Queries Here..

thetechfreak

Legend Never Ends
There is any site like codecademy for Java ? where I can learn Java interactively doing coding.

- - - Updated - - -

Anybody? [MENTION=47611]furious_gamer[/MENTION] [MENTION=10170]JGuru[/MENTION]
Courses on coursera and edX are actually quite good. You should try those when they start.
 
OP
furious_gamer

furious_gamer

Excessive happiness
Most of courses are totally for beginners like explaining concept of if/else, array etc. I don't want that cause i'm shifting from C/C++.

I thought you wanted to do Java Coding online while learning. Silly me... I learn java by reading Head First and I suggest you do the same.
 

jacobsons

New to this forum
Hi Furious Gamer,

You can try here to solve your problem I hope you will get better solution here Brave Little Coder | Learn how to program in C# and Java
 
Code:
public class test implements Runnable{
            public void run(){
            for(int i=1;i<6;i++){
                System.out.println(" "+i);
                try{
                    Thread.sleep(500);
                }catch(InterruptedException E){
                    System.out.println("Thread Interrupted");
                }
                
            }
            System.out.println("Run one is terminated");
        }


    public static void main(String agrs[]){
    test obj = new test();
    Thread objT = new Thread(obj);
    objT.start();
    try{
    objT.join(); 
    }
    catch(InterruptedException e){
        
    }
    System.out.println("Main one is terminated");
    }
    
}

I'm don't get these two things threading in java.
1). When I have to run class which implement runnable in Java. In main to start run method first I have to create class obj (i.e test obj).
Then I have pass it to thread constructor(i.e Thread objT = new Thread(obj)). Why I can't run it directly.

2). What is use of join statement?
I use it here so in program Main class wait till child class i.e test finish its execution.
And why we have to use catch Exception method in join statement
 

tkin

Back to school!!
Code:
public class test implements Runnable{
            public void run(){
            for(int i=1;i<6;i++){
                System.out.println(" "+i);
                try{
                    Thread.sleep(500);
                }catch(InterruptedException E){
                    System.out.println("Thread Interrupted");
                }
                
            }
            System.out.println("Run one is terminated");
        }


    public static void main(String agrs[]){
    test obj = new test();
    Thread objT = new Thread(obj);
    objT.start();
    try{
    objT.join(); 
    }
    catch(InterruptedException e){
        
    }
    System.out.println("Main one is terminated");
    }
    
}

I'm don't get these two things threading in java.
1). When I have to run class which implement runnable in Java. In main to start run method first I have to create class obj (i.e test obj).
Then I have pass it to thread constructor(i.e Thread objT = new Thread(obj)). Why I can't run it directly.

2). What is use of join statement?
I use it here so in program Main class wait till child class i.e test finish its execution.
And why we have to use catch Exception method in join statement
1. As Runnable is an interface, you cannot initialize it. But if you implement Runnable in your class you need to initialize your class to start a thread. Your class contains the main method which is Static, so you cannot refer to your test class object using 'this' i.e you cannot write this.start as its a Static method. Hence you need to initialize your test class object in the main method.

But you can do this to control your main class thread.

Code:
public class ThreadTest extends Thread {

	public static void main(String agrs[]){

		for(int i=1;i<6;i++){
			System.out.println(" "+i);
			try{
				Thread.sleep(500);
			}catch(InterruptedException E){
				System.out.println("Thread Interrupted");
			}

		}
		System.out.println("Main thread is terminated");
	}

}

In java, the class that contains the main method is loaded by the classloader and executes in jvm. It's thread, i.e the main thread is always started by jvm, from this thread the child threads, i.e your threads are spawned. So you can control this thread by directly calling Thread.sleep, but as mentioned above, you need to initialize the object if you want to spawn a child thread and control it, as in your example.

PS: You need to implement run() if you implement Runnable but since I am extending Thread class I don't need to do that.

2. If you don't use join then this is the output:
Code:
Main one is terminated
 1
 2
 3
 4
 5
Run one is terminated

Here what happens, child thread is spawned that executes the run method. It goes on, but your main method does not wait, it finishes execution, meanwhile your child thread goes on to execute the rest of the operations asynchronously.
 

phrick

Broken In
I have two questions about Java:
1) It is said that standard java is not needed in Android app developent, which Java is used then?
Any free tutorials...?
And
2) Can python be used instead of Java for Android app developement? Jython?
Please do reply...
 
You need to learn Basic Java syntax and OOP concepts for Android Development.
For Java and Android Tutorials try channel TheNewBoston on youtube.
 

thetechfreak

Legend Never Ends
I have two questions about Java:
1) It is said that standard java is not needed in Android app developent, which Java is used then?
Any free tutorials...?
And
2) Can python be used instead of Java for Android app developement? Jython?
Please do reply...

Nope. Java knowledge is required.

Also, an online course is starting at coursera(android app developement) and yeah it's free :)
 
OP
furious_gamer

furious_gamer

Excessive happiness
I have two questions about Java:
1) It is said that standard java is not needed in Android app developent, which Java is used then?
Any free tutorials...?
And
2) Can python be used instead of Java for Android app developement? Jython?
Please do reply...

1. Android framework is built with the help of Java, and to know Android, you should have some understanding in Java(not per se), but an OOPS language. But knowing Java will surely help.
2. That's bullshit. To develop Andriod, you need Android framework/language. Jython/Python et al used for other purposes. Ex, if you want to create an backend REST service for your android application, you can do so by Python. but not your Android app.

You have Google, the best friend of any developer, so go try that before asking these questions.(Not meant to be rude, but you should preach this before you become just another developer)
 

phrick

Broken In
1. Android framework is built with the help of Java, and to know Android, you should have some understanding in Java(not per se), but an OOPS language. But knowing Java will surely help.
Ok. Thank you!
2. That's bullshit. To develop Andriod, you need Android framework/language. Jython/Python et al used for other purposes. Ex, if you want to create an backend REST service for your android application, you can do so by Python. but not your Android app.

You have Google, the best friend of any developer, so go try that before asking these questions.(Not meant to be rude, but you should preach this before you become just another developer)

I did google but the result answers are pretty vague. makeuseof.com says standard java not need but helps to know before-hand while android dev courses just start abruptly without going into teaching java. Sorry but still I am not sure which Java to learn (if there are more than one).
Also, I was studying python from May 2014 so thought maybe it might be possible to make do with it, as there flavours of it like ironPython, jython (java implementation in Python); sorry if I hurt your feelings but I really am a noob. Sorry again.
 
OP
furious_gamer

furious_gamer

Excessive happiness
Ok. Thank you!


I did google but the result answers are pretty vague. makeuseof.com says standard java not need but helps to know before-hand while android dev courses just start abruptly without going into teaching java. Sorry but still I am not sure which Java to learn (if there are more than one).
Also, I was studying python from May 2014 so thought maybe it might be possible to make do with it, as there flavours of it like ironPython, jython (java implementation in Python); sorry if I hurt your feelings but I really am a noob. Sorry again.

:D Hurt feelings? lulz.

BTW there is just one java. Just this one.

And I get it, since you are from Python, learning Java is somewhat difficult for you. So, just learn this Oracle(earlier called Sun) Java, and you are good to go with Android. If you want to learn Native Android or with frameworks like PhoneGap etc, some basic understanding in Java might be helpful.

Get some good Java book, for getting basic understanding. Just one tip, don't compare Python with Java. Boiler plate codes are a common sight in Java, and LoC will be high. But that is what makes Java, as "Java".
 

phrick

Broken In
:D Hurt feelings? lulz.

BTW there is just one java. Just this one.

And I get it, since you are from Python, learning Java is somewhat difficult for you. So, just learn this Oracle(earlier called Sun) Java, and you are good to go with Android. If you want to learn Native Android or with frameworks like PhoneGap etc, some basic understanding in Java might be helpful.

Get some good Java book, for getting basic understanding. Just one tip, don't compare Python with Java. Boiler plate codes are a common sight in Java, and LoC will be high. But that is what makes Java, as "Java".
Thanks man, that really helped!
 

TheSloth

The Slowest One
Could anybody guide me to link where I can learn (Java)Spring framework well?? Its important as I missed few classes in starting and I want to join now but will not be able to understand without basic knowledge. Also, if I feel like the source provided here is better than the teaching staff, then I will continue online. So please help me out here. A link to a website and if possible, a link to a Youtube channel too. Pleeeeeeease reply as soon as possible. Thank you.
 

TheSloth

The Slowest One
ok i need help here with these 2 code :

1. public class Hello1 {
int b;
public static void main(String[] args) {
int a=9;
Hello1 h1 = new Hello1();
System.out.println("h1.b - "+h1.b);
System.out.println(a);
}
}

This one executes normally.
-------------------------------------------------------------------------

2. class A2{
int a;
A2 objA2 = new A2();
public void show(){
System.out.println("A2-show()");
System.out.println(objA2.a);
}
}
public class InsideObjectCreation {
public static void main(String[] args) {
A2 objA = new A2();
objA.show();
}
}

Here stackoverflow error at line 3.

I want to know how the object of the class is created before even executing the same class completely. Try to be as much detailed as possible with the flow of the program.
 

quicky008

Technomancer
can anyone help me print the following pattern using java: ( see attached image)


if anyone knows how it can be done please post the solution here.
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    10.3 KB · Views: 193

TheSloth

The Slowest One
@quicky008

Code:
import java.util.Scanner;

public class PatternNumPyramid {
    public static void main(String args[]) {
        System.out.println("Enter the number of rows you want to print");
        Scanner input = new Scanner(System.in);
        int numOfRows = input.nextInt();
        int  i, j, k;                                           [I]//to be used in for{} loops[/I]
        boolean flag; [I]                                     //flag is used to ensure that loop k(for space) runs only once after coming to next line[/I]
        for(i=0 ; i<=numOfRows; i++){          [I]//i is to go to next line[/I]
            flag=true;
            for(j=i; j>=0; j--){                         [I]//j is to print in decreasing order[/I]
                if(flag){
                    for(k=numOfRows-j; k>0; k--){ [I] //k is to print spaces[/I]
                        System.out.print(" ");
                    }flag=false;
                }
                System.out.print(j);
                if(i>0){
                    if(j==0){
                        for(int x=1; x<=i; x++){   [I] //x is to print in increasing order[/I]
                            System.out.print(x);
                        }
                    }
                }
            }
            System.out.println("");            
        }
    }
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. numOfRows :
(a) As you see, the base of the pyramid starts with digit 5, and total number of rows you want to print are 5+1(=6). So I decided to take that number as common and it also helps to count number of spaces in each row(explained below)

(b) In the condition of the first loop, i<=5(number of rows), is used to print last row which starts from the digit 5. If you use i<5(number of rows) then it will just print 5 rows where 5 row starts from digit 4. You will miss the last row starting from 5.

(c) The above source code is in Java, so I used Scanner object input to take the common value of number of rows, digits at last row and space counts. Scanner is a class from util Library of Java, available to take input from the console. System.in parameter is input stream connected to Keyboard. I used it so that You can print the pyramid with any numerical value.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. Spaces :
(a)Then comes the part about spaces. How to decide how many spaces should come?
If you see carefully, you will see that in the first row, there are five spaces and then 0 is printed. In next row, four spaces and then comes the 101. The spaces keep decreasing as we go to next row. So I decided to start the number of spaces should be equal to number of rows we want to print. ANd then number of rows should be keep on decreasing as we move to next row.

(b)How to decrease number of spaces in next row you ask? Remember, the spaces has to decrease by one count at each row. I used the logic as,
number of spaces at current row(k) = required number of rows(numOfRows) - value of current row(i) or value of j

(i) number of current row-is obvious one, for example
number of spaces at current row = 5 - 0 (value of current row or i) = 5. then we come to next row,
number of spaces at current row = 5 - 1 (value of current row or i) = 4, next row
number of spaces at current row = 5 - 2 (value of current row or i) = 3, and so on

(ii) how can we use j at the same position you ask? Notice the first digit in each row, it is count in increasing order. We can use same logic as above to find the number of spaces required at the current row.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3. Digits :
(a) Forget everything and concentrate on just one row. I am using variable j to print the digits in increasing order and x for printing in increasing order. For example, in second last row, 4 3 2 1 0 1 2 3 4 . Here, I used j to print 4 3 2 1 0 and then I checked the value of j. If the value of j is 0, then I need to print the digits in increasing order, so I declared a new variable x to and print the digits till the value of j, i.e. 4.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I made some comments in few lines, to explain why did I take certain variables and their values. Try to change them and experiment a bit to understand the flow of the program. For example you can remove that flag variable and the check the output to see why I needed that variable. Use i to calculate the number of spaces required and check output and so on.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Output -
How I approached for solution?
a. I just wrote a code to print values in decreasing order like 0 10 210 3210 (its without spaces)
b. Then I modified code to get different rows like
0
10
210
3210
c. Modified code to get spaces at front
0
10
210
3210
d. (final output)Then I had to print digits in reverse order too, used x for that
Code:
[CENTER]        0
      101
    21012
  3210123
[/CENTER]

Tell me if you have any doubts :)

- - - Updated - - -

PS: Formatting is all messed up, the last output is the final output, I coudln't format it properly. Sorry.

- - - Updated - - -

PPS : this source code is definitely not the optimized one. I am still learning how to write optimized source codes
 
Last edited:

quicky008

Technomancer
Could anybody guide me to link where I can learn (Java)Spring framework well?? Its important as I missed few classes in starting and I want to join now but will not be able to understand without basic knowledge. Also, if I feel like the source provided here is better than the teaching staff, then I will continue online. So please help me out here. A link to a website and if possible, a link to a Youtube channel too. Pleeeeeeease reply as soon as possible. Thank you.
did you find any reliable source for learning spring framework?Are free tutorials for it available online?Also can you suggest from where can one learn other frameworks like strutts,hibernate etc?

Also can anyone tell me what are node js and angular js?Do they have any similarity with OOPs languages like java?Is it particularly challenging to learn for beginners?
 
Top Bottom