Creating programs – Java Tutorial

Creating programs – Java Tutorial

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:

The Java Programming Language

 

public States that the program is accessible to other programs.
class Identifies the program as a class.
{ } These braces are required at the beginning and end of each method or statement.
static Means the main() method will never change.
void Means main() will not return a value when called.
(String[] args) Represents an argument passed to the main() method.
boolean, char, double, int Data 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. 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.