For java assginmnent | Computer Science homework help

  

Exercise 1 [10 points]: 

Write a static method named numUnique that accepts an array of integers as a parameter and returns the number of unique values in that array. The array may contain positive integers in no particular order, which means that the duplicates will not be grouped together. For example, if a variable called list stores the following values: 

int[] list = {7, 5, 22, 7, 23, 9, 1, 5, 2, 35, 6, 11, 12, 7, 9}; 

then the call of numUnique(list) should return 11 because this list has 11 unique values (1, 2, 5, 6, 7, 9, 11, 12, 22, 23, and 35). It is possible that the list might not have any duplicates. For example if the list contain this sequence of values: 

int[] list = {1, 2, 11, 17, 19, 20, 23, 24, 25, 26, 31, 34, 37, 40, 41}; Then a call to the method would return 15 because this list contains 15 different values. 

If passed an empty list, your method should return 0. 

Note: It might be beneficial to sort the array before you count the unique elements. To sort an integer array, you can use Arrays.sort() method. 

Exercise 2 [10 points]: 

In this exercise, you need to complete the following static methods and then use them. 

public class Password{ 

public static boolean passwordValidator(String pass1, String pass2){ 

// Your code goes here… 

public static String passwordGenerator(int length){ 

// Your code goes here 

• The method passwordValidator takes two alpha-numeric passwords and returns true only if all the following conditions are met: 

i) Length of the password must be between 8 and 32 characters. 

ii) Password should contain at least one upper case and one lower case alphabet. iii)  Password should contain at least two numbers

iv) Password should contain at least one special character from this list {!, ~, _, %, $, #}

v) Both the password strings must match. 

• The method passwordGenerator – generates and returns a random password that satisfy the above-mentioned criteria with a specified length. 

• Now, write a PasswordTester class – in which you should test these static methods. 

Here is an example test run: 

Enter a new password: test123 

Re-enter the same password: test123 

Password should contain at least 8 characters. 

Enter a new password: qwerty123 

Re-enter the same password: qwerty123 

Password must contain at least 1 capital letter character. 

Enter a new password: qwerTy123 

Re-enter the same password: qwerTy123 

Password must contain at least 1 special symbol. 

Enter a new password: qwerTy_1 

Re-enter the same password: qwerTy_1 

Password must contain at least 2 numbers. 

Enter a new password: qwerTy_12 

Re-enter the same password: qwerty_12 

Both passwords must match. 

Enter a new password: qwerTy_12 

Re-enter the same password: qwerTy_12 

Success! 

The test cases shown above are sample only. When you test your code, you should test it for all possible cases. If the user cannot select a valid password within 7 tries, then your program should suggest four random passwords (with different length) to the user that satisfies all rules for the password

Take snapshots of different test runs for your program and attach those snapshots during submission. 

Exercise 3 [10 Points]: 

DateUtil: Complete the following methods in a class called DateUtil: 

• boolean isLeapYear(int year): returns true if the given year is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400. 

• boolean isValidDate(int year, int month, int day): returns true if the given year, month and day constitute a given date. Assume that year is between 1 and 9999, month is between 1 (Jan) to 12 (Dec) and day shall be between 1 and 28|29|30|31 depending on the month and whether it is a leap year. 

• int getDayOfWeek(int year, int month, int day): returns the day of the week, where 0 for SUN, 1 for MON, …, 6 for SAT, for the given date. Assume that the date is valid. 

• String toString(int year, int month, int day): prints the given date in the format “xxxday d mmm yyyy”, e.g., “Tuesday 14 Feb 2012”. Assume that the given date is valid. 

To find the day of the week (Reference: Wiki “Determination of the day of the week”): 

1. Based on the first two digit of the year, get the number from the following “century” table. 

  

1700- 

1800- 

1900- 

2000- 

2100- 

2200- 

2300- 

2400- 

 

4   

2   

6   

4   

2   

0   

6   

2. Take note that the entries 4, 2, 0, 6 repeat. 

3. Add to the last two digit of the year. 

4. Add to “the last two digit of the year divide by 4, truncate the fractional part”. 

5. Add to the number obtained from the following month table: 

6. Add to the day. 

7. The sum modulus 7 gives the day of the week, where 0 for SUN, 1 for MON, …, 6 for SAT. 

For example: 2012, Feb, 17 

(6 + 12 + 12/4 + 2 + 17) % 7 = 5 (Fri) 

The skeleton of the program is as follows: 

  

/* Utilities for Date   Manipulation */ public class DateUtil

// Month’s name – for printing public static String   strMonths[] 

= {“Jan”, “Feb”,   “Mar”, “Apr”, “May”, “Jun”, 

“Jul”, “Aug”,   “Sep”, “Oct”, “Nov”, “Dec”}; 

// Number of days in each month (for non-leap years) public static int   daysInMonths[] 

= {31, 28, 31, 30, 31,   30, 31, 31, 30, 31, 30, 31}; 

// Returns true if the given year is a leap year public static boolean   isLeapYear(int year) { …… } 

// Return true if the given   year, month, day is a valid date 

// year: 1-9999 

// month: 1(Jan)-12(Dec) 

// day: 1-28|29|30|31. The last   day depends on year and month public static boolean isValidDate(int year, int month, int day) {   …… } 

// Return the day of the week,   0:Sun, 1:Mon, …, 6:Sat public static int getDayOfWeek(int year, int month, int day) { ……   } 

// Return String “xxxday d   mmm yyyy” (e.g., Wednesday 29 Feb 2012) 

public static String printDate(int year, int month, int day) { …… } 

public static void main(String[] args) { 

System.out.println(isLeapYear(1900)); // false System.out.println(isLeapYear(2000)); // true 

System.out.println(isLeapYear(2011)); // false 

System.out.println(isLeapYear(2012)); // true 

System.out.println(isValidDate(2012, 2, 29)); // true 

System.out.println(isValidDate(2011, 2, 29)); // false 

System.out.println(isValidDate(2099, 12, 31)); // true 

System.out.println(isValidDate(2099, 12, 32)); // false 

System.out.println(getDayOfWeek(1982, 4, 24)); // 6:Sat 

System.out.println(getDayOfWeek(2000, 1, 1)); // 6:Sat 

System.out.println(getDayOfWeek(2054, 6, 19)); // 5:Fri System.out.println(getDayOfWeek(2012, 2, 17)); // 5:Fri 

System.out.println(toString(2012, 2, 14)); // Tuesday 14 Feb 2012 

You can compare the day obtained with the Java’s Calendar class as follows: 

// Construct a Calendar instance with the given year, month and day 

Calendar cal = new GregorianCalendar(year, month – 1, day); // month is 0-based 

// Get the day of the week number: 1 (Sunday) to 7 (Saturday) int dayNumber = cal.get(Calendar.DAY_OF_WEEK); 

String[] calendarDays = { “Sunday”, “Monday”, “Tuesday”, “Wednesday”, 

“Thursday”, “Friday”, “Saturday” }; 

// Print result 

System.out.println(“It is ” + calendarDays[dayNumber – 1]); 

Foot Note: 

The calendar we used today is known as Gregorian calendar, which came into effect in October 15, 1582 in some countries and later in other countries. It replaces the Julian calendar. 10 days were removed from the calendar, i.e., October 4, 1582 (Julian) was followed by October 15, 1582 (Gregorian). The only difference between the Gregorian and the Julian calendar is the “leap-year rule”. In Julian calendar, every four years is a leap year. In Gregorian calendar, a leap year is a year that is divisible by 4 but not divisible by 100, or it is divisible by 400, i.e., the Gregorian calendar omits century years which are not divisible by 400. Furthermore, Julian calendar considers the first day of the year as march 25th, instead of January 1st. 

This above algorithm work for Gregorian dates only. It is difficult to modify the above algorithm to handle preGregorian dates. A better algorithm is to find the number of days from a known date. 

Exercise 4 [15 Marks] 

Ø Address Book Entry. Your task is to create a class called “AddressBookEntry” that contains an address book entry. The following table describes the information that an address book entry has. 

a. Provide the necessary accessor/getter and mutator/setter methods for all the ivars. 

b. You should provide a constructor that will initialize ivars with the supplied values from the client. 

c. Also provide a “public String toString()” method that will return a string in the following format: 

“Name: ”+<name>+”tt Address: “+<address>+” tt Tel: “+ <tel> + “ttEmail:” + <email>; 

where <name>, <address>, <tel> and <email> should be replaced by the receiver’s current state or field’s values. 

  

Save this class in a separate file called “AddressBookEntry.java” and compile it. 

Ø AddressBook. Create another class called “AddressBook” in “AddressBook.java” and save it the same directory. The AddressBook class should contain 100 entries of the “AddressBookEntry” objects (use the class you created in the above exercise). You can use the “AddressBookEntry” class as long as both of the classes reside in the same folder. You should design and provide the following public interfaces for the address book class. In addition, please provide constructors as you think appropriate. 

a. Add entry – will add a new address book entry at the end of the address book, and returns “true” if the entry is successfully added, “false” otherwise. 

b. Delete entry – will delete an existing valid entry from the address book. The client should supply an index number to this method that will specify which entry from the address book the client wants to delete. This method returns “true” if the deletion is successful, and returns “false” otherwise. Notice that the index must be a valid entry value form the address book. 

c. View all entries – will display the non-empty entries of the address book with their sequence number on the screen. For example: 

1. Name: Alex Address: 123 abc St. Kamloops Tel: 123-234-2192  Email: [email protected] 

2. Name: Dan Address: 231 XYZ St. Kamloops Tel: 223-222-2234  Email: [email protected] … 

  

d. Update an entry – will update a specified non-empty entry. (One way to design this method is that the client would supply an index number to update that entry, for example, if the method is called “updateEntry”, then to update the first entry of the address book client may call the method as follows: 

updateEntry(1, “Poly”, “123 McGill Rd. Kamloops”, “123-435-7532”, [email protected]); 

Ø Finally create a test client called “AddressBookTestClient” in another separate file that will have the main method and test the functionality of the address book for all the method. Make sure you test it for all possible boundary conditions in many possible ways. 

Calculate Your Essay Price
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more