Question: Save the following java program into xxxxxH3.java and complete insertsorted and searchsorted methods and in main method write your name in line 126. Based on

Save the following java program into xxxxxH3.java and complete insertsorted and searchsorted methods and in main method write your name in line 126. Based on process method, create input and save it in input.txt file.

1

2 // Array implementation of sorted list: xxxxxH3.java

3

4 import java.util.*;

5 import java.io.*;

6 public class xxxxxH3{

7 // class Variables

8 private int n, count = 0;

9 String arr[];

10

11 //use a short word for prt to save typing

12 PrintStream prt = System.out;

13

14 //insert x in a sorted list if list is not full

15 public int insertsorted(String x){

16 int p, j;

17 prt.printf(" \t\tInsert %s in a sorted list:", x);

18 if (count == n) return 0;//list is full

19 // complete the rest

20 return 1;

21 } // end insertsorted

22

23 //sequential serach for x in a sorted list

24 // if successful return position of x in the

25 // sorted list otherwise return -1;

26 public int searchsorted(String x){

27 prt.printf(" \t\tSearch for %s in a sorted list:", x);

28 if (count == 0) return -1; //list is empty

29 // complete the rest

30 return -1;

31 } // end searchsorted

32

33 //delete x from a sorted list

34 public int deletesorted(String x){

35 prt.printf(" \t\tDelete %s from a sorted list:", x);

36 if (count == 0) return 0;//list is empty

37 int i, p = searchsorted(x);

38

39 if (p == -1) return 0; //x is not found

40

41 //Shift to left array from position p

42 for (i = p ; i < count-1 ; i++)

43 arr[i] = arr[i+1];

44 // end for

45 count--;

46 return 1; //successful deletion

47 } // end delete

48

49 // print list elements formatted

50 public void printlist(){

51 int i;

52 prt.print(" \tList contents: ");

53 for (i = 0; i < count; i++)

54 prt.printf("%s, ", arr[i]);

55 // enf for

56 } // end printlist

57

58 // insert, delete and search in the list

59 private void process(){

60 int j, m, k, p, s;

61 String x;

62 prt.print("\tArray implementation of sorted list. This program first reads:n array

size," +

63 " \tNo. of elements to insert, followed by elements to insert," + 64 " \tNo. of elements to search, followed by elements to search," + 65 " \tNo. of elements to delete, followed by elements to delete" + 66 " \t\tTo compile: javac xxxxxH3.java" +

67 " \t\tTo execute: java xxxxxH3 < any data file");

68 // open input file

69 Scanner inf = new Scanner(System.in);

70 try{

71 //read array size

72 n = inf.nextInt();

73 //read no. of elements to insert

74 m = inf.nextInt();

75 // Allocate Space for array

76 arr = new String[n];//

77 prt.printf(" \tCreating array of size %4d:",n);

78 prt.printf(" \tInsert %2d elements in the sorted list.", m); 79 for(j = 1; j <= m; j++){

80 x = inf.next();//read x to insert

81 s = insertsorted(x); //insert x

82 if (s == 1)

83 prt.printf(" Successful insertion.");

84 else

85 prt.printf(" Failed insertion.");

86 } // end for

87 printlist();//print linked list elements

88

89 //read no. of elements to search in list

90 m = inf.nextInt();

91 prt.printf(" \tSearch for %d elements in sorted list.", m); 92 for(j = 1; j <= m; j++){

93 x = inf.next(); // read x to serach

94 s = searchsorted(x); //delete x

95 if (s >= 0)

96 prt.printf(" found at position %d", s);

97 else

98 prt.printf(" not found.");

99 }// end for

100

101 //read no. of positions to delete from list

102 m = inf.nextInt();

103 prt.printf(" \tDelete %d elements from sorted list.", m); 104 for(j = 1; j <= m; j++){

105 x = inf.next(); // read x to delete

106 s = deletesorted(x);//delete x

107 if (s == 1)

108 prt.printf(" Successful deletion");

109 else

110 prt.printf(" Failed deletion.");

111 }// end for

112 printlist();//print list elements

113

114 // close input file

115 inf.close();

116 } catch (Exception e){

117 prt.print(" Exception " + e + " ");}

118 } // end process

119

120 // main method

121 public static void main(String args[]) throws Exception{ 122 xxxxxH3 lst = new xxxxxH3();

123 lst.process();

124

125 //MAKE SURE TO WRITE YOUR NAME IN NEXT LINE 126 System.out.printf(" \tAuthor: Name Date: " + 127 java.time.LocalDate.now());

128 } // end main

129 } // end class xxxxxH3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!