From scratch to learn Java-10. Make full use of existing objects, java-10 make full use

Source: Internet
Author: User

From scratch to learn Java-10. Make full use of existing objects, java-10 make full use

1. Design of superclass and subclasses;
2. Establish an inheritance level;
3. Override method.

Program StringLister: displays a series of strings alphabetically to the screen using array lists and special for loops. These strings come from an array and command line parameters.

 1 package com.jsample; 2  3 import java.util.*; 4  5 public class StringLister { 6     String[] names = { "Spanky", "Buckwheat", "Daria", 7         "Stymie", "Marianne", "Scotty", "Tommy", "Chubby" }; 8  9     public StringLister(String[] moreNames){10         Vector<String> list = new Vector<String>();11         for (int i = 0; i < moreNames.length; i++){12             list.add(moreNames[i]);13         }14         Collections.sort(list);15         for (String name : list){16             System.out.println(name);17         }18     }19 20     public static void main(String[] args){21         StringLister lister = new StringLister(args);22     }23 }
View Code

 

Output:

Buckwheat
Chubby
Daria
Marianne
Scotty
Spanky
Stymie
Tommy

 

Program PointTester: Programs that use Point, Point3D, and Point4D objects and move them on the screen.

 1 package com.jsample; 2  3 import java.awt.*; 4  5 public class PointTester { 6     public static void main(String[] args){ 7         Point objcet1 = new Point(11,22); 8         Point3D object2 = new Point3D(7,6,64); 9         Point4D object3 = new Point4D(12,56,73,90);10 11         System.out.println("The 2D point is located at (" + objcet1.x12                 + ", " + objcet1.y + ")");13         System.out.println("\tIt's being moved to (4,13)");14         objcet1.move(4,13);15         System.out.println("The 2D point is now at (" + objcet1.x16             + ", " + objcet1.y + ")");17         System.out.println("\tIt's being moved -10 units on the x "18             + "and y axes");19         objcet1.translate(-10,-10);20         System.out.println("The 2D point ends up at (" + objcet1.x21             + ", " + objcet1.y + ")\n");22 23         System.out.println("The 3D point is located at (" + object2.x24             + ", " + object2.y + ", " + object2.z + ")");25         System.out.println("\tIt's being moved to (10, 22, 71)");26         object2.move(10,22,71);27         System.out.println("The 3D point is now at (" + object2.x28             + ", " + object2.y + ", " + object2.z +")");29         System.out.println("\tIt's being moved -20 units on the x,y "30             + "and z axes");31         object2.move(-20,-20,-20);32         System.out.println("The 3D point ends up at (" + object2.x33             + ", " + object2.y + ", " + object2.z + ")\n");34 35         System.out.println("The 4D point is located at (" + object3.x36                 + ", " + object3.y + ", " + object3.z + ", " + object3.t + ")");37         System.out.println("\tIt's being moved to (9, 1, 7, 4)");38         object3.move(9,1,7,4);39         System.out.println("The 4D point is now at (" + object3.x40                 + ", " + object3.y + ", " + object3.z + ", " + object3.t + ")");41         System.out.println("\tIt's being moved 20 units on the x,y "42                 + "and z axes");43         object3.move(20,20,20,20);44         System.out.println("The 4D point ends up at (" + object3.x45                 + ", " + object3.y + ", " + object3.z + ", " + object3.t + ")");46     }47 }
View Code

 

Subordinate class:

Point -- java. awt.

Point3D: records the object's 3D coordinates, moves the object to the new coordinate, and moves the three coordinates to a specific distance.

 1 package com.jsample; 2  3 import java.awt.*; 4  5 public class Point3D extends Point{ 6     public int z; 7  8     public Point3D(int x, int y, int z){ 9         super(x,y);10         this.z=z;11     }12 13     public void move(int x, int y, int z){14         this.z += z;15         super.translate(x,y);16     }17 }
View Code

 

Point4D: records the four-dimensional coordinates of the object, moves the object to the new coordinate, and moves the four coordinates to a specific distance.

 1 package com.jsample; 2  3 public class Point4D extends Point3D{ 4     public int t; 5  6     public Point4D(int x, int y, int z, int t){ 7         super(x,y,z); 8         if (t < 0) 9             return;10         this.t = t;11     }12 13     public void move(int x, int y, int z, int t){14         this.z += z;15         this.t += t;16         super.translate(x,y);17     }18 }
View Code

Output:

The 2D point is located at (11, 22)
It's being moved to (4, 13)
The 2D point is now at (4, 13)
It's being moved-10 units on the x and y axes
The 2D point ends up at (-6, 3)

The 3D point is located at (7, 6, 64)
It's being moved to (10, 22, 71)
The 3D point is now at (17, 28,135)
It's being moved-20 units on the x, y and z axes
The 3D point ends up at (-3, 8,115)

The 4D point is located at (12, 56, 73, 90)
It's being moved to (9, 1, 7, 4)
The 4D point is now at (21, 57, 80, 94)
It's being moved 20 units on the x, y and z axes
The 4D point ends up at (41, 77,100,114)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.