Experiments in Java

Problems for Session J1: An Introduction to Java

Problem J1-A: Generating verse

Write a Java program that prints your favorite poem or short piece of verse. If you have no favorite writing, consider using Lewis Carroll's Jaberwocky.

'Twas brillig and the slithy toves
  did gyre and gimble in the wabe.
All mimsy were the borogoves
  and the mome raths outgrabe.

Problem J1-B: Name replication

Write a Java program that asks for a person's name and then prints the name one time, then two times, then three times, and then four times. For example, if the user entered William, your program would print

William William
William William William
William William William William

Problem J1-C: Errors galore

The following Java program contains a large number of errors. Identify as many as you can by hand and suggest how to correct them. Once you have done so, enter the revised program in the computer and attempt to compile it. Record any errors the compiler reports and attempt to correct those errors. Note that once you have corrected some errors the compiler may then report additional errors.

impart SimpleInpt
import SimpleOutput();

 * A very erroneous program.
 * @author Samuel A. Rebelsky
 * @version 1.0 of March 1998
public class Erroneous (
   * Do something.
  public void main(String() args) {
    SimpleInput in = new SimpleInput;
    SimpleOutput out = old SimpleOutput;
    double val

    Out.print("Please enter a number: );
    val - in.readdouble();
    in.print(val - 1 + " is " + val - 1);
  { / /main(String[])

Problem J1-D: Predicting output

What do you think will happen when a user runs the following program and enters Lewis Carroll on one line? Separate lines?

import SimpleInput;
import SimpleOutput;

 * Illustrate textual input and output.
 * Copyright (c) 1998 Samuel A. Rebelsky.  All rights reserved.
 * @author Samuel A. Rebelsky
 * @version 1.0 of March 1998
public class Namer {
   * Ask for a name and do some funny things with it.
  public static void main(String[] args) {
    // Set up input and output.
    SimpleInput in = new SimpleInput();
    SimpleOutput out = new SimpleOutput();
    // The person's first name
    String first_name;
    // The person's last name
    String last_name;

    // Get the name
    out.print("Please enter your full name: ");
    first_name = in.readString();
    last_name = in.readString();
    out.println("I will record your name in my database as " +
                last_name + ", " + first_name + ".");
  } // main(String[])
} // Namer

Problem J1-E: Temperature conversion

Reflect on the previous programs and see if you can design a program that converts Celsius to Fahrenheit. Use the conversion formula

F = C * (9.0/5.0) + 32.0

Begin by designing your program on paper, rather than on the computer! Experience shows that we do much better when we design before coding. At this stage in your career, you should first design on paper, then sketch your code on paper. Only once you've sketched the code should you move to the computer.

Problem J1-G: Bar graphs

Write a program that creates a bar graph for the following fictitious data on Web browser usage:

Internet Explorer       35%
Netscape Navigator      40%
HotJava                 10%
Other                   10%

Model your output after the following example:

  Lines of code in "Hello World"

C:                    ****
C (w/comments):       *******
Java:                 *******
Java (w/comments):    *********************
Pascal:               ******
Pascal (w/comments):  **********
Scheme:               *
Scheme (w/comments):  **
                      |   |    |    |    |
                      1   5    10   15   20

Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Source text last modified Wed Oct 6 12:29:51 1999.

This page generated on Wed Oct 6 12:33:46 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu