CSE/IT 213 - Flow Control: New Mexico Tech
CSE/IT 213 - Flow Control: New Mexico Tech
import java.util.*;
import java.text.*;
public class CentimetersToInches {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter centimers: ");
double cm = in.nextDouble();
double totalInches = cm * INCH_PER_CM;
double feet = (int) (totalInches / INCHES_PER_FOOT);
double inches = totalInches - feet * INCHES_PER_FOOT;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(cm + " cm = " + feet + " ft. "
+ df.format(inches) + " in.");
}
private static final double INCH_PER_CM = 1/2.54;
private static final int INCHES_PER_FOOT = 12;
}
1
$ java CentimetersToInches
Enter centimers:
7
7.0 centimeters = 0.0 ft. 2.76 in.
static keyword
Instance variables or methods in a class may be declared
static.
Static variables and methods are associated with the
class not an object of the class.
Static variables are like global variables. But, they exist
inside a class.
Only a single copy of a static variable exists. Unlike
other instance variables which are copied to each new
object.
3
Static Methods
A static method is like a regular C function that is
defined inside a class.
Regular methods send messages to objects. There is
no receiver object for static methods. They operate on
a class.
Like static variables, the full name of a static method
is className.staticMethod() //For example Foo.bar()
refers to the class Foos static bar() method.
The Math class contains static methods, such as max(),
abs(), sin(), cos(), etc.. Their full names are Math.max(),
Math.sin(), and so on. Math.max()
4
final keyword
Instance variable, methods, and classes can be declared
final.
Final instance variables declares variable as a constant.
Set it and forget about it. Cannot change once set.
Final methods are methods that cannot be overridden.
No inherited class can redefine their meaning.
Final classes cannot be subclassed (i.e. inherited).
Control Flow
a lot like C
import java.util.*;
public class Middle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("enter three numbers");
System.out.print("enter number one: ");
int first = in.nextInt();
System.out.print("enter number two: ");
int second = in.nextInt();
System.out.print("enter number three: ");
int third = in.nextInt();
7
int middle;
if ( first >= second && first >= third) {
middle = Math.max(second, third);
}
else if (second >= first && second >= third) {
middle = Math.max(first, third);
}
else {
middle = Math.max(second, third);
}
System.out.println("middle = " + middle);
}
}
$ java Middle
if (condition) {
statement
} else {
statement
}
condition a boolean value (true or false). Like C the
relational operators are
<, , >, , ==, ! =
and the logical operators
&&, ||, !
8
$ java StringTest
foo equals foo
This is wrong!
This is an artifact of the java compiler. The compiler
finds all identical strings in a class and makes only one
copy. Identical strings point to each other.
The == operator is testing for object identity, not
equality. That is, do the objects point to the same
object.
This code has the same literal string but are not equal
using ==
public static void main(String[] args) {
String s1 = "foo";
String s2 = String.valueOf(new char [] {f,o,o} );
if (s1 == s2) {
System.out.println(s1 + " equals " + s2);
}
else {
System.out.println(s1 + " does not equal " + s2);
}
}
}
java StringTest
foo does not equal foo
10
For Loops
import java.util.*;
public class SumN {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Sum the numbers 1 to N");
System.out.print("enter N: ");
int n = in.nextInt();
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += i;
}
System.out.println("sum of 1 to " + n + " is " + sum);
}
12
}
$ java SumN
Sum the numbers 1 to N
enter N: 100
sum of 1 to 100 is 5050
$ java PrintArray
10
20
20
40
While loops
public class OneAtATime {
public static void main(String[] args) {
String s = "abcdef";
int i = 0;
while (i < s.length()) {
System.out.println(s.charAt(i));
i++;
}
}
}
15
$ java OneAtATime
a
b
c
d
e
f
16
Do loops
$ java BottlesOfBeer
there are 6 left
there are 5 left
there are 4 left
there are 3 left
there are 2 left
there is 1 left
there is no more left
do loops always execute at least once; while loops only
execute if condition is true
switch Statement
works on integer types
public class DaysOfWeek {
public static void main(String[] args) {
for (int i = 0; i < 7; i++) {
switch(i) {
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
18
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
case 5:
case 6:
System.out.print("Thursday");
System.out.print(" or Friday");
System.out.println(" or Saturday");
break;
}
}
}
}
$ java DaysOfWeek
Sunday
Monday
Tuesday
Wednesday
Thursday or Friday or Saturday
Thursday or Friday or Saturday
Thursday or Friday or Saturday
$ java GiveMeABreak
i = 1000
once break is encountered the code jumps to the end
of the for or while loop block and begins executing the
statements right after the block.
20
$ java PrintOdds
9
7
5
3
1
21
Arrays
two ways to declare them
int [] months; //preferred
int months []; //C - style
This just sets up a reference to the name of the array.
No storage allocated for the array.
Need to use new keyword to allocate space.
months = new [12];
or combine into one statement
22
int [] months