VB3/Java
Mt Lebanon HS 2004-5
David Kosbie



Resources:


Assignments:


Week #2:   Programming Team problems (part 1)
    date:        9-Feb-05

For the next few weeks, we'll cover all the problems on the Programming Team's web site.

#1.  Do all the problems from 19-Jan-05 and 21-Jan-05.  The 21-Jan problems are more challenging, so be sure to get to those early in the week!

Hints:

We discussed some hints in class today.  Here are a couple others:

Be sure to get an early start on these problems -- some are quite challenging, especially towards the end of the assignment, and you'll want to be able to ask questions (via email)...

You may need String.charAt(), which takes a String and returns the kth character of the String, as in:
     String s = "yahoo";
     char c = s.charAt(1);
This will set c to the second character (since the first character is charAt(0)), or 'a'.  Note that Java uses single-quotes for characters and double-quotes for strings.  So 'a' equals the ASCII value of lower-case a, which equals 97, which is precisely what c is set to by the example code above.

You find the length of a string using String.length(), as in:
     String s = readString();
     System.out.println("This string has " + s.length() + " characters in it.");
Notice that a string's length is obtained as a method whereas an array's length is obtained as a property, as the following code demonstrates:
     String s = readString();
     int[] a = { 2, 4, 6, 8, 10};
     int slen = s.length();  // Notice that it's s.length(), not s.length
     int alen = a.length; // Notice that it's a.length, not a.length()

 


Week #1:   Java Basics
    date:        2-Feb-05

#1. Review the code we did in class:  JavaBasics.java. Modify it in interesting ways.

#2. Try all the problems from:  http://www.kosbie.net/ml/04-05/pteam/1-feb-05.htm
If you can't do one, that's ok, but take a shot at it. You can send me email with any questions you may have.

#3. Try all the "interesting" problems from:  http://www.kosbie.net/ml/04-05/math-with-java/

Hints:

You may need Math.round(), which takes a double (or a float, which we've not yet discussed) and rounds it to the nearest integer, as in:
     double d = readDouble();
     int n = Math.round(d);

If you want to round to the nearest 10th, first multiply by 10, then round, then divide by 10 (think about it).

You may also need Math.abs(), which takes an integer or a double and returns the absolute value of the same, as in:
     double d = readDouble();
     double absD = Math.abs(d);

Then there's Math.pow(), which takes two doubles a and b, and returns a to the b, as in:
     double a = readDouble(),
     b = readDouble();
     double aToTheB = Math.pow(a,b);

Also, there's Math.sqrt(), which takes one double and returns the square root of that number, as in:
     double d = readDouble();
     double sqrtOfD = Math.sqrt(d);
Of course, we don't need Math.sqrt, since Math.sqrt(d) is equivalent to Math.pow(d,0.5), but Math.sqrt reads a bit nicer in this case, I think.

As you may have guessed, there are many more Math methods, as well as a huge number of other methods. Where to learn about ALL of them? In the online API, of course! Check out: http://java.sun.com/j2se/1.4.2/docs/api/index.html.  On the bottom left, you first scroll to the class you are interested in. For example, scroll to "Math" in that list and select it. After that, the main section on the right will explain every method in that class.