Assignment 1: First Programs!
Due Thursday, September 15th, before midnight
The goals of this assignment are:
-
Work with string input and output
-
Work with variables, expressions and operations
-
Get comfortable with
int
,double
, andString
1. Task Tracker
Write a program, Tasks.java
that computes the time when the user will finish
their tasks. You can assume that all times are integers that represent the time
on a [24-hr "military" clock.
For example, 7 PM is 19:00, or 19, in this representation.
$ javac Tasks.java
$ java Tasks
Enter the current hour: 23
Enter the task duration: 5
You will finish at 4:00 o'clock.
$ java Tasks
Enter the current hour: 9
Enter the task duration: 5
You will finish at 14:00 o'clock.
Hints:
-
Use modulo % to compute the completion time.
2. Calorie Tracker
Write a program called Calories.java
that computes the amount of calories
a person burns in a week while walking. On average, a person burns 100 calories
for each mile walked. Unlike the previous question, where we read data from the
console, use command-line arguments to get the number of miles walked.
$ javac Calories.java
$ java Calories 3.5
You burned 350.0 calories!
$ java Calories 0
You burned 0.0 calories!
Hints and requirements
-
Use a double to store the number of miles
-
Use
System.out.printf
to print the number of calories -
Assume that your first command line argument (e.g. args[0]) is a positive double!
It’s ok if your program throws an exception if no argument is given! When this happens, you will get a runtime error. For example |
$ java Calories
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at Calories.main(Calories.java:3)
3. Pet Agency
You are in charge of writing a script for the Bryn Mawr Pet Adoption Agency. In
Adopt.java
, write a program that will ask the user for an animal, an
integer, a noun, and and a decimal number. Your program should then use this
information to generate a short description of what the pets need at the adoption agency.
(i.e. a This question is an example of a
[madlib](https://en.wikipedia.org/wiki/Mad_Libs)).
Here are two examples (user input in bold):
$ java Adopt.java
Animal: owls
Integer: 3
Noun: hats
Decimal: 1.3
Welcome to the Bryn Mawr Pet Adoption Agency!
We have 3 owls in need of hats and 1.3% of your love!
$ java Adopt.java
Animal: slugs
Integer: 3000
Noun: peaches
Decimal: 110.3
Welcome to the Bryn Mawr Pet Adoption Agency!
We have 3000 slugs in need of peaches and 110.3% of your love!
Hints:
-
Use either a
float
ordouble
variable to hold the decimal type -
Use
System.out.printf
to print the decimal value with single precision.
4. What to hand-in
-
The programs,
Task.java
,Calories.java
, andAdopt.java
. -
Make sure each program has a header containing your name, date, and purpose of the program
-
A brief write-up containing your name, assignment number, and a few sentences about how long you spent on the assignment, any customizations you made, and any interesting bugs you solved.
4.1. How to hand-in
-
Copy your programs to your dropbox, into the folder called
A01
.