Homework Assignments - Spring 2010
Intro to Object-oriented Programming with Java
(COP 2551)

Assignments, due dates, and hints are given below, most recent
first. Homework is due by midnight of the date shown. Send
your homework as an email attachment from your FCCJ email account
to sdifranc@fscj.edu.
Include your name, homework number, and the class and reference
number in the subject line as described in the
homework guidelines.
Note: If you are asking for help on an assignment, rather
than turning it in, make sure to include the word "Help" in
the subject line.
Hw07: ArrayLists of objects (due 3/11)
For this assignment you will write a program
to maintain a simple portfolio of stocks. First,
write a
public
class
Stock
with these features:
instance variables
coName
(the name of the company),
sharesHeld
(the number of shares of that company
owned), and
sharePrice
(the current price of one share of stock).
A two-argument constructor that sets
coName
and
sharesHeld
to the arguments specified, and the
sharePrice
to 0.00. Validate the argument for
sharesHeld
to ensure it is non-negative; set it to 0 if
it is.
Three accessor methods, each of which
returns the value of one of the instance
variables (getCoName,
getSharesHeld,
getSharePrice).
One mutator
method
setSharePrice
that sets the current price per share (PPS).
Validate the argument to be non-negative;
set it to 0.00 if it is.
Include in
your class a main
method that helps test the class in these ways:
-
Declares an
ArrayList
of Stock
objects that represents a stock portfolio.
-
Instantiates three
Stock
objects and adds them to the
ArrayList.
(For example, "Xerox" with 100 shares, "Dow
Chemical" with 150 shares, and "General
Motors" with 1050.)
-
Prompts
the user to provide today's PPS for each
stock, extracting the name of the company
for the prompt by traversing the
ArrayList,
and using the
setSharePrice
method to set the instance variable.
-
Produces a
nicely formatted report of current portfolio
value like the one in this sample
executable:
Stock.class
Remember:
Start with a minimal
main
method. Get your class working before you try to
include all the functionality described above.
Submit your
program as
Stock.java
Hw06: Array statistics (due 3/4)
Write a program to compute and display some
statistics for the grades in this array:
int [] grades = {98, 87, 78, 100,
99, 67, 69, 50,
88, 100, 88, 79,
60, 75, 93, 97,
40, 98, 88, 62, 58,
85, 92, 93,
59, 98, 94, 95, 87,
47, 69, 79,
89, 85, 68, 75}; |
You should compute these statistics in
separate methods (except for number of grades),
and return the value for display in main:
number of grades, maximum grade, minimum grade,
mean (average) grade, and:
- The average (mean) grade (
x). This
is calculated simply as the sum of the
grades divided by the number of grades in
the array.
The standard deviation. To
compute the standard deviation s,
use this formula:

Where xi is
each individual grade, x is
the mean of the grades, and n is
the number of grades.
Determine
which of the grades, if any, are "unusual"
in the sense that their corresponding z-score
is < -2.0
or
>
+2.0. A z-score
is computed as:

Here is a sample executable, using the above
data: ArrayStats.class
This is a team assignment. Please review the
guidelines for team assignments. Here are
the teams:
- Nathan B (belang) & Vishni M (mahavi)
- Kerri B (boydka) & John C (caoij)
- Seta C (setac) & Kenny F (feieka1)
- Cassandra C (spracm) & Lee M (lem27)
- Garry D (dimagf) & Brad G (gonzbd1)
- Eddy H (hensea) & Mehrdad J (jamsm)
- Ellis H (hollel) & Misagh K (kerim2)
- David R (reevda2) & Robert W (walkrd2)
- Jason V (voorjb) & Jason T (truejr)
Submit your program as
ArrayStats.java.
Whoever is designated to submit the program,
please make sure the cc your teammate so they
will know it has been submitted.
Hw05: Writing a class (due 2/25)
Write a class named
CorporateName that has one instance
variable representing the name of a corporation.
Objects of the class will have one instance
variable (a String) representing the name (e.g.,
"International Business Machines"). The class
should have methods to:
- Instantiate a new object with the
corporate name specified as an argument (a
String)
- A setName
method that allows the name to be changed to
a new name
- A getName
method that returns a
String representing the corporate
name
- A toString
method that returns a
String representing the corporate
name in quotations (i.e.,
"International Business Machines")
- A shortName
method that returns an uppercase
String of of the
first letters of each of the words in the
corporate name (i.e., "IBM")
- A domainName
method that returns a lower case
String in the
format www.shortName.com
Use this main
method to test your class. Remember not to write
the whole class at one time; comment out the
lines in main that
you do not want to test yet.
public
static void main( String [] args )
{
CorporateName cn1 =
new
CorporateName("International
Business Machines");
CorporateName cn2 =
new CorporateName("American
Micro Devices");
System.out.println("Corporate
name #1 is "
+ cn1.getName());
System.out.println("Corporate
name #2 is "
+ cn2); /* will invoke
toString */
System.out.println("Short name
for Corporate name #2 is "
+ cn2.shortName());
System.out.println("A suggested
domain name is "
+ cn2.domainName());
cn2.setName("Cubic Conundrum
Corporation");
System.out.println("Revised
corporate name #2 is "
+ cn2 );
} |
Here is a sample executable:
CorporateName.class
Save and submit your program as
CorporateName.java.
Hw04: Iteration (due 2/11)
Write a program that will display a
two-dimensional table of the annual cost of
gasoline, where the columns represent different
prices per gallon of gasoline and the rows
different miles per gallon of auto efficiency.
Here is an example executable :
MPG.class that produces this output:
This program
displays the annual cost of gasoline for different mpg
and price per gallon of gas assuming 18,000 miles driven
annually.
MPG 1.50 1.75 2.00 2.25
2.50 2.75 3.00 3.25
-------------------------------------------------------------------
5 5,400 6,300 7,200 8,100 9,000 9,900 10,800
11,700
10 2,700 3,150 3,600 4,050 4,500 4,950
5,400 5,850
15 1,800 2,100 2,400 2,700 3,000 3,300
3,600 3,900
20 1,350 1,575 1,800 2,025 2,250 2,475
2,700 2,925
25 1,080 1,260 1,440 1,620 1,800 1,980
2,160 2,340
30 900 1,050 1,200 1,350 1,500 1,650
1,800 1,950
35 771 900 1,028 1,157 1,285 1,414
1,542 1,671
40 675 788 900 1,013 1,125 1,238
1,350 1,463
45 600 700 800 900 1,000 1,100
1,200 1,300 |
Use the principles of encapsulation and
generalization design your program with
efficient functions. The formula for annual cost
is:
annualCost = (18000 / mpg)
* pricePerGallon
You can round the annual cost to the nearest
dollar and insert a comma if needed by using the
printf method and
the conversion specifier
",8.0" where 8 is the field width.
As an added challenge, you can ask the user
to input the annual miles driven (and use this
value in the calculation above (rather than
18,000).
Save and submit your program as
Hw04_xxx.java,
where xxx are your
initials.
Hw03: Selection Structures (due 2/4)
Obesity is a growing health problem in the
United States, greatly increasing the risk of
developing conditions such as high blood
pressure, diabetes (type 2), heart disease,
stroke, gallbladder disease and cancer of the
breast, prostate and colon. The
American
Obesity Association reports that:
The number of adults in the United States
who are overweight or obese has continued to
increase, as shown in the table below.
Currently, 64.5 percent of U.S. adults, age
20 years and older, are overweight and 30.5
percent are obese. Severe obesity prevalence
is now 4.7 percent, up from 2.9 percent
reported in the 1988 - 1994 National Health
and Nutrition Examination Survey (NHANES) by
the Centers for
Disease Control and Prevention (CDC).
Increase in Prevalence (%) of Overweight (BMI > 25),
Obesity (BMI > 30) and Severe Obesity (BMI > 40)
Among U.S. Adults. |
| |
Overweight
(BMI > 25) |
Obesity
(BMI > 30) |
Severe Obesity
(BMI > 40) |
|
1999 to 2000 |
64.5 |
30.5 |
4.7 |
|
1988 to 1994 |
56.0 |
23.0 |
2.9 |
|
1976 to 1980 |
46.0 |
14.4 |
No
Data |
|
Source: CDC, National Center for Health Statistics,
National Health and Nutrition Examination Survey. Health, United
States, 2002. Flegal et. al. JAMA. 2002;288:1723-7. NIH,
National Heart, Lung, and Blood Institute, Clinical Guidelines
on the Identification, Evaluation and Treatment of Overweight
and Obesity in Adults, 1998.
|
BMI is calculated with this formula, using a fruitful method:
BMI = ( weight (lbs) / height (inches)2 ) x 704.5
Note that in the English system of measurement, there are 12
inches in one foot. For example, a 195-pound man 5 feet and 10
inches tall ( = 12 x 5 + 10 = 70 inches) would have a BMI of:
( 195 / (70)2 ) x 704.5 = ( 195 / 4900 ) x 704.5
= 28.0
Write a program that computes BMI for a person, given as input
the person's weight in pounds and height in inches. You should
provide appropriate prompts for the input required. Your program
should output the calculated BMI, and the phrase "You are ..."
followed by: "Overweight", "Obese", or "Severely Obese" if the BMI
is in the ranges defined in the table above, "Underweight" if BMI is
< 20, and "Normal" if BMI is in the range 20 <= BMI < 25.
For an extra (optional) challenge: If the person has a BMI of 25
or more, also compute how many pounds the person should lose to
attain a BMI of 24, and output the message: "You should try to lose
at least x pounds" (where x is the number of pounds
calculated). If the person has a BMI of less than 20, compute how
many pounds the person should gain to attain a BMI of 20, and output
the message: "You should try to gain at least x pounds"
(where x is the number of pounds calculated). If a person has
a BMI in the "Normal" range, output the message: "You are
maintaining a healthy weight."
Here is a compiled class from the sample solution you can run to
see the interface and to check your work:
Bmi.class
Save your class and submit your program as
Hw03_xxx.java, where
xxx are your
initials.
Hw02: Writing and using methods (due 1/28)
Write a program to calculate the future value
of an investment paying compound interest, based
on user inputs for principal (p)
deposited (the amount of the investment), the
annual interest rate (r) paid (in
percent), and term (t) of the investment
(in months). Assume that interest is paid
monthly. The formula for computing future value
(fv) is:
fv = p(1 + (r / 100))t
Note that r and t need to be in the same time
units: if t is given in months, then r should be
the monthly interest rate. You can convert
annual interest rate to monthly interest rate by
dividing it by 12.0.
Write your class (program) with three
methods:
- A main method
that obtains user inputs and displays the
results
- A splash
method that displays a splash screen
describing what the program does
- A calcFV
method that calculates and returns the
future value, given three values as
arguments by the calling method: principal,
interest rate, and term.
Here is a compiled program you can use to
observe the interface and to test your program:
Compound_Interest.class.
Note
that this is a compiled file and cannot be
opened in an editor or IDE. You can run it,
however, with the JRE. Use this procedure:
- Right-click on the file, and from the
context menu download it to the folder of
your choice on your hard drive or thumb
drive. (In the screen session below it has
been saved to C:\temp)
- Start a command window (Start
-> Run -> command)
- Change to the drive and folder where you
saved the file
- Run the file:
java Compound_Interest
Here is a sample command window that starts
the program:
Microsoft(R) Windows DOS
(C)Copyright Microsoft Corp 1990-2001.
H:\>c:
C:\>cd \temp
C:\TEMP>java Compound_Interest
*** Investment Calculator 1.2 ***
This program computes the future value of an investment that
pays compound annual interest for a fixed number of months.
------------------------------------------------------------
Amount of investment? 1000
Annual interest rate? 5
Number of months? 24
Future value will be: $1,104.94
C:\TEMP>
|
Save your class and submit your program as
Hw02_xxx.java, where xxx are your
initials.
Hw01: Using The
JCreator IDE
(due 1/21)
Modify the program given in the
JCreator IDE Tutorial to prompt the user for a number of students
who have signed up for the senior class trip to Washington,
D.C., then calculate the number of full buses (each can hold 42
students) needed, and the number of students that will be on the
last partially-filled bus (i.e., the number of students
"left over"), if any, and display the result.
Save your source code with the filename
Hw01_xxx.java, where
xxx are your
initials, and email to me as an attachment from your student
email account as described in the homework guidelines.
Note: This means the name of your class must be
Hw01_xxx:
public
class Hw01_xxx
{ ...
|