Contents
- ./AccountCheck.java
- ./ArrayLength.java
- ./ArraysExample.java
- ./ArrayVars.java
- ./Countdown.java
- ./Counter.java
- ./Equals.java
- ./Grades.java
- ./StringConditionals.java
./AccountCheck.java 1/9
[top][prev][next]
/**
* This class demonstrates scope and control flow in Java.
*
* @author Sara Sprenkle
*
*/
public class AccountCheck {
/**
* Called when user runs
* java AccountCheck
*/
public static void main(String[] args) {
int purchaseAmount = 700;
int availableCredit = 500;
boolean approved = false;
if (purchaseAmount < availableCredit) {
availableCredit -= purchaseAmount;
/* scope of following declared variable
is within this block of code
and cannot be seen outside of this block.
boolean approved = false;
*/
approved = true;
}
if( ! approved )
System.out.println("Denied");
}
}
./ArrayLength.java 2/9
[top][prev][next]
/**
* This class demonstrates use of "length" field for arrays
* as well as for loops 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 = array.length -1; i >= 0; i--) {
System.out.println(array[i]);
}
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/9
[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/9
[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?
int x = 1;
// This will display fibNums[1]
System.out.println(fibNums[x++]);
x = 1;
// This will display fibNums[2]
System.out.println(fibNums[++x]);
System.out.println(otherFibNums == fibNums);
String[] myArgs = args;
System.out.println(myArgs == args);
}
}
./Countdown.java 5/9
[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!");
}
}
./Counter.java 6/9
[top][prev][next]
/**
* This class demonstrates using a while loop
*
* @author Sara Sprenkle
*/
public class Counter {
public static void main(String[] args) {
int counter = 0;
while (counter < 5) {
System.out.println(counter);
counter++;
}
System.out.println("Done: " + counter);
}
}
./Equals.java 7/9
[top][prev][next]
/**
* Demonstrates how various checks of
* equality work 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 = "same"; //for initial discussion
String string4 = args[0]; //enter "same" as a command-line argument
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)
}
}
./Grades.java 8/9
[top][prev][next]
/**
* This class demonstrates using a switch statement.
*
* Your task: Explain the output when grade is set to be 'b'
* and then when grade is set to 'd'.
*
* @author Sara Sprenkle
*
*/
public class Grades {
/**
* Called when user runs
* java Grades
*/
public static void main(String[] args) {
char grade = 'b';
switch(grade) {
case 'a':
case 'A':
System.out.println("Congrats!");
break;
case 'b':
case 'B':
System.out.println("Not too shabby!");
break;
case 'c':
case 'C':
case 'd':
case 'D':
System.out.println("You are passing but you could improve.");
case 'f':
case 'F':
System.out.println("Not good. You failed.");
default:
System.out.println("Error: not a grade");
}
}
}
./StringConditionals.java 9/9
[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];
// this can never be true
if( string1 == argString) {
System.out.println("Exact match!");
}
else 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.