Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,156,296 members, 7,829,684 topics. Date: Thursday, 16 May 2024 at 10:30 AM

Java Tutorial For Beginners - Programming (3) - Nairaland

Nairaland Forum / Science/Technology / Programming / Java Tutorial For Beginners (23727 Views)

C++ Tutorial For Beginners... Part 1... / For Beginners: Learn How To Create A Simple Android Native App / Programming Challenge For Beginners N20000 (2) (3) (4)

(1) (2) (3) (4) (5) (Reply) (Go Down)

Re: Java Tutorial For Beginners by nnasino(m): 12:46pm On Dec 14, 2014
DonaldGenes:
Op, drop your BBM pin

Fortunately, I am learning Java but have gone far.
I have Created some Program using NetBeans
...
Re: Java Tutorial For Beginners by romkey(m): 1:39pm On Dec 14, 2014
this can be regarded as the best thread ever.

1 Like

Re: Java Tutorial For Beginners by nnasino(m): 2:30pm On Dec 14, 2014
romkey:
this can be regarded as the best thread ever.
wow, thanks man, appreciate that.
Re: Java Tutorial For Beginners by gememerald(m): 3:54pm On Dec 14, 2014
@OP how do you integrate opengl with java for 3d programming?
Re: Java Tutorial For Beginners by Lordmykel(m): 5:24pm On Dec 14, 2014
#following
Re: Java Tutorial For Beginners by nnasino(m): 5:31pm On Dec 14, 2014
gememerald:
@OP how do you integrate opengl with java for 3d programming?

you canuse the JOGL wrapper/binder
Re: Java Tutorial For Beginners by gememerald(m): 6:30pm On Dec 14, 2014
nnasino:
you canuse the JOGL wrapper/binder

Does it come with netbeans or eclipse or I have to download the library seperately? @OP
Re: Java Tutorial For Beginners by Yinkash100(m): 8:07pm On Dec 14, 2014
Thanks bro
i must follow
Re: Java Tutorial For Beginners by nnasino(m): 7:19am On Dec 15, 2014
gememerald:


Does it come with netbeans or eclipse or I have to download the library seperately? @OP
you download d library separately
Re: Java Tutorial For Beginners by uyigideon(m): 8:20am On Dec 15, 2014
Please sir, I want to build mobile application on phonegap, i was ask to get Ios and Adriod Key to be able to build, because i am completely novice, but i was given a free mobile App to Edith and build on Phone gape, i have downloaded Java on my desktop, but i don't no how to set the home page to get my license key i was ask to get. Please help me out with this.
Re: Java Tutorial For Beginners by gememerald(m): 8:41am On Dec 15, 2014
nnasino:
you download d library separately
Thanks
Re: Java Tutorial For Beginners by Nobody: 10:08pm On Dec 15, 2014
For those into ".NET", lets create our own thread. VB.NET, C#.NET and Microsoft sql server.
Re: Java Tutorial For Beginners by sampark201: 1:48am On Dec 16, 2014
Sir , Do you give Private Tutorials..
Re: Java Tutorial For Beginners by nnasino(m): 8:49am On Dec 16, 2014
uyigideon:
Please sir, I want to build mobile application on phonegap, i was ask to get Ios and Adriod Key to be able to build, because i am completely novice, but i was given a free mobile App to Edith and build on Phone gape, i have downloaded Java on my desktop, but i don't no how to set the home page to get my license key i was ask to get. Please help me out with this.
I don't understand your question. but I don't think you need Java to build phone gap applications.
Re: Java Tutorial For Beginners by nnasino(m): 8:51am On Dec 16, 2014
sampark201:
Sir , Do you give Private Tutorials..
sorry, I don't.
Re: Java Tutorial For Beginners by uyigideon(m): 12:39am On Dec 17, 2014
This was the instruction given to me:

Generating a private key

Download and install Java.

Set Java_Home directory (http://docs.oracle.com/cd/E19182-01/820-7851/inst_cli_jdk_javahome_t/index.html).

Open the command prompt (cmd.exe) as an Administrator, then Run the following command: $ keytool -genkey -v -keystore [keystore_name].keystore -alias [alias_name] -keyalg RSA -keysize 2048 -validity 10000

Keytool will ask for keystore password. Enter password and confirm:
Re: Java Tutorial For Beginners by nnasino(m): 12:49pm On Dec 17, 2014
uyigideon:
This was the instruction given to me:

Generating a private key

Download and install Java.

Set Java_Home directory (http://docs.oracle.com/cd/E19182-01/820-7851/inst_cli_jdk_javahome_t/index.html).

Open the command prompt (cmd.exe) as an Administrator, then Run the following command: $ keytool -genkey -v -keystore [keystore_name].keystore -alias [alias_name] -keyalg RSA -keysize 2048 -validity 10000

Keytool will ask for keystore password. Enter password and confirm:
Alright, send me a pm and detail where exactly you experience the issue. But i can assure you that you can start development without all these keysigning ish. You can work locally on your system you don't have to use phonegap build.
Re: Java Tutorial For Beginners by nnasino(m): 12:49pm On Dec 17, 2014
Hello everyone,
Back to business.
Today, i'll digress a little. I'll be teaching some tips and tricks on how to make the best out of your netbeans ide. I'll introduce code templates.

Code templates are simply snippets of code which we use frequently and therefore we make them shorter to save us typing. There are many builtin templates in netbeans and we can add ours to them. To use code templates simply type the short form of the template and press the tab key. Some of the inbuilt code templates include:
sout --
System.out.println(""wink;
//The cursor will be placed within the quotes
psvm --
public static void main(String[] args){} 

St --
String

fa --
false

if --
 if{}

ife --
 if{}else{}

etc
Now try the above for yourself. Type them then press the tab key.
There are many more built-in code templates available to you in netbeans and not just for java but for most of the languages supported by netbeans. You can check them in the Options>Editor>Code Templates.

You can also create your own code templates depending on what you use frequently. We will create a code template for catching exceptions. There should be a builtin template for try catch but we will just create this to show how it is done.
Open Options>Editor>Code Templates. Click New, type the short form for your new template, we will name ours error. The template is then created. Within the expanded text area, type the following:

try{
${cursor}
}catch(Exception exc){

}

The ${cursor} is where the user's cursor will be placed when the template is inserted.

Provided that you setup your shortcuts as we did in the first lesson then the following shortcuts will work for you. If you didn't set them up, then go to options, keymap and change netbeans to eclipse.

Ctrl-shift-O == This is to organize your imports. That is, if you use classes in your code which you need to import, pressing this shortcut helps you automatically import them. Try this, type
Scanner
and press Ctrl-shift-O. this will add
import java.util.Scanner;
to the top of your file.

Ctrl-shift-F == format code. This is used to format your code. This handles your indentation and generally makes your code look better.

That's all for now. I'll be back soon. Cheers
Re: Java Tutorial For Beginners by god2good: 10:13pm On Dec 17, 2014
Thanks Man

1 Like

Re: Java Tutorial For Beginners by haibe(m): 12:54pm On Dec 18, 2014
nnasino:

This is perfectly correct. But add the two closing braces for main and the class }}.
i dont get, I thought double' was for numbers with decimals? Shouldn't 'int' be used Instead?
Re: Java Tutorial For Beginners by haibe(m): 1:15pm On Dec 18, 2014
nnasino:

- Write a program to calculate simple interest. the formula for simple interest is simple interest = (principal * rate * time) /100. use variables extensively.


We've come to the end of this lesson. Stay motivated everyone and remember i'm here to take any questions you have.
Cheers!!

public class Simpleinterest {
public static void main (String [] args){
int principal = 2000;
int rate = 5;
int time = 5;
int result = (principal * rate * time)/100;
System.out.print(result);

}

}
Re: Java Tutorial For Beginners by mgb4reel(m): 9:42pm On Dec 18, 2014
haibe:


public class Simpleinterest {
public static void main (String [] args){
int principal = 2000;
int rate = 5;
int time = 5;
int result = (principal * rate * time)/100;
System.out.print(result);

}

}
Nice attempt bro but it would be better if you make it interactive by letting the users provide the values for the variables(principal, rate, and time). Also, the variables declarations you use aint the best to use. Nice one once again. Lets keep the ball rolling. Thumbs up to boss nnasino, Jah bless!

1 Like

Re: Java Tutorial For Beginners by nnasino(m): 7:20am On Dec 19, 2014
haibe:
i dont get, I thought double' was for numbers with decimals? Shouldn't 'int' be used Instead?
The principle might have a decimal that's why we used double. Generally, when dealing with money we usually use doubles. Also, when we divide by 100 the result could be a fraction so we need to be able to represent fractional numbers.

1 Like

Re: Java Tutorial For Beginners by realowded(m): 3:56pm On Jan 10, 2015
nnasino:
Hello everyone,
Back to business.
Today, i'll digress a little. I'll be teaching some tips and tricks on how to make the best out of your netbeans ide. I'll introduce code templates.

Code templates are simply snippets of code which we use frequently and therefore we make them shorter to save us typing. There are many builtin templates in netbeans and we can add ours to them. To use code templates simply type the short form of the template and press the tab key. Some of the inbuilt code templates include:
sout --
System.out.println(""wink;
//The cursor will be placed within the quotes
psvm --
public static void main(String[] args){} 

St --
String

fa --
false

if --
 if{}

ife --
 if{}else{}

etc
Now try the above for yourself. Type them then press the tab key.
There are many more built-in code templates available to you in netbeans and not just for java but for most of the languages supported by netbeans. You can check them in the Options>Editor>Code Templates.

You can also create your own code templates depending on what you use frequently. We will create a code template for catching exceptions. There should be a builtin template for try catch but we will just create this to show how it is done.
Open Options>Editor>Code Templates. Click New, type the short form for your new template, we will name ours error. The template is then created. Within the expanded text area, type the following:

try{
${cursor}
}catch(Exception exc){

}

The ${cursor} is where the user's cursor will be placed when the template is inserted.

Provided that you setup your shortcuts as we did in the first lesson then the following shortcuts will work for you. If you didn't set them up, then go to options, keymap and change netbeans to eclipse.

Ctrl-shift-O == This is to organize your imports. That is, if you use classes in your code which you need to import, pressing this shortcut helps you automatically import them. Try this, type
Scanner
and press Ctrl-shift-O. this will add
import java.util.Scanner;
to the top of your file.

Ctrl-shift-F == format code. This is used to format your code. This handles your indentation and generally makes your code look better.

That's all for now. I'll be back soon. Cheers


Boss! Please keep the ball rolling... I have jst got a laptop and a generator... I want to perfect my java skills in two months...
Re: Java Tutorial For Beginners by realowded(m): 12:32am On Jan 16, 2015
nnasino:
Methods
Methods are used to group a set of statements performing one task. When you write code which you notice keeps repeating itself then you might need to create a method/function to handle that task. Functions can take values called parameters which it will use in its computation. An example of a function is a function that checks if a number is a prime number or not. Functions/methods can return an answer or not. When a function does not return any value it is of type void. Defining a function:

type nameOfMethod(parameter list){
body of method;
return avalue; //if type is not void
}

If you are not returning any value then type should be
void
. If you are returning a value then type should be the type of the value you want to return (int, float, double, String etc). Also, if you are returning a value then you must have the return keyword in the body of the method. The parameter list, is a comma separated list of the parameters to be passed to the function. The parameters must have the type and the name by which they will be accessed.
An example

int sum(int a, int b){
//This function sums two numbers
return a + b; //using the two parameters passed to it.
}

The above function takes, two

Getting user input from the user:
We haven't discussed classes or objects so far but we will need to use them in our exercises so i'll give crash course on both. A class is simply a framework for creating objects. It contains variables and functions within it which represent properties and behaviours of the objects it will be used to create respectively. For example, a vehicle class is a framework for creating vehicles. It's properties can include speed, colour, size, etc while it's behaviours include move(), stop(), changeOil() etc. So with a single class definition we can create numerous objects each having their own properties. The good thing about objects is that we can use them without knowing how they are implemented. A car can be driven without knowing what engine is in it or how it was built. Objects can also be nested within one another. A steering wheel is a fully developed object with its own components. But it is contained in the vehicle object. The vehicle doesn't need to know anything about how the steering wheel was built/created all it needs to know are what it can be used for and how to use it.
We will delve into objects later on but for now we need to use one in our program ( we will be using it regularly).

The
Scanner
class is a built-in java class for reading input. It can read from files, standard input (user) and other streams. To use these functions we need to create an object of the Scanner class.
In java this is done by:
Scanner scannerObject;

We have declared the scanner class, but before we can use it we need to initialize it.
New Concept:
Constructor
: A constructor is a function that is used to initialize a newly created object. We'll stop there for now and continue. Just note that the constructor of a class is a function with the same name as the class. Now the
Scanner
class's constructor takes one parameter which represents where the input comes from. In our case, the input comes from Standard input (keyboard), so the parameter is
System.in
.
So to declare and initialize the Scanner object we have:

Scanner scan = new Scanner(System.in);

This creates an object for us. We also need to import the Scanner class. We do this by putting
import java.util.Scanner;
at the top of the file. For now ignore all these, we will still treat objects and classes later on. But for now just copy this code into your file.
Note: If you use netbeans, set up your key map with the eclipse settings so you can follow with the shortcut keys i will be using to do this, click Tools menu > options, then click the keymap tab and change the netbeans to eclipse, click apply and then ok.

Netbeans will be very helpful when working with objects. Now you have set up your key map. You can use the following:
Type Sca and press Ctrl+space, this will bring up a list of all classes matching the what you've typed select the one you require and press enter, in this our case the one in java.util. Doing this will automatically import the class for us.
Also, we can type Scanner and declare and initialize the object without importing. The IDE will definitely complain, at this point press Ctrl+shift+O, this imports all missing classes.
As we move on, i'll be introducing more function keys for making our work easy.

Now to our challenges:
Challenge 1:
Write a program which displays the number of prime numbers from zero to a number. Number should be provided by the user.
We break the problem into components:
- Get number from the user.
- check if each number is a prime number. if they are increment the count.
- Display the count to the user.

To get input from the user we use the Scanner class
-We will be checking many numbers therefore we will need a loop
-We will be checking if a number is prime so we will create a function to check

The program therefore goes:



class Primes{
static boolean isPrime(int number){ //isPrime is declared as static, so we can use it within main (main is also static)
if(number == 2)
return true;
for(int i = 2; i < number; i++){
if(number % i == 0) //If the number can be divided by any other number apart from 1 and itself
return false; //Then it is not prime
}
return true; //If not it is prime
}
public static void main(String[] args){ //type psvm and press tab, netbeans will complete this for you
int number = 0; //Variable for holding the maximum number
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the maximum number" ); //type sout and press tab
number = scan.nextInt();
int count = 0; //The number of primes found
for(int i = 2; i <= number; i++){
if(isPrime(i))
count++;
}
System.out.println("The number of primes between 1 and " + number + " is " + count);
}
}
Please bro, explain each line of codes...
Re: Java Tutorial For Beginners by nnasino(m): 8:53am On Jan 16, 2015
Good morning everyone, a happy new year to you all. Sorry I've been AWOL for so long. I'm having issues with my connection and I'm not comfortable writing with my phone but as soon as I resolve that, the thread will be revived.
Re: Java Tutorial For Beginners by nnasino(m): 9:41am On Jan 16, 2015
realowded:

Please bro, explain each line of codes...
realowded:

Please bro, explain each line of codes...

class Primes{
static boolean isPrime(int number){ //isPrime is declared as static, so we can use it within main (main is also static)
if(number == 2)
return true;
for(int i = 2; i < number; i++){
if(number % i == 0) //If the number can be divided by any other number apart from 1 and itself
return false; //Then it is not prime
}
return true; //If not it is prime
}
public static void main(String[] args){ //type psvm and press tab, netbeans will complete this for you
int number = 0; //Variable for holding the maximum number
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the maximum number" ); //type sout and press tab
number = scan.nextInt();
int count = 0; //The number of primes found
for(int i = 2; i <= number; i++){
if(isPrime(i))
count++;
}
System.out.println("The number of primes between 1 and " + number + " is " + count);
}
}



Alright, explanation line by line

line 1: We define our class
Primes

line 2 to 10: The function
isPrime
takes an integer as input and returns
true
if it is a prime number and false if not.
line 2: declare the function.
static
means that we don't need to create an instance of the class
Prime
to use the function.
boolean
is the return type of the function and
isPrime
is the name of the function.
within the parentheses, we specify that the function will take an integer as its only parameter
line 3-4: if the number is equal to 2 return true; (2 is a prime number)
line 5: Loop starting from 2 up to the number - 1.
line 6-7: If the number can be divided without remainder by any of these numbers then it is not a prime so we return
false
;
line 9: If at the end of the loop (the we know that there is number from 2 to the number -1 that divides the number in question without remainder. Therefore, it is a prime number and we return
true
;
line 11: This is the main method which will be executed by the JVM.
line 12: Variable for holding the maximum number which will be fetched from the user
line 13: Create a
Scanner
object ( this is used to read input from the user)
line 15: Read an integer from the user
line 16: Create a variable that will keep count the number of primes
line 17: Loop from 2 to number;

2 Likes

Re: Java Tutorial For Beginners by swankylay(m): 7:20am On Jan 27, 2015
Wow. Cool. Pls I v a del latitudel core i5 4gig ram 320 HDD. I wanna learn. How do I her started. I tried dloading from that link but I just got confused. Pls gimme a step by step easy way to DL n what exactly to DL. Thanks my teaxhers
Re: Java Tutorial For Beginners by sunnico(m): 11:47am On Jan 27, 2015
swankylay:
Wow. Cool. Pls I v a del latitudel core i5 4gig ram 320 HDD. I wanna learn. How do I her started. I tried dloading from that link but I just got confused. Pls gimme a step by step easy way to DL n what exactly to DL. Thanks my teaxhers
1. first to download the Java jdk
a. copy and paste the following address:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
b. click on the accept license agreement,


c. Then click on the download link that includes your system platform e.g window xp/7/8, with your corresponding system architecture either 32 or 64 bit, and then install it

2. To download the Netbeans IDE
a. copy and paste the following address:https://netbeans.org/downloads/index.html
b. ensure that the platform is in windows(i.e your os platform)although by default the selected platform on the web is windows.
b. you can download the one that has all features 1.e 205 mb


I Hope this Helps

1 Like

Re: Java Tutorial For Beginners by swankylay(m): 9:02pm On Jan 27, 2015
Wow. Thanks this will surely do.


sunnico:

1. first to download the Java jdk
a. copy and paste the following address:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
b. click on the accept license agreement,


c. Then click on the download link that includes your system platform e.g window xp/7/8, with your corresponding system architecture either 32 or 64 bit, and then install it

2. To download the Netbeans IDE
a. copy and paste the following address:https://netbeans.org/downloads/index.html
b. ensure that the platform is in windows(i.e your os platform)although by default the selected platform on the web is windows.
b. you can download the one that has all features 1.e 205 mb


I Hope this Helps
Re: Java Tutorial For Beginners by leye4u: 8:25pm On Feb 10, 2015
looks like the lecturer here is on strike..... please call off the strike please

(1) (2) (3) (4) (5) (Reply)

I Want To Learn Computer Programming, What Language Should I Learn First? / Programmers: At What Age Did You Start Programming? / Community Project(strictly Java)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 91
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.