CSCI 1321 (Principles of Algorithm Design II), Spring 2011:
Practice Problems


Contents

Here are some problems you can use as practice for improving your Java programming skills. These are completely optional and not for credit, but I strongly recommend that you write some programs in addition to the assigned homework/project. (I will be adding to this list as the semester goes on.)

  1. Write a Java program that prints more than one line of text to standard output (i.e., a program similar to the ``hello world'' example from class, but producing more output, of your choice).

  2. Write a Java program that includes a loop of some sort -- to print the first 10 positive integers, say, or their squares.

  3. Write a Java class for rational numbers, including a main method to do some simple testing. Include whatever methods you think would be useful and not too time-consuming to code. (You might start with just a constructor and a toString method, and then add methods to do arithmetic operations.)

  4. Write a Java interface called Car with a method drive that prints something. Write some classes that implement Car, printing different things, and a class that tests all your classes.

  5. Write a Java class called Car with a method drive that prints something. Write some subclasses of Car, having some of them use Car's implementation of drive and some overriding it to print different things. Write a class that tests all your classes.

  6. Write a Java program that takes a String containing a sentence and counts the number of words in it. (For simplicity, consider ``words'' to be substrings separated by spaces.) (One of my example programs shows a couple of ways to do this, but you should try it yourself first.)

  7. Write a Java program that creates an array of ints, passes it to a method that computes and returns the sum of its elements, and prints the results. You can fill the array however you choose -- with randomly-generated values, say, or using a formula (e.g., element a[i] could be i or i+1).

  8. Write a Java program similar to the one for the previous problem, but using an array of Integer objects.

  9. Write a Java program that creates an array of ints and passes it to a method that sorts the array (without calling Arrays.sort). Print the contents of the array before and after sorting.

  10. Write a Java class for something that could be sorted -- e.g., rational numbers (sorted by value), or books (sorted by author then title) -- and include a main program that creates an array of objects of your class and sorts them using Arrays.sort.



Berna Massingill
2011-01-20