Solution to Assignment 1 program

Assignment 1 When solving the problems in this assignment, you must follow the application programming interface (API) expected in each problem. You should implement all the attributes and operations mentioned in the API. Note that there is no main method in the APIs. That is, you should not perform any data processing within the main method. You should rather use the main method to test other methods, prompt the user for some inputs, and display the results returned by your methods.  
1.       A) Create a class named AddressBook that has the following field names: firstName, middleName, lastName, homeAddress, businessPhone, homePhone, cellphone, skypeId, facebookId, and personalWebSite. Use appropriate data types to store the values for these fields in AddressBook objects. Create appropriate get and set methods to retrieve and assign values to these names. For example, getMiddleName(viveAddressBook) should return the middle name of the person Vive. Similarly, vive.setPersonalWebsite(url) should set the personal website of the person Vive to the specified URL object. Using the get and set methods, create a comparison method compareNames(name1, name2) that compares the first, middle, and last names of strings name1 and name2. Assume that name1 and name2 follow the following format: “FirstName M. LastName”. Test your program for correct, partially correct (e.g., name string without the middleName), and incorrect inputs (e.g., phone number containing special characters).
b) A group of AU friends decide to run the Banff, Alberta, Marathon. Their names, times (marathon completion time in minutes), and number of years participated are given below:  Extend the AddressBook class from the first part to store the additional data. Now, write a method to find the fastest runner. Print the name, address, and his/her time (in minutes) on three separate lines. Find the second fastest runner. Print the name, address, his/her time (in minutes), and the difference in time with the fastest runner. Compute the average time of completion taken by these runners.  Finally, print the name and number of years participated for each runner if the runner’s time of completion is equal to or better than the average time of completion.  

c) Solve the following problem using a program:  Suppose you save $100 each month into a savings account with an annual interest rate of 5%. Thus, the monthly interest rate is 0.05/12 = 0.00417. After the first month, the value in the account becomes 100 * (1 + 0.00417) = 100.417 After the second month, the value in the account becomes (100 + 100.417) * (1 + 0.00417) = 201.252 And after the third month, the value in the account becomes (100 + 201.252) * (1 + 0.00417) = 302.507 … and so on. Write a program that randomly generates monthly savings amounts for the 15 runners in Part B. Each monthly saving should be in the range of $100 to $800. Extend the AddressBook class to store the monthly savings generated by the random number generator. Then, display the final account value for each of the 15 runners.    

  2.       Space Inc. will give a quarterly and annual bonus to its employees only if the savings of the quarter and/or the year are greater than or equal to quarterly minimum (monthly commitment x 3) and/or the annual minimum (monthly commitment x 12) amount, respectively. The quarterly bonus is 3% of eligible quarterly savings, and the annual bonus is 5% of annual savings if eligible. If the annual savings exceeds the committed amount by at least 25%, Space Inc. matches the additional savings (25% or above) as part of the annual bonus.                I.      An employee has committed to save $2000 per month. Her quarterly savings are as follows: Q1 – $5000, Q2 – $7000, Q3 – $4000, and Q4 – $8000.              II.      Another employee has committed to save $3000 per month. His quarterly savings are as follows: Q1 – $6000, Q2 – $9000, Q3 – $10000, and Q4 – $17000. Write a program to compute the total bonus amount earned by these two employees in the year.  

3.       Write a program that prompts the user to enter two points (x1, y1) and (x2, y2). Calculate and display the distance between the two points using the formula below.  Round the answer up to 2 decimal points. You can use Math.pow(a,0.5) to compute the square root of an expression.  Math.pow()  returns a double.       For example, the distance between the points (−2, −3) and (−4, 4) is approximately 7.28, as shown below.          

4.       “The Pythagorean Theorem relates the lengths of the three sides of any right triangle. The legs of a right triangle (the two sides of the triangle that meet at the right angle) are customarily labelled as having lengths “a” and “b”, and the hypotenuse (the long side of the triangle, opposite the right angle) is labelled as having length “c”. The lengths are related by the following equation: a ^2 + b^2 = c ^2.” – refer to http://www.purplemath.com/modules/pythagthm.htm for details. This equation allows you to find the length of a side of a right triangle when they’ve given you the lengths for the other two sides, and, going in the other direction, allows you to determine if a triangle is a right triangle when they’ve given you the lengths for all three sides. This equation can alternatively be written as c = sqrt of (a^2+b^2).  You can find the square root of a number by calling the standard function Math.sqrt. For example, the statement double y = Math.sqrt(x) sets y to the square root of x.                I.      Given the right triangles described below, write a program to compute the lengths of the remaining sides using a program. a.       a = 48 and c = 80 b.       a = 84 and c = 91              II.      Determine if the following triangles are right-angled triangles: a.       a = 45, b = 55, and c = 75 b.       a = 28, b = 45, and c = 53

    5.       Douglas Hofstadter’s Pulitzer-prize-winning book Gödel, Escher, Bach contains many interesting mathematical puzzles. In Chapter XII, Hofstadter mentions a wonderful problem that is well within the scope of the control statements in Java. The problem can be expressed as follows: Pick some positive integer and call it n. If n is even, divide it by two. If n is odd, multiply it by three and add one. Continue this process until n is equal to 1. Hofstadter illustrates this process with the following example, starting with the number n = 15: 15 is odd, so I make 3n+1: 46 46 is even, so I take half: 23 23 is odd, so I make 3n+1: 70 70 is even, so I take half: 35 35 is odd, so I make 3n+1: 106 106 is even, so I take half: 53 53 is odd, so I make 3n+1: 160 160 is even, so I take half: 80 80 is even, so I take half: 40 40 is even, so I take half: 20 20 is even, so I take half: 10 10 is even, so I take half: 5 5 is odd, so I make 3n+1: 16 16 is even, so I take half: 8 8 is even, so I take half: 4 4 is even, so I take half: 2 2 is even, so I take half: 1   As you can see from this example, the numbers go up and down, but eventually—at least for all numbers that have ever been tried—come down to end in 1. In some respects, this process is reminiscent of the formation of hailstones, which get carried upward by the winds over and over again before they finally descend to the ground. Because of this analogy, this sequence of numbers is usually called the Hailstone sequence, although it goes by many other names as well. Write a program that reads in a number from the user and then displays the Hailstone sequence for that number, followed by a line showing the number of steps taken to reach 1.    

6.       Google Inc. is looking to recruit three of the Boston runners. The criteria for selection are as follows:          I.            Average final marks in bachelor’s degree (store up to 2 decimal places). The fifteen candidates have the following grades: 82.30%, 85.10%, 77.77%, 69.93%, 93.03%, 88.61%, 55.99%, 87.49%, 88.00%, 91.20%, 66.79%, 76.65%, 55.89%, 90.01%, and 87.9%.        II.            Ability to communicate as one of the three values – “excellent”, “average”, and “poor”.  The fifteen candidates have the following ability to communicate, respectively: poor, poor, average, average, average, poor, excellent, excellent, excellent, average, excellent, average, excellent, excellent, poor.      III.            Innovation as one of the two values – “brilliant” and “average” (store as a Boolean; brilliant = true and average = false).  The fifteen candidates have the following innovative abilities: brilliant, average, average, average, brilliant, brilliant, average, brilliant, average, brilliant, average, brilliant, brilliant, average, average.     IV.            Ability to regulate one’s own skill as a probability value between 0 and 1.0 – 1.0 implies excellent regulatory capabilities and 0.0 implies no skills to regulate (store as a double).  The fifteen candidates have the following regulatory abilities: 0.5, 1.0, 0.8, 0.0, 1.0, 0.7, 0.8, 0,9, 0.5, 0.6, 0.3, 0.2,  0.5, 0.3, 0.8. Store these values for the fifteen candidates in an extended AddressBook class. In general, Google will not consider a candidate with average marks of less than 85%. Google will consider a candidate with average marks of less than 85% only if the candidate at least has 0.5 regulatory abilities and at least ‘average’ ability to communicate.  Google will only consider a candidate with poor communication ability if the candidate has a ‘brilliant’ innovation capability. Write a program that will help Google to programmatically determine eligibility of the fifteen candidates for these positions, and print the output on the console.  

   7.       Write a program that iterates through numbers from 0 to 113 using a loop. Print the numbers, one number per line. As you print each number, say x, also print the following when appropriate, separated by commas:                  I.      If the number is odd, print “x is odd”              II.      If the number is divisible by 5, print “hi five”            III.      If the total of a number (x) and its subsequent number (x+1) is a value divisible by 7, print “wow”           IV.      If the number is prime, print “prime”.    

  8.       Modify the following program to the specifications given below:                I.      Add a new status – SingleParent – where the tax is computed as a SINGLE but with a further reduction of $5000 per child.              II.      Add a new tax condition – if the income is greater than $249,999 for SINGLE, then add a tax of 25% on income amount above $150,000; if the income is greater than $349,999 for MARRIED, then add a tax of 35% on income amount above $200,000.            III.      Unknown status – if the status doesn’t belong to SINGLE or MARRIED or SINGLE_PARENT, then compute a 33% tax on the income.   import java.util.Scanner; public class TaxReturn {     /**       Constructs a TaxReturn object for a given income and       marital status, and computes the tax.       @param anIncome the taxpayer income       @param aStatus either SINGLE or MARRIED    */      public TaxReturn(double anIncome, int aStatus)    {        income = anIncome;       status = aStatus;    }      public double getTax()    {        double tax = 0;         if (status == SINGLE)       {           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);       }       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);       }       return tax;    }      public static final int SINGLE = 1;    public static final int MARRIED = 2;    private static final double RATE1 = 0.15;    private static final double RATE2 = 0.28;    private static final double RATE3 = 0.31;    private static final double SINGLE_BRACKET1 = 21450;    private static final double SINGLE_BRACKET2 = 51900;    private static final double MARRIED_BRACKET1 = 35800;    private static final double MARRIED_BRACKET2 = 86500;    private double income;    private int status;      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());    } }  

 
Powered by