Google

Lesson Two

Java Basics


In these next few lessons, you'll be learning the basics of the language and how to apply the language in its' uses. Say you wanted to convert minutes to hours and minutes. You'd probably need three variables, which are data that can change. You'd also need data types (such as: char for characters, boolean for data that is either true or false, or int for integer values) to specify what kind of variables you will be using. Here's a simple program that does that for you.

public class Time
{
public static void main(String[] args)
{
int minutes, hours, remMinutes;
minutes = 197;
hours = minutes / 60;
remMinutes = minutes % 60;
System.out.print("The time worked on the job is " + minutes);
System.out.print(" minutes or " + hours);
System.out.print(" hours and " + remMinutes);
System.out.print(" minutes");
}
}

The syntax, or rules of the language, require that the name of the class be the same as the name of the program. So, the name of the class is Time, the name of the program has to be Time.java. Here's a table that helps define what we're doing:

publicStates that the program is accessible to other programs.
classIdentifies the program as a class.
{ } These braces are required at the beginning and end of each method or statement.
staticMeans the main() method will never change.
voidMeans main() will not return a value when called.
(String[] args)Represents an argument passed to the main() method.
boolean, char, double, intData types that tell the compiler what form the variable data will be taking.
variable = expression;Declaration of the variable and what the variable is equal to.
System.out.println();Is the statement that processes your program.

You can use the expression of the variable to perform calculations and shorten your output. The + in the string System.out.print("The time worked on the job is " + minutes); is used to concatenate, or add a variable to, a string of data.

If you have a variable named "num" that contains the number "21" and you want to add it to a sentence, you would use the + sign, or concatenate, the variable to add it to the sentence like this:

System.out.print("You must be " + num + " to enter the night club.");

If you were to remove the + signs, the sentence would read:

You must be num to enter the night club.

This is not to be confused with the + sign being an operand. An operand is a symbol used to add two variables as shown below:

num1 + num2 = num3;

With this in mind, you could write another program, like this one, that will calculate the cost of carpeting a room.

public class Carpet
{
public static void main(String[] args)
{
int length = 15;
int width = 25;
double price = 2.29;
double costToCarpet = length * width * price;
System.out.print("The cost of carpeting a " + length);
System.out.print(" by " + width + " room is $");
System.out.println(costToCarpet);
}
}

Another program could be a way of calculating your weekly pay.

public class Payroll
{
public static void main(String[] args)
{
double wage = 6.25;
double hoursWorked = 35.75;
double total = wage * hoursWorked;
double withholding = total * .15;
double net = total - withholding;
System.out.println("My wage is $" + wage);
System.out.println("My hours are " + hoursWorked);
System.out.println("My gross is $" + total);
System.out.println("My withholding tax is $" + withholding);
System.out.println("My net pay is $" + net);
}
}

You can write a program for almost any function that you can think of in everyday life, although some functions may be a little more cumbersome than others.

Question: Okay, cool. But what about "object-orientation"?

Answer: That's our next focus, along with methods and classes.


Lesson One | Home | Lesson Three

If you have a questions about any of the lessons, feel free to ask.

We get our music from
1