giáo trình Java By Example phần 7
Số trang: 52
Loại file: pdf
Dung lượng: 127.54 KB
Lượt xem: 22
Lượt tải: 0
Xem trước 6 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu giáo trình java by example phần 7, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 7import java.awt.*;import java.applet.*;public class Applet16 extends Applet{ TextField textField1, textField2, textField3; int avg1, avg2, avg3; public void init() { textField1 = new TextField(5); textField2 = new TextField(5); textField3 = new TextField(5); add(textField1); add(textField2); add(textField3); textField1.setText(0); textField2.setText(0); textField3.setText(0); } public void paint(Graphics g) http://www.ngohaianh.info { g.drawString(Your bowlers averages are: , 50, 80); String s = textField1.getText(); g.drawString(s, 75, 110); avg1 = Integer.parseInt(s); s = textField2.getText(); g.drawString(s, 75, 125); avg2 = Integer.parseInt(s); s = textField3.getText(); g.drawString(s, 75, 140); avg3 = Integer.parseInt(s); } public boolean action(Event event, Object arg) { repaint(); return true; } }When you run Applet16, you can enter bowling scores into the three boxes at the top of the appletsdisplay area. After you enter these averages, theyre displayed on-screen as well as copied into the threevariables avg1, avg2, and avg3.Nothing too tricky going on here, right? http://www.ngohaianh.infoNow examine the listing. Remember in Chapter 10, The while and do-while Loops, when youlearned to keep an eye out for repetitive program code? How about all those calls to getText(),drawString(), and valueOf() in Listing 13.1? The only real difference between them is thespecific bowlers score thats being manipulated. If you could find some way to make a loop out of thiscode, you could shorten the program significantly. How about a for loop that counts from 1 to 3?But how can you use a loop when youre stuck with three different variables? The answer is an array. Anarray is a variable that can hold more than one value. When you first studied variables, you learned that avariable is like a box in memory that holds a single value. Now, if you take a bunch of these boxes andput them together, what do you have? You have an array. For example, to store the bowling averages foryour three bowlers, youd need an array that can hold three values. You could call this array avg. Youcan even create an array for a set of objects like the TextField objects Applet16 uses to get bowlingscores from the user. You could call this array textField.Now you have an array called avg that can hold three bowling averages and an array calledtextField that can hold three TextField objects. But how can you retrieve each individual averageor object from the array? You do this by adding something called a subscript to the arrays name. Asubscript (also called an index) is a number that identifies the element of an array in which a value isstored. For example, to refer to the first average in your avg array, youd write avg[0]. The subscript isthe number in square brackets. In this case, youre referring to the first average in the array (arraysubscripts always start from zero.) To refer to the second average, youd write avg[1]. The third averageis avg[2].If youre a little confused, look at Figure 13.2, which shows how the avg[] array might look inmemory. In this case, the three bowling averages are 145, 192, and 160. The value of avg[0] is 145,the value of avg[1] is 192, and the value of avg[2] is 160.Figure 13.2 : An array can hold many values of the same type.Example: Creating an ArraySuppose that you need an array that can hold 30 floating-point numbers. First, youd declare the arraylike this: float numbers[];Another way to declare the array is to move the square brackets to after the data type, like this: float[] numbers;After declaring the array, you need to create it in memory. Java lets you create arrays only using the newoperator, like this: http://www.ngohaianh.info numbers = new float[30];The last step is to initialize the array, a task that you might perform using a for loop: for (int x=0; xpublic void init(){ textField = new TextField[3]; avg = new int[3]; for (int x=0; x } } public boolean action(Event event, Object arg) { repaint(); return true; } } Tell Java that the program uses classes in the awt package. Tell Java that the program uses classes in the applet package. Derive the Applet17 class from Javas Applet class. Declare TextField and int arrays. Override the Applet classs init() method. Create the textField and int arrays with three elements each. Loop from 0 to 2. Create a new TextField object and store it in the array. Add the new TextField object to the applet. Set the new TextField objects text. Override the Applet classs paint() method. Display a line of text. Loop from 0 to 2. Get the text from the currently indexed TextField object. Draw the retrieve text on the applets display area. Convert the value and store it in the integer array. Override the Applet objects action() method. Force Java to redraw the applets display area. Tell Java everything went okay.At the beginning of Listing 13.2, youll see a couple of strange new variable declarations ...
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 7import java.awt.*;import java.applet.*;public class Applet16 extends Applet{ TextField textField1, textField2, textField3; int avg1, avg2, avg3; public void init() { textField1 = new TextField(5); textField2 = new TextField(5); textField3 = new TextField(5); add(textField1); add(textField2); add(textField3); textField1.setText(0); textField2.setText(0); textField3.setText(0); } public void paint(Graphics g) http://www.ngohaianh.info { g.drawString(Your bowlers averages are: , 50, 80); String s = textField1.getText(); g.drawString(s, 75, 110); avg1 = Integer.parseInt(s); s = textField2.getText(); g.drawString(s, 75, 125); avg2 = Integer.parseInt(s); s = textField3.getText(); g.drawString(s, 75, 140); avg3 = Integer.parseInt(s); } public boolean action(Event event, Object arg) { repaint(); return true; } }When you run Applet16, you can enter bowling scores into the three boxes at the top of the appletsdisplay area. After you enter these averages, theyre displayed on-screen as well as copied into the threevariables avg1, avg2, and avg3.Nothing too tricky going on here, right? http://www.ngohaianh.infoNow examine the listing. Remember in Chapter 10, The while and do-while Loops, when youlearned to keep an eye out for repetitive program code? How about all those calls to getText(),drawString(), and valueOf() in Listing 13.1? The only real difference between them is thespecific bowlers score thats being manipulated. If you could find some way to make a loop out of thiscode, you could shorten the program significantly. How about a for loop that counts from 1 to 3?But how can you use a loop when youre stuck with three different variables? The answer is an array. Anarray is a variable that can hold more than one value. When you first studied variables, you learned that avariable is like a box in memory that holds a single value. Now, if you take a bunch of these boxes andput them together, what do you have? You have an array. For example, to store the bowling averages foryour three bowlers, youd need an array that can hold three values. You could call this array avg. Youcan even create an array for a set of objects like the TextField objects Applet16 uses to get bowlingscores from the user. You could call this array textField.Now you have an array called avg that can hold three bowling averages and an array calledtextField that can hold three TextField objects. But how can you retrieve each individual averageor object from the array? You do this by adding something called a subscript to the arrays name. Asubscript (also called an index) is a number that identifies the element of an array in which a value isstored. For example, to refer to the first average in your avg array, youd write avg[0]. The subscript isthe number in square brackets. In this case, youre referring to the first average in the array (arraysubscripts always start from zero.) To refer to the second average, youd write avg[1]. The third averageis avg[2].If youre a little confused, look at Figure 13.2, which shows how the avg[] array might look inmemory. In this case, the three bowling averages are 145, 192, and 160. The value of avg[0] is 145,the value of avg[1] is 192, and the value of avg[2] is 160.Figure 13.2 : An array can hold many values of the same type.Example: Creating an ArraySuppose that you need an array that can hold 30 floating-point numbers. First, youd declare the arraylike this: float numbers[];Another way to declare the array is to move the square brackets to after the data type, like this: float[] numbers;After declaring the array, you need to create it in memory. Java lets you create arrays only using the newoperator, like this: http://www.ngohaianh.info numbers = new float[30];The last step is to initialize the array, a task that you might perform using a for loop: for (int x=0; xpublic void init(){ textField = new TextField[3]; avg = new int[3]; for (int x=0; x } } public boolean action(Event event, Object arg) { repaint(); return true; } } Tell Java that the program uses classes in the awt package. Tell Java that the program uses classes in the applet package. Derive the Applet17 class from Javas Applet class. Declare TextField and int arrays. Override the Applet classs init() method. Create the textField and int arrays with three elements each. Loop from 0 to 2. Create a new TextField object and store it in the array. Add the new TextField object to the applet. Set the new TextField objects text. Override the Applet classs paint() method. Display a line of text. Loop from 0 to 2. Get the text from the currently indexed TextField object. Draw the retrieve text on the applets display area. Convert the value and store it in the integer array. Override the Applet objects action() method. Force Java to redraw the applets display area. Tell Java everything went okay.At the beginning of Listing 13.2, youll see a couple of strange new variable declarations ...
Tìm kiếm theo từ khóa liên quan:
lập trình Java tin học ứng dụng lập trình windows lập trình C# mẹo hay cho tin học thủ thuật windowsGợi ý tài liệu liên quan:
-
Tài liệu bồi dưỡng giáo viên sử dụng SGK Tin học 10 Cánh diều (Định hướng Tin học ứng dụng)
61 trang 244 0 0 -
101 trang 200 1 0
-
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 185 0 0 -
20 trang 184 0 0
-
Cách gỡ bỏ hoàn toàn các add on trên Firefox
7 trang 184 0 0 -
Giáo trình Mạng máy tính (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
189 trang 164 0 0 -
bảo mật mạng các phương thức giả mạo địa chỉ IP fake IP
13 trang 159 0 0 -
Giáo trình Tin học ứng dụng: Phần 1 - Trường ĐH Tài nguyên và Môi trường Hà Nội
125 trang 151 0 0 -
Bài giảng Tin học ứng dụng: Kiểm định trung bình - Trường ĐH Y dược Huế
25 trang 149 0 0 -
175 trang 125 0 0