Post on 16-Jan-2016
CSC 156
Java Computer Science I LAB 6
Student Name Section
Instructor Due Date
Project 1 2 3 4 5 6 TOTALMaximum Points
2 points 2 points 2 points 2 points 1 point 1 point 10 points
Your Score
PROJECT ONE ( Creating a GUI Business Application )Objective To construct a Java class which demonstrates the operation of an investment account.
PROJECT DESCRIPTIONWrite and compile a GUI application which computes the amount of a
certificate of deposit on maturity. Use the starter code provided within Figure 1 as a guideline to develop your GUI.
Information About This ProjectThis particular program illustrates using the compound interest formula from
finance to calculate the maturity value of a principal deposit.For example, consider the following sample data.
amount of deposit: $ 60,000.00number of years: 10interest rate: 6.75 ( annual compounding )
To compute the maturity amount for these values, the following expression is evaluated:
60000.00 ( 1 + 6.75 / 100 ) 10
yielding the amount $ 115,300.21 .
Steps To Complete This Project
STEP 1 Open a Java Text Editor and Type the Program Code
Open a Java text editor and create a new Java file and name it as: Maturity.java
When the editor opens, type the program code given in Figure 1 , which follows. Include your name, data and course information in the heading portion of your program code. The program code generates the graphical user interface below.
© Copyright 2006 by P.E.P. Page 1 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section
PROJECT ONE
Figure 1 Source Code for the Investment Program
//This program determines the total amount on maturity.
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.text.DecimalFormat;
public class Maturity extends JFrame{ private JLabel amtL, yearL, interestL, valueL; private JTextField amtTF, yearTF, interestTF, valueTF;
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler; private ExitButtonHandler ebHandler;
private static final int WIDTH = 400; private static final int HEIGHT = 300;
public Maturity() { //create four labels amtL = new JLabel("Amount deposited : ", SwingConstants.RIGHT); yearL = new JLabel("Duration in years : ", SwingConstants.RIGHT); interestL = new JLabel("Interest rate : ", SwingConstants.RIGHT); valueL = new JLabel("Value on maturity : ", SwingConstants.RIGHT);
© Copyright 2006 by P.E.P. Page 2 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section //create four textfields amtTF = new JTextField(10); yearTF = new JTextField(10); interestTF = new JTextField(10); valueTF = new JTextField(10);
//create Calculate Button calculateB = new JButton("Calculate"); cbHandler = new CalculateButtonHandler(); calculateB.addActionListener(cbHandler);
//create Exit Button exitB = new JButton("Exit"); ebHandler = new ExitButtonHandler(); exitB.addActionListener(ebHandler);
PROJECT ONE
Figure 1 Source Code for the Investment Program ( continued )
//set the title of the window setTitle("Maturity Amount");
//get the container Container pane = getContentPane();
//set the layout pane.setLayout(new GridLayout(5,2));
//place the components in the pane pane.add(amtL); pane.add(amtTF); pane.add(yearL); pane.add(yearTF); pane.add(interestL); pane.add(interestTF); pane.add(calculateB); pane.add(exitB); pane.add(valueL); pane.add(valueTF);
//set the size of the window and display it setSize(WIDTH,HEIGHT); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); }
private class CalculateButtonHandler implements ActionListener {
© Copyright 2006 by P.E.P. Page 3 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section public void actionPerformed(ActionEvent e) { double year, amt, interest, value; DecimalFormat twoDigits = new DecimalFormat("0.00");
amt = Double.parseDouble(amtTF.getText()); year = Double.parseDouble(yearTF.getText()); interest = Double.parseDouble(interestTF.getText()); value = amt * Math.pow((1.0 + (interest / 100.0)), year); valueTF.setText("" + twoDigits.format(value)); } }
PROJECT ONE
Figure 1 Source Code for the Investment Program ( continued )
private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } }
public static void main(String args[]) { Maturity interestProductObject = new Maturity(); }}
STEP 2 Compile the Program
After you have typed the given program code, compile it.
STEP 3 Run the Program
Run your program using the sample data given in the Information About This Project section of this lab project.
Take a screen snapshot displaying the input values and the corresponding output value. Paste the snapshot in a word processing document and submit the hardcopy for credit.
STEP 4 Modify the Program
Close your application and return to your Java editor to modify your program code.
Include, in your code, a new label field and a new text field to give the user the ability to enter the compound frequency of the investment. To understand how to modify your code for this entry, consider the example
© Copyright 2006 by P.E.P. Page 4 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section data, given in the Information About This Project section of this lab project, when the compounding frequency is semi - annual ( twice per year ). In this case the
given data would be:
amount of deposit: $ 60,000.00number of years: 10interest rate: 6.75 ( semi - annual compounding )
and to compute the maturity amount for these values, the following expression is evaluated:
60000.00 ( 1 + 6.75 / 100 / 2 ) 20
yielding the amount $ 116,536.43 .
STEP 5 Run the Modified Program
Run the modified program with the above sample data and take a screen snapshot displaying the input values and the corresponding output value.
Paste the snapshot in a word processing document and submit the hardcopy
for credit. Submit your completed program code as well. PROJECT TWO ( Creating a GUI Business Application )
Objective To write a program that generates a depreciation report.
PROJECT DESCRIPTION
Write a program that has the capability of generating a depreciation report based on given information.
Information About This Project
This particular program illustrates the importance of a business system application using a graphical user interface.
The type of depreciation used in the report is straight - line or linear depreciation.
In accounting, the amount D of annual, straight - line depreciation is computed using the formula:
D =asset cost salvage
value
asset life
where the asset cost is the purchase price of the asset, the salvage value is the expected residual value upon disposal of the asset and the asset life is the lifetime of the item.
Steps To Complete This Project
© Copyright 2006 by P.E.P. Page 5 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section STEP 1 Open a Java Text Editor
If it is not open, launch a Java text editor.
STEP 2 Open a New File
Open a new Java file named Depreciation.java . Within the file, write the program code which will generate a depreciation report. Sample depreciation report code is given in Figure 1 , which follows.
When executed, the code shows the GUI application given below.
When the user enters an asset cost and presses Enter , a label shows the amount of depreciation, as shown in the sample screen given below.
PROJECT TWO
Figure 1 Source Code for the Depreciation Program
import java.awt.*;import java.awt.event.*;import java.text.DecimalFormat;
class Depreciation extends Frame implements ActionListener, WindowListener{ Label directions; TextField cost; Label value;
Depreciation(String s) {
super(s); setSize(300,100); setLayout(new FlowLayout()); addWindowListener(this); directions = new Label("enter the asset cost: "); add(directions); cost = new TextField(5); add(cost); cost.addActionListener(this); value = new Label(" ");
© Copyright 2006 by P.E.P. Page 6 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section value.setFont(new Font("Tahoma 12 point bold. ", 20, 20)); add(value); setVisible(true); }
public void windowClosed(WindowEvent event) { }public void windowDeiconified(WindowEvent event) { }public void windowIconified(WindowEvent event) { }public void windowActivated(WindowEvent event) { }public void windowDeactivated(WindowEvent event) { }public void windowOpened(WindowEvent event) { }public void windowClosing(WindowEvent event) { System.exit(0); }
public void actionPerformed(ActionEvent event) {
DecimalFormat twoDecimal = new DecimalFormat("0.00"); int c = Integer.parseInt(cost.getText()); double amount = (c – 500) / 10; cost.setText(" "); value.setText(" amount = $" + twoDecimal.format(amount));}}class TestDepreciation{ public static void main(String args[]){ new Depreciation("Depreciation"); }}
PROJECT TWO
The above program code assumes that the asset salvage value is $ 500 and the asset life is 10 years.
STEP 3 Run the Application
Compile the file using the TestDepreciation class.
Run the application, and enter an asset cost of 5000 in the text field and press. Enter . The depreciation amount is then displayed in the application’s label.
Close the application and return to the Java code editor.
STEP 4 Modify the Application
Modify the deprecation application such that the user will have the ability to enter not only the asset cost but the salvage value and asset life as well. The input of these three items may be through JOptionPane Input dialog boxes.
STEP 5 Run the Modified Application
Run the modified application and test it with the sample data given below.
Figure 2 Depreciation Scenario
© Copyright 2006 by P.E.P. Page 7 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section
asset type computer system
asset cost $ 5,000.00salvage value $ 300.00asset life 5 years
Take print screen snapshots of the entire operation of the application. Submit the snapshots and your completed program code for credit.
PROJECT THREE ( Implementing GUI Frame Objects )
Objective To type a simple Java program involving a frame object.
PROJECT DESCRIPTION
Onto your Java text editor, copy the program code given in Figure 1 , which follows. Then run the program and observe the frame object created by the program code.
Information About This Project
This particular program illustrates an example of using the frame object.
Steps To Complete This Project
STEP 1 Open a Java Text Editor and Type the Program Code
Open a Java text editor. Open a new Java file and name it as TestFrame since this is the theme for this project. Click OK to close the New dialog box. When the Java editor opens, type the program code given in Figure 1 below. Include your name, data and course information in the heading portion of your program code.
© Copyright 2006 by P.E.P. Page 8 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section
Figure 1 Sample Code for Building a Frame Object in Java
import java.awt.Frame; //includes the Frame class
class TestFrame{ public static void main(String args[]) { String messageOne = ""; String messageTwo = ""; messageOne += "creates a 250 x 100 pixel frame"; messageOne += "with title \"Frame Example\"."; System.out.println(messageOne);
//begin frame construction Frame frame = new Frame("Frame Example"); frame.setSize(250, 100); //250 pixels wide by 100 pixels high frame.setVisible(true); //displays frame on the screen //end frame construction
messageTwo += "To quit, click this window"; messageTwo += " and then press Ctrl + C."; System.out.println(messageTwo); }//end main }//end class TestFrame
PROJECT THREE
STEP 2 Compile and Run the Program
After you have typed the given program, compile it. If the program compiles correctly, run it and you will see a frame appear, as shown below.
To close the frame click the Console screen and then use the keyboard shortcut
. Ctrl + C to close the program. the output window and press Ctrl + C .
STEP 3 Modify the Program
Return to your Java Code editor and modify the application to include
© Copyright 2006 by P.E.P. Page 9 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section color in the frame.
To do this, add the following import statement to the heading declarations portion of the code.
import java.awt.Color;
Locate the following line of code
}//end class TestFrame Directly below the definition of class TestFrame , add a definition for a new class named ColorFrame . The code for this class is given below.
class ColorFrame extends Frame{ ColorFrame(String s) { super(s); setBackground(Color.blue); setSize(250, 100); setVisible(true); //displays frame on the screen }}}//end class ColorFrame
Locate the following block of code in the main() method.
//begin frame construction
.
.
.
//end frame construction
Comment block this entire group of statements. PROJECT THREE
Directly below this comment block, insert the line of code given below.
new ColorFrame("Frame Example");
STEP 4 Run the Modified Program
Run your program. Note the frame now has a color of blue.
Take a screen snapshot of the color frame and submit it for credit. Close the running application and return to your Java Code editor.
© Copyright 2006 by P.E.P. Page 10 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section STEP 5 Add a Button
Return to your Java Code editor and modify the application to include color in the frame.
To do this, add the following import statement to the heading declarations portion of the code.
import java.awt.Button; //include Button class
Locate the following three lines of code in your editor.
class ColorFrame extends Frame{ ColorFrame(String s)
Before the constructor definition, but within the class ColorFrame definition, add the following line of code.
Button button; // declare a button
Locate the following line of code in your editor.
setVisible(true); //displays frame on the screen
Directly below this line, insert the following code block.
setLocation(400,50);button = new Button("click to continue");//creates a button
add(button); //makes button a component
After you make these modifications, run your application and observe the button that appears. Refer to the snapshot given below.
PROJECT THREE
Take a screen snapshot of the color frame and submit it for credit.
Close your running frame and return to your Java Code editor and supplement your code to give the user the ability to exit the application when the button is clicked.
Test your new code.
STEP 6 Print Your Source Code
When completed, submit your completed source code for this project.
© Copyright 2006 by P.E.P. Page 11 of 12
CSC 156
Java Computer Science I LAB 6
Student Name Section
© Copyright 2006 by P.E.P. Page 12 of 12