diff options
| author | mokha <mokha@cisco.com> | 2019-05-05 19:25:48 -0600 |
|---|---|---|
| committer | mokha <mokha@cisco.com> | 2019-05-05 19:25:48 -0600 |
| commit | 46e409f3ca33ec3c7546d0589301c2a4f967c10a (patch) | |
| tree | 55ed5a3297517719af1856f8fd0fa848dd4b94b5 /src/main/java/ca/mokhan/assignment1 | |
| parent | c66b9591c81f93e3966050cc7c88981018e9b542 (diff) | |
collapse assignment1 dir
Diffstat (limited to 'src/main/java/ca/mokhan/assignment1')
12 files changed, 661 insertions, 0 deletions
diff --git a/src/main/java/ca/mokhan/assignment1/AddressBook.java b/src/main/java/ca/mokhan/assignment1/AddressBook.java new file mode 100644 index 0000000..9fcfa82 --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/AddressBook.java @@ -0,0 +1,178 @@ +package ca.mokhan.assignment1; + +import java.util.Objects; + +public class AddressBook implements Comparable<AddressBook> { + private String businessPhone; + private String cellPhone; + private String facebookId; + private String firstName = ""; + private String homeAddress; + private String homePhone; + private String lastName = ""; + private String middleName = ""; + private String personalWebSite; + private String skypeId; + + public AddressBook() { + this(""); + } + + public AddressBook(String firstName) { + this(firstName, "", ""); + } + + public AddressBook(String firstName, String middleName, String lastName) { + this(firstName, middleName, lastName, "", "", "", "", "", "", ""); + } + + public AddressBook( + String firstName, + String middleName, + String lastName, + String businessPhone, + String cellPhone, + String facebookId, + String homeAddress, + String homePhone, + String personalWebSite, + String skypeId) { + this.businessPhone = businessPhone; + this.cellPhone = cellPhone; + this.facebookId = facebookId; + this.firstName = firstName; + this.homeAddress = homeAddress; + this.homePhone = homePhone; + this.lastName = lastName; + this.middleName = middleName; + this.personalWebSite = personalWebSite; + this.skypeId = skypeId; + } + + public String getBusinessPhone() { + return this.businessPhone; + } + + public String getCellPhone() { + return this.cellPhone; + } + + public String getFacebookId() { + return this.facebookId; + } + + public String getFirstName() { + return this.firstName; + } + + public String getHomeAddress() { + return this.homeAddress; + } + + public String getHomePhone() { + return this.homePhone; + } + + public String getLastName() { + return this.lastName; + } + + public String getMiddleName() { + return this.middleName; + } + + public String getPersonalWebSite() { + return this.personalWebSite; + } + + public String getSkypeId() { + return this.skypeId; + } + + public void setBusinessPhone(String value) { + this.businessPhone = value; + } + + public void setCellPhone(String value) { + this.cellPhone = value; + } + + public void setFacebookId(String value) { + this.facebookId = value; + } + + public void setFirstName(String name) { + this.firstName = name; + } + + public void setHomeAddress(String address) { + this.homeAddress = address; + } + + public void setHomePhone(String value) { + this.homePhone = value; + } + + public void setLastName(String name) { + this.lastName = name; + } + + public void setMiddleName(String name) { + this.middleName = name; + } + + public void setPersonalWebSite(String value) { + this.personalWebSite = value; + } + + public void setSkypeId(String value) { + this.skypeId = value; + } + + public static String compareNames(String name1, String name2) { + return Integer.toString(name1.compareTo(name2)); + } + + public int compareTo(AddressBook other) { + return this.firstName.compareTo(other.firstName) + + this.middleName.compareTo(other.middleName) + + this.lastName.compareTo(other.lastName); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof AddressBook)) return false; + AddressBook that = (AddressBook) o; + return Objects.equals(businessPhone, that.businessPhone) + && Objects.equals(cellPhone, that.cellPhone) + && Objects.equals(facebookId, that.facebookId) + && Objects.equals(firstName, that.firstName) + && Objects.equals(homeAddress, that.homeAddress) + && Objects.equals(homePhone, that.homePhone) + && Objects.equals(lastName, that.lastName) + && Objects.equals(middleName, that.middleName) + && Objects.equals(personalWebSite, that.personalWebSite) + && Objects.equals(skypeId, that.skypeId); + } + + @Override + public int hashCode() { + return Objects.hash( + businessPhone, + cellPhone, + facebookId, + firstName, + homeAddress, + homePhone, + lastName, + middleName, + personalWebSite, + skypeId); + } + + @Override + public String toString() { + return this.firstName; + } +} diff --git a/src/main/java/ca/mokhan/assignment1/App.java b/src/main/java/ca/mokhan/assignment1/App.java new file mode 100644 index 0000000..6dc05d2 --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/App.java @@ -0,0 +1,7 @@ +package ca.mokhan.assignment1; + +public class App { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/main/java/ca/mokhan/assignment1/BanffMarathonRunner.java b/src/main/java/ca/mokhan/assignment1/BanffMarathonRunner.java new file mode 100644 index 0000000..afc0472 --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/BanffMarathonRunner.java @@ -0,0 +1,51 @@ +package ca.mokhan.assignment1; + +import java.util.ArrayList; +import java.util.Arrays; + +public class BanffMarathonRunner extends AddressBook { + private int time; + private int years; + + public BanffMarathonRunner(String firstName, String lastName, int time, int years) { + super(firstName, "", lastName); + this.time = time; + this.years = years; + } + + public int compareTo(AddressBook other) { + BanffMarathonRunner runner = (BanffMarathonRunner) other; + return Integer.compare(this.time, runner.time); + } + + @Override + public String toString() { + return super.getFirstName() + " " + this.years; + } + + public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners) { + Arrays.sort(runners); + return runners[0]; + } + + public static BanffMarathonRunner getSecondFastestRunner(BanffMarathonRunner[] runners) { + Arrays.sort(runners); + return runners[1]; + } + + public static int getAverageTime(BanffMarathonRunner[] runners) { + int sum = 0; + for (BanffMarathonRunner runner : runners) sum += runner.time; + return sum / runners.length; + } + + public static String getAboveAverageRunners(BanffMarathonRunner[] runners) { + int average = getAverageTime(runners); + ArrayList<String> winners = new ArrayList<String>(); + + for (BanffMarathonRunner runner : runners) + if (runner.time >= average) winners.add(runner.toString()); + + return String.join(System.lineSeparator(), winners); + } +} diff --git a/src/main/java/ca/mokhan/assignment1/BonusOnSavings.java b/src/main/java/ca/mokhan/assignment1/BonusOnSavings.java new file mode 100644 index 0000000..41090ec --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/BonusOnSavings.java @@ -0,0 +1,36 @@ +package ca.mokhan.assignment1; + +public class BonusOnSavings { + double annualRate = 0.0; + double quarterlyRate = 0.0; + + public BonusOnSavings() { + this(0.03, 0.05); + } + + public BonusOnSavings(double quarterlyRate, double annualRate) { + this.quarterlyRate = quarterlyRate; + this.annualRate = annualRate; + } + + public double computeBonus(double monthlyCommitment, double q1, double q2, double q3, double q4) { + double quarterlyTarget = monthlyCommitment * 3; + double annualTarget = monthlyCommitment * 12; + + return this.quarterlyBonus(quarterlyTarget, q1) + + this.quarterlyBonus(quarterlyTarget, q2) + + this.quarterlyBonus(quarterlyTarget, q3) + + this.quarterlyBonus(quarterlyTarget, q4) + + this.annualBonus(annualTarget, q1 + q2 + q3 + q4); + } + + private double quarterlyBonus(double target, double actual) { + return (actual >= target) ? actual * this.quarterlyRate : 0.0; + } + + private double annualBonus(double target, double actual) { + if (actual < target) return 0.0; + + return (actual * this.annualRate) + ((actual - target) * 0.25); + } +} diff --git a/src/main/java/ca/mokhan/assignment1/Candidate.java b/src/main/java/ca/mokhan/assignment1/Candidate.java new file mode 100644 index 0000000..7df9535 --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/Candidate.java @@ -0,0 +1,75 @@ +package ca.mokhan.assignment1; + +import java.util.ArrayList; + +public class Candidate extends AddressBook { + private double grade = 0.0; + private Communication communication; + private boolean isInnovative; + private double regulatoryCapability; + + public Candidate( + String firstName, + String lastName, + double grade, + String communication, + boolean isInnovative, + double regulatoryCapability) { + super(firstName, "", lastName); + this.grade = grade; + this.setCommunication(communication); + this.isInnovative = isInnovative; + this.regulatoryCapability = regulatoryCapability; + } + + public boolean isInnovative() { + return this.isInnovative; + } + + public double getGrade() { + return this.grade; + } + + public double getRegulation() { + return this.regulatoryCapability; + } + + public String getCommunication() { + return this.communication.toString(); + } + + public void setCommunication(String communication) { + this.communication = Communication.findBy(communication); + } + + public void setGrade(double grade) { + this.grade = grade; + } + + public void setInnovation(boolean innovation) { + this.isInnovative = innovation; + } + + public void setRegulation(double regulatoryCapability) { + this.regulatoryCapability = regulatoryCapability; + } + + public boolean isEligible() { + if (this.grade >= 85.0) { + return this.communication.isAtLeast(Communication.Average) || this.isInnovative(); + } else { + return (this.regulatoryCapability >= 0.5 + && this.communication.isAtLeast(Communication.Average)); + } + } + + public static ArrayList<Candidate> getEligibleCandidates(Candidate[] candidates) { + ArrayList<Candidate> eligible = new ArrayList<Candidate>(); + + for (Candidate candidate : candidates) { + if (candidate.isEligible()) eligible.add(candidate); + } + + return eligible; + } +} diff --git a/src/main/java/ca/mokhan/assignment1/CartesianCoordinateSystem.java b/src/main/java/ca/mokhan/assignment1/CartesianCoordinateSystem.java new file mode 100644 index 0000000..d6681f9 --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/CartesianCoordinateSystem.java @@ -0,0 +1,7 @@ +package ca.mokhan.assignment1; + +public class CartesianCoordinateSystem { + public double calculateDistance(double x1, double y1, double x2, double y2) { + return Math.round(Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5) * 100) / 100.0; + } +} diff --git a/src/main/java/ca/mokhan/assignment1/Communication.java b/src/main/java/ca/mokhan/assignment1/Communication.java new file mode 100644 index 0000000..2e4c2a8 --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/Communication.java @@ -0,0 +1,40 @@ +package ca.mokhan.assignment1; + +public class Communication implements Comparable<Communication> { + private String name; + private Integer ranking; + + public Communication(String name, Integer ranking) { + this.name = name; + this.ranking = ranking; + } + + public int compareTo(Communication other) { + return this.ranking.compareTo(other.ranking); + } + + public boolean isAtLeast(Communication other) { + return this.compareTo(other) >= 0; + } + + @Override + public String toString() { + return this.name; + } + + public static final Communication Poor = new Communication("poor", 0); + public static final Communication Average = new Communication("average", 1); + public static final Communication Excellent = new Communication("excellent", 2); + + public static Communication findBy(String name) { + switch (name) { + case "poor": + return Communication.Poor; + case "average": + return Communication.Average; + case "excellent": + return Communication.Excellent; + } + throw new IllegalArgumentException("Unknown communication type"); + } +} diff --git a/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java b/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java new file mode 100644 index 0000000..44eac10 --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java @@ -0,0 +1,84 @@ +package ca.mokhan.assignment1; + +import java.util.ArrayList; +import java.util.Currency; +import java.util.Locale; +import java.util.Random; + +public class EmployeeSavings extends AddressBook { + private double accountValue; + private double[] monthlyInterests; + private double[] monthlySavings; + private double monthlyContribution; + public static final double ANNUAL_INTEREST_RATE = 0.05; + + public EmployeeSavings(String firstName, String lastName) { + this(firstName, lastName, new Random().nextInt(800 - 100) + 100.0); + } + + public EmployeeSavings(String firstName, String lastName, double monthlyContribution) { + super(firstName, "", lastName); + this.monthlyContribution = monthlyContribution; + this.accountValue = predictBalanceAfterMonths(12); + } + + // what is d1, d2? + public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2) { + super(firstName, "", lastName); + } + + public double getAccountValue() { + return this.accountValue; + } + + public double[] getMonthlyInterests() { + return this.monthlyInterests; + } + + public double[] getMonthlySavings() { + return this.monthlySavings; + } + + public double[] calculateInterests() { + this.monthlyInterests = new double[12]; + for (int i = 1; i <= 12; i++) this.monthlyInterests[i - 1] = predictInterestAfterMonths(i); + return this.monthlyInterests; + } + + public double[] generateMonthlySavings() { + this.monthlySavings = new double[12]; + for (int i = 1; i <= 12; i++) this.monthlySavings[i - 1] = predictBalanceAfterMonths(i); + return this.monthlySavings; + } + + public double predictBalanceAfterMonths(int months) { + double monthlyRate = ANNUAL_INTEREST_RATE / 12.0; + double balance = 0; + + for (int i = 0; i < months; i++) + balance = (balance + this.monthlyContribution) * (1 + monthlyRate); + + return Math.round(balance * 1000) / 1000.0; + } + + public double predictInterestAfterMonths(int months) { + return Math.round( + (predictBalanceAfterMonths(months) - (months * this.monthlyContribution)) * 1000) + / 1000.0; + } + + public static String getReport(EmployeeSavings[] accounts) { + ArrayList<String> statement = new ArrayList<String>(); + + for (EmployeeSavings account : accounts) statement.add(account.toString()); + + return String.join(System.lineSeparator(), statement); + } + + @Override + public String toString() { + Currency currency = Currency.getInstance(Locale.getDefault()); + return String.format( + "%s %s%.2f", super.getFirstName(), currency.getSymbol(), this.getAccountValue()); + } +} diff --git a/src/main/java/ca/mokhan/assignment1/HailstoneSequence.java b/src/main/java/ca/mokhan/assignment1/HailstoneSequence.java new file mode 100644 index 0000000..b0e3c48 --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/HailstoneSequence.java @@ -0,0 +1,17 @@ +package ca.mokhan.assignment1; + +import java.util.ArrayList; + +public class HailstoneSequence { + public static ArrayList<Integer> getHailstoneSequence(int n) { + return getHailstoneSequence(n, new ArrayList<Integer>()); + } + + public static ArrayList<Integer> getHailstoneSequence(int n, ArrayList<Integer> items) { + items.add(n); + + if (n == 1) return items; + else if (n % 2 == 0) return getHailstoneSequence(n / 2, items); + else return getHailstoneSequence((n * 3) + 1, items); + } +} diff --git a/src/main/java/ca/mokhan/assignment1/Number.java b/src/main/java/ca/mokhan/assignment1/Number.java new file mode 100644 index 0000000..d96274f --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/Number.java @@ -0,0 +1,46 @@ +package ca.mokhan.assignment1; + +import java.util.ArrayList; + +public class Number { + public static boolean isDivisibleBy5(int n) { + return isDivisibleBy(n, 5); + } + + public static boolean isDivisibleBy7(int n) { + return isDivisibleBy(n, 7); + } + + public static boolean isOdd(int n) { + return !isDivisibleBy(n, 2); + } + + public static boolean isPrime(int n) { + if (n <= 1) return false; + + for (int i = n - 1; i > 1; i--) if (isDivisibleBy(n, i)) return false; + + return true; + } + + public static boolean isDivisibleBy(int n, int denominator) { + return n % denominator == 0; + } + + public static ArrayList<String> iterate() { + ArrayList<String> items = new ArrayList<String>(); + ArrayList<String> row = new ArrayList<String>(); + for (Integer i = 0; i < 113; i++) { + row.clear(); + row.add(String.format("%d", i)); + + if (isOdd(i)) row.add(String.format("%d is odd", i)); + if (isDivisibleBy5(i)) row.add("hi five"); + if (isDivisibleBy7(i + (i + 1))) row.add("wow"); + if (isPrime(i)) row.add("prime"); + + items.add(String.join(",", row)); + } + return items; + } +} diff --git a/src/main/java/ca/mokhan/assignment1/TaxReturn.java b/src/main/java/ca/mokhan/assignment1/TaxReturn.java new file mode 100644 index 0000000..91a226a --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/TaxReturn.java @@ -0,0 +1,91 @@ +package ca.mokhan.assignment1; + +import java.util.Scanner; + +public class TaxReturn { + private double income; + private int status; + private int children; + + /** + * Constructs a TaxReturn object for a given income and marital status, and computes the tax. + * + * @param income the taxpayer income + * @param status either SINGLE or MARRIED + */ + public TaxReturn(double income, int status) { + this(income, status, 0); + } + + public TaxReturn(double income, int status, int children) { + this.income = income; + this.status = status; + this.children = children; + if (this.isSingle()) this.income -= children * 5000; + } + + public double getTax() { + double tax = 0; + + if (isSingle()) { + if (income <= SINGLE_BRACKET1) tax = RATE1 * income; + else if (income <= SINGLE_BRACKET2) + tax = RATE1 * SINGLE_BRACKET1 + RATE2 * (income - SINGLE_BRACKET1); + else + tax = + RATE1 * SINGLE_BRACKET1 + + RATE2 * (SINGLE_BRACKET2 - SINGLE_BRACKET1) + + RATE3 * (income - SINGLE_BRACKET2); + + if (income > 249999.0) tax += (income - 150000) * 0.25; + } else { + if (income <= MARRIED_BRACKET1) tax = RATE1 * income; + else if (income <= MARRIED_BRACKET2) + tax = RATE1 * MARRIED_BRACKET1 + RATE2 * (income - MARRIED_BRACKET1); + else + tax = + RATE1 * MARRIED_BRACKET1 + + RATE2 * (MARRIED_BRACKET2 - MARRIED_BRACKET1) + + RATE3 * (income - MARRIED_BRACKET2); + + if (income > 349999.0) tax += (income - 200000) * 0.35; + } + return tax; + } + + public static final int SINGLE = 1; + public static final int MARRIED = 2; + + public static final double RATE1 = 0.15; + public static final double RATE2 = 0.28; + public static final double RATE3 = 0.31; + + public static final double SINGLE_BRACKET1 = 21450; + public static final double SINGLE_BRACKET2 = 51900; + + public static final double MARRIED_BRACKET1 = 35800; + public static final double MARRIED_BRACKET2 = 86500; + + private boolean isSingle() { + return this.status == SINGLE; + } + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + System.out.print("Please enter your income: "); + double income = in.nextDouble(); + + System.out.print("Enter S (single) or M (married): "); + String input = in.next(); + int status = 0; + + if (input.equalsIgnoreCase("S")) status = TaxReturn.SINGLE; + else if (input.equalsIgnoreCase("M")) status = TaxReturn.MARRIED; + else { + System.out.println("Bad input."); + return; + } + TaxReturn aTaxReturn = new TaxReturn(income, status); + System.out.println("The tax is " + aTaxReturn.getTax()); + } +} diff --git a/src/main/java/ca/mokhan/assignment1/Triangle.java b/src/main/java/ca/mokhan/assignment1/Triangle.java new file mode 100644 index 0000000..cf45ec2 --- /dev/null +++ b/src/main/java/ca/mokhan/assignment1/Triangle.java @@ -0,0 +1,29 @@ +package ca.mokhan.assignment1; + +public class Triangle { + public static double NULL = 0.0; + private double a, b, c; + + public Triangle(double a, double b, double c) { + this.a = a; + this.b = b; + this.c = c; + } + + public double getA() { + return this.a; + } + + public double getB() { + if (this.b == NULL) this.b = Math.sqrt(Math.pow(this.getC(), 2) - Math.pow(this.getA(), 2)); + return this.b; + } + + public double getC() { + return this.c; + } + + public boolean isRightTriangle() { + return Math.pow(this.getA(), 2) + Math.pow(this.getB(), 2) == Math.pow(this.getC(), 2); + } +} |
