|
Spring Semester 2005
|
Strings?
Your programs will ALL have this structure:So that much is known.class _____________ { public static void main(String[] args) { // your code here } }
Statements are sentences in Java.In English after each sentence we put a period. In Java we put a semicolon.
So your programs will ALL look like this:
You can have none, one or more instructions in a program.class _____________ { public static void main(String[] args) { <instruction in Java>; <instruction in Java>; <instruction in Java>; <instruction in Java>; } }The types of instructions we have learned:
- print statements (
System.out.print__(...))- declarations of variables (
int n;,double a;,String u;)- assignment statements (
n = n + 1;,a = Math.sqrt(2);,u = u.substring(1);)Your programs for Homework One are all composed of these kinds of instructions.
You already know the difference between
System.out.print(...)andSystem.out.println(...).The arguments to these methods are expressions.
(Arguments are like ingredients in a recipe).
So we can have
System.out.print(1)(the expression is a number)System.out.print(1+1)(the expression is an addition)System.out.print("1")(the expression is aString)System.out.print("1" + "1")(the expression is a concatenation ofStrings)
For that we need a specialized kind of object.(To make a phone call you need a phone, to bake a cake you need an oven, etc.)
So here's how we read an
integer:So I decided to refer to my console reader asConsoleReader craig; // choose a name craig = new ConsoleReader(System.in); // create an actual console reader (and start calling it craig) int n; n = craig.readInt();craig.Then I prepare a location (call it
n) where I will put myinteger.And then I assign to
nthe value of an expression (craig.readInt()).The expression retrieves the value from the keyboard and makes it available to us.
So we assign (put/place) that value to (into)
n.Reading
doubles is slightly different yet very similar:And here's how we readdouble a; a = craig.readDouble();Strings:String u; u = craig.readLine();
We've seen how we do that already: assignment statements.But we need to be careful: expressions have types and so have variables.
Thus the following are for your practice and review:
int a = 2 / 3;(OK, the expression evaluates to an integer)int a = 2.0 / 3;(anintcan't hold adouble)int a = Math.sqrt(2);(anintcan't hold adouble)String b = 3;(anintis not aString)String b = 3 +"4";(OK, expression evaluates to aString)double c = 3;(OK, theintreceives a.0fractional part)int c = (int)3.14;(OK, the fractional part is discarded explicitly)Again, by and large this is all you need.
Strings. Why?Assume the following code:Isn't it so much better (easier) to write the code as such:int x = 3; int y = 4; System.out.print("The sum of"); System.out.print(x); System.out.print(" and "); System.out.print(y); System.out.print(" is "); System.out.println(x + y);Why do we need to put theint x = 3, y = 4; System.out.println("The sum of " + x + " and " + y + " is " + (x + y))x + yin parentheses?
Just likemagically printsSystem.out.println(2)2in your command prompt window so doesmagically amount to the square root ofMath.sqrt(2)2in an expression.
Math.sqrt(2)amounts to adouble.So don't try to store that value in an
intvariable.Can you determine what
Math.round(...)does and the type it returns?Here's a link to class
java.lang.Maththat has all these functions.So does this work:
Why or why not? (How do you find out?)int n; long m = 2; n = m + 1;
What do you do about it?frilled.cs.indiana.edu%cat Alpha.java class Alpha { public static void main(String[] args) { int n; long m = 2; n = m + 1; } } frilled.cs.indiana.edu%javac Alpha.java Alpha.java:5: possible loss of precision found : long required: int n = m + 1; ^ 1 error frilled.cs.indiana.edu%
StringsThis amounts to knowing the difference between the twosubstringmethods.
"wonderful".substring(2, 5)amounts to"nde"(Andy?)"wonderful".substring(3)amounts to"derful"(rhymes with"careful"?).If
ais aStringhow many characters does it have?
StringsIfais aStringit hasa.length()characters.What is the index of the first one? (Answer:
0)What is the index of the last one? (Answer:
a.length() - 1)
For us, for now, only when we want to truncate.Here's an example:
So things are not as simple as they should be.frilled.cs.indiana.edu%cat Alpha.java class Alpha { public static void main(String[] args) { double amount = 4.35; // in dollars amount = 4.35 * 100; // in cents (has type double) System.out.println(amount); // does it print 435.0? int cents = (int) amount; System.out.println(cents); // have we lost a cent? } } frilled.cs.indiana.edu%javac Alpha.java frilled.cs.indiana.edu%java Alpha 434.99999999999994 434 frilled.cs.indiana.edu%
Now things are OK, but why do we still need thefrilled.cs.indiana.edu%cat Alpha.java class Alpha { public static void main(String[] args) { double amount = 4.35; // in dollars amount = 4.35 * 100; // in cents (has type double) System.out.println(amount); // does it print 435.0? int cents = (int) (Math.round(amount)); System.out.println(cents); // have we lost a cent? } } frilled.cs.indiana.edu%javac Alpha.java frilled.cs.indiana.edu%java Alpha 434.99999999999994 435 frilled.cs.indiana.edu%(int)?
By now this should be the old hat. (See above).
char Acharis a numeric code that prints like a character.So for example
'a'has value 97 and prints like lowercase a'a' + 1has value 98Take a look at this program:
How do you print the 20 characters that follow the character with code 45?frilled.cs.indiana.edu%cat cat Alpha.java cat: cat: No such file or directory class Alpha { public static void main(String[] args) { char a = 97; System.out.println(a); } } frilled.cs.indiana.edu%javac Alpha.java frilled.cs.indiana.edu%java Alpha a frilled.cs.indiana.edu%Oh, that's just like the square root approximation (or the loan calculation).
chars. frilled.cs.indiana.edu%cat Alpha.java class Alpha { public static void main(String[] args) { char a = 97; // change to 45 [1] System.out.println(a); // [2] // get code of next char, store it in a a = (char) (a + 1); // a + 1 is an int! [3] System.out.println(a); // print next char [4] // perform [3] and [4] over and over //(for as many times as you need or want to) } } frilled.cs.indiana.edu%javac Alpha.java frilled.cs.indiana.edu%java Alpha a b frilled.cs.indiana.edu%
In class we will also go over the
MilitaryFormat.java program as well.
Mon Jan 24 20:16:41 EST 2005