Lab 6 - Papademas.net

kitewormsΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

220 εμφανίσεις




CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
1

of
11


Instructor


Due Date






Project

1

2

3

4

5

6

TOTAL


Maximum 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 cons
truct a Java class which demonstrates the operation of an investment



account.



PROJECT DESCRIPTION




Write 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 Project



This 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
.





a
mount of deposit:

$

60
,
000.00



number of y
ears:

10



i
nterest rate:


6.75



(

annual compounding

)




T
o compute the maturity amount

for these values
, the

following

expression

is evaluated
:




60000.00

(

1

+

6.75

/

100

)

10





yieldin
g the amount $

115
,
300.2
1

.




Steps To Complete This Project


STEP 1


Open a Java Text Editor and Type the Program Code





Open
a Java
t
ext
e
ditor

and create a new Java file and name it as:




Maturity.java




When th
e
e
ditor opens, type the program c
ode
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.






CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
2

of
11

PROJECT ONE




Figure 1

Source Code for the Investm
ent Program






//This
p
rogram 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, va
lueL;


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 HEI
GHT = 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);



//create four textfields


amtTF = new JTextField(10);


yearTF = new JTextField(10);


interestTF = new JTextFi
eld(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);






CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
3

of
11

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(inter
estTF);


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);


}



pri
vate class CalculateButtonHandler implements



ActionListener


{



public void actionPerformed(ActionEvent e)


{



double year, amt, interest, value;


DecimalFormat twoDigits = new

DecimalFormat("0.00");



amt = Double.parseDouble(amt
TF.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));


}


}









CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
4

of
11

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 stati
c 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 cred
it.







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 frequen
cy of the investment. To understand how

to modify your code for this entry, consider the example data, given in the

Information About This

Project

section of this lab project, when the

compounding frequency is semi

-

annual (

twice per year

). In thi
s case the



given data would be:







a
mount of deposit:

$

60
,
000.00




number of y
ears:

10




i
nterest rate:


6.75



(

semi

-

a
nnual 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 va
lue.



Paste the snapshot in a word processing document and submit the hardcopy



for credit. Submit your completed program code as well.




CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
5

of
11




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 applicat
ion 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 cos
t


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


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.







CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
6

of
11

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(" ");


value
.setFont(new Font("Tahoma 12 point bold. ", 20, 20));


add(
value
);


setVisible(
true
);


}


public void windowClo
sed(WindowEvent event)
{

}

public void windowDeiconified(WindowEvent event)
{

}

public void windowIconified(WindowEvent event)
{

}

public void windowActivated(WindowEvent event)
{

}

public void windowDeactivated(WindowEvent event)
{

}

public void windowOp
ened(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 Test
Depreciation

{

public static void main(String args[])

{

new
Depreciation
("
Depreciation
");
}

}







CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
7

of
11

PROJECT TWO




The above program c
ode

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 t
o



enter not only the asset cost but the salvage value and asset life as well. The



input of these three items may be through
JOptio
nPane

Input dialog boxes.


STEP 5


Run the Modified Application




Run the modified application and test it with the sa
mple data given below.


Figure 2

Depreciation Scenario


asset type

computer system


asset cost

$

5,000.00

salvage value

$

300.00

asset life

5 years




Take print screen snapshots of the entire operation of the application.



Submit the snapshots and

your completed program code for credit.



























CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
8

of
11



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 Pro
ject


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.



Figure 1

Sample Code for Building a Frame Object in Java





import java.awt.Frame;


//includes the Frame class


cla
ss 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



mess
ageTwo += "To quit, click this window";


messageTwo += " and then press Ctrl + C.";


System.out.println(messageTwo);


}
//end main

}
//end class TestFrame










CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
9

of
11

PROJECT THREE



STEP 2

Compile and Run the Program





After you have typed the g
iven 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.


th
e

outp
ut
window

and press

Ctrl


+

C


.


STEP 3

Modify the Program



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.
Color
;




Locate the following line of code





}
//end class TestFrame



Directly below th
e definition of class
TestFrame

, add a definition for a new
class named
Color
Frame

. The code for this class is given below.






class ColorFra
me 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.




CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
10

of
11

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.


STEP 5

Ad
d 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 t
hr
ee lines of code in your editor.





class ColorFrame extends Frame



{





ColorFrame(String s)


Befor
e

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 cont
inue");




//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.






CSC 156




Java Computer Science
I



LAB
6





Student Name


Section



© Copyright 200
6

by P.E.P.



Page
11

of
11

PROJECT THREE




Take a screen snapshot of t
he 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.