[SOLVED] CS1010J Program 1

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon 20s1-ARR-ov7dvy.zip (271.8 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
๐Ÿ”’ Securely Powered by:
Secure Checkout
5/5 - (2 votes)

Exercise 1: An Array of Integers

Given an array of distinct positive integers and another positive integer ๐‘ก๐‘ก, perform the following three tasks.

1. Report the largest difference between adjacent integers in the array.

For example, if the array is {13, 3, 4, 21}, the largest difference between neighbours is 17 (= 21 – 4). Note that the difference should be reported as a positive integer.

2. Report the number of tough numbers in the array. Atoughnumberisaninteger๐‘›๐‘›๐‘›๐‘›๐‘›๐‘›oftheform๐‘›๐‘›๐‘›๐‘›๐‘›๐‘›=๐‘˜๐‘˜โˆ—2 +1,where๐‘˜๐‘˜and๐‘›๐‘›are

positive integers, ๐‘˜๐‘˜ is odd, and ๐‘˜๐‘˜ < 2๐‘›๐‘›. A few examples are shown below.

๐‘›๐‘›

  • 3isatoughnumberbecause3=1*21 +1and1<21.
  • 4 is NOT a tough number.
  • 13isatoughnumberbecause13=3*22 +1and3<22.
  • 21isNOTatoughnumber.Itcanbeexpressedas21=5*22 +1but5>22.

Consequently, the array {13, 3, 4, 21} contains 2 tough numbers. 3. Report the number of smiling numbers in the array.

A smiling number is a positive integer that has the sum of its digits equals to a given positive integer ๐‘ก๐‘ก.

For example, if the array is {13, 3, 4, 21} and ๐‘ก๐‘ก = 4, there are 2 smiling numbers in the array: 13 and 4; the sum of digits of each of them is 4.

 

In the given skeleton program ArrayOfInts.java, complete the following 4 methods.

  1. 1) ย int maxDiff(int[] arr) that returns the largest difference between adjacent

    integers in arr.

  2. 2) ย boolean isToughNum(int num) that checks if num is a tough number. It

    returns true if so, or false otherwise.

  3. 3) ย int countToughNum(int[] arr) that returns the number of tough numbers

    in arr.

  4. 4) ย int countSmilingNum(int[] arr, int t) that returns the number of smiling numbers in the array, i.e. the number of array elements that have the sum of their digits equals to t.

You may assume that arr contains at least 2 elements. Moreover, it doesnโ€™t contain duplicate elements.

Take note of the following additional instructions/restrictions:

  1. You are not allowed to change the main() method or method headers of other given methods.
  2. Your methods may invoke each other and you may create additional arrays or write additional methods as necessary.

Two sample runs of the program are shown below with the user’s input shown in bold. Sample run #1

Sample run #2

Enter the size of array: 4 Enter the array: 13 3 4 21 Maximum difference = 17 Number of tough numbers = 2 Enter t: 4

Number of smiling numbers = 2

Enter the size of array: 5

Enter the array: 11 14 23 9 5

Maximum difference = 14

Number of tough numbers = 2 Enter t: 5

Number of smiling numbers = 3

 

Exercise 2: Employees

A company stores employee information in a table. One example is shown in Table 1 below. Each row of the table shows the information of one employee and it contains three fields: staff ID, age and salary. You may assume that all three fields are positive integers, staff ID is unique to each employee, and age is between 18 and 67, both ends inclusive.

ID

Age

Salary

111

35

4500

112

27

6600

113

25

2000

114

60

10000

115

60

6000

117

35

5000

118

60

30000

119

27

6600

122

40

4000

123

35

3400

Table 1. Records of 10 employees

Task 1

Suppose the above table is implemented as a two-dimensional (2D) array in your program. The company would like you to sort the 2D array by age in non-decreasing order, and for those employees with the same age, further sort by salary in non-decreasing order. If two employees have the same age and salary, their relative order in the original table should be preserved. Table 2 below shows the result of sorting Table 1 according to the stated criteria.

ID

Age

Salary

113

25

2000

112

27

6600

119

27

6600

123

35

3400

111

35

4500

117

35

5000

122

40

4000

115

60

6000

114

60

10000

118

60

30000

Table 2. Records of 10 employees after sorting

 

  1. 1) ย Write a method void swap(int[][] employees, int row1, int row2) that swaps two rows, row1 and row2, of the employees array.
  2. 2) ย Write a method void sort(int[][] employees) that sorts the employees array according to the criteria stated on the previous page. You may choose selection sort, bubble sort or enhanced bubble sort covered in CS1010J. However, you are not allowed to call Java sorting APIs (e.g. you cannot use Arrays.sort()).

Task 2

The company is interested to know which age most employees are of (i.e. most common age).

3) Write a method int[][] mostCommonAge(int[][] sortedEmployees) that checks which age is most common among employees. Note that there may be more than one most common age (see example below). The method then stores respective employees in a new 2D array and returns this new array. Respective employees should be stored in the new 2D array in the same order they appear in the parameter sortedEmployees array. You may assume that the company has at least one employee. Note that sortedEmployees array is already sorted in Task 1.

For the example shown in Table 2 on the previous page, there are two most common ages: 35 and 60. Hence the mostCommonAge() method will return a new array containing the following six employees.

ID

Age

Salary

123

35

3400

111

35

4500

117

35

5000

115

60

6000

114

60

10000

118

60

30000

Table 3. Two ages with the most number of employees

Complete the skeleton program Employees.java for the above two tasks. Take note of the following additional instructions/restrictions:

  1. main(), readInput() and printArray() methods are complete and given. You are not allowed to change them.
  2. You are not allowed to change the method headers of other given methods.
  3. Your methods may invoke each other and you may create additional arrays or write

    additional methods as necessary.

CS1010J Programming Methodology – 5 of 7 – Practical Assessment

Two sample runs of the program are given below with the user’s input shown in bold. For your convenience of testing, test inputs are also appended to the back of the skeleton program.

Sample run #1

Enter the number of rows: 4 Enter data:
114 60 10000
115 60 6000

117 35 5000
118 60 30000
Sorted by age, then by salary:
117 35 5000
115 60 6000
114 60 10000
118 60 30000
Employees with the most common age:

115 60
114 60
118 60
6000
10000
30000

(Sample run #2 on the next page)

CS1010J Programming Methodology – 6 of 7 – Practical Assessment

Sample run #2

Enter the number of rows: 10 Enter data:
111 35 4500
112 27 6600

113 25 2000
114 60 10000
115 60 6000
117 35 5000
118 60 30000
119 27 6600
122 40 4000
123 35 3400
Sorted by age, then by salary: 113 25 2000

112 27   6600
119 27   6600
123 35   3400
111 35   4500
117 35   5000
122 40   4000
115 60   6000
114 60   10000
118 60   30000
Employees with the most common age:
123 35   3400
111 35   4500
117 35   5000
115 60   6000
114 60   10000
118 60   30000

 

  • 20s1-ARR-ov7dvy.zip