CMPSCI 121
"Introduction to Problem Solving with Computers" using Java

Department of Computer Science
University of Massachusetts Amherst

Home
Resources
Grading
Weekly Schedule
Syllabus
Homework
Exams

Bulletin Board
Staff

Programming Assignment #1

Due in OWL, 9:00am, Tuesday February 8, 2005.
Extra credit of 5 points if handed in before 11:59pm, Monday February 7, 2005.

Important notes:

  • This programming assignment contains three problems, and can earn a total of 100 points as described below.
  • Extra credit described below accumulates points into an overall extra credit count that may be used at the end of the semester to determine letter grade border cases at the end of the semester.
  • File, class and method names should be exactly as described below. You may, however, if needed add additional helper variables and methods.
  • Each method's return type, as well as the number, order, and types of parameters must be as specified. Parameter names don't have to be the same.

Problem 1: Hello, UMass. (20 points)
[Collaboration allowed; may be done during lab]

There is a great tradition in computer science of starting to learn a new computer language by first writing a small program that does nothing but prints to the screen the message "Hello, world!" Here is a Java program that does just this:

public class MyProgram {
  public static void main (String[] args) {
    System.out.println ("Hello, world!");
  }
}

Type this program into DrJava, click the "Save" button near the top of the window, name the saved file MyProgram (it will actually be saved as MyProgram.java), and press the "Compile All" button also near the top of the window. If you typed it correctly, and it compiled without errors, you can select the "Run Document's main method" menu item from the "Project" menu, and you should see the friendly greeting appear in the interaction pane at the bottom of the window!

For Problem 1, modify this program to print "Hello, UMass!" and hand it in through the OWL system for this assignment.

Here is what the interaction pane should like like after it runs:

Welcome to DrJava.
> java Hello
Hello, UMass!
>

Problem 2: A Person class. (30 points)
[Collaboration allowed; may be done during lab]

Create a representation for person objects that captures the person's name, height and year of birth. Define a Java class Person with a three-parameter constructor with the following arguments (in this order): a String for his or her name, a real-valued floating point number for height, an integer for birthyear. Also define three query methods to access these values. You must name these query methods name, height, and birthYear.

Although you should create some person objects and interact with them to test out your code, you only need to hand in the code that defines your Person class, not code that would create an manipulate Person objects.

Here are some DrJava interactions with the class:

Welcome to DrJava.
> Person bclinton = new Person ("Bill Clinton", 74.5, 1946); > bclinton.height(); 74.5 > bclinton.name(); "Bill Clinton" > Person arnold = new Person ("Arnold Schwarzenegger", 70, 1947); arnold.birthYear(); 1947 > int bclintonAge = 2005 - bclinton.birthYear(); > bclintonAge 59

Problem 3: A Movie class. (50 points)
[No collaboration allowed]

Create a representation for movie objects that captures the movie's title, running length in hours, year of first release, and whether or not it is foreign. Define a Java class Movie with a four-parameter constructor with the following arguments (in this order): a String for the movie title, a real-valued floating point number for the running length in hours, an integer for the year it was released, and a boolean for whether or not it is foreign. Also define four query methods to access these values. You must name these query methods title, length, releaseYear, and isForeign.

Although you should create some movie objects and interact with them to test out your code, you only need to hand in the code that defines your Movie class, not code that would create an manipulate Movie objects.

Extra credit (5 points): Add to your Movie class a boolean instance variable that indicates whether or not the movie is currently playing in theaters, and a query method called currentlyPlaying that returns this value. Also include a command method called setCurrentlyPlaying that takes one boolean argument, and sets this instance variable.

Here are some DrJava interactions with the class:

Welcome to DrJava.
> Movie nemo = new Movie ("Finding Nemo", 1.683, 2003, false); > nemo.length(); 1.683 > nemo.title(); "Finding Nemo" > Movie aviator = new Movie ("The Aviator", 2.8, 2004, true); aviator.releaseYear(); 2004 > aviator.currentlyPlaying(); true

And if you implemented the extra credit, an interaction could look like:

Welcome to DrJava.
> Movie aviator = new Movie ("The Aviator", 2.8, 2004, true);
> aviator.currentlyPlaying();
true
> aviator.setCurrentlyPlaying(false);
> aviator.currentlyPlaying(); false

How to hand it in

  • Go to the OWL Web site at owl.cs.umass.edu.
  • Click on the link "Computer Science (UMass Amherst, Boston, Dartmouth, Lowell & STCC)"
  • Login. Your login is your student ID number. Your password is your last name. (If you were not registered in CMPSCI 121 before January 20th, you will not already have a login/password for OWL. You can request one by clicking on "Login Help" on the left hand side of the screen, then click on "I am a student", then "Request Login", then the small red arrow to the right of " CMPSCI 121 - Spring 2005 - Intro to Problem Solving w/ Computers". Then fill out the form and click on the "Request Login" button. It may take more than a day for your to obtain a login, so you must do this several days before the assignment is due.)
  • Find "Programming Assignment #1"
  • Paste your code into the relevant text boxes.