201271050130- Teng Jiangnan - "object-oriented programming (java)" twelfth week learning summary
Items |
Contents |
This work belongs courses |
https://www.cnblogs.com/nwnu-daizh |
Where this requirement in the job |
https://www.cnblogs.com/nwnu-daizh/p/11867214.html |
Job learning objectives |
(1) master Vetor, Stack, Hashtable three classes of common use and the API; (2) master ArrayList, LinkList two classes of common use and the API; (3) a set of java understood framework composition; (4) to grasp the Java GUI framework to create and property of the commonly used class of API; (5) understand the Java GUI 2D graphics rendering commonly used class of API; |
The first part: Summary Chapter IX, Chapter X of theoretical knowledge
Chapter IX collection
1, Java collection framework
Java framework to achieve encapsulation of the set of various data structures;
The so-called framework is a class library, the framework includes some common interface and superclass, programmers implement these interfaces or create these super subclass can easily design programs required classes;
Collection (or called a container) are a class of elements and a method comprising a plurality of elements included in the operation, it contains elements may be formed from the same type of object composition, may also consist of different types of objects;
Collections Framework: a unified set of class libraries Java architecture.
2, the role of collections
(1) Java collection class provides support for basic data types;
(2) For example: Vector, Hashtable, Stack and the like;
3, using the collections
In the Java collection class contains java.util package;
import java.util. *;
4, the characteristics of collections
Only hold objects:
Note: Arrays can receive basic data types and data objects;
If the collection classes want to use the basic data types, using the flexibility of the mailbox set of classes, the data can be encapsulated into the basic data types of the data object type of package, and then processed into a collection;
5, the old and new collections
Providing Vector (vector) in the JDK1.1 and Java1.0, the Hashtable (hash table), Stack (Stack), the Properties (attribute sets) and other collection class, although in these classes, but independent of each other, the lack of a unified focus mechanism.
6, Vector class
Vector class similar to the array of variable length;
Vector objects can only be stored;
Vector elements accessed by subscripts;
Vector class key attributes:
--capacity represents the number of elements set up to accommodate;
--capacityIncrement indicate how much capacity each increase;
--size represents a collection of the current element;
7, key methods of the Vector class
——void addElement(Object obj)
——void add(int index, Object element)
——Object elementAt(int index)
——void insertElementAt(Object obj, int index)
8, Stack categories:
Stack Vectoe class is a subclass of the class;
Stack class described stack data structure, i.e. FILO;
The key method Stack class:
--public void push (Object item) // Pushes an item onto the stack
--public Object pop () // remove the top element and the object as the value of this function returns the object
--public Object peek () // Check the top element without removing it
Are --public Boolean empty () // test stack is empty
9, Hashtable class
Hashtable class to find elements by key;
Hashtable hash class with the code (hashCode) to determine a value. All objects have a hash code, may be obtained by hashCode () method of class Object;
10, the basic frame set:
Chapter 10 Graphics Programming
About 10.1 AWT and Swing
Users with a computer system (various programs) interaction interfaces: a user interface concepts 1. (User Interface) of
concepts in a graphical user interface (Graphical User Interface): A graphical representation of the user interface
3.AWT:
a. Java abstract window toolkit (AbstractWindow Toolkit, AWT) contained in the java.awt package, which provides a number of component classes used to design containers and the GUI.
Methods b AWT library to handle user interface elements: the creation and behavior of graphical elements commission to deal with local GUI toolkit.
c. Applications written in AWT rely on the local user interface GUI elements will be exposed some flaws. For example, menus, scroll bars and text fields these user interface elements on different platforms, there are some subtle differences in operating behavior.
4. Swing
a. Swing based user interface library non peer GUI toolkit.
b.Swing has a richer and more convenient user interface elements in the set.
c.Swing dependence on the underlying platform so few small bug related to the platform.
d.Swing will bring a unified visual experience on the cross-platform.
e.Swing javax.swing library is placed in the bag.
Relations f. AWT and Swing of
(1) Most Swing AWT component has its equivalent assembly.
Name (2) Swing components are generally added in front of the name of a component AWT letter "J", such as: JButton, JFrame, JPanel like.
Create a framework 10.2
10.2.1 Components
Element constituting a graphical user interface, that is used to use.
Graphically (can be displayed on the screen, the user can interact with) -Button, Checkbox, Scrollbar, Choice, Frame
10.2.2 container
Java is a component in the container and arranged to accommodate assembly.
The containers are commonly used frame (Frame, JFrame) Example: Frame fra = new Frame ( "This is a window");
Common API java.awt.Frame class (textbook page 414) void setResizable (booleanb) scaling frame void setTitle (String s) provided in a frame header void setIconImage (Image image) will be used as the frame icon Image
10.2.3 Adding Components
Container class provides a way to add (), is used to add additional components in the container object class components.
The container itself is also a component that can be added to one container to another container, container nesting achieved.
10.2.4 Create a framework (Frame) of
(1) creates an empty frame: in Java, often using frame (Frame) to create the initial interface, i.e., the top-level window GUI
AWT library based on a Frame-based peer. Swing version of this class JFrame, JFrame is a Frame subclass.
(2) positioning the frame and the frame attribute
Location: setLocation setBounds and methods commonly used class attributes Component
Title: title frame
IconImage: Framework icon
(3) determining the size of the frame: screen size information obtained by invoking a method of the Toolkit class.
Toolkit kit = Toolkit.getDefaultToolkit (); // generate Toolkit objects
Dimension screenSize = kit.getScreenSize (); // returns the screen size Dimension object
intscreenWidth = screenSize.width; // get the screen width Dimension object
intscreenHeight = screenSize.height ; // get the screen height Dimension object
10.3 Graphics Programming
Graphics Programming This part will be reflected in the experiment.
Comprising the following features:
a 2D graphics processing
using color 2
2 font using
1 0.4 display image
(1) in the Java application, once the image stored in a location on the Internet or locally, they can be directly read into the java application.
String filename = “…”; Image image= ImageIcon(filename).getImage();
(2) the completion of loading the program into an image file, then call DrawImage Graphics class provides () displays it.
public void paintComponent (Graphics g)
{ … g.drawImage(image, x, y, null); }
Part II: Experimental part
Test Procedure 1:
Import java.util.Vector;
private int catNumber;
catNumber = i;
}
System.out.println("Cat #" + catNumber);
}
}
private int dogNumber;
dogNumber = i;
}
System.out.println("Dog #" + dogNumber);
}
}
public static void main(String[] args) {
Vector cats = new Vector();
for (int i = 0; i < 7; i++)
cats.addElement(new Cat(i));
cats.addElement(new Dog(7));
for (int i = 0; i < cats.size(); i++)
((Cat) cats.elementAt(i)).print();
}
}
//示例程序2 import java.util.*; public class Stacks { static String[] months = { "1", "2", "3", "4" }; public static void main(String[] args) { Stack stk = new Stack(); for (int i = 0; i < months.length; i++) stk.push(months[i]); System.out.println(stk); System.out.println("element 2=" + stk.elementAt(2)); while (!stk.empty()) System.out.println(stk.pop()); } }
//示例程序3 import java.util.*; class Counter { int i = 1; public String toString() { return Integer.toString(i); } } public class Statistics { public static void main(String[] args) { Hashtable ht = new Hashtable(); for (int i = 0; i < 10000; i++) { Integer r = new Integer((int) (Math.random() * 20)); if (ht.containsKey(r)) ((Counter) ht.get(r)).i++; else ht.put(r, new Counter()); } System.out.println(ht); } }
Test Procedure 2:
import java.util.*; public class ArrayListDemo { public static void main(String[] argv) { ArrayList al = new ArrayList(); // Add lots of elements to the ArrayList... al.add(new Integer(11)); al.add(new Integer(12)); al.add(new Integer(13)); al.add(new String("hello")); // First print them out using a for loop. System.out.println("Retrieving by index:"); for (int i = 0; i < al.size(); i++) { System.out.println("Element " + i + " = " + al.get(i)); } } }
import java.util.*; public class LinkedListDemo { public static void main(String[] argv) { LinkedList l = new LinkedList(); l.add(new Object()); l.add("Hello"); l.add("zhangsan"); ListIterator li = l.listIterator(0); while (li.hasNext()) System.out.println(li.next()); if (l.indexOf("Hello") < 0) System.err.println("Lookup does not work"); else System.err.println("Lookup works"); } }
Test Procedure 3:
package linkedList; import java.util.*; /** * This program demonstrates operations on linked lists. * @version 1.11 2012-01-26 * @author Cay Horstmann */ public class LinkedListTest { public static void main(String[] args) { List<String> a = new LinkedList<>(); a.add("Amy"); a.add("Carl"); a.add("Erica"); List<String> b = new LinkedList<>(); b.add("Bob"); b.add("Doug"); b.add("Frances"); b.add("Gloria"); // 将单词从B合并为A ListIterator<String> aIter = a.listIterator(); Iterator<String> bIter = b.iterator(); while (bIter.hasNext()) { if (aIter.hasNext()) aIter.next(); aIter.add(bIter.next()); } . The System OUT .println (A); // delete each second word B from the Biter = b.iterator (); the while (bIter.hasNext ()) { bIter.next (); // skip element IF (bIter.hasNext ()) { bIter.next (); // skip next element bIter.remove (); // delete the element } } . the System OUT .println (B); // batch: from a delete all the words in the B a.removeAll (B); the System. OUT .println (a); } }
Test Procedure 4 :
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); } }
Test Procedure 5:
package sizedFrame; import java.awt.*; import javax.swing.*; /** * @version 1.34 2015-06-16 * @author Cay Horstmann */ public class SizedFrameTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new SizedFrame(); frame.setTitle("SizedFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible ( to true); }); } } Class SizedFrame the extends the JFrame { public SizedFrame () { // Get the screen size Toolkit Kit = Toolkit.getDefaultToolkit (); the Dimension screenSize = kit.getScreenSize (); int screenHeight = screenSize.height; int screenWidth = screenSize.width; / / set the frame width, height and let the platform of choice for screen location the setSize (screenWidth / 2 , screenHeight / 2 ); setLocationByPlatform ( to true ); Image img// set the frame icon = new new the ImageIcon ( " icon.gif " ) .getImage (); the setIconImage (IMG); } }
Test Procedure 6:
package notHelloWorld; import javax.swing.*; import java.awt.*; /** * @version 1.33 2015-05-12 * @author Cay Horstmann */ public class NotHelloWorld { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new NotHelloWorldFrame(); frame.setTitle("NotHelloWorld"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } /** * A frame that contains a message panel */ class NotHelloWorldFrame extends JFrame { public NotHelloWorldFrame() { add(new NotHelloWorldComponent()); pack(); } } /** * A component that displays a message. */ class NotHelloWorldComponent extends JComponent { public static final int MESSAGE_X = 75; public static final int MESSAGE_Y = 100; private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public void paintComponent(Graphics g) { g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
Experimental summary :
In the process of learning this week, last week reviewed the content that is generic programming knowledge, learning new knowledge about the collections, in general, a little bit to keep up with everyone's progress. Before using the Eclipse software are not familiar with a lot of procedural errors from initial creation inaccurate. Follow the steps in the study group file operations, basically mastered the use of the method. This week's test program can basically complete autonomy, but for understanding the program is not enough, can only knock scripted code, I hope the follow-up could spend more time knocking on the code, write code to enhance and speed of thought!