# Assignment – Exc

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

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

112 εμφανίσεις

Ass
#

Text
Chapter

ASSIGNMENT
CPSC

223J SPRING 2003

Due

Points

Note
:
documentation according to
Assignment_Guidlines.doc.

1

Ch2

Application
: Modify Exc. 2.26. Find the sum,
average, product, the largest and t
he smallest
integers of 5 integers. Use showInputDialog and
showMessageDialog methods for entering 5
numbers and outputting the result.

02
-
12

2

2

Ch3

Ch4

In class:
Convert your Ass #1 into an
applet.

In home:
It’s a given accuracy < 0.01 which you

e湴敲⸠.潭灵oe⁴桥⁶ 汵攠潦†y =⁥x瀨p⤬)
⡳(e⁥xc⸴⸳㈠.⤠睨wre

y =‱ +⁸⼱℠/
x
2
⼲⬠
x
3
/3 +…

C潭灵oe⁴桥⁶ 汵攠潦 y⁵湴n氠l桥⁳ 煵q湴na氠敬l浥湴n

…0.9, 1
.

-

-

1

2

3

C栴
-
5

In class:
Convert your Ass #2 into an
applet

and
calculate

y = 1 + x/ (1 * 3) +
x
2
/(2 * 5) +
x
3
/(3 * 6) +
x
4

/(4 * 7) + …

In home:
Write an
application
t
hat prints a table of
the binary and octal equivalents of the decimal
numbers in the range 128 through 256. If you are
not familiar with these number systems, read
Appendix C5 first. Place the results in a
JTextArea

with scrolling functionality. Display
th
e
JTextArea

in a message dialog. See the
solution for the opposite conversion, exc 4.25
below.

02
-
26

03
-
05

1

2

4

Ch6

In class:

that

prints a table of the hexadecimal equivalents of the
decimal numbers in the range 128
through 256.

In home: Applet

-

Modify exc. 6.34. Guess the
number between 1 and n, where n should be entered
by the user. Track the number of user’s steps to
g
u
e獳⁴桥⁮畭 e爮⁕獥⁴ e⁡摤楴楯湡氠i楥汤⸠

Tip
. So you
may

use
3

labels,
3

fields
(enter n,
g
uess number, number of steps so far)
and the
status bar. Your
guess number
input field will need
to be
registered
for the Enter key event:

input
GuessNum
Field = new JtextField(10);

input
GuessNum

03
-
05

03
-
12

1

2

5

Ch7

In

class:
4

that

call
03
-
12

1

method buildOutput similar to Fig.7.12 to mark the

guess number user entered with
an asterisk (*).
Note: Every row of the output should include
only
all numbers to be
considered

further
with the
current g
uess number *.

In home: Applet

Exc. 7.21.

Implement the
concept of a mechanical turtle. Use a 20
-
by
-
20
array floor that initialized to zeros. Read commands
from an array that contains them. Keep track of the
current position of the turtle at all times an
d
whether the pen is currently up or down.

03
-
19

2

6

Ch8

In class:
5. The
commands should be entered by the user and

all
command
s

entered by the user
should be
printed to
the screen

using
JTextArea

or comma
nd line
window.

In home:
A
pplication

Exc. 8.1
7

( Modify
the
Time
3

class
to represent the time internally as the
number of seconds since midnight
and use
class

TimeTest
3
)

03
-
19

04
-
09

1

2

7

Ch9

In class:
6
.
Use
you
r class
Time3

(or
Time 1

assignment) and modify
class

TimeTest
4

(Fig. 8.8)

JButton
s with the
following labels:
a
dd 1 to Hours, add 1 to Minutes
.
Implement methods:
incrementHour
,
incrementMinute
. Test all cases.

I
n home:
A
pplication

Exc.
9.6 (write an
inheritance hierarchy for class
Trapezoid,
Parallelogram, Rectangle

and
Square
.)

04
-
09

04
-
16

1

2

8

Ch10

In class:
A
pplication
.

R
ewrite y
our home
assignment #
7
to use composition rather
than
inheritance (
ch8.9 and 9.4
)
. Many programs written
with inher
itance could be written with composition

In home: Application with JFrame.
Exc.10.9.
Create class MyShape as an abstract class with
abstract method
public abstract
void draw
(Graphics g).
MyLine, MyOval, MyRect, inherit
MyShape. Introduce driver MyTest with
polymorhism
using a JFrame that has a paint
method.

04
-
16

04
-
23

1

2

9

Ch11

In class:

A
pplication
.
Exc. 11.12 (4th edition exc.
10.12). Write an applicat
ion that inputs a telephone
number as a string in the form (666) 678
-
9910.

See
Fig.11.18

In home: Application with JFrame.

Exc.11.14

(4th edition exc. 10.14).
Write a program that
04
-
23

04
-
30

1

2

alphabetized a list of strings. See Fig.7.10 and
11.18

10

Ch12

In class:

Application with JFrame.
Extend your
home assignment #
9 to prints a table in a text area
indicating the number of occurrences of each
different word

length

in the text.

In
home
:
Application with JFrame.

Exc. 12.21
(4th edition exc. 11
.21). Use the javax.swing.Timer
class to delay drawing 100 lines. In the constructor
write :

timer =
new

Timer

(milisec_delay,
this
);

timer.start();

The instance of class
Time
r will call
this

object’s
actionPerformed
method every milisec_delay.
The

acti
onPerformed

has only call
repaint
.

04
-
30

05
-
7

1

2

11

Ch13

In home
:
Application with JFrame.

Exc.
13.27
-

13.29 (old edition
12.27
-

12.29
)
. Draw the selected
shape (

rectangle, oval, line, rounded rectangle)
from a

JComboBox

using the mouse button
.

05
-
14

2

12

Ch13

In class:

Enhance your home assignment #10 to
allow the user

1. to select the drawing color from a
JColorChooser

dialog box

2. to specify whether a shape should be filled or
empty when it is drawn. The user should click a
JCheckBox

to i
ndicate filled or empty.

In home
:

Exc. 14.7
-
14.8 (old edition 13.7
-
13.8)
.
Draw a circle and square etc. for the radius( = side)
entered by the user via
JSlider
.

05
-
14

1

Exc.4.25.
Write an
application

that inputs an int
eger containing only 0s and 1s (i.e., a "binary" integer)
and prints its decimal equivalent. [
Hint
: Use the modulus and division operators to pick off the "binary
number's" digits one at a time, from right to left. Just as in the decimal number system, whe
re the rightmost
digit has a positional value of 1 and the next digit to the left has a positional value of 10, then 100, then
1000, etc., in the binary number system the rightmost digit has a positional value of 1, the next digit to the
left has a positio
nal value of 2, then 4, then 8, etc. Thus, the decimal number 234 can be interpreted as 4 * 1
+ 3 * 10 + 2 * 100. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8
or, 13.]

Solution:

// Exercise 4.25 Solution: Binary
.java

// Program prints the decimal

// equivalent of a binary number.

// Java core packages

import java.awt.*;

// Java extension packages

import javax.swing.JOptionPane;

public class Binary {

// main method begins execution of Java application

p
ublic static void main( String args[] )

{

int binary, // binary value

bit, // bit positional value

decimal; // decimal value

bit = 1;

decimal = 0;

binary = Integer.parseInt(

JOptionPane.s
howInputDialog(

"Enter a binary number: " ) );

// convert to decimal equivalent

while ( binary != 0 ) {

decimal += binary % 10 * bit;

binary /= 10;

bit *= 2;

}

JOptionPane.showMessageDialog(

null, "Decimal is: " + decimal, "Binary",

JOptionPane.INFORMATION_MESSAGE );

} // end method main

} // end class Binary