Introduction |
Please print a copy of this assignment, read it carefully, and
highlight material you think will be useful to you while you
are working on the program or submitting it.
This first real programming assignment is designed to ensure that you know the fundamentals of writing, testing, and debugging simple programs using the Eclipse Integrated Development Environment (Eclipse IDE). You will fill-in parts of one program and then write three simple programs in this assignment: each consists of a main method whose body includes statements that
In this assignment you may assume that all input entered by the user of your program is correct; in the next assignment we will practice validating user input. You can better understand this assignment, and check the behavior of your programs against my solutions, by downloading the Program #1 Executables zip file, unzipping it, and running the programs it contains. On a PC
Note that although you can run these three programs, you cannot examine their Java code. Before writing your programs, run my executables and examine the form in which the do their input/output, and use the same form in the programs that you write. To start working on this assignment, download and unzip Program #1 Project Folder, which contains statements from the first program you must write. Rename this project folder Program1). Then create three more new Java classes in it (as you did for one class in Program #0). The existing class is named ExpressionTest; each of the three remaining classes will contain a program that you will write from scratch to solve one problem; name the classes EscapeVelocity, ChangeMaker, and PizzaPricer. Write, run, and debug each class/program as you did in Program #0. When you finish each part, submit its .java file. Only one programmer of the pair should dropoff the programs: the same one for each part. It doesn't matter which of the pair submits, but that person should submit all the parts. Of course, each program should contain both student names (see below). IMPORTANT: Download the Drop Time program and examine and run it; use it as a model for all three of your programs. It declares variables, prompts the user for inputs (stored in some variables), computes values (stored in other variables), and prints the values of some variables along with some text.
Please cut, paste, and fill in the following comment at the top of each of
your programs (see how I did this in the Drop Time program mentioned
above).
Ensure that the names and email addresses of both programmers in the
pair appear in each program that you write, at the top of this big comment.
All programs prompt the user for information on the console. To get access to the class and its needed method names, you will have to add the import statement import edu.uci.ics.pattis.introlib.Prompt; after the big comment above (and of course, you will also have to add the introlib.jar file to Eclipse, if you haven't done so). This import gives this program access to all methods in the Prompt class. Note that you will also use methods from the Math class, but this class is implicitly/automatically imported into every Java program. Finally, it is a mistake to try to do all these problems in one sitting (especially on the night that they all are due). While all are simple, each part gets progressively harder. Sit down during the first lab and try to finish the first part; over the weekend, try to finish the second and third parts; if you succeed and have more time, finsh the last part (otherwise save it for next week) -maybe you will finish them all quickly. Even though the programming problems get more complicated, the first one is likely to cause you the most trouble: understanding the basics of the assignment (shared by all three programs), setting up everything, etc. It is important that you encounter all these difficulties early, by attempting to finish one problem over the first lab/weekend, well before the entire assignment is due; this will give you time to seek and obtain help, if you need it. |
Part 1: Calculating Escape Velocity |
The velocity needed to escape the gravitation field of a planet is given by
the mathematical formula
+--------- / 2 g mP v = / -------- \/ rPwhere g is the Gravitational constant (6.67x10-11 in the metric system), mP is the mass of the planet (in kilograms), rP is the radius of the planet (in meters) and v is the escape velocity in (in meters/second). Here is a short table of planets and their (approximate) calculated escape velocities.
Write a program that prompts the user for a planet's mass and radius, and then computes and displays the escape velocity of the planet. Verify these answers in the table above to ensure that your program is working correctly. To use the sqrt function (function is a mathematical term), remember to call the Math.sqrt method (method is a programming term) in your program. The Drop Time program, mentioned above, also uses such a call to a method in the Math library class, and has similar code to what you need to write. Examine this program and use it as a model for the one that solves the Escape Velocity program. Remember to fill in all the information in the comment at the top of the program. |
Part 2: Formulas -> Expressions |
The following three lines contain a total of 13 formulas
There are 7 on the first line, and 3 each on the second and third lines.
The class ExpressionTest contains a driver program to test all these
formulas: it declares the needed variables, prompts the user for their
values, and prints the answers.
But, you need to translate each of the 13 formulas into a correct Java
Expression (each now just says answer = 0;).
For maximum credit, your expressions should contain no redundant
parentheses; parentheses are redundant if they can be removed, but
leave the expression always still computing the correct answer, still
performing the required operations in the correct order.
For example, the parentheses in (a*b)/c are redundant because
even if we remove them, the expression a*b/c will still multiply
a by b before dividing by c (by Java's rules of
operator precedence and associativity).
The parentheses in a/(2.*b) are NOT redundant because if we remove
them, the result will be the quantity a/2. times b.
Note, some students might write a/2./b but I find this expression
more difficult to understand: you don't have to minimize
parentheses; you just want to write no redundant ones.
There is a difference between these goals (reread the material above for
clarification).
Finally, for the mathematical constants π and e in these formulas,
use Math.PI and Math.E instead of writing your own literals
that approximate these values.
|
Part 3: Change Maker |
Write a program that prompts the user for some amount of change to make (an
int from 1 to 99 cents; don't worry about bad input),
and then computes and displays the number of quarters, dimes, nickels, and
pennies to make that amount of change.
Always compute the fewest coins to make that change.
For example, if the user entered 69 the program would display
To make change for 69 cents: 2 quarters 1 dimes 1 nickels 4 penniesDon't worry about displaying things like 1 dimes (mismatching a singular and plural). Algorithm Hint: After the user inputs the amount of change to make, first determine the number of quarters to return (using / on ints: here truncation is useful), and then update the amount of change to make to be the remaining amount to return after accounting for the quarters returned; repeat this process for dimes, then nickels, and finally for pennies. For example, to make 69 cents in change, the program computes 2 quarters, leaving 19 cents in change to be made by dimes, nickels, and pennies; then the program computes 1 dime, leaving 9 cents in change to be made by nickels and pennies; etc. Finally, note that some amounts may be zero: To make change for 55 cents: 2 quarters 0 dimes 1 nickels 0 penniesSome solutions make use the the % operator; there are many different ways to solve this problem. Before programming the solution in Java, develop a procedure that allows you to calculate the right values when you follow it, and then automate that solution by translating it into a Java program. |
Part 4: Pizza Pricer |
Write a program that computes pizza prices.
The program should prompt the user to enter the diameter of the pizza (as an
int, in inches, not as a double) and the price of the pizza
(as a double, in dollars).
Then the program should compute and display
Test and check your program on various sizes and prices of pizzas (and compare its results with my executable). See the sample program Trip Planner for a similar program. Use this program as a model for the one that you are writing. |