• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Java Java introduction!

Wasted

New Member
Joined
Jun 12, 2007
Messages
307
Reaction score
1
Location
United States
I didn't see much if anything relating to Java. So, I'm putting those prefixes to use! First off to introduce Java programing some vocabulary is going to be discussed. It is not necessary to understand the given vocabulary at the moment but all helps. The following tutorial will utilize the command prompt and the java JDK. For more help on this part visit Sun.

What Is an Object?
An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner.
What Is a Class?
A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.
What Is Inheritance?
Inheritance provides a powerful and natural mechanism for organizing and structuring your software. This section explains how classes inherit state and behavior from their superclasses, and explains how to derive one class from another using the simple syntax provided by the Java programming language.
What Is an Interface?
An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface. This section defines a simple interface and explains the necessary changes for any class that implements it.
What Is a Package?
A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. This section explains why this is useful, and introduces you to the Application Programming Interface (API) provided by the Java platform.

Assuming most know a variable,integer, etc is. So, that part will be skipped and I'm going to dive right into If, Else and Switch statements! Compiling the code to form a .class file will only work if the code doesn't contain any errors!

If, Else, and Switch Statements-
In Java, if statements will allow your computer program to do something only if some condition is met. For example, suppose you have a program called RaceCars.java, and you include the following code:
Code:
public class RaceCars {
     public static void main (String args[]) {
     int gasTankGallons = 12;
     if (gasTankGallons <= 0 ) System.out.println(“Your car just stalled out!”);
     }
}
You started by declaring a variable called gasTankGallons, and setting it equal to 12. You then asked the program to evaluate gasTankGallons and see if it met a condition, in this case, if the gasTankGallons were less than or equal to 0. If it met the condition, it would print the line: “Your car just stalled out!”

Sometimes you’ll want the program to do more than one thing if the condition is met. For instance, you may want this racecar program to display the text and take money away from the racecar driver. To do more than one thing, just create a new block, as shown in bold below. Remember, blocks start with { and end with }.
Code:
public class RaceCars {
     public static void main (String args[]) {
     int gasTankGallons = 12;
     int raceWinnings = 500;
     [B]if (gasTankGallons <= 0 ) {
     System.out.println(“Your car just stalled out!”);
     raceWinnings = raceWinnings – 100;
     }
     }
}
[/B]
You may also want your program to do one thing if the condition is true and another thing if the condition is false. Let’s pretend that if the gasTankGallons are not less than or equal to 0, the driver gets $100 and a message. In that case, you’ll add an else statement block to the code, as shown below in bold:
Code:
public class RaceCars {
     public static void main (String args[]) {
     int gasTankGallons = 12;
     int raceWinnings = 500;
     if (gasTankGallons <= 0 ) {
          System.out.println(“Your car just stalled out!”);
          raceWinnings = raceWinnings – 100;
     }
     [B]else {
          raceWinnings = raceWinnings +100;
          System.out.println(“Another successful lap!”);
     }[/B]
}
}

If and else statements work well when there are two options: either it meets the condition or it doesn’t. But when there are more than two options to consider, you’ll need to use a switch statement. The bad news is that when you use a switch statement, each option has to be very specific. They cannot be a range or an inequality as we used with the if/else statements. Going back to the RaceCars program, let’s say users will get different messages for coming in first, second, or third in the race:
Code:
public class RaceCars {
     public static void main (String args[]) {
     char winner;
     switch (winner) {
          case ‘A’:
          System.out.println(“Great work, Driver A!!”);
          break;
          case ‘B’:
          System.out.println(“Driver B, you’re the winner!!”);
          break;
          case ‘C’:
          System.out.println(“Driver C left everyone in the dust!!”);
          break;
     }
     }
}
You started by declaring a variable called winner that will store either driver A, B, or C. You can set it equal to each one to see if the program works. You then asked the program to evaluate winner and see if it met each condition. In this case, you’re checking to see which person was the winner. If it met the condition, it would print the line, and then the command break; tells it to get out of the switch statement.

[size=+2]To be continued...(whenever I have time)[/size]

I realize that this isn't very user friendly and the base of the tutorial is random. The user friendly aspect will pull my attention later.

Yours,
Wasted​
 
Last edited:
I have thought about learning Java, but I am still not sure :p Maybe this tutorial would make me reconsider :p
 
Last edited:
To quote a real life friend of mine, "Java is C++ Lite".

I prefer C++ way over Java, there's a bit to much missing in Java...
 
Very true. But, I started with Java and know it very well now. It was easy to learn and made the learning process of C++ easier (for me anyway) :D
 
Back
Top