+ 1

about implementation of print_all method in a que

https://www.sololearn.com/compiler-playground/cv46qA30fM0D In the above code You will need to design and implement a Print_all() method that prints List elements in square brackets (use underscore for empty array entries e.g. [b][r][e][d][_][_]) output should be: (some code is written in R.H.S which we have to run) [b][r][e][d][_][_]) add(2,e) [b][r][e][e][d][_]) add(5,r) [b][r][e][e][d][r][_][_][_][_][_][_]) add(5,e) [b][r][e][e][d][e][r][_][_][_][_][_]) remove(4) [b][r][e][e][e][r][_][_][_][_][_][_]) remove(4) [b][r][e][e][r][_][_][_][_][_][_][_]) remove(4) [b][r][e][e][_][_][_][_]) set(2,i) [b][r][i][e][_][_][_][_]) [b][r][i][e][_][_][_][_][_][_][_][_]) if possible share the solution with your code and explanation

5th Feb 2023, 12:48 AM
RISHABH BHUTANI
RISHABH BHUTANI - avatar
12 odpowiedzi
+ 3
You mean the array's content must be displayed with the "add(2, 'e') commands? but why, is that necessary?
5th Feb 2023, 8:49 AM
Ipang
+ 2
Okay let me check your code to see what I can do. Can you tell me what commands were given for the last 2 rows of brace enclosed array elements in that output example? why the last one get more blank elements?
5th Feb 2023, 8:56 AM
Ipang
+ 2
I just checked the que, I mistyped....leave the last one.....que is till second last array
5th Feb 2023, 8:58 AM
RISHABH BHUTANI
RISHABH BHUTANI - avatar
+ 2
I can see the code by the link in post Description ...
5th Feb 2023, 9:04 AM
Ipang
+ 2
I think you need to review the add() method. In main() there is ... add(5, 'r'); But at that time, <occupancy> was only 4, thus it didn't work. Please also check whether it is necessary to call resize() in remove(). I'll try to look further ...
5th Feb 2023, 10:06 AM
Ipang
+ 1
No, add(2,e) is just the command....I just want the array to be printed after every command
5th Feb 2023, 8:51 AM
RISHABH BHUTANI
RISHABH BHUTANI - avatar
+ 1
Check this code once......Idk what's the issue and I'm getting the wrong output import java.util.*; public class AA2 { char[] a = new char[1]; int occupancy=0; public AA2(int length) { a = new char[length]; } public int Occupancy() { return occupancy; } public char get(int i) { return a[i]; } public char set(int i, char x) { char y = a[i]; a[i] = x; return y; } public void add(int i, char x) { if(occupancy+1>a.length) { resize(); } for(int j = occupancy; j>i; j--) { a[j]= a[j-1]; } a[i]=x; occupancy++; } public char remove(int i) { char x = a[i]; for(int j=i; j<occupancy-1; j++) { a[j] = a[j+1]; } occupancy--; if(a.length >= 3*occupancy) resize(); return x; } public void resize() { char[] b = new char
5th Feb 2023, 9:03 AM
RISHABH BHUTANI
RISHABH BHUTANI - avatar
5th Feb 2023, 9:08 AM
RISHABH BHUTANI
RISHABH BHUTANI - avatar
+ 1
I have just created the print_all method, rest all the code was in the question. section 2.1 of this book: http://opendatastructures.org/ods-java.pdf que is this......already did part1 but struck at part2 part1. Use C# to implement the ArrayStack List class based on the methods of Section 2.1 (of all three versions of the book). Your implementation, however, must differ from the one in the Java version of the book in the following ways: Instead of the generic type, make the data specifically of type char [10 points] Rename the global variable n to occupancy method "size()" to "Occupancy()" [10 points] Add your own explanatory comment after each semicolon. The quality of these comments will affect your grade [15 points] part2. Phase 2 [50 points] Create a Main() method that tests your implementation of the ArraStack List. You will need to design and implement a Print_all() method that prints List elements in square brackets (use underscore for empty array entries e.g. [b][r][e][d][_][_]). The test must mimic the sequence of actions depicted in Figure 2.1 (of all three versions of the book). To reach the initial state, however, you must follow a sequence of actions like the one depicted below: a <-- new array(6) a[0] <-- 'b' a[1] <-- 'r' a[2] <-- 'e' a[0] <-- 'd' n <-- 4 a <-- new array(1) n <-- 0 add(0, 'b') add(1, 'r') add(2, 'e') resize() add(3, 'e') You may implement any of the procedures depicted below, or create your own valid one, that will result in an array of a backing array size 6 and the correct List elements in place. Record another 40 - 60 seconds to present the result of your test starting from the status above. Use input statements to pause and control the run of the program. Design and implement Print_all() [15 points] Creating an appropriate initialization sequence in a correctly defined Main() method that com
5th Feb 2023, 4:14 PM
RISHABH BHUTANI
RISHABH BHUTANI - avatar
+ 1
we are adding add(2,6) first, at that time occupancy is occupancy is 4
5th Feb 2023, 4:39 PM
RISHABH BHUTANI
RISHABH BHUTANI - avatar
+ 1
Thanks for your effort ipang, I really appreciate that. here's the complete and correct code: import java.util.*; public class AA2 { char[] a = new char[1]; int occupancy=0; public AA2(int length) { a = new char[length]; } public int Occupancy() { return occupancy; } public char get(int i) { return a[i]; } public char set(int i, char x) { char y = a[i]; a[i] = x; return y; } public void add(int i, char x) { if(occupancy+1>a.length) { resize(); } for(int j = occupancy; j>i; j--) { a[j]= a[j-1]; } a[i]=x; occupancy++; } public char remove(int i) { char x = a[i]; for(int j=i; j<occupancy-1; j++) { a[j] = a[j+1]; } occupancy--; if(a.length >= 3*occupancy) resize(); return x; } public void resize() { char[] b = new char[(Math.max(occupancy*2,1))]; for(int i=0; i<occupancy; i++){ b[i]= a[i]; } a = b; } public void print_all() { for (int i = 0; i < occupancy; i++) { System.out.print("["+ a[i] + "]"); } for (int i = occupancy; i < a.length; i++) { System.out.print("[_]"); } System.out.println(); } public static void main(String[] args) { AA2 aa2 = new AA2(6); aa2.add(0, 'b'); aa2.add(1, 'r'); aa2.add(2, 'e'); aa2.add(3, 'd'); aa2.print_all(); aa2.add(2, 'e'); aa2.print_all(); aa2.add(5, 'r'); aa2.print_all(); aa2.add(5, 'e'); aa2.print_all(); aa2.remove(4); aa2.print_all(); aa2.remove(4); aa2.print_all(); aa2.remove(4); aa2.print_all(); aa2.set(2, 'i'); aa2.print_all(); aa2.add(4, 'e'); aa2.print_all(); } }
5th Feb 2023, 5:24 PM
RISHABH BHUTANI
RISHABH BHUTANI - avatar
+ 1
Alright bro, good job for solving the problem 👍
5th Feb 2023, 5:36 PM
Ipang