1. The link to download
CMIS 212 Module 2 Assignment
Part 1:
1. "Java How to Program" textbook (JHtP) Ex3.5
2. Ex3.11.
4. Ex3.13.
Hints for Ex3.11:
a). After you declare the second String instance variable, you need to modify
your constructor to:
//Constructor:
public GradeBook( String course, String instructor )
{
setCourse(course);
setInstructor(instructor);
} // end constructor
b). Note that there is NO main( ) method in GradeBook.java. The
GradeBookTest.jva on Pages 89 - 90 DOES have main( ) method. On line 11
(and 13 as well) of GradeBookTest.java program on Page 90, you create an
object of GradeBook. Every time, you run a Java program, it looks for the
main( ) method first, in other words, you must have a main() method in the
program you want to run. As you create an object of the GradeBook program
you modified in Ex3.11 a-d, you use the newly changed GradeBook. If you try
to run a program without a main() method, you will get "main method not
found" error.
c). Now, since you have TWO arguments in the GradeBook constructor, when
you create a new object of GradeBook in the GradeBookTest program, you
need to pass TWO values over, for example:
GradeBookGradeBook("BIBL 105", "Sam Smith" );
If you just pass one or 3 values over, you will get an error. Alos, make sure
the values you pass over match with the data type you have in the GradeBook
constructor.
d). For Ex3.11, after you make changes instructed in a-d on p.104, to compile
the programs:
javac GradeBook.java GradeBookTest.java
To run the program:
java GradeBookTest
2. Hints for Ex3.13: // Note: this one does NOT have main() method
// Later, you will create InvoiceTest.java to instantiate Invoice objects
// Invoice class: Invoice.java
public class Invoice
{
private String partNumber;
private String partDescription;
private int quantity;
private double pricePerItem;
// four-argument constructor
public Invoice( String part, String description, int count, double price )
{
;
;
if ( count > 0 ) // determine whether count is positive
; // valid count assigned to quantity
if ( price > 0.0 ) // I will let you finish the rest:
...
} // end four-argument Invoice constructor
// set part number:
public void setPartNumber( String part )
{
; //accept the passed part value and assign it to instance variable partNumber
} // end method setPartNumber
//You will finish the other set and get methods:
...
Hints for InvoiceTest.java:
//Q1: Ask yourself, what's the purpose of a Test program?
//Q2: Why do we have Invoice.java and InvoiceTest.java? Why not combining
them in one? Think from object-oriented programming point of view.
public class InvoiceTest
{
public static void main( String args[] )
{ //create an instance of Invoice passing 4 argument values here.
//Question1: Do you have 4 arguments in the Invoice constructor?
//Question2: What would happen if you just pass 3 values here?
Invoice Invoice( "1234", "Hammer", 2, 14.95 );
// display invoice1
System.out.println( "Original invoice information" );
System.out.printf( "Part number: %sn", invoice1.getPartNumber() );
3. //I will let you finish the rest of the program to display more information of
invoice1:
Part 2:
"Java How to Program" textbook (JHtP) Ch4:
1. Ex4.13 In addition to answer the question of "What type of repetition would
be appropriate for calculating the sum of the first 100 positive integers?", also
write down your loop statement to calculate the sum of the first 100 positive
integers, that is to get total which is sum of 1 + 2 + 3 + ... + 100.
2. Ex4.15
3. Ex4.17
Hints for Ex4.17:
1. You will have two files:
a) Gas.java and
b) GasTest.java. The Gas.Test.java is very simple:
public class GasTest
{
public static void main( String args[] )
{
Gas Gas();
application.calculateMPG();
} // end method main
} // end class GasTest
2. In Gas.java file, obviously you will need a method called calculateMPG,
which will ask the user to input miles driven and gallons used as many times
as the user wants until the user does not want to enter anymore (for example,
you can tell the user to enter -1 if they want to stop entering).
public void calculateMPG()
{
Scanner Scanner( System.in );
int miles; // miles for one tankful
int gallons; // gallons for one tankful
int ; // total mailes for trip
int ; // total gallons for trip
double milesPerGallon; // miles per gallon for tankful
double totalMilesPerGallon; // miles per gallon for trip
...
4. // exit if the input is -1; otherwise, proceed with the program
while ( miles != -1 )
{
// prompt user for gallons and obtain the input from user
System.out.print( "Enter gallons: " );
();
...
// add gallons and miles for this tank to totals:
...
// calculate miles per gallon for the current tank
if ( gallons != 0 )
{ milesPerGallon = (double) miles / gallons;
System.out.printf( "MPG this tankful: ....
} // end if statement
// calculate miles per gallon for the total trip
if ( totalGallons != 0 )
{
...
} // end if statement
// prompt user for new value for miles:
System.out.print( "Enter miles (-1 to quit): " );
//retrieve the value and save it into miles:
...
} // end while loop
} // end method calculateMPG
//Note that this is NOT a complete program, just a hint.
Part 3:
T.4
4.x 13
Part 4:Top of Form
5. 1. SJP Tutorial 8.
2. SJP Ex8.12 (Mortgage Calculator Application)
Hint for Ex8.12:
To increment the year by 5 in the while loop:
while ( years <= 30 )
{
// calculate payment period:
...
// getmonthlyPayment:
...
// appendresultintooutputJTextArea:
...
years += 5; // incrementcounter
}