New Mexico High School Supercomputing Challenge
Summer Teacher Training Session – WNMU
Create, Compile, and Run Your Program
Case Sensitivity and Whitespace
Java
– Quick Background
Created by Sun Microsystems ~ 1995
Can produce applications and applets
Java is not Javascript
Similar to C (C++) in syntax
Major differences between C++ and Java
Can get “Java” for free
To write your program use the text editor
“pico”
mode:~> pico
Example.java
To compile your program use the “javac”
command (creates Example.class)
mode:~> javac
Example.java
To run your program use the “java” command
mode:~> java Example
For a program named “ProgramName.java”, the minimum
code is required:
class ProgramName
{
public static void main(String args[])
{
}
}
“ProgramName” should be descriptive of what the
program does.
The main program body goes between the inner pair of
braces.
Use the command “System.out.println();” and its
variants.
Comments are ignored by the compiler …
class ProgramName
{
public static void main(String args[])
{
// Single line comments can appear
alone
/* or can span
multiple lines like this
message */
}
}
Ex. Class != class (will get an error while
compiling)
Compiler Ignores most whitespace. Better to
structure into statements and code blocks for your benefit.
Use semicolons after statements! (not after “block
headers” in general)
class Hello
{
public static void main(String args[])
{
System.out.println(“Hello World!”);
}
}
class Hello {public
static void main(String args[]) {System.out.println(“Hello World!”);}}
General syntax:
data_type
identifier; // reserves a named location in memory
(or)
data_type
identifier=initial_value; // initializes memory location
int length=5;
int width=7;
int height=2;
int volume;
volume=length*width*height;
byte (-128,
127)
short (-32768,
32767)
int (+-
2 billion) Use This!
long HUGE
Floating Point types
(decimal numbers)
float (+-
3E38 6-7 digits of accuracy)
double (+-
1E308 15 digits of accuracy) Use This!
class Box // with
more variables
{
public static void main(String args[])
{
String s=”The volume is: ”;
int length=5;
int width=7;
int height=2;
int volume;
volume=length*width*height;
System.out.println(s + volume);
}
}
abstract boolean break byte case catch char
class const continue default do double else
extends false final finally float for goto
if implements import instanceof int interface
long native new null package private protected
public return short static strictfp super
switch
synchronized this throw transient true try
void
volatile while
Class (Program)
Name – Descriptive words, capitalization on first letter of each word
Ex. BoxVolume
Variable Name –
Descriptive words, capitalization on first letter of every word after
the first word
Ex. boxVolume
Constant Name –
All capital letters
Ex. PI
5 basic operators:
+, -, *, /, %
Note: When “+” is used with a
String it concatenates the arguments!
Order of operations apply.
()’s inside to outside
*, / left to right
+, - left to right
Increment and decrement operators ++ and –-
respectively
Ex. j++; same as j=j+1; (similar with --)
Other shorthand operators +=, -=, *=, /=
Ex. j+=num; same as j=j+num;
Logical (conditional) statements
allow you to make decisions about whether or not to execute a particular block
of code based upon logical expressions
Logical expressions
produce a true or false (Boolean) result.
Use relational operators: > >= == != <= <
Example:
class Bool
{
public static void main (String args[])
{
boolean test;
test = (75 < 50);
System.out.println(test);
}
}
Output:
false
General
Syntax:
if (expression)
{
//code to execute if expression is true
}
Example:
class Grade // This
is preferred over the next program
{
public static void main (String args[])
{
int grade = 75;
if (grade >= 60)
{
System.out.println(“You pass
the class!”);
}
}
}
or
class Grade // The
previous program is preferred
{
public static void main (String args[])
{
int grade = 75;
boolean pass;
pass = (grade >= 60);
if (pass)
{
System.out.println(“You pass
the class!”);
}
}
}
General Syntax
if (expression)
{
// code
to execute if expression is true
}
else
{
// code
to execute if expression is false
}
Example:
class Grade
{
public static void main (String args[])
{
int grade = 43;
if (grade >= 60)
{
System.out.println(“You pass
the class!”);
}
else
{
System.out.println(“Sorry, you
fail …”);
}
}
}
Example:
class Grade
{
public static void main (String args[])
{
int
grade=85;
if
(grade >= 60)
{
if
(grade >= 80)
{
System.out.println(“Excellent Work!”);
}
else
{
System.out.println(“Satisfactory Work.”);
}
}
else
{
System.out.println(“You fail the class
…”);
}
}
}
&&
(and), || (or), ! (not)
Example
class Grade
{
public static void main (String args[])
{
int grade=85;
if ((grade >= 60) &&
(grade <= 80))
{
System.out.println(“Satisfactory
Work.”);
}
}
}
class Grade
{
public static void main (String args[])
{
int grade=73;
if (grade >= 90)
{
System.out.println(“A”);
}
else if (grade >= 80)
{
System.out.println(“B”);
}
else if (grade >= 70)
{
System.out.println(“C”);
}
else
{
System.out.println(“Unsatisfactory”);
}
}
}
Loops
allow you to repeat a block of code as many times as you wish.
There
are three types of loops – all which can be used to accomplish the same task.
General
Syntax:
for
(initialization_expression; loop_condition; increment expression)
{
// code to repeat
}
example:
class SumUp
{
public static void main (String args[])
{
int limit=20;
int sum=0;
for (int j=1; j<=limit; j++) //
j++ shorthand for j=j+1
{
sum+=j; // this is the same as
sum=sum+j
}
System.out.println(“Sum of first 20
integers is: “ + sum);
}
}
General
Syntax:
while
(condition)
{
// code to repeat
}
example
class SumUp2
{
public static void main (String args[])
{
int integer=1;
int limit=20;
int sum=0;
while (integer <= limit)
{
sum+=integer;
integer++;
}
System.out.println(“Sum of first 20
integers is: “ + sum);
}
}
General
Syntax:
do
{
// code to repeat
}
while (condition); // notice the semicolon!
example
class SumUp3
{
public static void main (String args[])
{
int integer=1;
int limit=20;
int sum=0;
do
{
sum+=integer;
integer++;
} while (integer <= 20);
System.out.println(“Sum of first 20
integers is: “ + sum);
}
}
Stop!! Do Exercise 4
Memory
Constructs which allow for storage of multiple values in a single contiguous
unit.
Uses
a single identifier (for the array) and an index number (for the value
within an array).
0 |
1 |
2 |
3 |
4 |
79 |
84 |
52 |
87 |
66 |
“Bill_Exam”
Bill_Exam[0]=79,
Bill_Exam[1]=84, Bill_Exam[2]=52 …
Arrays
also have a length: Bill_Exam.length=5
Declaring
Arrays: int Bill_Exam[]=new int[5]; // reserves memory space
Bill_Exam[0]=79;
Bill
Exam[1]=84;
Etc.
Note: Before
elements are given specific values, the array is filled with all “zeros”.
(or) int Bill_Exam[]={79,84,52,87,66};
class Grade
{
public static void main(String args[]) //
?
{
int bill_grade[]={79,84,52,87,66};
double fin_grade; // why not “final”
int temp=0;
for (int j=0; j <
bill_grade.length; j++)
{
temp+=bill_grade[j];
}
fin_grade=temp/bill_grade.length;
System.out.println(“Bill\’s final
grade is: “ + fin_grade);
}
}
How would we do this program
otherwise?
What are the benefits?
Stop!! Do Exercise 5