JAVA®Programming 1
JAVA®Programming 1
Programming
I
JAVA
Environment
*a JAVA
application contains
a subprogram called main
*there can also be other subprograms which
are generally referred to as methods
*it is convention is to have class names
begin with an upper case letter, and
object and method names begin with a
lower case letter
JAVA
Template
// The "X" class.
import java.awt.*;
import hsa.Console;
public class X
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console();
// Place your program here. 'c' is the output console
} // main method
} // X class
Comments
*there are two sorts of comments in JAVA
/* This is a comment. It does not end here...
but keeps on going until it reaches a */
// This is also a comment which ends at the end of the line
*every program should have a header with the
following information
//name:
//date:
//description:
*include comments throughout the program to indicate
what is happening
Example Programs
1. c.print("Hello world!");
output: Hello world!
2. c.print("a");
c.print("b");
output: ab
3. c.println("a");
c.println("b");
output: a
b
4. c.println();
output:
[nothing]
Example Programs
4. c.println(1 + 2);
output: 3
5. c.println(1 + 2 + " is three");
output: 3 is three
6. c.print(1/3);
output: 0
7. c.print(1.0/3.0);
output: 0.3333333333333333
[16 fractional digits]
8. c.print(Math.sqrt(2));
output: 1.4142135623730951
[16 fractional digits]