Homework #1 – Java 211, Mendelsohn
Due: One week after lecture #1, 10
minutes before class begins.
DL Students have an additional day until 11:59 PM (Central
time) for submission.
TIP: Always begin
SMALL and SIMPLE. Begin with an
extremely simple program. Compile and
run it. If it works begin adding extra
functionality.
- Get
the JDK installed on your machine.
Configuring it may be a bit of a pain, but persevere….
- Say
you are about to take a road trip through the Canadian wilderness. Create a class called ‘DistanceConvert’ that
will convert miles to kilometers.
Feel free to modify the ‘TemperatureConvert’ class to make it
work. However, do not simply
copy and paste the code. You
will benefit much more by typing it in yourself from scratch.
-
Comment your code
-
Use whitespace (but don’t overdo it…)
-
Use proper indenting
- Write
a version called ‘BadDistanceConvert’ in which you demonstrate an example
of a logic error. Be sure to
comment your code to indicate where the error is.
- Create a class called
Geometry in which you have two variables, one called length and one called
radius. Your program should take
the length variable and use it to calculate the volume of a cube. The program should use the radius to
calculate the volume and circumference of a sphere. Output the values of all those
variables.
I strongly recommend that you complete questions 1 through 4
by Wednesday’s class.
- Create a class that has some
code in it that outputs all of the numbers from –100 to 0 to the screen on
the same line with a space between in increments of 7. For 2 extra points, separate each number
by a comma (but there should be no comma after the last number). You must use a loop!
-100 –93 -86, etc
Bonus version: -100, -93, …
- Create
a class in which you have a variable called ‘number’. You should test to see if the number is
evenly divisible by 3, 4, or 5. If
it is, output that information to the screen. Otherwise, output that it isn’t. Your output should look identical to
mine (except for the value of ‘number’).
The value of ‘number’ is 20.
20 is not evenly divisible by 3.
20 is evenly divisible by 4.
20 is evenly divisible by 5.
- Write
a program in which you have a variable with a randomly generated number
between 1 and 25 (see below). If the
number is evenly divisible by 3, output to the screen “Divisible by
3”. Otherwise, output “Not
divisible by 3”. (Hint: To
determine whether a number is evenly divisible by another, take a look at
the arithmatic operators discussed in lecture) Next, modify your program to generate a
random number 20 times. Output each
of the numbers and also how many of them were and were not evenly
divisible by 3.
To generate a random number between
1 and 25 you can use the code below:
int num;
num = (int) (Math.random() * 25) + 1; //will be discussed in lecture #3
Sample
output for the completed problem:
Notes:
-
All your programs should be commented, demonstrate
proper use of whitespace and indentation.
Look at the programs in your book for examples of proper form.
-
Submit the various files for your assignment as a
ZIP file to COL. If you do not know
how to Zip files, you can view a rather detailed tutorial
here.
-
At the top of your code, put your name, the filename,
and homework problem. All files
submitted with your homework must follow this pattern:
//Your Name, Your Section
//File Name (e.g. DistanceConvert.java)
//Homework #1, Question 2
//
public class DistanceConvert
{
....
}