Skip to content
Snippets Groups Projects
Commit d77693ae authored by W. Spencer Smith's avatar W. Spencer Smith
Browse files

Remove Java files from spec via uml lecture (using C++ in 2018)

parent 9d2bc3fc
No related branches found
No related tags found
No related merge requests found
Showing
with 0 additions and 269 deletions
public class BankAccount
{
private double balance;
public BankAccount()
{ balance = 0;}
public void deposit(double amount)
{ balance = balance + amount;}
public void withdraw(double amount)
{ balance = balance - amount;}
public double getBalance()
{ return balance;}
}
\ No newline at end of file
public class BankAccountInterface implements Measurable
{ private double balance;
public BankAccountInterface()
{ balance = 0;
}
//..
public double getBalance()
{ return balance;
}
public double getMeasure()
{ return balance;}
}
\ No newline at end of file
public class DataSet
{
private double sum;
private double maximum;
private int count;
public DataSet()
{
sum = 0;
count = 0;
maximum = 0;
}
public void add(double x)
{
sum = sum + x;
if (count == 0 || maximum < x) maximum = x;
count++;
}
public double getAverage()
{
if (count == 0) return 0;
else return sum/count;
}
public double getMaximum()
{ return maximum;
}
}
\ No newline at end of file
public class DataSetBankAccount
{
private double sum;
private BankAccount maximum;
private int count;
public DataSetBankAccount()
{
sum = 0;
count = 0;
maximum = null;
}
public void add(BankAccount x)
{
sum = sum + x.getBalance();
if (count == 0 || maximum.getBalance() < x.getBalance()) maximum = x;
count++;
}
public double getAverage()
{ if (count == 0) return 0;
else return sum/count;
}
public BankAccount getMaximum()
{ return maximum;
}
}
\ No newline at end of file
public class DataSetInterface
{
private double sum;
private Measurable* maximum;
private int count;
public DataSetInterface()
{
sum = 0;
count = 0;
maximum = null;
}
public void add(Measurable x)
{
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure()) maximum = x;
count++;
}
public double getAverage()
{ if (count == 0) return 0;
else return sum/count;
}
public Measurable getMaximum()
{ return maximum;
}
}
public class DataSetPoint
{
private double sum;
private PointT maximum;
private int count;
public DataSetPoint()
{ sum = 0;
count = 0;
maximum = null;
}
public void add(PointT x)
{
sum = sum + x.distToOrigin();
if (count == 0 || maximum.distToOrigin() < x.distToOrigin()) maximum = x;
count++;
}
public double getAverage()
{ if (count == 0) return 0;
else return sum/count;
}
public PointT getMaximum()
{
return maximum;
}
}
\ No newline at end of file
public class DataSetStrategy
{ private double sum;
private Object maximum;
private int count;
private Measurer measurer;
public DataSetStrategy(Measurer aMeasurer)
{ sum = 0;
count = 0;
maximum = null;
measurer = aMeasurer;
}
public void add(Object x)
{
sum = sum + measurer.measure(x);
if (count == 0 || measurer.measure(maximum) < measurer.measure(x)) maximum = x;
count++;
}
public double getAverage()
{ if (count == 0) return 0;
else return sum/count;
}
public Object getMaximum()
{ return maximum;
}
}
\ No newline at end of file
import java.awt.Rectangle;
public class DataSetStrategyTest
{
public static void main(String[] args)
{
class RectangleMeasurer implements Measurer
{
public double measure(Object anObject)
{
Rectangle aRectangle = (Rectangle) anObject;
double area = aRectangle.getWidth() * aRectangle.getHeight();
return area;
}
}
Measurer m = new RectangleMeasurer();
DataSetStrategy data = new DataSetStrategy(m);
data.add(new Rectangle(5, 10, 20, 30));
data.add(new Rectangle(10, 20, 30, 40));
System.out.println("Average area = " + data.getAverage());
Rectangle max = (Rectangle) data.getMaximum();
System.out.println("Maximum area = " + m.measure(max));
}
}
\ No newline at end of file
public class DataSetTest
{ public static void main(String[] args)
{ DataSetInterface bankData = new DataSetInterface();
bankData.add(new BankAccountInterface());
BankAccountInterface b = new BankAccountInterface();
b.deposit(134.56);
bankData.add(b);
System.out.println("Average balance = " + bankData.getAverage());
Measurable max = bankData.getMaximum();
System.out.println("Highest balance = " + max.getMeasure());
DataSetInterface pointData = new DataSetInterface();
pointData.add(new PointTInterface(1.0, 1.0));
pointData.add(new PointTInterface(2.0, 2.0));
pointData.add(new PointTInterface(3.0, 3.0));
System.out.println("Average distance to origin = " + pointData.getAverage());
max = pointData.getMaximum();
System.out.println("Greatest distance to origin = " + max.getMeasure());
}
}
\ No newline at end of file
public interface Measurable
{
double getMeasure();
}
\ No newline at end of file
public interface Measurer
{
double measure(Object anObject);
}
\ No newline at end of file
import static java.lang.Math.*;
public class PointT {
private double xc;
private double yc;
public PointT(double x, double y) {
xc = x;
yc = y;}
// ..
public double distToOrigin() {
return sqrt(pow(xc,2.0) + pow(yc,2.0));
}
}
\ No newline at end of file
import static java.lang.Math.*;
public class PointTInterface implements Measurable
{
private double xc;
private double yc;
public PointTInterface(double x, double y) {
xc = x;
yc = y;
}
//..
public double distToOrigin() {
return sqrt(pow(xc,2.0) + pow(yc,2.0));
}
public double getMeasure(){
return distToOrigin();
}
}
\ No newline at end of file
import java.awt.Rectangle;
class RectangleMeasurer implements Measurer
{
public double measure(Object anObject)
{
Rectangle aRectangle = (Rectangle) anObject;
double area = aRectangle.getWidth() * aRectangle.getHeight();
return area;
}
}
\ No newline at end of file
import java.util.Arrays;
public class TestArraysSortComparable
{ public static void main(String [] args)
{ PointT[] p = new PointT[6];
p[0] = new PointT(0.0, 0.0);
p[1] = new PointT(10.0, 30.0);
p[2] = new PointT(4.0, 15.0);
p[3] = new PointT(41.0, 7.0);
p[4] = new PointT(9.0, 70.0);
System.out.println("p[0] compared to p[1]: " + p[0].compareTo(p[1]));
PrintPath(p, 6);
Arrays.sort(p);
PrintPath(p, 6);
}
private static void PrintPath(PointT[] pl, int n)
{ int i;
PointT p;
System.out.println();
System.out.println("i" + "\t" + "xcoord" + "\t" + "ycoord");
for (i = 0; i < n; i++)
{ p = pl[i];
System.out.println(i + "\t" + p.xcoord() + "\t" + p.ycoord());
}
System.out.println();
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment