Contents
- ./AccountCheck.java
- ./ArrayLength.java
- ./ArraysExample.java
- ./ArrayVars.java
- ./CommandLineArgs.java
- ./Countdown.java
- ./Equals.java
- ./StringConditionals.java
./AccountCheck.java 1/8
[top][prev][next]
/**
* This class demonstrates control flow in Java
*
* @author Sara Sprenkle
*
*/
public class AccountCheck {
/* Alternatively, could make the variable have a "class-level" scope
instead of just being within the method.
Probably not appropriate for this problem, but may be appropriate
in other cases.
static boolean approved = false;
*/
/**
* Called when user runs
* java AccountCheck
*/
public static void main(String[] args) {
int purchaseAmount = 700;
int availableCredit = 500;
if (purchaseAmount < availableCredit) {
availableCredit -= purchaseAmount;
/* scope of variable is within this block of code
and cannot be seen outside of this block. */
boolean approved = true;
approved = true;
}
if( ! approved )
System.out.println("Denied");
}
}
./ArrayLength.java 2/8
[top][prev][next]
/**
* This class demonstrates use of "length" field for arrays and the foreach
* loop.
*
* @author Sara Sprenkle
*
*/
public class ArrayLength {
/**
* Called when user runs
* java ArrayLength
*/
public static void main(String[] args) {
int[] array = new int[10];
for (int i = 0; i < array.length; i++) {
array[i] = i * 2;
}
for (int i = array.length -1; i >= 0; i--) {
System.out.println(array[i]);
}
// alternative for loop to iterate through the array
for( int element : array ) {
System.out.println(element);
}
}
}
./ArraysExample.java 3/8
[top][prev][next]
// import the Arrays class so that we can use its functionality
import java.util.Arrays;
/**
* This class demonstrates using arrays and the Arrays class
*
* @author Sara Sprenkle
*
*/
public class ArraysExample {
/**
* Called when user runs
* java ArraysExample
*/
public static void main(String[] args) {
double[] array = new double[10];
// fill the array with PI... mmm... pie... using the Arrays class
Arrays.fill(array, Math.PI);
for( int i=0; i < array.length; i++ ) {
System.out.println("array[" + i + "] = " + array[i]);
}
}
}
./ArrayVars.java 4/8
[top][prev][next]
/**
* Demonstrates that assigning an array variable to another variable
* does *not* make a copy of an array. Both variables are
* referencing the same array.
* @author CSCI209
*/
public class ArrayVars {
public static void main(String[] args) {
int [] fibNums = {1, 1, 2, 3, 5, 8, 13};
int [] otherFibNums;
otherFibNums = fibNums;
otherFibNums[2] = 99;
System.out.println(otherFibNums[2]);
System.out.println(fibNums[2]);
// Each statement above will output 99. Why?
}
}
./CommandLineArgs.java 5/8
[top][prev][next]
/**
* Example demonstrates use of command-line arguments
*/
public class CommandLineArgs {
public static void main(String[] args) {
if( args.length < 1 ) {
System.out.println("Error: invalid number of arguments");
System.exit(1);
}
// use the argument ...
System.out.println("First argument is: " + args[0] );
for(int i = 0; i < args.length; i++ ) {
System.out.println(args[i]);
}
}
}
./Countdown.java 6/8
[top][prev][next]
/**
* This class demonstrates a counting for loop
*
* @author Sara Sprenkle
*
*/
public class Countdown {
public static void main(String[] args) {
System.out.println("Counting down...");
for (int count=5; count >= 1; count--) {
System.out.println(count);
}
System.out.println("Blastoff!");
}
}
./Equals.java 7/8
[top][prev][next]
/**
* Demonstrates different equals with Strings
* Run as
* java Equals <command-line argument>
* @author Sara Sprenkle
*/
public class Equals {
public static void main(String args[]) {
String string1 = "same";
String string2 = string1;
// The following statement doesn't create a _new_ String object/memory
// allocation Java memory optimization
String string3 = "same";
String string4 = args[0]; //enter "same" as a command-line argument
//String string4 = "same"; //for first half of lecture
System.out.println("string1 == string2? " + (string1==string2));
System.out.println("string2 == string3? " + (string2==string3));
System.out.println("string1 == string4? " + (string1==string4));
// output should be
// true
// true
// false, regardless of what user enters
System.out.println("string1 equals string2? " + (string1.equals(string2)));
System.out.println("string2 equals string3? " + (string2.equals(string3)));
System.out.println("string1 equals string4? " + (string1.equals(string4)));
// output should be
// true
// true
// true (depending on what user enters)
}
}
./StringConditionals.java 8/8
[top][prev][next]
/**
* Example using methods in conditionals.
* Enter "same" as command-line argument to guess the string.
*
* @author Sara Sprenkle
*/
public class StringConditionals {
public static void main(String args[]) {
String string1 = "same";
if( args.length < 1 ) {
System.out.println("Error: invalid number of arguments");
System.out.println("Usage: java StringConditionals <guess>");
System.exit(1);
}
String argString = args[0];
if( string1.equals(argString) ){
System.out.println("You guessed my string!");
} else {
System.out.println("Nice try, but you didn't guess my string.");
}
}
}
Generated by GNU Enscript 1.6.5.90.