Name: ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________
| Ex2.java | Ex3.java |
public class Ex2
{
public static void main(String[] args)
{
String s;
System.out.println(s.length());
}
} |
public class Ex3
{
public static void main(String[] args)
{
String s = null;
System.out.println(s.length());
}
} |
Try compiling and running this. Do you get a compile-time or run-time error? What is the problem? |
Try compiling and running this. Do you get a compile-time or run-time error? What is the problem? |
String s = null;
is OK, but
double x = null;
is not?
HW02.java that reads in number n, followed by
n strings, then prints out the strings columnwise (with spaces
between column elements). A sample run looks like this:
~/$ java HW02 4 hello goodbye destiny alive h g d a e o e l l o s i l d t v o b i e y n e yRules: you must define and use in your program a function
public static int maxLength(String[] A)
that takes an array of Strings as an argument and returns the
length of the longest string. If I were you, I would define
and thoroughly test this function before doing anything else!
A is an array, A.length
gives the length/size/number-of-elements
for A.
Note that there are no ()s!
s is a string, s.length()
gives the length/size/number-of-characters
for s. Note the ()'s!System.out.println(...) prints its
argument followed by a newline.
System.out.print(...) does the same, but without sticking the
newline at the end.
System.out.println(), i.e. calling println
with no arguments, prints a newline and nothing else.s is a String, the character
at index i in s is given by
s.charAt(i).
java.lang.math (available with
import java.lang.*) provides a max function.
You call it like this: int i = Math.max(a,b);
submit -c=IC211 -p=hw02 HW02.java