Assignment #1:  Intro to Java and Java Swing


Assigned:   Friday, Jan. 26
Due:   Monday, Feb. 5   by midnight

Contents:


Overview

Topic(s): Intro to Java; Java tools; primitive types, classes, & objects; control flow, methods, & parameter passing; input & output; strings, arrays, & lists; exceptions & handling; programming style
Related Reading: Chapter 1, and Sections 4.6, 4.7


Practice Problems

To see the solutions for these Practice Problems, go to Solutions, and select the appropriate Chapter and Exercise number.

  1. Odd numbered exercises at the end of Chapter 1.


Problems to be Submitted (25 points)

When you turn in your assignment, you must include a signed cover sheet (PDF version) with your assignment (you're assignment will not be graded without a completed cover sheet).

You are allowed to submit your assignment via email, but if you choose to do so, you must bring a hardcopy of your assignment along with a completed cover sheet to the instructor at the next class. (Note: Do not email the instructor any .zip file attachments, as SLU's email may not deliver these emails, i.e. the instructor may not receive your email.)

  1.   Read and Study Chapter 1, Sections 1.1, 1.3, 1.4-1.6, 1.8-1.13
  2.   (4 points)

    Create a class with a main() method that computes an estimate for π based on Liebniz' formula for computing pi:

  3.   (10 points)

    1.   (3 points)

      Create a class called SumTester that has a main() method which computes and prints out the sums of all numbers in the given ranges:

      • the sum of 1 to 50
      • the sum of 150 to 450
    2.   (3 points)

      In the same class, create a method that computes the sum between a starting value, low, and an ending value, high, and returns that sum. Have main() call this method and print out the result for the following sums:

      • the sum of 25 to 75
      • the sum of 125 to 375
    3.   (3 points)

      Finally, create a separate class, called SumNums, that has a constructor with input parameters for the low and high values.

      In your SumNums class, you also need to create two other methods. The first is fullSum(), which returns the sum of all numbers between low and high. The second is skipSum(), which returns the sum of every other number in the range between low and high (i.e. it skips every other number). Have main() in the original class create instances of SumNums, and compute and output both the full sum and skip sum for the following number ranges:

      • 40 to 160
      • 185 to 315
    4.   (1 point)  
      You've now seen three different approaches to creating software for computing the sums over a range of numbers -- which approach do you think offers the best software design? Explain your reasoning.

  4.   (5 points)

    Create a program that queries a user for names, then adds those names to an ArrayList of Strings. Once the user enters each name, have the program query the user for another name, until such time as the user enters a quit command like "done", "exit", or "quit".

    Once the user has entered all desired names, use the Collections.sort() method to sort the ArrayList, and then print out the final names in sorted order.

    Note 1: You'll want to use String's equals() method for comparison -- the "==" operator doesn't provide the comparison you typically want with strings... see String comparison in Java for more info.

    Note 2: Sections 1.10 and 1.11 are particularly helpful for this problem.

  5.   (6 points)

    Following is the start of a program (similar to HelloWorldFrame) for administering computer-based quizzes to students: QuizGUI.java

    Given this starting point, modify the program so that it does the following:

    Note: You should supply at least 5 of your own questions. Looking at the code, you should see that they don't need to be True/False, so at least a couple of your questions should be different.