Pages: [1]   Go Down
Print
Author Topic: Soluzione esame di giorno 9-11 - Compito B  (Read 759 times)
0 Members e 1 Utente non registrato stanno visualizzando questa discussione.
Simone Faro
Moderator
Matricola
*****
Offline Offline

Gender: Male
Posts: 67


WWW
« on: 09-11-2010, 19:46:43 »

Code:
import java.io.*;
import java.util.*;
import java.math.*;

interface Cerchio09  extends Comparable {
public BigDecimal getRaggio();
public BigDecimal getCirconferenza();
public BigDecimal getArea();
public int compareTo(Object o);
}

interface BSTNode09<E>  {
public E getElement();
public BSTNode09<E> left();
public BSTNode09<E> right();
public BSTNode09<E> parent();
}

interface BST09<E>  extends Iterable<E> {
public BSTNode09<E> getMin(BSTNode09<E> node);
public void insert(E element);
public int size();
public BSTNode09<E> next(BSTNode09<E> node);
public Iterator<E> iterator();
}

class Cerchio implements Cerchio09  {
static final BigDecimal pi = new BigDecimal("3.14");
private final BigDecimal raggio, circonferenza, area;
public Cerchio(BigDecimal raggio) {
this.raggio = new BigDecimal(raggio.toString());
circonferenza = raggio.multiply(pi);
area = raggio.multiply(raggio).multiply(pi);
}
public BigDecimal getRaggio() {return new BigDecimal(raggio.toString());}
public BigDecimal getCirconferenza() {return new BigDecimal(circonferenza.toString());}
public BigDecimal getArea() {return new BigDecimal(area.toString());}
public int compareTo(Object o)  throws ClassCastException {
Cerchio b = (Cerchio) o;
return area.compareTo(b.getArea());
}
}

class BSTNode<E> implements BSTNode09<E> {
private E element;
private BSTNode<E> right, left, parent;
public BSTNode(E element, BSTNode<E> parent) {
this.element = element;
left = right = null;
this.parent = parent;
}
public E getElement(){return element;}
public BSTNode<E> left(){return left;}
public BSTNode<E> right(){return right;}
public BSTNode<E> parent(){return parent;}
public void setLeft(BSTNode<E> left) {this.left=left;}
public void setRight(BSTNode<E> right) {this.right=right;}
public void setParent(BSTNode<E> parent) {this.parent=parent;}
}

class BSTIterator<E extends Comparable> implements Iterator<E> {
BST<E> tree;
BSTNode<E> current;
public BSTIterator(BST<E> t) {tree = t; current=(BSTNode<E>) tree.getMin(tree.getRoot());}
public boolean hasNext() {return (current!=null);}
public E next() {
BSTNode<E> tmp = current;
current = (BSTNode<E>) tree.next(current);
return tmp.getElement();
}
public void remove() {}
}

class BST<E extends Comparable> implements BST09<E> {
private BSTNode<E> root;
int size;
public BST()  {
size = 0;
root = null;
}
public BSTNode09<E> getMin(BSTNode09<E> node) {
if(node==null) return null;
BSTNode<E> tmp = (BSTNode<E>) node;
while(tmp.left()!=null) tmp=tmp.left();
return tmp;
}
public BSTNode<E> getRoot() {return root;}
public void insert(E element) {
BSTNode<E> tmp = (BSTNode<E>) root;
BSTNode<E> parent = null;
while(tmp!=null) {
parent = tmp;
if(tmp.getElement().compareTo(element)>0) tmp=tmp.left();
else tmp=tmp.right();
}
if(parent==null) root = new BSTNode<E>(element, null);
else {
if(parent.getElement().compareTo(element)>0) parent.setLeft(new BSTNode<E>(element,parent));
else parent.setRight(new BSTNode<E>(element,parent));
}
}
public int size() {return size;}
public BSTNode09<E> next(BSTNode09<E> node) {
if(node.right()!=null) return getMin(node.right());
else {
BSTNode<E> tmp = (BSTNode<E>) node.parent();
while(tmp!=null && tmp.getElement().compareTo(node.getElement())<0) tmp=tmp.parent();
return tmp;
}
}
public Iterator<E> iterator() {return new BSTIterator(this);}
}

public class esame  {
    public static void main( String args[] ) throws IOException {
Scanner input = new Scanner(new File("input.txt"));
PrintWriter output = new PrintWriter("output.txt");
BST<Cerchio> tree = new BST<Cerchio>();

while( input.hasNext() ) {
String raggio = input.next();
Cerchio c = new Cerchio(new BigDecimal(raggio));
tree.insert(c);
}
for(Cerchio c : tree)  output.write(c.getArea() .toString() + "\n");
input.close();
output.close();
}

}
Logged

________________________________
Simone Faro, Ph.D.
Dipartimento di Matematica e Informatica
Università di Catania
________________________________
Pages: [1]   Go Up
Print
Jump to: