/*
* U of M Hello world
*/
// This is a program that will say hello world
public class FirstProgram
{
public static void main(String[] args)
{
System.out.println("U
of M Hello World");
}
}
We need:
Strongly type language: variable must has declared type. There are eight primitive types. Six of them are numbers, one is character (used in Unicode), and one is a boolean.
Type | Storage | Range |
---|---|---|
byte | 1 byte | -128 to 127 |
short | 2 bytes | -32768 to 32767 |
int | 4 bytes | -2147483648 to 2147483647 |
long | 8 bytes | "Really small" to "Really big" |
Note:
The integer types do not depend on the machine on which you will
be running Java code, only performance will vary.
The sizes of all numeric types are platforms independent. (not 2
or 4).
Long integer has L suffix (e.g. 400000000L), Hex numbers has
prefix 0xABCD.
Type | Storage | Precision |
---|---|---|
float | 4 bytes | 6-7 significant digits |
double | 8 bytes | 15 significant digits |
Note:
double implies twice precision as float. (e.g. Use in matrix
computation)
float literal has suffix F.
default will be used as double.
int a; float b; char ch1, ch2, thisIsALongVariableName; boolean flag; int size;
int i = 10;
int foo; // this is a declaration
foo = 37; // this is an assignment
char myvar = '\u0041'; // decimal 65 => this is actually a A
Examples:
String firstName;
firstName = "Richard";
String lastName = "Sinn";
// Initializes fullName to "Richard Sinn":
String fullName = firstName + " " + lastName;
// Print out some strings:
System.out.print ("Your instructor's name is: ");
System.out.println (fullName);
// Initializes s1 to "d S":
// 6 is the first you want to copy
// 9 is the first one you do not want to copy
// Start from 0
// Length is 9-6 = 3
String s1 = fullName.substring (6, 9);
// Initializes nameLength to 12:
int nameLength = fullName.length ();
// Initializes ch to 'c':
// Range from 0 to fullName()-1
char ch = fullName.charAt (2);
// Replacement Example:
String greeting = "Hello";
greeting = greeting.substring(0, 3) + "p!"; // Help!
// Examples of comparisons:
// == indicates whether the String stores in the same place
// *ALWAYS* use equals to compare
String copy1 = fullName;
String copy2 = "Richard Sinn";
boolean testA = (copy1 == fullName); // true
boolean testB = (copy2 == fullName); // false
boolean testC = copy1.equals (fullName); // true
boolean testD = copy2.equals (fullName); // true
// Initializes i1 to 8 and i2 to -1:
// remember counting starts with 0
int i1 = fullName.indexOf (lastName);
int i2 = fullName.indexOf ("Carmen");
// Page 70 for more functions
Syntax:
if (condition)
{
block
}
else
{
block2
}
Examples:
int temperature; // Assume it gets initialized somehow. if (temperature < 32) System.out.println ("It is cold."); boolean move = false; if (temperature < 0) { System.out.println ("It is so cold that I will move to CA."); move = true; } if (move) System.out.println ("I am selling my house."); else System.out.println ("I am staying put."); boolean wearCoat; if (temperature >= 60) { System.out.println ("I am happy again."); wearCoat = false; } else { System.out.println ("It is chilly."); wearCoat = true; }
Examples:
int i = 0; while ((i * i) != 81) ++i; System.out.println ("The square root of 81 is: " + i); int j = 0; while (j < 10) { System.out.println ("j = " + j); ++j; }
Examples:
char ch = 'z'; do { System.out.print (ch); --ch; } while (ch != 'a'); // Loop doing the same thing while (ch != 'a') { System.out.print (ch); --ch; }
Examples:
int sum = 0; for (int i = 1; i <= 100; ++i) sum += i; int loops = 0; for (int j = 1000; j > 0; j /= 2) { System.out.println ("j = " + j); ++loops; } System.out.println ("It took " + loops + " loops.");
Example:
int day; // Assume this was initialized somewhere. boolean exerciseDay = false; boolean weekend = false; switch (day) { case 1; // Monday System.out.println("Monday ... Arr ... sucks"); break; case 2: // Tuesday exerciseDay = true; break; case 4: // Thursday exerciseDay = true; break; case 0: // Sunday break; case 6: // Saturday weekend = true; break; default: // Everything else. System.out.println ("Ordinary work day."); break; }
// This function adds all numbers from 1 to the argument // and returns the sum. int computeSum (int arg) { int sum = 0; for (int i = 1; i <= arg; ++i) sum += i; return sum; } // This function prints out all of the sums for each // odd number from 1 to 19. void functionB () { for (int i = 1; i <= 19; i += 2) System.out.println ("computeSum(i)=" + computeSum (i)); }
Example:
public static void main (String[] args) { // Print out the hello world System.out.println("Testing - Hello World"); }
Examples:
int[] temperatures = new int[31]; temperatures[0] = 45; temperatures[1] = 52; temperatures[2] = 53; // ... and so on. System.out.println ("The temperature on day 15 was " + temperatures[14]); int sum; for (int i = 0; i < temperatures.length; ++i) sum += temperatures[i]; float avg = sum / temperatures.length; // colors will be an array of length 4: String[] colors = { "red", "green", "blue", "orange" }; // Main function example public static void main (String[] args) { // Print out the command line arguments. for (int i = 0; i <. args.length; ++i) System.out.println ("Argument " + i + "=" + args[i]); }
Create a Java application (called Week2) that critiques a student based on his or her grade. The students name and grades are input to the application as command line arguments, in the following form:
java Week2 Sinn,Richard 98 97 96 96 95
Your application should contain five functions:
A few notes:
Float.valueOf ("62.5").floatValue ()
which will be helpful in converting a String (in the command line arguments) to a float (needed for the scores array).
Copyright 1996-2001 OpenLoop Computing. All rights reserved.