Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 266 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My book: the java journal: chapters 6-10

#1
Chapter 6: variables

A variable is a primitive data type used in code. some examples of variables are strings printed out to form text or integers added, subtracted, multiplied or divided and printed out.


public class FirstClass {
public static void main(String[] args) {
String Hello = "hello java";
char h = 'h';
char e = 'e';
char y = 'y';
int A = 12;
int B = 2;
int Sum = A + B;
int Divide = A/B;
int Multiply = A*B;
int Subtract = A-B;
double D = 123.456;

System.out.println(Hello);//will print out "hello java"
System.out.print(h);//will print out "h"
System.out.print(e);//will print out "e"
System.out.print(y);//will print out "y"
System.out.print("\n");//will make a new line
System.out.println(A + " " + B);//will print out 12 2
System.out.println("Sum - " + Sum);//will print out the sum of A + B
System.out.println("Divide - " + Divide);//will print out the answer of A / B
System.out.println("Multiply - " + Multiply);//will print out the answer of A * B
System.out.println("Subtract - " + Subtract);//will print out the answer of A - B
System.out.println(D);//will print out 123.456
}
}


Chapter 7: user input

In Java, user input is done using 'Scanners' A scanner is an object that takes anything the user inputs and sets it as a string (unless an int is specifically defined):


import java.util.Scanner;


public class UserInput {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String text;
System.out.print("Enter some text: ");
text = scan.next();
System.out.println(text);
}
}


Lets have a look at this code piece by piece:

______
Scanner scan = new Scanner(System.in);

This creates a new Scanner object called 'scan' and uses the System's console (System) as the input device (.in).
______
text = scan.next();

This sets the previously created string named 'text' to the next string inputted by the scanner. Along with .next() which means next thing inputted, the scanner also has:

.nextLine() -- looks at the line of input after the current line
.hasNextInt() -- reads for the next integer inputted
.hasNextDouble() -- reads for the next double inputted
.skip(String) -- skips any word put in the parenthesis


Procedure 1: simple calculator

This is not a new chapter, Procedures will be small programs created using what we have learned in the earlier chapters. In this procedure, we will be using a simple calculator using user input and variables.


import java.util.Scanner;

public class UserInput {

public static void main(String[] args) {
int a, b, answer;
Scanner scan = new Scanner(System.in);
System.out.print("Enter first number: ");
a = scan.nextInt();//reads for next integer
System.out.print("Enter second number: ");
b = scan.nextInt();//reads for next integer
answer = a + b;
System.out.println("Answer = " + answer);
answer = a - b;
System.out.println("Answer = " + answer);
answer = a / b;
System.out.println("Answer = " + answer);
answer = a * b;
System.out.println("Answer = " + answer);
answer = a % b;
System.out.println("Answer = " + answer);

}
}


When this program is run the output will be the following:

Enter first number: 12
Enter second number: 5
Answer = 17
Answer = 7
Answer = 2
Answer = 60
Answer = 2

~~~!!! End Of Unit 1 !!!~~~






~~~!!! Start Of Unit 2 !!!~~~

Chapter 8: IF Statements

An if statement is a conditional statement that says: if<conditional>{codeblock will happen}

There are three 'types' of if statements:__ if <conditional> {code block will happen} - basic If-then statement
Else if <conditional> {code block will happen} - says 'If the first if statement does not apply, do this.'
Else {code block will happen} - says, 'if the above statements do not apply, do this.'

A good example of if statements would be:__public static void main(String[] args) {
Int x = 10;

If (x > 10){
System.out.print(“X is greater than 10.”);
}
Else if (x < 10){
System.out.print(“X is less than 10.”);
}
Else{
System.out.print(“X is equal to 10.”);
}
}
}


If one takes the above code and puts it into basic sentences, it will look like:__ The integer 'x' is equal to 10.
If x is greater than 10, print out “X is greater than 10.”
Otherwise, if x is less than 10, print out “X is less than 10.”
If neither of the above statements apply to x, print out “X is equal to 10.”

If statements can be used not only with static (never changing) conditions but also with things like user input. For example:


import java.util.Scanner;

public class UserInput {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String password = “xyxy”;
String text;
System.out.print("Enter A Password: ");
text = scan.next();


if(text != password){
System.out.print(“Access denied.”);
}
Else{
System.out.print(“Access granted.”);
}
}
}


The above program asks the user for a password, if the user enters “xyxy”, access is granted. However, if the user does not enter “xyxy”, access is denied.

Along with != (does not equal) and == (exactly equals), The if statement has a variety of other conditionals that can be used. Some of these are:

.EqualsIgnoreCase(String) -
If (somestring.EqualsIgnoreCase(anotherstring)){code block will happen}
This conditional throws away upper case and lower case.

.Equals(String) -
If (Somestring.Equals(anotherstring)){code block will happen}
This conditional is like == but is more precise.

.Contains(String) -
If (somestring.contains(anotherstring)){code block will happen}
This conditional check if one string contains another string, for example: If (anatomy.contains(tom){code block will happen}

Chapter 9: Loops

In java there are loops that allow the programmer to have a block of code repeated while a conditional is true/false. There are three main types of loops, these are:

The while loop:
ο The while loop says while <conditional> {code block will happen}. The while loops is useful for long and fast loops. One example of this type of loop is:


public class loops {

public static void main(String[] args) {
int x = 10;
while(x != 0){
System.out.println("X = " + x);
x--;
}

}

}


The for loop:
ο The for loop is like a while loop but it is used for less repetitions. To write this type of loop we use: for (conditional; conditional >/</<=/>=/etc.;conditional++/conditional--/++conditional/--conditional). One example of this typeof loops is:


public class loops {

public static void main(String[] args) {
for (int i = 10; i > 0; i--){
System.out.println("i = " + i);
}

}

}


The do-while loop:
ο The do while loop is like a large for loop, It has the same speed and the same durability but it takes more lines of code. To write this type of loop we use: do {code block}while<conditional>. One example of this loop is:


public class loops {

public static void main(String[] args) {
int x = 10;
do{
System.out.println("X =" + x);
x--;
}while(x != 0);

}

}


As you can see, each of these loops is created differently but they are all able to do the same thing. It is up to the programmer which one he/she wants to use. Another factor to think about when using loops is called 'Nested looping'. A nested loop is a loop within a loop. The nested loops have the same syntax as the original loop. Here are some examples of nested loops:


ο Do While loop

public class loops {

public static void main(String[] args) {
int x = 10;

do{
int y = 0;
System.out.println("X =" + x);
x--;
do{

System.out.println("\tY =" + y);
y++;
}while(y != 10);
}while(x != 0);

}

}

ο While loop


public class loops {

public static void main(String[] args) {
int x = 10;
int y = 0;

while(x != 0){
while(y != 10){
System.out.println("X = " + x);
x--;
System.out.println("Y = " + y);
y++;
}
}

}

}


ο For loop

public class loops {

public static void main(String[] args) {
for (int i = 0; i < 10; i++){
System.out.println("I = " + i);
for (int j = 10; j > 0; j--){
System.out.println("J = " + j);
}
}

}

}


Chapter 10: Switch Statements

A Switch statement in java is like an 'if statement' but more organized. However, in spite of the organization of the switch statement, these types of conditional statements take up a lot of lines of code. One example of switch statements is:


public class switch {

public static void main(String[] args) {

int day = 5;

switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid Day.");
break;
}
}
}


The above code will print out “Friday”. As you can see, this is just like an If statement but it takes up a lot of lines of code.
Reply

#2
Woo! This is great bro! :biggrin: Nice work again
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through