201871010118- Tang Jingbo "object-oriented programming (java)" twelfth week learning summary

Bowen beginning of the text format: (2 points)

project

content

This work belongs courses

https://www.cnblogs.com/nwnu-daizh/

Where this requirement in the job

https://www.cnblogs.com/lily-2018/p/11441372.html

Job learning objectives

(1) master Vetor, Stack, Hashtable three classes of common use and the API;

(2) master ArrayList, and the use of two classes LinkList common API.

(3) master the Java GUI framework to create and property of the commonly used class of API;

(4) apply pair programming (Pair programming), two people experience in program development cooperation.

Essay Bowen body content include:

The first part: Summary Chapter IX, Chapter X of theoretical knowledge (40 points)

 

1. Data Structure: into a linear data structure, such as linear form, stacks, queues, strings, arrays, and files.

                          B. non-linear data structures like trees and FIG.

1) all data elements in a linear same table must be the same data type.

  Linear list structure can be divided according to their order of storage tables and lists.

2) Stack: it is a special linear table, a last in first out (LIFO) configuration.

  Stack limiting linear table insertion and deletion operations only in the trailer, the trailer is called stack, called the header bottom of the stack.

3) Queue: all inserts defined only in one end of the table, the table linearly deleted are all carried out at the other end of the table. It is a first in first out (FIFO) structure.

  One end of the table referred to permit insertion of the tail end, allowing delete called queue head.

2. :( collection container) comprising a plurality of elements and to provide a method of operating earth element contained, it contains elements may be formed from the same type of object composition, it may also consist of different types of objects.

  1) Collections Framework: JAVA architecture unified set of class libraries.

  2) a set of action classes (included in the package java.util): basic data provide some support structure, such as Vector, Hashtable, Stack like.

  3) Characteristics of the collections: a receiving objects only;.

                                      b. collection class object is an instance of receiving the Object class (once the objects into a collection class, which class information is lost)

  4) Vector type: variable-length array similar. It can only store objects whose elements are accessed through the index.

  5) Stack class (Vector subclass): It describes the stack data structure. (All objects have a hash code, can be obtained by the Object class hashCode method.)

3. A set of basic interface framework: (basic cluster configuration framework) a.Collection: root interface in the collection hierarchy, JDK not directly provide the interface implementation class.

                                           b.Set: can not contain duplicate elements, that element must be unique. Objects may not be stored in the order of storage. (Set interface to achieve class has HashSet, TreeSet)

                                           c.List: ordered set can contain duplicate elements. It provides a way to access by index. Class that implements it has ArrayList and LinkedLis (eg ArrayList: arrays can automatically increase capacity)

                                           d.Map:Map interface maps to a unique key value. It contains key-value pairs. Map can not contain duplicate key. SortedMap is a key in ascending order Map.

3. The user interface: a user computer system (various programs) interaction interface

  Graphical User Interface: graphically presented user interface

4.AWT: Java abstract window toolkit (Abstract WindowToolkit, AWT) contained in the java.awt package, which provides a number of component classes used to design containers and the GUI.

  Methods AWT library handles the user interface elements: the creation and behavior of graphical elements commission to deal with local GUI toolkit.

5.Swing: Swing user interface libraries are non-peer-based GUI toolkit.

  Swing has a richer and more convenient user interface elements of the collection.

  Swing dependence on the underlying platform so few small bug related to the platform.

  Swing javax.swing library is placed in the bag.

Most AWT components has its Swing equivalent components. The name of Swing components are generally added in front of AWT components name a letter "J".

Create a frame

1. Component: Component graphical user interface, that is used to graphically represented by (can be displayed on the screen, and users can interact)

2. The object is typically created by a subclass of the Component class is called a subclass or indirectly assembly.

The container: the container is a component of the Java and arranged to accommodate assembly. The containers are commonly used frame (Frame, JFrame)

Location:
  - the setLocation setBounds and commonly used method of the Component class
common properties
  -Title: a frame header
  -IconImage: frame icon

  Determining the size of the frame: screen size information obtained by invoking a method of the Toolkit class.

4. The information displayed in the assembly

Can be called in the AWT add () method added directly to the assembly AWTFrame, then added to the contents of pane in Swing components.

Wherein the content pane is used to add components, add the following code:

  Container contentPane = getContentPane();

Users can also create their own a component class, and on the assembly drawing, at this time need to reload paintComponent ().

User self-built components can also be added to the content pane.

public void setEnabled (boolean b): Set whether the component may be activated.
  When the value of parameter b is true, the component may be activated.
  When the value of parameter b false, the components can not be activated.
  By default, the assembly can be activated.
public void setVisible (boolean b): Visibility assembly disposed in the container.
  When the b value of true, the components visible in the container.
  When the b value of false, components are not visible in the container.
  Window assembly type addition, other types of components are visible by default.

Part II: Experimental part

Experiment 1: Test Procedure 1 (6 points)

Chapter 9 introduced sample programs, test procedures and code comments.

※ command to run using JDK edit, run the following three sample program, in conjunction with the results of running the program understanding;

※ master Vetor, Stack, Hashtable uses three classes and common API.

vector :

Experimental procedures are as follows:

 1 import java.util.Vector; // automatic growth of an array of objects
 2 
 3 class Cat {
 4      private int catNumber;
 5      Cat(int i) {
 6         catNumber = i;
 7     }
 8      void print() {
 9         System.out.println("Cat #" + catNumber);
10      }
11 }
12 public class Cats{
13 public static void main(String[] args){
14        Vector<Cat> cats= new Vector<Cat>();
15        for(int i=0; i<7; i++)
16            cats.addElement(new Cat(i)); 
17        for(int i=0; i<cats.size(); i++)
18 (cats.elementAt (i)) print ();. // mandatory type conversion
19    }
20} 
results are as follows:

 

 

Stack:

Experimental procedures are as follows:

 1import java.util. *;
 2 public class Stacks // stack (last out)
 3 {
 4 static String [] months = { "gold", "silver", "copper", "iron"};
 5    public static void main(String[] args){
 6       Stack<String> stk = new Stack<String> ();
 7       for(int i=0; i<months.length; i++)
 8 stk.push (months [i]); // push
 9       System.out.println(stk);
10       System.out.println("element 2=" + stk.elementAt(2));
11       while(!stk.empty())
12 System.out.println (stk.pop ()); // the output element out of the stack
13   }
14} 
The results are as follows:

 

 

 Hashtable:

Experimental procedures are as follows:

import java.util. *;
 2 class Counter {
 3 int i = 1; // without permission modifier: friendly type
 4 public String toString () {// the other types of data into a data string type
 5         return Integer.toString(i);
 6     }
 7 }
 8 
 9 public class Statistics {
10     public static void main(String[] args) {
11         Hashtable ht = new Hashtable();
12         for (int i = 0; i < 10000; i++) {
13 Integer r = new Integer ((int) (Math.random () * 20)); // generates 0-20 (excluding 20) the random number integer
14 if (ht.containsKey (r)) // r is determined whether a hash table key element
15 ((Counter) ht.get (r)) i ++;. // get their values ​​by the method of
16           else
17 ht.put (r, new Counter ()); // ht absent
18 }
19         System.out.println(ht);
20     }
21} 
The results are as follows:

 

 

Experiment 1: Test Procedure 2 (6 points)

※ use JDK commands to edit and run ArrayListDemo LinkedListDemo two programs combined result of the program understand the program;

ArrayListDemo:

Experimental procedures are as follows:

 1 import java.util. *;
 2 
 3 public class ArrayListDemo // ArrayList used by an array of
 4 {
 5     public static void main(String[] argv) {
 6         ArrayList al = new ArrayList();
 7         // Add lots of elements to the ArrayList...
 8         al.add(new Integer(11));
 9         al.add(new Integer(12));
10         al.add(new Integer(13));
11 al.add (new String ( "hello")); // index starts from 0, adding four elements
12         // First print them out using a for loop.
13         System.out.println("Retrieving by index:");
14         for (int i = 0; i < al.size(); i++) {
15             System.out.println("Element " + i + " = " + al.get(i));
16         }
17      }
18} 
The results are as follows:

 

 

 LinkedListDemo:

Experimental procedures are as follows:

 1 import java.util. *;
 2 
 3 public class LinkedListDemo
 4 {
 5     public static void main(String[] argv)
 6     {
 7 // Create a list
 8         LinkedList l = new LinkedList();
 9         l.add(new Object());
10         l.add("Hello");
11         l.add("zhangsan");
12 ListIterator li = l.listIterator (0);
13         while (li.hasNext())
14             System.out.println(li.next());
15         if (l.indexOf("Hello") < 0)   
16             System.err.println("Lookup does not work");
17         else
18             System.err.println("Lookup works");
19    }
20} 
The results are as follows:

 

 

Experiment 1: Test Procedure 3 (6 min)

※ editing program run debug materials 360 9-1 combined result of the program to understand the program under Elipse environment;

※ master ArrayList, and use LinkList two classes of common API.

Experimental procedures are as follows:

1 import java.util. *;
 2 
 3 /**
 4  * This program demonstrates operations on linked lists.
 5  * @version 1.11 2012-01-26
 6  * @author Cay Horstmann
 7  */
 8 public class LinkedListTest
 9 {
10    public static void main(String[] args)
11    {
12 // Create a and b are two lists
13       List<String> a = new LinkedList<>();//泛型
14       a.add("Amy");
15       a.add("Carl");
16       a.add("Erica");
17 
18       List<String> b = new LinkedList<>();//泛型
19       b.add("Bob");
20       b.add("Doug");
21       b.add("Frances");
22       b.add("Gloria");
23 
24 // merger of a and b word
25 
26 ListIterator <String> aIter = a.listIterator ();
27       Iterator<String> bIter = b.iterator();
28 
29       while (bIter.hasNext())
30       {
31          if (aIter.hasNext()) aIter.next();
32          aIter.add(bIter.next());
33       }
34 
35       System.out.println(a);
36 
37 // delete an element from every other element in the second list
38 
39       bIter = b.iterator();
40       while (bIter.hasNext())
41       {
42          bIter.next(); // skip one element
43          if (bIter.hasNext())
44          {
45             bIter.next(); // skip next element
46             bIter.remove(); // remove that element
47          }
48       }
49 
50       System.out.println(b);
51 
52       // bulk operation: remove all words in b from a
53 
54       a.removeAll(b);
55 
56 System.out.println (a); // print out a list of all the elements by a method AbstractCollection class toString
57    }
58} 
The results are as follows:

 

 

Experiment 2: Test Procedure 1 (6 points)

※ run the following program, observe the result of the program .

import javax.swing.*;

public class SimpleFrameTest

{

   public static void main(String[] args)

   {

     JFrame  frame = new JFrame();

     frame.setBounds(0, 0,300, 200);

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     frame.setVisible(true);    

   }

}

Experimental procedures are as follows:

 1 import javax.swing.*;
 2 public class SimpleFrameTest
 3 {
 4    public static void main(String[] args)
 5    {
 6 JFrame frame = new JFrame (); // Create a class object frame
 7 frame.setBounds (0, 0,300, 200); // define coordinates and the width and height
 8 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // close operation window
 9 frame.setVisible (true); // window is visible    
10    }
11 }

 

 

※ In elipse IDE debugger run textbooks 10-1 407 program, run in conjunction with the results of the program to understand the program; compared with the above procedures, thinking similarities and differences;

※ grasp empty frame creation method;

※ understand the main thread and event-dispatch thread concept;

※ master GUI technology to create top-level window.

Experimental procedures are as follows:

 1 package simpleFrame;
 2 
 3 import java.awt.*;
 4 import javax.swing.*;
 5 
 6 /**
 7  * @version 1.33 2015-05-12
 8  * @author Cay Horstmann
 9  */
10 public class SimpleFrameTest
11 {
12    public static void main(String[] args)
13    {
14 EventQueue.invokeLater (() -> // lambda expression: open a queue by thread
15          {
16 SimpleFrame frame = new SimpleFrame (); // Create a class object
17 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // close operation settings object Click
18 frame.setVisible (true); // page is visible
19          });
20    }
21 }
22 
23 class SimpleFrame extends JFrame
24 {
25    private static final int DEFAULT_WIDTH = 300;
26    private static final int DEFAULT_HEIGHT = 200;
27 
28 public SimpleFrame () // constructor
29    {
30 setSize (DEFAULT_WIDTH, DEFAULT_HEIGHT); // set the size
31    }
32} 
The results are as follows:

 

 

Experiment 2: Test Procedure 2 (6 points)

※ commissioning textbook program 412 in elipse IDE 10-2, the binding appreciated program run results;

The method used to set the properties of the frame ※ grasp OK.

Experimental procedures are as follows:

1 package simpleFrame;
 2 
 3 import java.awt.*;
 4 import javax.swing.*;
 5 
 6 /**
 7  * @version 1.34 2015-06-16
 8  * @author Cay Horstmann
 9  */
10 public class SizedFrameTest
11 {
12    public static void main(String[] args)
13    {
14 EventQueue.invokeLater (() -> // lambda expression: open a queue by thread
15          {
16 JFrame frame = new SizedFrame (); // Create a class object frame
17 frame.setTitle ( "SizedFrame"); // set the title
18 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Close Operation
19 frame.setVisible (true); // set the visibility
20          });
21    }
22 }
23 
24 class SizedFrame extends JFrame//继承
25 {
26 public SizedFrame () // constructor
27    {
28 // get the screen dimensions
29 
30 Toolkit kit = Toolkit.getDefaultToolkit (); // objects generated Toolkit
31       Dimension screenSize = kit.getScreenSize();
32       int screenHeight = screenSize.height;
33       int screenWidth = screenSize.width;
34 
35       // set frame width, height and let platform pick screen location
36 
37 setSize (screenWidth / 2, screenHeight / 2); // size
38       setLocationByPlatform(true);
39 
40       // set frame icon
41 
42       Image img = new ImageIcon("icon.gif").getImage();
43       setIconImage(img);      
44    }
45} 
The results are as follows:

 

 Experiment 2: Test Procedure 3 (6 min)

※ commissioning textbooks 418 program 10-3 in elipse IDE, the combined operating results to understand the program;

※ grasp add components in the frame;

※ master custom component usage.

Experimental procedures are as follows:

1 package simpleFrame;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 /**
 7  * @version 1.33 2015-05-12
 8  * @author Cay Horstmann
 9  */
10 public class NotHelloWorld
11 {
12    public static void main(String[] args)
13    {
14 EventQueue.invokeLater (() -> // lambda expression: open a queue by thread
15          {
16             JFrame frame = new NotHelloWorldFrame();
17             frame.setTitle("NotHelloWorld");//标题
18             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19             frame.setVisible(true);
20          });
21    }
22 }
23 
24 /**
25  * A frame that contains a message panel
26  */
27 class NotHelloWorldFrame extends JFrame//继承
28 {
29 public NotHelloWorldFrame () // constructor
30    {
31 add (new NotHelloWorldComponent ()); // add window
32       pack();
33    }
34 }
35 
36 /**
37  * A component that displays a message.
38  */
39 class NotHelloWorldComponent extends JComponent
40 {
41    public static final int MESSAGE_X = 75;
42    public static final int MESSAGE_Y = 100;
43 
44    private static final int DEFAULT_WIDTH = 300;
45    private static final int DEFAULT_HEIGHT = 200;
46 
47 public void paintComponent (Graphics g) // Drawing
48    {
49       g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y);
50    }
51    
52    public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
53} 
The results are as follows:

 

 Third, the test summary: (19 points)

By learning this week, I learned JAVA language easier to achieve some knowledge of data structures, theory class feel that he can understand, but a hands-on practical classes find themselves still have a lot of knowledge do not know, therefore, to learn in the back in addition to practice the code, I will more than open book to remember some details and concepts. Trying to learn this course.

Guess you like

Origin www.cnblogs.com/2360689227t/p/11885688.html