Homework 2

Write a program that will perform the following:

How many integers will you enter? 5
Please enter your integers:
4 -2 0 100 4
Now, please enter a String:
Potato
Your integers in reverse are: 4 100 0 -2 4
Your String, with shuffled-in spaces, is: P o t a t o

You will be using arrays and Strings to do this. Obviously, your code should work for any number of integers and any String, not just the examples.

Your program will be graded by automated script. This means that your output should look EXACTLY like it does above.

What to hand in: Your program must be named HW2.java. It must compile with this line:

javac HW2.java

You will lose at least 50% of your grade if that line does not compile. For all homeworks, submit your *.java file(s) only - do not submit any *.class files.

Variables and Types

The built in types of Java... are not very interesting. They're very similar to C++: int, double, char, and boolean (instead of bool) are the common ones. There is also: byte, long, short (which are integral), and float (which is decimal). Operators are the same as well: +,-,*,/,%. All the rules about integer division/modulus still apply.

So what happens when this is run?

  int i = 13;
  int j = 7;
  int k = 2;
  double m = 0.0;
  System.out.println("values: " + (i%j) + " " + (j/k) + " " + (j+m)/k);
  boolean a = true;
  boolean b = false;
  System.out.println("values: " + (a+b));
  System.out.println("values: " + (a||a) + " " + (a || b) + " " + (b || b) + " " 
                    + (a&&a) + " " + (a && b) + " " + (b && b));
  char x = 'y';
  char y = 'x';
  System.out.println("values: " + x + " " + y + " " + (x+y));

What would happen if we took out the () inside the printlns?

Conversions.

Just as with C++, types can be converted to one another, but with more restrictions.

Widening conversions are converting a type to a "larger" type, like int to double. There are 19 possible:

These can be done automatically:

      int i = 3;
      double d = i;
    

the int 3 is widened to a double. Widening usually doesn't lose information.

Narrowing can lose information and precision, and requires a "cast". There are 22 Narrowings

      float fmin = Float.NEGATIVE_INFINITY;
      float fmax = Float.POSITIVE_INFINITY;
      System.out.println("long: " + (long)fmin + ".." + (long)fmax);
      System.out.println("int: " + (int)fmin + ".." + (int)fmax);
      System.out.println("short: " + (short)fmin + ".." + (short)fmax);
      System.out.println("char: " + (int)(char)fmin + ".." + (int)(char)fmax);
      System.out.println("byte: " + (byte)fmin + ".." + (byte)fmax);
    

Everything can be converted to a string. That's what is happening when you do this: "STRING " + 3; There are a number of other conversions that we'll talk about later on.

When do these conversions happen?

  1. During assignment, as we saw with widening.
  2. During function calls:
  3. 	  public static double foo(double argh){
    	     return (argh*argh)/(argh-1);
    	  }
    	  public static void main(String[] args){
    	     int i = 3;
    	     System.out.println("foo: " + foo(i));
    	  }
    	
  4. to Strings when using the +.
  5. When casting: int i = (int)3.3;
  6. Numeric promotion. This is what happens when you multiple an int by a double. The int is promoted to a double, and then the multiplication occurs.

Strings

As with C++, strings are not a "primitive type," but are objects of class String. As you have already seen, you can make string constants by putting text in quotes. String stringVariable = "String Constant";

One important difference is you can NOT treat a string like an array. stringVariable[1]= 'p'; will cause your compiler to yell at you. However, Java Strings DO have a function called .charAt(int), which will retrieve a character at a certain position. Additionally, Strings have a large number of other useful functions, including .length(), indexOf(), .substring(), .toUpperCase() and many, many more. It's worth taking a look in your book or online to see all the things Strings can do.

As with C++, the '+' operator works with Strings: stringVariable = stringVariable + " is constant string";

Arrays

In java, there are only dynamic arrays.

      int[] anIntArray;
      boolean[] aBoolArray = new boolean[10];
      String[] aStringArray = { "array", "of", "String" };
    

As with C++, building an array is a three-step process of declaration, allocation, and assignment. The declaration (like int[] anArray) creates the variable, not the memory itself, so we need the "new" to allocate the memory in the heap. The brackets can go on either side of the variable name.

Access is done as with C++. Note, however, that we can always use .length to arrive at the length of an array. This is not a function, as with Strings, but is an attribute of the array:

      int[] ia = new int[101];
      for (int i = 0; i < ia.length; i++)
          ia[i] = i;
      int sum = 0;
      for (int i = 0; i < ia.length; i++)
          sum += ia[i];
      System.out.println(sum);
    

If we change one of the < to a <=, our C++ experience tells us to expect either a Segmentation Fault or a silent bit of weirdness, which is kind of horrible. Instead we get a message about an ArrayIndexOutOfBoundsException, and an indication of which line caused it to happen, which seems strangely wordy, but much, much more useful.

Multidimensional arrays work just the same, but are easier to allocate:

  int[][] anArr = new int[10][9]; //An array with 10 rows and 9 columns
  for (int i = 0; i < anArr.length; i++) //anArr.length is 10, just like C++
    for (int j = 0; j < anArr[i].length; j++) //anArr[i].length is 9, just like C++
      anArr[i][j]=0;

Problems