Linked list (Java implementation) Link.java
public class link{private Node first; Public Link () {this.first = null; }//Determine if NULL public boolean isEmpty () {if (This.first = = null) return true; return false; }//Insert head node public void Insertheadnode (int data) {node TMP = new Node (data); if (This.first = = null) first = TMP; else {Tmp.setnext (First.getnext ()); First.setnext (TMP); } return; }//Insert Data node under index public void insertnode (int data, int index) {node p = this.first; int cnt = 0; while (cnt! = index) {p = P.getnext (); cnt++; } node NewNode = new node (data); Newnode.setnext (P.getnext ()); P.setnext (NewNode); return; }//delete the Head node public node Deleteheadnode () {node tmp = first; This.first = Tmp.getnext (); return TMP; }//find the data in the link Public Node findNode (int data) {node p = this.first; while (P! = null) {if (p.getdata () = = data) return p; if (p = = null) break; p = P.getnext (); } return null; }//display the link public void DisplayLink () {Node p = first; while (P! = null) {System.out.println (P.getdata ()); p = P.getnext (); } return; } }
Node.java
public class Node{ private int data; private Node next; //construction public Node(){ this.data = 0; this.next = null; } public Node(int data){ this.data = data; this.next = null; } //get public Node getNext(){ return this.next; } public int getData(){ return data; } //set public void setNext(Node next){ this.next = next; } public void setData(int data){ this.data = data; } //show public void nodeDisplay(){ System.out.println("{"+data+"}"); } }
Testofnode.java
public class testofnode{public static void Main (string[] args) {//test of node node TMP1 = new Node (100) ; Tmp1.nodedisplay (); Link TMP2 = new link (); Test of Insertheadnode & DisplayLink Tmp2.insertheadnode (00); Tmp2.displaylink (); Test of IsEmpty System.out.println (Tmp2.isempty ()); System.out.println ("+++++\n\n"); Test of Insertnode Tmp2.insertnode (11,0); Tmp2.insertnode (22,1); Tmp2.insertnode (33,2); Tmp2.insertnode (44,3); Tmp2.insertnode (55,4); Tmp2.insertnode (66,5); Tmp2.displaylink (); Test of Delete the head Node System.out.println ("++++"); Tmp2.deleteheadnode (); Tmp2.displaylink (); Test of Find the data if (Tmp2.findnode (3310)!=null) {System.out.println ("truely find the data:" + 3310 ); }else{System.out.println ("Fasle"); } System.out.println("++++++"); Test of Insertheadnode Tmp2.insertheadnode (1111); Tmp2.insertheadnode (2222); Tmp2.insertheadnode (3333); Tmp2.displaylink (); }}
Sort-Country gold medal silver medal separately sorted Testofnode.java
public class testofnode{public static void Main (string[] args) {//test of node node TMP1 = new Node (100) ; Tmp1.nodedisplay (); Link TMP2 = new link (); Test of Insertheadnode & DisplayLink Tmp2.insertheadnode (00); Tmp2.displaylink (); Test of IsEmpty System.out.println (Tmp2.isempty ()); System.out.println ("+++++\n\n"); Test of Insertnode Tmp2.insertnode (11,0); Tmp2.insertnode (22,1); Tmp2.insertnode (33,2); Tmp2.insertnode (44,3); Tmp2.insertnode (55,4); Tmp2.insertnode (66,5); Tmp2.displaylink (); Test of Delete the head Node System.out.println ("++++"); Tmp2.deleteheadnode (); Tmp2.displaylink (); Test of Find the data if (Tmp2.findnode (3310)!=null) {System.out.println ("truely find the data:" + 3310 ); }else{System.out.println ("Fasle"); } System.out.println("++++++"); Test of Insertheadnode Tmp2.insertheadnode (1111); Tmp2.insertheadnode (2222); Tmp2.insertheadnode (3333); Tmp2.displaylink (); }}
Test.java
import java.util.Arrays;public class Test{ public static void main(String[] args){ Country American = new Country(46,37,38); Country English = new Country(27,23,17); Country China = new Country(26,18,26); Country Russia = new Country(19,18,19); Country Germany = new Country(17,10,15); Country[] countrys = new Country[5]; countrys[0] = American; countrys[1] = English; countrys[2] = China; countrys[3] = Russia; countrys[4] = Germany; Arrays.sort(countrys); for(Country cty:countrys) System.out.println(cty.gold + " " + cty.silver + " " + cty.copper + " " + cty.sum); }}
Coffee Class-Object-oriented programming Coffee.java
Package coffee;//added method abstract class addimplement{private String name; public void SetName (String name) {this.name = name; } public String GetName () {return this.name; } public abstract void Add (); }class Addsugarimplement extends addimplement{public void Add () {System.out.println ("We have added the Sugar^_ ^"); }}class Addmilkimplement extends addimplement{public void Add () {System.out.println ("You have added the Milk ^_ ^"); }}//the implementation of the abstract classes public abstract class coffee{private addimplement Addimpl; Then, in the call to add, the output public void Add () {addimpl.add (); }; This setting function should be called first public void Setaddimpl (Addimplement addimpl) {This.addimpl = Addimpl; };} Class Instantcoffee extends coffee{public void Setaddimpl (Addimplement addimpl) {System.out.println Instantcoffee ^_^"); Super.setaddimpl (Addimpl); }}class Lattecoffee extends coffee{public void Setaddimpl (Addimplement addimpL) {System.out.println ("This is a lattecoffee ^_^"); Super.setaddimpl (Addimpl); }}class Mochacoffee extends coffee{public void Setaddimpl (Addimplement addimpl) {System.out.println ("This is a Mochacoffee ^_^"); Super.setaddimpl (Addimpl); } }
Maintest.java
Package Coffee;import Java.util.scanner;public class Maintest {public static void main (string[] args) {Sca Nner in = new Scanner (system.in); String choice = new String (); String addsth = new string (); while (true) {System.out.println ("What kind of the coffe would you want?"); Choice = In.nextline (); System.out.println ("What would do I like to add?") Sugar or Milk? "); Addsth = In.nextline (); if (Choice.equals ("Instantcoffee")) {Coffee C1 = new Instantcoffee (); if (Addsth.equals ("Sugar")) {addimplement A1 = new addsugarimplement (); C1.setaddimpl (A1); C1.add (); }else if (addsth.equals ("Milk")) {addimplement a2 = new addmilkimplement (); C1.setaddimpl (A2); C1.add (); }else{System.out.println ("Sorry,we don ' t has this kind of things *_*"); }}else if (ChoIce.equals ("Lattecoffee")) {Coffee C1 = new Lattecoffee (); if (Addsth.equals ("Sugar")) {addimplement A1 = new addsugarimplement (); C1.setaddimpl (A1); C1.add (); }else if (addsth.equals ("Milk")) {addimplement a2 = new addmilkimplement (); C1.setaddimpl (A2); C1.add (); }else{System.out.println ("Sorry,we don ' t has this kind of things *_*"); }}else if (Choice.equals ("Mochacoffee")) {Coffee C1 = new Mochacoffee (); if (Addsth.equals ("Sugar")) {addimplement A1 = new addsugarimplement (); C1.setaddimpl (A1); C1.add (); }else if (addsth.equals ("Milk")) {addimplement a2 = new addmilkimplement (); C1.setaddimpl (A2); C1.add (); }else{System.out.println ("Sorry,we don T" has this kind of things *_*"); }}else{System.out.println ("We don t has this kind of coffee*_*"); } } }}
Simulates a file copy process Test.java
Package test;//system classified as abstract class attribute{int cnt;//public abstract void Operation1 (); public abstract void output (); public abstract void copy (); public abstract void Traversedirectory ();} Category Class folder extends attribute{private attribute [] parts = new ATTRIBUTE[100]; private String name; public int cnt = 0; private int index = 0; Constructor Section folder () {} folder (String name) {this.name = name; }//Copy operation//public void Operation1 () {//System.out.println ("Copy file:" + name);//} public void Output () {S Ystem.out.println ("+" + name); } public void Add (Folder name) {Parts[index] = name; index++; name.cnt = this.cnt + 1; } public void Add (File name) {Parts[index] = name; index++; name.cnt = this.cnt + 1; The public boolean remove (attribute a) {for (int i = 0; i < index; i++) {if (Parts[i].equals (a)) { Parts[i] = null; return true; }} return false; }//There are several operations public int getchild () {return index; }//Traverse operation public void Traversedirectory () {this.output (); for (int i = 0, i < index; i++) {for (int k = 0; k <= this.cnt; k++) {System.out.print (" "); } this.parts[i].traversedirectory (); }} public void Copy () {System.out.println ("Copy file:" + name); for (int j = 0; J < This.index; J + +) {this.parts[j].copy (); }}}//class file extends attribute{public String name; public int cnt = 0; File (String name) {this.name = new String (name); } public void Output () {System.out.println ("-" + name); } public void Copy () {System.out.println ("Copy file:" + name); }//public void Operation1 () {///SYSTEM.OUT.PRINTLN ("Copy file:" + name),//} public void Traversedirectory () { This.output (); }}publicClass test{public static StringBuffer st = new StringBuffer (); public static void Main (string[] args) {Folder document = new Folder ("My Profile"); File book = new file ("Java programming idea. pdf"); Folder Music = new Folder ("My Music"); File Music1 = new file ("You Are My eyes. mp3"); File Music2 = new file ("Without You.mp3"); Folder Picture = new Folder ("My Photos"); File Pic1 = new file ("My photo at the White House in the United States"); File Pic2 = new file ("My photos in Singapore"); File PIC3 = new file ("My photos in Antarctica"); File Pic4 = new file ("My photos in South Africa"); File PIC5 = new file ("I have a big photo with Xi"); Document.add (book); Document.add (music); Music.add (MUSIC1); Music.add (MUSIC2); Picture.add (PIC1); Picture.add (PIC2); Picture.add (PIC3); Picture.add (PIC4); Picture.add (PIC5); Document.copy (); System.out.println ("-------------------------------"); Document.traversedirectory (); Picture.traversedirectory(); }}
Java Internship two