Objectives:
Due: Before the next class (Wednesday).
Create a directory for cs209
and a
subdirectory for assign0
within
the cs209
directory.
In jEdit, create a Java class called Intro
in a file
named Intro.java
. The Intro
class's main
method should print out, on separate lines,
your name, your favorite color, and your favorite summer Olympic
sport. For example:
Sara Sprenkle Favorite Color: Purple Favorite Summer Olympic Sport: Swimming
All the top-down design principles from CS111 and CS112 apply in any computer science course. Do not try to write the entire program in one go. Break the problem into manageable pieces.
You should write only a little code at a time, then compile and execute it (see directions below) so that you find bugs quickly. You're just getting used to the new Java syntax, so I'm sure you'll run into problems. If you run into problems, compare your code to the examples from class.
Make sure your program is well-documented, as discussed in class.
In a terminal, from the directory that
contains Intro.java
, compile your program
using
javac Intro.java
If there are errors, fix those errors and re-compile until it works.
In a terminal, from the directory that
contains Intro.java
, execute your program
using the command
java Intro
If the output is not what you expect, edit your program, recompile and execute.
When you're sure your program is working correctly, execute your
program using the command
java Intro >
output
, which writes the output from java
Intro
into the file output
.
Objective: The goal of this part of the assignment is to get you familiar with the required Java syntax, the compiler's error messages, and how to fix the errors so that the program compiles.
Save Assign0.java into
your assign0
directory.
This program contains at least 5 errors. Some are compiler errors that the compiler will catch. Others are logic errors that the compiler won't catch.
Comment out the original code and note the cause of the error. Then, correct the code.
After you're sure the program works correctly, save the output in
a file called debugged.out
as you did in
the last problem.
Write a Java class whose main
method determines the
total molecular weight of a molecule based on the number of hydrogen,
carbon, and oxygen atoms it contains. You should use the following
weights:
Atom | Weight (g/mol) |
---|---|
H | 1.0079 |
C | 12.011 |
O | 15.9994 |
Note: The above weights should be class constants.
Since we haven't discussed input, you will need to hardcode the number of atoms in the program.
You should have an appropriately named Java class, in the
appropriately named file. Your code will mostly be in
the main
method.
A sample run is shown below:
The number of Hydrogen atoms: 3 The number of Carbon atoms: 2 The number of Oxygen atoms: 1 Weight of molecule is 43.0451 g/mol
After you're sure the program behaves correctly, run it a few times
and save the output to files named
like weight1.out
Copy your assign0
directory into your
turnin directory,
i.e., /home/courses/cs209/turnin/yourusername
For example:
cp -r assign0 /home/courses/cs209/turnin/sprenkles/
There is no printed part of this assignment.
You will be evaluated based on
turnin
directory