COMP 202: Assignment 1
Introduction to
programming
Fall Session, 2004
Due date: Monday September 20 at 23:55 on WebCT, 2004
Browsing and WEB CT
Before getting started with the questions, familiarize yourself with
the course website, WebCT, and whichever IDE or text editor you will use
throughout the course (e.g. JCreator or NetBeans.)
- Find an appropriate computer and start a World Wide Web browser (e.g.
Netscape or IE).
- Go to CS 202 web site whose URL is: http://www.cs.mcgill.ca/~cs202. Browse
this site, making yourself familiar with the content. You may want to print
some of the pages that give you information about how to acquire and install
Java and JCreator on your home computer (if you will work at home.) You may want to bookmark the site so
that you can revisit it easily. Notice that assignments are posted here with
solutions (not yet).
- Now go to the WebCT web site URL http://www.mcgill.ca/webct/. You must
login. You do this by entering your McGill ID number and the PIN number given
to you when you received your ID. You will see a list of courses using WEB CT
that you have registered for. Select COMP 202. Now browse the site. Notice
where the communication tools are, email, chat, and find the assignments area.
This is where you will be submitting and receiving your assignments. (Instructions on submitting found at the end of the assignment.)
Q1: Your first Java application program (3 points)
In this question you
will have to use Java Development Kit (JDK or Java 2 SDK) and an Integrated
Development Environment (IDE) to input, compile and run a simple Java
application.
If you are using the Computer Science Teaching Labs, JDK should
already be installed, and during the tutorials, the TAs will show you how to
input, compile, and run a Java application.
In other cases, such as using a
home computer, click here
for instructions on downloading and using the necessary software
(JCreator).
Type the following program in a file called Age.java,
modifying the program with your information where indicated: // ----------------------------------------------------------
// Age.java (Application)
//
// Author: Yannick Daoudi
// Entered by: -your name here- -your ID number here-
// Classes: Age
// Date: January 8, 2004
// Description: Computing and printing the student's age
// at the end of the current year.
// Bugs: No known bugs.
// ----------------------------------------------------------
public class Age
{
public static void main (String[] args)
{
int birth_year; // student's birth year
int year; // current year
int age; // age of student
// initialize variables
birth_year = 1980; // your birth year here
year = 2004;
// compute age of student
age = year - birth_year;
// print result and good bye message
System.out.println("You're turning " + age + " this year.");
System.out.println("Good Bye!");
} // main
} // class Age
Compile, and run the program.
Remember to backup your work. Save your
work on a diskette regularly and, if you are working in the labs, each time you
log off.
Q2: Your first buggy Java application program (7 points)
This question
is similar to the previous one and is meant to familiarize you with some basic
aspects of Java's syntax and compile time messages that report syntax errors.
Follow the instructions given for the previous question to input the program
presented below. Note that you should name the project a1q3 and the
source code file as Buggy.java. This program has many errors: //--------------------------------------------------------
//Buggy.java (Application)
//
// Author: Yannick Daoudi
// Entered by: -your name here- -your ID number here-
// Classes: VeryBuggy
// Date: September 1, 2004
//
// Description: This program will estimate the amount
// of calories burned every year depending on
// the weight of a person and the distance
// travelled everyday by foot or by bicycle.
// Bugs: A few.
//--------------------------------------------------------
public class VeryBuggy
{
public static void main (String[] args)
{
/* declare variables */
String name;
integer weightKg; distance_km; total_Kilometers;
double factor, calories;
// initialize variables
name = Jack; //Your name here
weight_kg = 66; // your weight here
distance_km = 12; // average distance you travel on any given day of the year
factor = 0.5; // approximate number of calories burned per Kg per Km
/* compute amount of calories burned every year cycling or walking
totalKilometers = distance_km * 365;
calories = factor * weight_kg * totalKilometers;
// print result and good bye message
System.out.Print("Hi " + Jack + ", you would spend an extra " calories);
System.out.Print( calories every year by cycling or walking);
System.out.println(" instead of taking your car.")
System.out.println("Goodbye + Name + !");
} // class buggy
Study this program and try to fix as many errors as you can before
you type it in. After fixing the program, compile it. If the compiler reports
syntax errors, correct the errors and recompile till no more errors were found.
Make a note of each error you correct. Note that by double clicking onto the
error message, JCreator takes you to the appropriate place in the source code
containing that error. Fix all errors and submit the corrected program.
Annotate the source code to show where the errors were. You may annotate
by adding comments to the program.
Q3: Your own Java application (10 points)
In this question you will have
to create a simple Java application.
This application should ask the user for
the approximate distance travelled on any given day of the year, and then
compute the amount of CO2 that would be produced by driving a car on that
distance every day of the year, as well as the amount of CO2 that would be saved
by taking the bus, and by walking/cycling instead.
Motor vehicles are
responsible for almost a large portion of annual emissions of carbon dioxide
(CO2), the primary global-warming gas. For example, when driving a car, a person
produces (approximately) an extra 200 grams of CO2 per kilometers, whereas when
taking a bus, a person only produces an extra 40 grams of CO2 per
kilometers.
Your program should looking something like the following example:
Welcome to the CO2 savings calculator!
Enter the average distance travelled on any given day of the year:
12
Driving a car, you would produce an extra 876.0 Kg of CO2 every year.
You can save 700.8 Kg of CO2 every year by taking the bus instead.
You can save 876.0 Kg of CO2 every year by walking or cycling instead.
Thanks for using the CO2 savings calculator, if you'd like to know
what else you can do to help save the planet, visit www.davidsuzuki.org
and take the Nature Challenge. Bye!
Note: you will need to use the Keyboard
class for this question.
IMPORTANT instructions:
To submit an assignment you must login to WebCT,
choose COMP-202, go the the ``Evaluation and Activity'' folder, and select
``Assignments''. You will see two ``boxes'' called ``BOX'', and ``PROBLEM'' for
each assignment. Normally you should select the first one (``BOX''.) A new page
will appear. There is an ``Upload file'' button, a ``Remove file'' button and a
``Submit assignment'' button. For each file in your assignment you have to click
on the ``Upload file button'', find your file by browsing through the
directories on the computer that you are using until you find the desired file,
and choose ``OK'' or ``Upload''. Once you have uploaded all of your files, click
on ``Submit assignment''. Your assignment will be automatically time-stamped by
WebCT. IMPORTANT: You can submit only once. Also, the problem box should be used
only on extraordinary circumstances. In such cases, e-mail your course
instructor about it. After submitting your assignment make sure to keep a
backup copy for your records. We are very careful with assignments, but
sometimes assignments do get misplaced. Clearly indicate in the source code of
every file of your assignment, using comments, the following information:
1.Name: ...
2.ID number: ...
3.Course number: ...
4.Section
number: ...
6.Assignment number: ...
7.Where you developed your program:
(i.e. at home, in the CS lab, in the Engineering lab ...)
All assignments are due at 23:55 on WEB CT. Also please remember the policy
on cheating as described in the course outline. You must do this assignment by
yourself.
MARKING Section
Every assignment will have a marking scheme section.
This is provided to help both the student and the TA. It provides direction for
how the assignment should be graded by the TA.
- Question 1: Your First Java Application Program (3 points)
- 2 points - JAVA file entered as stated
- 1 point - CLASS file that is executable
- Question 2: Your First Buggy Java Application (7 points)
- 1 point - CLASS file that is executable
- 4 points - All errors found and documented on the source code using
comments
- 2 point - Program commented and formatted nicely.
- Question 3: Your own Java Application (10 points)
- 3 points - CO2 amounts are calculated correctly.
- 4 points - Program is structured nicely (relevant variable names,
comments, etc.)
- 2 point - output formatted nicely.
- 1 point - CLASS file that is executable
Note that for
all questions Formatting the code nicely and providing comments is mandatory.