2023 Exercise 1 10 points Write a static method named numUnique that accepts an array of integers | Assignment Collections

Computer Science 2023 Java Lab

2023 Exercise 1 10 points Write a static method named numUnique that accepts an array of integers | Assignment Collections

 

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

 

We give our students 100% satisfaction with their assignments, which is one of the most important reasons students prefer us to other helpers. Our professional group and planners have more than ten years of rich experience. The only reason is that we have successfully helped more than 100000 students with their assignments on our inception days. Our expert group has more than 2200 professionals in different topics, and that is not all; we get more than 300 jobs every day more than 90% of the assignment get the conversion for payment.

Place Order Now

#write essay #research paper #blog writing #article writing #academic writer #reflective paper #essay pro #types of essays #write my essay #reflective essay #paper writer #essay writing service #essay writer free #essay helper #write my paper #assignment writer #write my essay for me #write an essay for me #uk essay #thesis writer #dissertation writing services #writing a research paper #academic essay #dissertation help #easy essay #do my essay #paper writing service #buy essay #essay writing help #essay service #dissertation writing #online essay writer #write my paper for me #types of essay writing #essay writing website #write my essay for free #reflective report #type my essay #thesis writing services #write paper for me #research paper writing service #essay paper #professional essay writers #write my essay online #essay help online #write my research paper #dissertation writing help #websites that write papers for you for free #write my essay for me cheap #pay someone to write my paper #pay someone to write my research paper #Essaywriting #Academicwriting #Assignmenthelp #Nursingassignment #Nursinghomework #Psychologyassignment #Physicsassignment #Philosophyassignment #Religionassignment #History #Writing #writingtips #Students #universityassignment #onlinewriting #savvyessaywriters #onlineprowriters #assignmentcollection #excelsiorwriters #writinghub #study #exclusivewritings #myassignmentgeek #expertwriters #art #transcription #grammer #college #highschool #StudentsHelpingStudents #studentshirt #StudentShoe #StudentShoes #studentshoponline #studentshopping #studentshouse #StudentShoutout #studentshowcase2017 #StudentsHub #studentsieuczy #StudentsIn #studentsinberlin #studentsinbusiness #StudentsInDubai #studentsininternational