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:
  • 779 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My book: the java journal: chapters 11-16

#1
Chapter 11: Multiple Methods

Contrary to what you may be thinking, there are multiple methods in java. To create a method we do public/private void methodname(). Public/private defines whether or not the contents of said methods can be seen by other methods. Along with void, methods be type specific. For example, public int methodname(), public double methodname(). Methods can also have operators in their parenthesis. For example, public void methodName(String a, String b, int c, double d){code block}. One example of multiple methods is:

import java.util.Scanner;


public class loops {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a, b;
System.out.print("Enter first number: ");
a = scan.nextInt();
System.out.print("Enter second number: ");
b = scan.nextInt();
System.out.print("A+B= " + add(a,b));
}
public static int add(int a, int b){//static means never changing and is used for syntax
return a + b;
}
}

The above code does the following:

1. Sets a to an inputted number.
2. Sets b to an inputted number.
3. Calls the method ‘add’ and substitutes a and b in the method parenthesis with a and b define in the main method.
4. Returns the sum of a and b (return a + b)
5. Goes back to the method that it was called and prints out the returned answer along with “A+B= ”.

Procedure 2: Menu Program

In this procedure we will be making a menu program using input, if statements and loops.

import java.util.Scanner;


public class Menu {
static String password;
static int logintries = 5;
public static void main(String[] args) {
run();//goes to a method named "run"
}

private static void run() {
Scanner scan = new Scanner(System.in);
String loginreg;
System.out.print("Login or Register?: ");
loginreg = scan.next();

if (loginreg.equalsIgnoreCase("Login")){
login();
}else if (loginreg.equalsIgnoreCase("register")){
register();
}else{
run();
}

}

private static void register() {
Scanner reg = new Scanner(System.in);
String regpass;
System.out.print("Enter Desired Password: ");
regpass = reg.next();

password = regpass;//sets the password to the input recieved

if (password == ""){
register();//if the passwords length is 0 (no letters or whitespace) make a new one
}else{
logintries = 5;
login();
}

}

private static void login() {
if (password != ""){//if the password is larger then an empty string
if (logintries != 0){
Scanner pass = new Scanner(System.in);
String chance;
System.out.print("Enter your password: ");
chance = pass.next();

if (chance.equals(password)){
Menu();
}
else{//if chance != password
logintries--;
login();
}
}else{//if tries = 0;
register();
}
}else{//if password.length == 0
register();
}
}

private static void Menu() {
Scanner menuchoice = new Scanner(System.in);
int choice;
System.out.println("**************************************");
System.out.println("* *");
System.out.println("* Menu *");
System.out.println("* *");
System.out.println("* 1.Calculator *");
System.out.println("* 2.Change password *");
System.out.println("* 3.Log out *");
System.out.println("* *");
System.out.println("* *");
System.out.println("**************************************");

choice = menuchoice.nextInt();

if(choice == 1){
calculator();
}
else if(choice == 2){
register();
}else if (choice == 3){
System.exit(0);//shuts down the program
}
else{
Menu();
}

}

private static void calculator() {
Scanner type = new Scanner(System.in);
String mathtype;
System.out.print("Enter math type(mod,/,*,+,-): ");
mathtype = type.next();

if(mathtype.equals("mod")){
Scanner math = new Scanner(System.in);
double a,b;

System.out.print("Enter first number: ");
a = math.nextDouble();
System.out.print("Enter second number: ");
b = math.nextDouble();
System.out.println("Answer: " + mod(a,b));

Scanner again = new Scanner(System.in);
String yesno;

System.out.print("Use calculator again?(y/n): ");
yesno = again.next();

if (yesno.equalsIgnoreCase("y")){
calculator();
}else if (yesno.equalsIgnoreCase("n")){
Menu();
}
}
else if(mathtype.equals("+")){
Scanner math = new Scanner(System.in);
double a,b;

System.out.print("Enter first number: ");
a = math.nextDouble();
System.out.print("Enter second number: ");
b = math.nextDouble();
System.out.println("Answer: " + add(a,b));

Scanner again = new Scanner(System.in);
String yesno;

System.out.print("Use calculator again?(y/n): ");
yesno = again.next();

if (yesno.equalsIgnoreCase("y")){
calculator();
}else if (yesno.equalsIgnoreCase("n")){
Menu();
}
}
else if(mathtype.equals("-")){
Scanner math = new Scanner(System.in);
double a,b;

System.out.print("Enter first number: ");
a = math.nextDouble();
System.out.print("Enter second number: ");
b = math.nextDouble();
System.out.println("Answer: " + sub(a,b));

Scanner again = new Scanner(System.in);
String yesno;

System.out.print("Use calculator again?(y/n): ");
yesno = again.next();

if (yesno.equalsIgnoreCase("y")){
calculator();
}else if (yesno.equalsIgnoreCase("n")){
Menu();
}
}
else if(mathtype.equals("*")){
Scanner math = new Scanner(System.in);
double a,b;

System.out.print("Enter first number: ");
a = math.nextDouble();
System.out.print("Enter second number: ");
b = math.nextDouble();
System.out.println("Answer: " + mult(a,b));

Scanner again = new Scanner(System.in);
String yesno;

System.out.print("Use calculator again?(y/n): ");
yesno = again.next();

if (yesno.equalsIgnoreCase("y")){
calculator();
}else if (yesno.equalsIgnoreCase("n")){
Menu();
}
}
else if(mathtype.equals("/")){
Scanner math = new Scanner(System.in);
double a,b;

System.out.print("Enter first number: ");
a = math.nextDouble();
System.out.print("Enter second number: ");
b = math.nextDouble();
System.out.println("Answer: " + div(a,b));

Scanner again = new Scanner(System.in);
String yesno;

System.out.print("Use calculator again?(y/n): ");
yesno = again.next();

if (yesno.equalsIgnoreCase("y")){
calculator();
}else if (yesno.equalsIgnoreCase("n")){
Menu();
}
}
else{
calculator();
}
}
public static double mod(double a, double b){
return a % b;
}
public static double add(double a, double b){
return a + b;
}
public static double mult(double a, double b){
return a * b;
}
public static double sub(double a, double b){
return a - b;
}
public static double div(double a, double b){
return a / b;
}
}
The above program requires the user to login/register a password then login to go to a menu, in the menu, they can choose to change their password, go to a calculator, or shut down the program.
~~~!!! End Of Unit 2 !!!~~~








































~~~!!! Start Of Unit 3 !!!~~~

Chapter 12: Arrays/ArrayLists

Arrays and ArrayLists are two topics in programming that are very important to the fundamentals of every programming language. An array is a preset group of integers, strings, characters, etc. There are three ways to form an array:
1. DataType[] arrayname = new DataType[ArraySize];
2. DataType arrayname[] = {item 0, item 1, item 2, item 3…item n};
3. DataType[] arrayname = {item 0, item 1, item 2, item 3…item n};
As you can see, the integer definitions of items in an array always start with ‘0’ instead of ‘1’. An example of an array is:

import java.util.*;//imports everything in the java.util package

public class Array{

static Scanner input = new Scanner(System.in);

public static void main(String[] args) {
int numNames = getInt("Number of names?");//calls the method “getint()”
String[] names = new String[numNames];//the array (takes the size of the array as the number of names inputed)
for (int i = 0; i < names.length; i++) {
names[i] = getString("Enter name #" + (i + 1));//sets each item of the array as a name inputed
}
for (int i = 0; i < names.length; ++i) {
System.out.println(names[i]);//prints out name at the index number (i) in the array
}
}

public static int getInt(String prompt) {
System.out.print(prompt + " ");
int integer = input.nextInt();
input.nextLine();
return integer;
}

public static String getString(String prompt) {
System.out.print(prompt + " ");
return input.nextLine();
}
}

In the above program, the user is asked to enter a number of names to enter. Once the number is entered, the number is set to the size of an array. Next, the program asks the user for names to fill in the array; once the array is full (the amount of names equals the number inputted) all the names in the array will be printed out. If one wants to print out a full array all together (instead of the items in it 1 by 1) you must use the .toString() function which changes the array to a string.For example:

System.out.print(ArrayName.toString());

Along with arrays, array lists are a huge part of programming. Array lists are like arrays but you can add things, remove things, etc. To create an array list you do the following:
ArrayList<Data Type> ArrayName = new ArrayList<Data Type>();
To add Things to an array one must use the .add function. For example:

ArrayList<String> names = new ArrayList<String>();
names.add(“Daniel”);
names.add(“Charlie”);
names.add(“Tracy”);

To remove items from an array one must use the remove function. For example;

names.remove(“Daniel”);
names.remove(“Charlie”);
names.remove(“Tracy”);

One example of array list use is:

import java.util.*;

class ArrayList{

public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
System.out.println("Initial size of al: " +
al.size());
// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " +
al.size());
// display the array list
System.out.println("Contents of al: " + al);
// Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " +
al.size());
System.out.println("Contents of al: " + al);
}
}

The above program’s output is:

Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]

ArrayLists can also be used with user input:

import java.util.*;

class InputArrayList{
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
ArrayList al = new ArrayList();
System.out.print("Amount of objects to put in arraylist?: ");
int size = scan.nextInt();

for (int i = 1; i < size + 1; i++){
Scanner items = new Scanner(System.in);
String addItem;
System.out.println(i+": Add something to the arraylist: ");
addItem = items.next();
al.add(addItem);
}

System.out.print("Size of ArrayList is: " +al.size()+ "\n");
System.out.print("Contenets of ArrayList are: " + al);
}
}


The above program does almost the same thing as the array program we made. It asks the user for a size, then it prompts the user to add things to the ArrayList. Once the amount of things in the ArrayList equal the size, the ArrayList is printed out.

Chapter 13: 2D arrays

A two-dimensional array is an array who’s items are arrays as well. To form one of these array you use:
DataType[][] Arrayname = {{item,item,item…},
{item,item,item…},
{item,item,item…}};

As you can see, all the items in the array “Arrayname” are put in other arrays. This is useful for things like maps, draw-outs, etc. Furthermore, you can easily pinpoint an item on the ‘graph’ of a two-dimensional array by using:

Arrayname[YvalIndex][XvalIndex]


For example:

String[][] Names = {{"Daniel", "John", "Willis"},
{"David", "Alex", "Andrew"},
{"Alec", "Adam", "Anthony"}};

System.out.print(Names[1][2].toString());

The Above will print out “Andrew” because it is at the 2nd Array (first array has index 0) and is the 2nd item in the array (first item has index 0).







Chapter 14: OOP (Object Oriented Programming).

Object oriented programming is programming with multiple classes. This keeps your code neat and organized. When working with mulitiple classes, you must create an object of other classes being used (hence, the name: ‘Object oriented’ programming).To do this, we use:

ClassName CustomName = new ClassName();

One example of object oriented programming is:

Main Class:

import java.util.*;

class Main{
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int size;
System.out.print("Enter size of arrayList: ");
size = scan.nextInt();

arraylist list = new arraylist();//creates an object of the arraylist class

for (int i = 1; i < size + 1; i++){
Scanner _scan = new Scanner(System.in);
String item;
System.out.print(i+": ");
item = _scan.next();
list.arli.add(item);
}
list.al();//will run the method 'al' in arraylist class
}
}

arraylist Class:

import java.util.ArrayList;


public class arraylist {
ArrayList arli = new ArrayList();
public void al(){
System.out.println("List size: "+ arli.size());
System.out.println("Items: "+arli.toString());

}
}


The above program will do the same thing as the Array-list program did two chapters ago. However, This program utilizes a second class called “arraylist” to optimize the organization of our code.

Chapter 15: Threads

Threads in java allow the program to run faster and more efficiently then programs without friends, a program that once took two second to compile and run can take 0.5 seconds using threads. What a thread does is take the load of the compiler by going through a certain path in the program by itself while letting other threads (or the compiler itself) to look at the other lines of code more productively. For example, one thread could take the role of an ‘If statement’ while another could have the role of an ‘Else statement’. One example of threads is:

class Thread
{
public static void main (String [] args)
{
MyThread mt = new MyThread ();
mt.start ();//starts up the thread
for (int i = 0; i < 50; i++)//this is the threads function/role
System.out.println ("i = " + i + ", i * i = " + i * i);
}
}
class MyThread extends Thread//is the thread
{
public void run ()
{
for (int count = 1, row = 1; row < 20; row++, count++)//does this seperatly than the above class but run simultaniously
{
for (int i = 0; i < count; i++)
System.out.print ('*');
System.out.print ('\n');
}
}
}

the output of this program is:

i = 0, i * i = 0
i = 1, i * i = 1
i = 2, i * i = 4
i = 3, i * i = 9
i = 4, i * i = 16
i = 5, i * i = 25
i = 6, i * i = 36
i = 7, i * i = 49
i = 8, i * i = 64
i = 9, i * i = 81
i = 10, i * i = 100
i = 11, i * i = 121
i = 12, i * i = 144
i = 13, i * i = 169
i = 14, i * i = 196
i = 15, i * i = 225
i = 16, i * i = 256
i = 17, i * i = 289
i = 18, i * i = 324
i = 19, i * i = 361
i = 20, i * i = 400
i = 21, i * i = 441
i = 22, i * i = 484
*
**
***
****
*****
******
*******
********
*********
*****i = 23, i * i = 529
i = 24, i * i = 576
i = 25, i * i = 625
i = 26, i * i = 676
i = 27, i * i = 729
i = 28, i * i = 784
i = 29, i * i = 841
i = 30, i * i = 900
i = 31, i * i = 961
i = 32, i * i = 1024
i = 33, i * i = 1089
i = 34, i * i = 1156
i = 35, i * i = 1225
i = 36, i * i = 1296
i = 37, i * i = 1369
i = 38, i * i = 1444
i = 39, i * i = 1521
i = 40, i * i = 1600
i = 41, i * i = 1681
i = 42, i * i = 1764
i = 43, i * i = 1849
i = 44, i * i = 1936
i = 45, i * i = 2025
i = 46, i * i = 2116
i = 47, i * i = 2209
i = 48, i * i = 2304
i = 49, i * i = 2401
*****
***********
************
*************
**************
***************
****************
*****************
******************
*******************


Without threading, this program would take much longer then it did to compile and run (This program took approximately 0.15 seconds to compile and run).


Chapter 16: Multi-Threading

Multi-Threading is using multiple threads in one program to run the program as efficiently as possible. For example, a multi-threaded GUI (Graphical User Interface) can keep responding to user inputs while it does something else, like print a document or send an email etc. One example of multi-threading is:

import java.util.Random;

class thread
{
public static void main (String [] args)
{
FinTrans ft = new FinTrans ();
TransThread tt1 = new TransThread (ft, "Deposit Thread");
TransThread tt2 = new TransThread (ft, "Withdrawal Thread");
tt1.start ();
tt2.start ();
}
}
class FinTrans
{
public static String transName;
public static double amount;
}
class TransThread extends Thread
{
private FinTrans ft;
TransThread (FinTrans ft, String name)
{
super (name); // Save thread's name
this.ft = ft; // Save reference to financial transaction object
}
public void run ()
{
for (int i = 0; i < 100; i++)
{
if (getName ().equals ("Deposit Thread"))
{
// Start of deposit thread's critical code section
ft.transName = "Deposit";
try
{
Thread.sleep ((int) (Math.random () * 1000));//pauses the thread for a random amount of time
}
catch (InterruptedException e)
{
}
Random r = new Random();
ft.amount = r.nextInt(6000)+1;
System.out.println (ft.transName + " " + ft.amount);
// End of deposit thread's critical code section
}
else
{
// Start of withdrawal thread's critical code section
ft.transName = "Withdrawal";
try
{
Thread.sleep ((int) (Math.random () * 1000));//pauses the thread for a randomamount of time
}
catch (InterruptedException e)
{
}
Random r = new Random();
ft.amount = r.nextInt(5000)+1;
System.out.println (ft.transName + " " + ft.amount);
// End of withdrawal thread's critical code section
}
}
}
}

The above program uses multithreading to “Withdraw” and “Deposit” a random amount. This program shows how multiple threads can be run at once and still give out results efficiently.

Procedure 3: Bank Program

In this procedure we will be using threading and Array-Lists to create a bank program.

import java.util.ArrayList;
import java.util.Random;


public class bank {
public static void main(String[] args){
banker bk = new banker();
bankarray bka = new bankarray();
bk.start();
bk.run();
bka.start();
bka.run();

System.out.print("Recent history: " + bka.bankal.toString());

}
}

class banker extends Thread {
public void run(){
Random r = new Random();
int money = r.nextInt(10000);



bankarray bka = new bankarray();

for(int i = 0; i < 11; i++){
bka.bankal.add(money);
}
}
}


class bankarray extends Thread{
ArrayList bankal = new ArrayList();
public void run(){
if (bankal.size() == 10){
System.out.println("Finished creating history");
}
}
}

The following will print out:

Finished creating history.
Recent History: [randomnumber,randomnumber,randomnumber….]

This program took about 0.25 seconds to compile and run

~~~!!! End Of Unit 3 !!!~~~
Reply

#2
Great so far! Hard work huh, I respect your dedication
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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