6.8
1.)Describe how to set up a timer and explain what it does.
When the timer is sent the start message, its clock starts ticking, when each interval of time passes, the listener’s actionPerformed method is triggered. This method runs the operations to move the object and repaint the panel, because the timer uses the computer’s clock to measure the intervals, they will not vary with the execution speed of the computer.
2.)Describe the factors that affect our perception of the movement of a graphical object.
3.)What causes flicker? How can flicker be eliminated.
4.)How do the direction and velocity of an object determine where it will be placed after a given unit of time
4.10
1.)write a code segment that uses an I/O dialog box to prompt the user for her name.
2.)write a code segment that display your name and address in a message box, name an address should be formatted on separate lines by using the “\n” character.
3.)why we need to used the methods Integer.parseInt and Double.parseDouble when receiving numeric input from an I/O dialog box.
4.) Give an example of a situation where ou would want a panel to set its preferred size, rather thant allow the size of the main window to determine that
3.7
1
a). a filled rectangle with corner point(45, 20) and size 100 by 50
-g.drawRectangle(45, 20, 100, 50)
b.)a line segment with end points(20, 20) and (100, 100)
-g.drawSegment(20, 20, 100, 100)
c.)a circle with center point(100, 100) and radius 50
-g.drawCircle(150, 150, 50, 50)
d.)a triangle with vertices (100, 100), (50, 50), and (200, 200)
-g.drawTriangle(100, 100, 50, 50, 200, 200)
2.)describe the design of a program that displays a filled blue rectangle on a red background
-The program uses a modified version of the have added a paintComponent method that draws a blue regtangle containing a red text message when the window opens and whenever it is refreshed. The paintComponent methid first calls the same method in the superclass, using the reserved word super. The reason is that the method in the superclass paints the background of the panel.
3.)how does one compute the center point of a panel
-The method getWidth() and getHight() return the current width and hight of a panel, respectively. It will compute the center point of a panel. If the methods are called before the window is opened, they return a default value of 0.
4.)list the three properties of a text font
-color, font size, font style
12.1
12-1
1.)what keeps a recursive definition from being circular
-The fact that sum(1) is defined to be 1 without making reference to further invocations of sum saves the process from going on forever and the definition fro being circular.
2.)what are two parts of any recursive method
-A method is said to be recursive if it calls itself. First, some function f(n) is expressed in term of f(n-1) and perhaps f(n-2) and so no. Second, to prevent the definition from being circular, f(1) and perhaps f(2) and so on are defined explicitly.
3.)why is recursion more expensive than iteration
-
4.)what are the benefits of using recursion
2.7
1-a.)White-new Color(255, 255, 255)
b.)Black-new Color(0, 0, 0)
c.)Highest Intensity Blue-new Color(200, 200, 255)
d.)Medium Gray-new Color(128, 128, 12 ![]()
2.)A frame is a class which the code for application windows in Java is located. A panel is a flat, rectangular area suitable for displaying other objects such as geometric shapes and images. A layout manager is an object which a frame or a panel uses in container object in Java.
3.)When we have more than one penal or other objects to display in a window, we have to be concern about how they are organized or laid out.
4.)write a code used to set the layout for adding panels to a 5-by-5 grid in a window
import javax.swing.*;
import java.awt.*;
public class GUIWindow{
public static void main(String[] args){
JFrame theGUI = new JFrame ();
theGUI.setTitle(”Fourth GUI Progrgam”);
theGUI.setSize(300, 200);
theGUI. setDefaultCloseOperation(JFrame.EXIT_OM_CLOSE_);
JPanel.panel1 = new JPanel();
panel1.setBackground(Color.white);
JPanel.panel2 = new JPanel();
panel2.setBackground(Color.black);
JPanel.panel3 = new JPanel();
panel3.setBackground(Color.gray);
JPanel.panel4 = new JPanel();
panel4.setBackground(Color.white);
Container pane = theGUI.getContentPane ();
pane.setLayout(new GridLayout (5, 5));
pane.add(panel1);
pane.add(panel2);
pane.add(panel3);
pane.add(panel4);
theGUI.setVisible(true);
CH9
12.1
1)array is a data structure. It helps user easy to manipulate millions of items and elements.
2) items in the array are called elements. They all must be in same type. The type can be any primitive or reference type. For example, there can be array of scores, name or student objects. Each array have to have same length.
3) Mary is using an array of doubles to store an employee’s wage amounts for each day of the week (Monday through Friday). Draw a picture of this array with sample items and references to each one.
12.2
1) Assume that the array a contains the five integers 34, 23, 67, 89, and 12. Write the values of the following expressions:a. a [1] a array’s 2nd element, so 23. b. a[a.length - 1] 4c. a[2] + a[3]67 + 89 = 156 2. What happens when a program attempts to access an item at an index that is less than 0 or greater than or equal to the array’s length? it’’s going to be out of bound, causing error.
9.3
1.) int i=0 for (i==0, i=0; i-) System.out.print(a[i]). 3.) int index; for (int i=0; i<a. length; i++){if(a[i]<0){index=i; break;} else{index=a.length;}System.out.print (index); 4a)It will change all the negative values of a to positive value by taking the absolute values of them.
9.4
1.)double[] abc=new double[15] string[] abcd=new sting[20].
2)It is another way to initialize the array.it assign the exact values to array’s element.
3-a)int[] aaa={100,90,75,60,88}
b.) double[] bbb={0.12,0.05,0.15}
c) string [] ccc={david, chen}.
4. When declaring an array variable.
9.5.
1.)The programmer will try to solve the problem by tracking the array’s logical size with a separate integer variable.
2.)When the array is not full, it must replace the array;s physical length with its logical size in the loop.9.6
1.A parallel array is situations in which it is convenient to declare.
2.One array should contain the exponent and the other array should contain 2 raised to that power.
3. String [] name = new String[50]int [] age = new int [50]int [] security = new int [50]String searchPerson;int correspondingAge = -1, correspondingSecurity = -1searchName = …for (int i = 0, i<name.length; i ++) if (searchName.equal (name[i]))correspondingAge = age [i]correpondingSecurity = security [i]break;}4.
9.7
1.An array in which each data item is accessed by specifying a pair of indices.
2.If an application require the variable tale reference an array of four elements. Each of these elements in turn reference and array of five integers. And the application will need to use two dimensional arrays.
8.1~8.8
8.1 1.A hypertext is a structure consisting of nodes and the links between them.2. Hypermedia is like hypertext, but it adds GUIs, Images, Sound, Animation, Applications.3. Assigning node addresses using machine-independent hypertext markup language. 8.21.HTML is developed as a machine-independent way of representing information in a networked-based hypermedia system. 2. It can indicate the format of textual elements or links to other nodes. 3. The text for the document goes here. 4. The HTML is a comment for the creator to reviews.8.31. The HTML programmer need to use forced line break because a web page author or designer wants to display several lines of text without words wrap.2. We are preformatted next in an HTML document when we want the browser to display text, with line breaks, extra spaces, and tabs.3.8.41. If you forget to close the markup tag for italics on a piece of text everything after the first quote will be italics.2.The purposes of the escape sequence in HTML is to let browser to display these characters rather than interpret them.8.51. The three types of HTML are: Unordered(bulleted)list-Numbered(ordered)list-Definition(association)lists-display terms and their associated definition. 8.6
Ch7 7.2~7.5
EX 7-2
1.)The structure of a query-controlled loop that processes repeated sets of inputs is the String variable doInAgain. This variable controls how many times the loop repeats, initially, the variable equals “y”. As soon as the user enters a string other than “y” or “Y”, the program terminates.2.)
EX 7-3
1.)The role the menu plays is to display a list of options, after this, the user can select the one they needs.2.)The menu-driven programs prompts fro additional inputs related to that option and performs the needed computations, after which it displays the menu again.
Ex 7-4 1
a.)%11s b.)%12s c.) %-15 d.)System.out.intf(“%60″, e.)System.out.printf(%10.2f)
2 a.)10,000.50, b.)45 632, c.)34.54
Ex7-5
1.)
2.)The programmer embeds the call to an input method in a try-catch statement, as its name implies, this statement consists of two parts. The statement within the try clause are executed until one of them throws an exception. If that happens, an exception object is created and sent immediately to the catch clause. The code within the catch clause is then executed. Alternatively, if no statement throws an exception within the try clause, the catch clause is skipped.
Chapter 6.1~6.5
6.1 1. (true true false true) (false true false true) (true false false true)2. Fill in the truth values in the following truth table:a.true b. false c.trued.false4. !> &&> //5. If ( min <= x && x<=max)System.out.println( “X is between the millennium and the maximum)6.2 1. a.If number greater than 0 that we do the work on action 1; if number smaller than 0 then we do the work on action 2. b. If number between 0 and 100, then we do the work on action 1; if number smaller than 0 and greater than 100, then we do the work on action 1. 2. That every line in a program is executed at least once. And this is not the same thing as testing all possible logical paths through a program, which would provide a more thorough test, but also might require considerably more test data. 3. They are equivalent from the perspective of testing the same paths through the program. For example if the employee type is 1, test data for he payroll program fall into just two equivalence classes: hours between 0 and 40 and hours greater than 4. It is near the boundaries between equivalence classes. And it si common for programs to fail at these points. For example, for the payroll program, this requirement means testing with hours equal to 39, 40 and 41. 5. It is with data at the limits of validity. For example, we choose hours worked equal to 0 and 168 hours. 6.6.31. A:the time is before noon B: the day is monday So A true and B true, than they will take the computer science quiz. And A true B false, than they will go to gym class. A false B true, than they will go to gym class. A false B false, than they will take the computer science quiz.2. What is the difference between a nested if statement and a multiway if statement ?Nested if statement provide one mechanism for dealing the complexity, and it offer an alternative. But Multiway statement is compared to the two-way and one-way if statements we have seen earlier.6 .41. Income need to change, which like: income > 20000 change to income > 20000 && income 50000.2. if (income > 10000)rate = 0.10;else if (income > 20000 && income 50000)rate = 0.40;elserate = 0.06.51.a)output will be 2, 3, run for three times which 2,3 2, 3 2, 3 b.)out put will be 1,2,3 1, , 31, 2, 3 run for three times6.61.a) To test the number, we must test number that is bigger than 0, for example 1, 2,3. But it the number is smaller than 0, the test is wrong.b). Test the number that is bigger than 0 and smaller than 100, example, 1, 2, 3, 99, 98, 97. The number that is bigger than 100 or smaller than 0 will no be test number.2.3. When a loop that does not executes a fixed number of times, tis test data should be cover all tree possibilities. 4. A program that tolerates errors in user inputs and recovers gracefully is robust program.
5.4 The Structure and Behavior of Methods
1. Explain the difference between formal parameters and actual parameters.
The formal parameters are parameters listed in a method’s definition, and the actual parameters are values passed to a method wen it is invoked.
2. How does Java transmit data by means of parameters?
When a method is called, the value of the actual parameter is automatically transfered to the corresponding formal parameter immediately before the method is activated.
3. Define a method sum. This method expects two integers as parameters and returns the sum of the numbers ranging from the first integer to the second one.
public the Sum(){
int sum;
sum = (int) Math.round (a + b);
debug(”Sum; “average);
retirn sum;
4. What is the purpose of local variables?
It is convenient to have temporary working storage for data in a method.