For Java Assginmnent

For Java Assginmnent

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

0

6

4

2

0

6

  1. Take note that the entries 4, 2, 0, 6 repeat.
  2. Add to the last two digit of the year.
  3. Add to “the last two digit of the year divide by 4, truncate the fractional part”.
  4. Add to the number obtained from the following month table:
  5. Add to the day.
  6. 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: ”++”\t\t Address: “++” \t\t Tel: “+ + “\t\tEmail:” + ;

where , , and 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: alex@tru.ca
  2. Name: Dan Address: 231 XYZ St. Kamloops Tel: 223-222-2234 Email: dan@tru.ca …

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”, poly@tru.cs);

Ø 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.

Order a unique copy of this paper
(550 words)

Approximate price: $22

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


We value our customers, and so ensure that our papers are 100 percent original. Our Team of professionals does not miss the mark; they ensure that step by step each paper is written uniquely. We never duplicate or work as we compare papers rest assured. We deliver our work a day before time to ensure that you don’t miss your deadlines. It is not only doing the work but delivering it at the right time. We capture the consequences of late remittances. .

Money-back guarantee

We value customer satisfaction here at popularessaywriters.com and make sure that you get the best value for your Money. It happens that sometimes you can pay twice for your order or may want to cancel it, or you feel that it doesn’t meet your requirements; our money back guarantee will give you the opportunity to get back your money. We will also refund 100% of money paid double. In case your paper does not satisfy your requirements , we request that you notify us via writing within 2 days otherwise on the third day we will assume that you have been satisfied. Do all your correspondences through our email address popularessaywriters@gmail.com.

Read more

Zero-plagiarism guarantee

At popularessaywriters.com, our professional writers know the consequence plagiarism does for our clients. We have updated software’s such as article checker and copyscape to check for originality of the custom papers before submission of the final paper to the you. Our guarantee to the customer is that we will write 100% original papers for them that are quality, timely and of low cost. We have experienced professional and competent PhD writers who will write quality custom papers for you..

Read more

Free-revision policy

. At popularessaywriters.com, we are proud to provide top-quality Essay writing service to our esteemed customers. We are ready to take up that challenging academic assignment that is giving you sleepless nights and simplify it for you according to your desired requirements. We are willing to revise your paper if it does not meet your requirements. At popularessaywriters.com, we do not compromise with quality; thus, we offer unlimited free revisions until the customer is satisfied with their custom paper. Our unlimited free revision services are provided under the following terms:.. .

Read more

Privacy policy

Popularessawriters.com knows that client’s information is an essential tool for our company. It means that whatever the client requests from our service is kept strictly confidential. It means that whoever writes for this company understands the terms and conditions hence you should not be worried because you will never see your work somewhere else...

Read more

Fair-cooperation guarantee

Rest assured that we will always be attentive to your needs and requirements. We belief in the phrase treat your neighbour as you would want them to treat you. We leave nothing to chance and always look forward to a good interaction with each other.. .

Read more

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