CSC171 Introduction to Computer Programming
Fall 2018
Homework 2

Due: September 28

Introducing errors and writing applications
  1. Enter, compile, and run the following application:
    public class Test
    {
       public static void main(String[] args)
       {
          System.out.println ("An Emergency Broadcast");
       }
    }
    

  2. Introduce the following errors, one at a time, to the above program. Record any error messages that the compiler produces. Fix the previous error each time before you introduce a new one. If no error messages are produced, explain why. Try to predict what will happen before you make each change.
    • a. change Test to test
    • b. change Emergency to emergency
    • c. remove the first quotation mark in the string
    • d. remove the last quotation mark in the string
    • e. change main to man
    • f. change println to bogus
    • g. remove the semicolon at the end of the println statement
    • h. remove the last brace in the program

  3. Write an application that stores your name, age, hobbies, favorite book, and favorite movie, then prints these on the screen. Choose descriptive names for your variables, and follow the naming conventions we discussed (e.g. String favoriteMovie;). Label each piece of information about yourself in the output. Example output, where ... represents your info:
    My name: ...
    My age: ...
    My hobbies: ...
    My favorite book: ...
    My favorite movie: ...
    
    OR
    ... is ... years old and likes ...
    

  4. You have a map that has a fractional scale of 1:24,000 meaning that 1 unit on the map is equal to 24,000 units on the ground. You are planning a hike on the map, and want to know how many miles you will walk on the ground. Design and implement a program in Java that reads from the user the length of a hiking trail on the map, and prints to the screen the equivalent number of miles on the ground. You may assume that the user will enter a non-negative number, and that 1 inch on the map = 24,000 inches on the ground, 12 inches = 1 foot, and 5280 feet = 1 mile. Each time you run your program, you will test your program on a different input. Include among your tests the following inputs: 0 and 18.5. For the input of 18.5 inches on the map, your program should output that the equivalent distance on the ground is 7.0 miles. Make sure to 1) prompt the user what they need to enter (i.e. number of inches on the map), 2) hand in the output of all your tests, in addition to the source code, 3) use appropriately named variables to store the input value and computed result(s), and 4) use appropriately named constants (i.e. descriptive and following the naming conventions we discussed) to store the conversion factors, e.g. final int INCHES_PER_FOOT = 12;

    What to hand in:

    For programs (e.g. problems 3 and 4), hand in a hard copy of your source code and outputs. Your source code should include appropriate comments and follow the style conventions discussed in class (e.g. consistent indentation within each block, separation of logical sections, appropriately named identifiers). For short answer questions (e.g. problem 2), you may use any editor (such as MS Word) to write your answers.
    The assignment should be done individually.

    [Back to the Assignments Index]