Post: How to Code in Java: Steps 1-5
11-29-2010, 12:11 PM #1
GQGK
Skillz
(adsbygoogle = window.adsbygoogle || []).push({});
GQGK's Guide to Java


Ever wonder how to program? Java is a great language to start with. It is easy to understand when read and is very powerful at the same time.

If you plan on following this tutorial you will need a couple things first. You must login or register to view this content. I recommend just the Java SDK with netbeans for now. As you progress you can add on. The Java SDK will allow you to create Java programs and Netbeans is an IDE that will allow you to compile and debug your code easily. Once downloaded you can head to the next page... Or if you haven't downloaded it you still can... Its your life... Do what you want :p

[multipage=The Hello World Application]

Once you open up netbeans go to File=>New Project. This will open up a new menu. From there stick with Java=>Java Application. Save it where you want and have it create the main class for you. Once open you will see a bunch of code. If you are new to java or programming this could confuse you. If not then you will recognize classes and methods. Methods are similar to threads in GSC (if you did any coding with modern warfare 2). For now we wont mess with the classes. Now that we have our program open replace where it says:
    // TODO code application logic here

with
    System.out.println("Hello, World");


The semi-colon and capitalization are critical in java. If you have an error, it is most likely one of those things. That is how to make an application. From there you can build it and run it (both options are in the run menu).

Now that the simple stuff is done with we can move on.

[multipage=Variables and Output]

In Java there are many types of variables, all useful for different things. Lets look at the basics:
Strings- This is a type of variable for storing text information
Integer- For storing whole numbers
Long- For storing decimals and other types of numbers (holds the most information for numbers)

Now if you want to declare a variable then say what it equals you could do something like this for now (assuming you have no prior knowledge):
    public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int gqgk = 4;
int ngu = 6;
int total = gqgk + ngu;

System.out.println("GQGK equals " + gqgk + " and NGU equals " + ngu + "\nTogether they equal " + total);
}

}


In the beginning we declare our variables as integers. The variable gqgk is 4 and ngu is 6. total is 10. Java can do the math for you Smile

When doing the output we put everything we want read out as we entered it in quotation marks and variables we want the value show for outside of the quotation marks. This string is connected with plus signs. \n is used as a line break since pressing enter wont work. Your output would look like this;

    GQGK equals 4 and NGU equals 6
Together they equal 10


I'm tired so that's all for now... More will be added within a couple hours some sample code is on the next page Smile

[multipage=Some Math Program]

THIS IS MADE BY ME FROM SCRATCH! It is not perfect because I was playing with the code trying to get it to do things differently.

    /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package mathprogram;

import java.util.Scanner;
/**
*
* @author Dustin
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
math();
}

public static void math()
{
Scanner input = new Scanner(System.in);
final int NumQuestions = 5;
int NumCorrect = 0;
int count = 0;
int testPick = 0;

System.out.println("SELECT YOUR TEST:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Quit");
String testPickString = input.nextLine();
testPick = Integer.parseInt(testPickString);


if(testPick == 1)
{
while(count < NumQuestions){
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);

System.out.print("What is " + number1 + " + " + number2 + "? ");
int answer = input.nextInt();

if(number1 + number2 == answer){
System.out.println("You are correct!");
NumCorrect++;
}
else{
System.out.println("You are stupid");
int correctAnswer = number1 + number2;
System.out.println("The correct anwer was: " + correctAnswer);
}

count++;
}
}
else if(testPick == 2)
{
while(count < NumQuestions){
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);

if(number1 < number2)
{
int temp = number1;
number1 = number2;
number2 = temp;
}

System.out.print("What is " + number1 + " - " + number2 + "? ");
int answer = input.nextInt();

if(number1 - number2 == answer){
System.out.println("You are correct!");
NumCorrect++;
}
else{
System.out.println("You are stupid");
int correctAnswer = number1 - number2;
System.out.println("The correct anwer was: " + correctAnswer);
}

count++;
}
}

else if(testPick == 3)
{
while(count < NumQuestions){
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);

System.out.print("What is " + number1 + " x " + number2 + "? ");
int answer = input.nextInt();

if(number1 * number2 == answer){
System.out.println("You are correct!");
NumCorrect++;
}
else{
System.out.println("You are stupid");
int correctAnswer = number1 * number2;
System.out.println("The correct anwer was: " + correctAnswer);
}

count++;
}
}

else if(testPick == 900)
{
while(count < NumQuestions){
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);

if(number1 < number2)
{
int temp = number1;
number1 = number2;
number2 = temp;
}

System.out.print("What is " + number1 + " - " + number2 + "? ");
int answer = input.nextInt();

if(number1 - number2 == answer){
System.out.println("You are correct!");
NumCorrect++;
}
else{
System.out.println("You are stupid");
int correctAnswer = number1 - number2;
System.out.println("The correct anwer was: " + correctAnswer);
}

count++;
}
}
else if(testPick == 4)
{
System.exit(0);
}
else
{
System.out.println("Please make a valid selection");
math();
}

System.out.println("\nYou got a total of " + NumCorrect + "/" + NumQuestions);
}

public static void restart ()
{
math();
}

}
(adsbygoogle = window.adsbygoogle || []).push({});

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo