SortedTrio - c =(tArr1.get(2 this.setFirst(a...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 import java.util.ArrayList public class SortedTrio <T extends Comparable<? super T>> extends Trio { public SortedTrio(){ //default constructor super() } public SortedTrio(T a, T b, T c){ // default setup, in case comparables fail, due to having multiples of the same. super(a,b,c) ArrayList<T> tArr1 = new ArrayList<T>() tArr1.add(a) tArr1.add(b) tArr1.add(c) T t1,t2 boolean swappedOnPrevRun = true while(swappedOnPrevRun) { swappedOnPrevRun = false for(int i = 0 { t1 = (tArr1.get(i)) t2 = (tArr1.get(i+1)) if(t1.compareTo(t2) > -1)// if current element is greater than the next { // swap the two elements swappedOnPrevRun = true // set element i+1 to where i used to be tArr1.set(i,t2) // release the old i into i+1 slot tArr1.set(i+1,t1) } } } a = (tArr1.get(0)) b = (tArr1.get(1))
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: c = (tArr1.get(2)) this.setFirst(a) this.setSecond(b) this.setThird(c) } public boolean contains(T a){ boolean value = false if(a == this.first() || a == this.second() || a == this.third()) value = true return value } public String toString() { Sheet1 Page 2 T one = (T)(this.first()) T two = (T)(this.second()) T three = (T)(this.third()) return "[" + one.toString() + ", " + two.toString() + ", " + three.toString() + "]" } } Sheet1 Page 3 // this variable keeps track of whether to continue sorting or exit i < tArr1.size() - 1 // we don't want the loop to end just yet, we're not done Sheet1 Page 4 Sheet1 Page 5 i++)// loop through every element in the array,except the last one Sheet1 Page 6...
View Full Document

This note was uploaded on 09/16/2009 for the course CS 3345 taught by Professor Ozbirn during the Spring '08 term at University of Texas at Dallas, Richardson.

Page1 / 6

SortedTrio - c =(tArr1.get(2 this.setFirst(a...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online