Jump to content

Recommended Posts

Posted

Hello maxcheater!!!!!!!!!

I came today with an idea to make a tutorial. Ofc there are found lot of tutorials on google but like it happend to me before, some of thems are little bored at start and some times make ppl to avoid due to the true java language words used there. In the meaning i tried to adapt this tutorial on my own words as much as i could. Hope this tutorial will help newcomers.

So here we start to learn some basics.

 

9 primitive data types

 

int - a number 32-bite signed between   -2,147,483,648 and 2,147,483,647

short - a 16-bit signed number between -32,768 and 32,767 / unsigned range 0 to 65,535

byte - 8-bit signed number between -128 and 127 / unsigned range 0 to 255

long - 64-bit signed number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 / unsigned range 0 to 18,446,744,073,709,551,615

double - a number with comma

float – number with comma (is better then double to save memory in large arrays)

char – 1 character

String – commune of characters

Boolean – takes always 2 values,  TRUE or FALSE

 

Symbols and Operators:

; - present the end of an action, statment.

[] - array declaration

"asd" - dopuble quote String message.

'a' - single quote character message.

+= , -= , *= , /=   x += y; is same like x= x+y; ;)

x++ - x=x+1

boolean operatos

x && y- (conditional and)  if and x and y are true, result is true and conversely. If x is false, y is not evaluated.

x & y - (boolean and) if and x and y are true, result is true and conversely. Both x and y are evaluated before the test.

x || y - (conditional or) If x or y are true, the result is true. If x is true, y is not evaluated.

x | y - (boolean or) If x or y are true, the result is true. Both x/y are evaluated before the test.

!x - (boolean not) If x is true, the result is false. If x is false, the result is true.

x ^ y - (boolean xor) If x is true and y is false, the result is true. If x is false and y is true, the result is true. Otherwise, the result is false. Both x and y are evaluated before the test.

How to writte a variable.

 

Type    name of variable

Int        value1;  (remember a variable always end with [ “ ; “])

Initialize    (give value to variable)

Value1=1000;

Other examples:

int value 1=  1000;

long a= 1234567810L;

float f=297.5F;

double d= 179999.8;

short b= 2945;

byte m=27;

char e=’E’;

String s=”erol”; when you create a String object you can’t change it anymore their values.

Boolean q=true; or q=false;

OPERASTORS

Operator                           Description                                             true example                      false example

<                                         Less then                                               3 < 8                              8 < 3

>                                     Greater then                                               4 > 2                              2 > 4  

==                                       Equal to                                                 7 == 7                             3 == 9    

<=                                          Less then or equal to                             5 <= 5                             8 =< 6    

>=                                             greater then or equal to                      7 >= 3                             1 >= 2        

!=                                       not equal to                                             5 !=6                               3 != 3

Comments in Java

Line comments – start with 2 slashes. And is when we comment 1 line.

Block comments - start with /* and end with */ when we comment in more then 1 line.

Java doc comment – start with /** and end with */ can use this to generate doc with a program named javadoc.

Rules how to put the name of a class.

1. Name must be same with the name of file. Even the caps.

2. Name can contain numbers, char, @, $.

3. Cannot use as class name words like (int, boolean, String etc).

4. Name of class cannot start with a number.

5. Cannot use words True, False, null as name of a class.

 

When starting writing on java you should know some specific things.

A class start as:  public class nameofclass

After it you writte {

Remember every { need another }. So if you open 3 { you need to close with 3 }.

An example.

public class test
{
     public static void sum()
        {
              int value1=20;
              int value2=50;
              int=sum=value1+value2;
       System.out.println(“sum is” +sum);
        } 
}

 

METHODS* - are instructed 1 time and called many times.

How to construct a method:

you can find more about methods here ---> Java Methods

 

declaration:

 

{

Body of the method

}

declaration

a) Public or private

b) Static (method that never change)

c) Void (method which does not turn value

Type of value ( when we have method that turn value)

d) Name of method

e) (

f) Arguments

g) )

 

CREATING METHODS WITH NO ARGUMENTS

public class test
{
public static void sum()
{
	int value1=20;
	int value2=50;
	int sum=value1+value2;
	System.out.println ("sum is" +sum);
}
}

Now this time we create one with argument.

public class test
{
public static void sum (int 200)  
{
	int value2=50;
	int sum=value1+value2;
	System.out.println("sum is" +sum);
}
}

Now method which  turn values.

public class test
{
public static int sum()
{
	int value1=20;
	int value2=50;
	int sum=value1+value2;
	System.out.println(sum());
	return sum;
}
}

 

Here we see that at public static we don’t have anymore void. Rember when there is void means that cannot turn values.

 

Method that return value with 1 argument.

 

public class test
{
	public static int sum(int value1)
	{

		int value2=50;
		int sum=value1+value2;
		System.out.println(sum(20));
		return sum;
	}

}

 

Set and get methods.

1*

public class test 
{
private int age;
public String getage;
public int getmosha()
{
	return age;
}
public void setage (int m)
{
	age=m;
}
}

2*

public class test3 
{
public static void main(String[]args)
{
	test isi=new test();
	isi.setage(22);
	System.out.println ("Age is"+isi.getage);
}
}

*Isi – is just a random name, you can choose whatever you want.

 

An example of set and get but now we add name and rank.

http://pastebin.com/Ryw1g1xL

we can also do it in a simple and shorter way. We delete the set method.

http://pastebin.com/VAke8m8S

you see in both links this: test isi=new test();  ----> means that 2 classes are connected together. and 1 mistake test class make the second one to give error.

 

Structures switch and if/else.

 

Taking a character from keyboard

1) Main()throwsException

2) Declariation of the character  (char e;)

3) Initialize of the character with the value token on keyboard

System.in.read(); - taking character from keyboard

out – screen / in- keyboard

read – method that read  / println – method that post smth

I’m gona show you an example that while you press smth on keyboard it say you if you typed or not.

1st example is with if and if else. Remember, it always start with if and end with else.

http://pastebin.com/tJtEFgAD

now let’s see the same but this time with switch structure.

http://pastebin.com/qKQQ87CP

you may notice that now in place of if we got case and in place of else we got default.

Vectors

type[]name=new type

when we have a group of indications with same name and type we can declare with a vector.

Example:

int erol=new erol[7];     // 0-6 (n-[n-1])

 

public class test3
{
public static void main(String[]args)
{
	int[]isi=new int[4];
	for (int i=0;i<=6;i++)
	{
		isi[i]=i*2;
		System.out.println(isi[i]);
	}
}
}

Take this example: http://pastebin.com/SX5UKHT5

You see it order some nrs written in random way.

To make it as a descending order you must change last for like this:

Was: for (int i=0;i<v.length;i++) make it for (int i=14;i>0;i--). (you see that the second one no need any other code, just math skill)  ;)

for those ppl who hate math here is an example: sort vector using comparator example

 

 

 

 

Posted

This code present the votes for some candidates. On this case we have 4 candidates and we gonna make a code that when we press 1 go +1 vote for candidate 1 and so on for all candidates.

 

import javax.swing.JOptionPane;

public class votat 
{
public static void main(String[]args)
{
	int numcandidates=4;
	int[] votes= new int[numcandidates];
	boolean processing=true;
	while(processing)
	{
		int v=new Integer(JOptionPane.showInputDialog("Vote for (0,1,2,3):")).intValue();
		if (v>=0&&v<votes.length)
		{
			votes[v]=votes[v]+1;
		}
		else
		{
			processing=false;
		}
		for (int i=0;i!=votes.length;i=i+1)
			System.out.println("Candidate"+i+"has"+votes[i]+"votes");
	}
}
}

 

processing=false; ---> means that if you press a wrong button it quit recognizing it as an illegal vote ;)

 

 

here i'm going to present a simple calculator. simpler then the ones on google.  ;)

 

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.TextField;

public class test22 extends Applet
{
Button b=new Button("additon");
Button b1=new Button("divide");
Button b2=new Button("multiplication");
Button b3=new Button("divide");
TextField t1=new TextField(10);
TextField t2=new TextField(10);
Color c=new Color(100,20,30);
int rez;
public void init()
{
	setBackground(c);
	add(t1);
	add(t2);
	add(b);
	add(b1);
	add(b2);
	add(b3);
}
public boolean action(Event e,Object args)
{
	String s1=t1.getText();
	String s2=t2.getText();
	int nr1=Integer.parseInt(s1);
	int nr2=Integer.parseInt(s2);
	if (e.target==b)
		rez=nr1+nr2;
	if(e.target==b1)
		rez=nr1-nr2;
	if(e.target==b2)
		rez=nr1*nr2;
	if(e.target==b3)
		rez=nr1/nr2;
	repaint();
	return true;
}
public void paint(Graphics g)
{
	g.drawString("rezultati="+rez,50,80);
}
}

Button b=new Button("additon"); this make a button just like in a real calculator. like this we make and for divide, multiplication and deduction.

TextField t1=new TextField(10); is a text field where you will writte the numbers. (10) is the size of this textfield.

Color c=new Color(100,20,30); this is color of the pop up window you will see after excecuting.

if (e.target==b)  means that if you pres button b which correspond on addition (you will see it as addition on window) ...

... will be take this action ----> rez=nr1+nr2;

and so one for other actions.

Note* the calculator will show results only for int numbers. so if you type 2 / 8 it will show as 0.

to make it show even numbers with comma we have to change int to double.

on this case need to change: from int rez; to double rez;

from int nr1=Integer.parseInt(s1); to double nr1=Integer.parseInt(s1);

and so even for nr2.

g.drawString("rezultati="+rez,50,80); 50 and 80 put the result in which position you want. starting from 0,0 the start point (upper-left). just change the numbers how you want and you see the changes yourself.

 

To be continued...

 

Posted

You'll teach me some java ofc,but thanks for your try.

You will help many newbiew who they want to learn the basic of java

Posted

to be continued (is not finished yet)...  :)

and why you post not finished works? anyway Gj...

 

EDIT:

short: −32,768 to +32,767 and 0 to +65,535

Posted

and why you post not finished works? anyway Gj...

coz i have more to say. like applets for example which i love a lot.

ofc like i mention before, there are lot of guides on goole, but their true english and java language make ppl little confuse. I just hope that with this tutorial i motivate ppl learn Java coz its damn really attractive language.

going to post how to make the calculator, the translator, clock etc etc......

Stay tunned and thnx for the good words guys.

Posted

EDIT:

0 to +65,535

this is unsigned range. btw thnx gonna add even unsigned ranges.

Posted

i'm studying html, but i never had the chance to learn a new language... like Java.

i've already expressed myself "inside", awsome guide Erol. It will help noobs like me to learn some java.

Posted

 

int - a number 32-bite signed between  -2,147,483,648 and 2,147,483,647

short - a 16-bit signed number between -32,768 and 32,767 / unsigned range 0 to 65,535

byte - 8-bit signed number between -128 and 127 / unsigned range 0 to 255

long - 64-bit signed number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 / unsigned range 0 to 18,446,744,073,709,551,615

 

Aka Integers.

 

double - a number with comma

float – number with comma (is better then double to save memory in large arrays)

 

 

Aka Floating Points.

  • 7 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...