SlideShare a Scribd company logo
Page 1 of 2 Updated on: 2020-01-23
CPS 151/Spring 2020 In-class Assignment 1
Textbook reference: See discussion of how to insert (add) and
remove (delete) an item in a partially filled
array while maintaining order of existing items (subsections
6.3.6, 6.3.7). The concept of a partially filled array
is discussed in subsection 6.1.3.
The program is supposed to:
a) allocate storage for an array of integers of a size specified by
the user
b) fill the array partially with increasing values (number of
values chosen by the user)
c) check that the item at a user specified index position can be
removed, maintaining order of other elements
d) check that a new item can be added at a user specified index
position, also maintaining order of other
elements.
Study the startup code and complete the program.
You need to complete the validity checks for index position
specified by the user. Note that only existing items
may be removed, but a new item could be added right after the
existing items in the list.
Sample output from startup code
Page 2 of 2 Updated on: 2020-01-23
Sample Output (successful insert/delete)
Sample output with invalid delete/insert positions
final static Scanner cin = new Scanner(System.in);
static int currentSize; // number of values actually in the
intList
static int[] intList; // reference to the partially filled array
storage
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
out.println("CPS 151 ICA 1 by
_________________________");
setup();
printList(intList, "nOriginal List");
checkDeletion();
checkInsertion();
printList(intList, "nFinal List");
out.println("nGoodbye");
} // end main
private static void checkDeletion() {
// Checking deletion
int position = getInt("nPosition to delete from: ");
// check validity of position
if (2 + 2 == 5) {
shiftUp(position);
currentSize--;
printList(intList, "nList after deletion");
} else {
out.println("Invalid delete position, no changes made");
} // end if
} // end method
private static void checkInsertion() {
// Checking insertion
int value = getInt("nValue to insert: ");
int position = getInt("At what position? ");
// check validity of position
if (2 + 2 == 5) {
shiftDown(position);
intList[position] = value;
currentSize++;
printList(intList, "nList after insertion");
} else {
out.println("Invalid insert position, no changes made");
} // end if
} // end method
// fills array with increasing values
private static void fillArrayInc(final int startValue, final int
howMany) {
// Validity check
if (howMany < 1 || howMany > intList.length) {
terminate("fillArrayInc: illegal argument, howMany = "
+ howMany);
}
for (int k = 0; k < howMany; k++) {
intList[k] = startValue + k;
}
currentSize = howMany;
} //end setSequenced
// prints partially filled array with a legend
private static void printList(final int[] arr, final String
legend) {
out.println(legend);
for (int k = 0; k < currentSize; k++) {
out.print(" " + arr[k]);
}
out.println();
} // end printList
// move items from pos+1:currentSize-1 one position up
(lower subscripts)
private static void shiftUp(int pos) {
// Write the code
} // end shiftUp
// move items from pos:currentSize-1 one position down
(higher subscripts)
private static void shiftDown(int pos) {
// Write the code
} // end shiftDown
private static void setup() {
int maxSize, initSize;
maxSize = getInt("Enter the maximum size: ");
intList = new int[maxSize];
initSize = getInt("Enter the starting size: ");
if (initSize > maxSize) {
terminate("starting size cannot be greater than
maximum size");
}
fillArrayInc(100, initSize);
} // end method
private static int getInt(String prompt) {
out.print(prompt);
return cin.nextInt();
} // end method
private static void terminate(String message) {
out.println("Error: " + message);
exit(0);
} // end terminate

More Related Content

Similar to Page 1 of 2 Updated on 2020-01-23 CPS 151Spring 2020 .docx

public class DoubleArraySeq implements Cloneable {    Priva.pdf
public class DoubleArraySeq implements Cloneable {     Priva.pdfpublic class DoubleArraySeq implements Cloneable {     Priva.pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdf
annaimobiles
 
Why following sort does not work (It does not sort last 2 - 3 numbe.pdf
Why following sort does not work (It does not sort last 2 - 3 numbe.pdfWhy following sort does not work (It does not sort last 2 - 3 numbe.pdf
Why following sort does not work (It does not sort last 2 - 3 numbe.pdf
gopalk44
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
kostikjaylonshaewe47
 
project2.classpathproject2.project project2 .docx
project2.classpathproject2.project  project2 .docxproject2.classpathproject2.project  project2 .docx
project2.classpathproject2.project project2 .docx
briancrawford30935
 
Given the following errors and class in Java- How are these errors fix.pdf
Given the following errors and class in Java- How are these errors fix.pdfGiven the following errors and class in Java- How are these errors fix.pdf
Given the following errors and class in Java- How are these errors fix.pdf
NicholasflqStewartl
 
Create a Dynamic Array container with this user interface Ge.pdf
Create a Dynamic Array container with this user interface  Ge.pdfCreate a Dynamic Array container with this user interface  Ge.pdf
Create a Dynamic Array container with this user interface Ge.pdf
sktambifortune
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
aromanets
 
Ecet 370 Education Organization -- snaptutorial.com
Ecet 370   Education Organization -- snaptutorial.comEcet 370   Education Organization -- snaptutorial.com
Ecet 370 Education Organization -- snaptutorial.com
DavisMurphyB81
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
dickonsondorris
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
Soluto
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
Jafar Nesargi
 
Should be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdfShould be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdf
fashionscollect
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
almaniaeyewear
 
ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com
donaldzs157
 
This file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfThis file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdf
deepaksatrker
 
New text document
New text documentNew text document
New text document
mirzaaabdulmanan
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
amazing2001
 
this file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfthis file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdf
flashfashioncasualwe
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
Mavoori Soshmitha
 

Similar to Page 1 of 2 Updated on 2020-01-23 CPS 151Spring 2020 .docx (20)

public class DoubleArraySeq implements Cloneable {    Priva.pdf
public class DoubleArraySeq implements Cloneable {     Priva.pdfpublic class DoubleArraySeq implements Cloneable {     Priva.pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdf
 
Why following sort does not work (It does not sort last 2 - 3 numbe.pdf
Why following sort does not work (It does not sort last 2 - 3 numbe.pdfWhy following sort does not work (It does not sort last 2 - 3 numbe.pdf
Why following sort does not work (It does not sort last 2 - 3 numbe.pdf
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
 
project2.classpathproject2.project project2 .docx
project2.classpathproject2.project  project2 .docxproject2.classpathproject2.project  project2 .docx
project2.classpathproject2.project project2 .docx
 
Given the following errors and class in Java- How are these errors fix.pdf
Given the following errors and class in Java- How are these errors fix.pdfGiven the following errors and class in Java- How are these errors fix.pdf
Given the following errors and class in Java- How are these errors fix.pdf
 
Create a Dynamic Array container with this user interface Ge.pdf
Create a Dynamic Array container with this user interface  Ge.pdfCreate a Dynamic Array container with this user interface  Ge.pdf
Create a Dynamic Array container with this user interface Ge.pdf
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Ecet 370 Education Organization -- snaptutorial.com
Ecet 370   Education Organization -- snaptutorial.comEcet 370   Education Organization -- snaptutorial.com
Ecet 370 Education Organization -- snaptutorial.com
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Should be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdfShould be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdf
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com
 
This file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfThis file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdf
 
New text document
New text documentNew text document
New text document
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
this file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfthis file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdf
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
 

More from karlhennesey

Resources Assigned readings, ERRs, the Internet,and other resources.docx
Resources Assigned readings, ERRs, the Internet,and other resources.docxResources Assigned readings, ERRs, the Internet,and other resources.docx
Resources Assigned readings, ERRs, the Internet,and other resources.docx
karlhennesey
 
Resource Review Documenting the Face of America Roy Stryker and.docx
Resource Review Documenting the Face of America Roy Stryker and.docxResource Review Documenting the Face of America Roy Stryker and.docx
Resource Review Documenting the Face of America Roy Stryker and.docx
karlhennesey
 
Resource Review Thelma Golden--How Art Gives Shape to Cultural C.docx
Resource Review Thelma Golden--How Art Gives Shape to Cultural C.docxResource Review Thelma Golden--How Art Gives Shape to Cultural C.docx
Resource Review Thelma Golden--How Art Gives Shape to Cultural C.docx
karlhennesey
 
Resource Review Representational Cityscape, and Ch. 3 of Oxfo.docx
Resource Review Representational Cityscape, and Ch. 3 of Oxfo.docxResource Review Representational Cityscape, and Ch. 3 of Oxfo.docx
Resource Review Representational Cityscape, and Ch. 3 of Oxfo.docx
karlhennesey
 
Resource Part 2 of Terrorism TodayYou work on a national se.docx
Resource Part 2 of Terrorism TodayYou work on a national se.docxResource Part 2 of Terrorism TodayYou work on a national se.docx
Resource Part 2 of Terrorism TodayYou work on a national se.docx
karlhennesey
 
Resources Appendix A, The Home Depot, Inc. Annual Report in Fun.docx
Resources Appendix A, The Home Depot, Inc. Annual Report in Fun.docxResources Appendix A, The Home Depot, Inc. Annual Report in Fun.docx
Resources Appendix A, The Home Depot, Inc. Annual Report in Fun.docx
karlhennesey
 
Resources Annotated Bibliography document. Research five websites t.docx
Resources Annotated Bibliography document. Research five websites t.docxResources Annotated Bibliography document. Research five websites t.docx
Resources Annotated Bibliography document. Research five websites t.docx
karlhennesey
 
Resources American History, Primary Source Investigator;Cente.docx
Resources American History, Primary Source Investigator;Cente.docxResources American History, Primary Source Investigator;Cente.docx
Resources American History, Primary Source Investigator;Cente.docx
karlhennesey
 
Resource University of Phoenix Material Data SetDownload the.docx
Resource University of Phoenix Material Data SetDownload the.docxResource University of Phoenix Material Data SetDownload the.docx
Resource University of Phoenix Material Data SetDownload the.docx
karlhennesey
 
Resource Ch. 6 & 7 of Financial AccountingComplete Brief Ex.docx
Resource Ch. 6 & 7 of Financial AccountingComplete Brief Ex.docxResource Ch. 6 & 7 of Financial AccountingComplete Brief Ex.docx
Resource Ch. 6 & 7 of Financial AccountingComplete Brief Ex.docx
karlhennesey
 
Resource Films on DemandCrime and Punishment”Experiment Res.docx
Resource Films on DemandCrime and Punishment”Experiment Res.docxResource Films on DemandCrime and Punishment”Experiment Res.docx
Resource Films on DemandCrime and Punishment”Experiment Res.docx
karlhennesey
 
Resource Managing Environmental Issues Simulation(or research a.docx
Resource Managing Environmental Issues Simulation(or research a.docxResource Managing Environmental Issues Simulation(or research a.docx
Resource Managing Environmental Issues Simulation(or research a.docx
karlhennesey
 
Resource Ch. 9 of Introduction to Business Create a 5-to-7 slide .docx
Resource Ch. 9 of Introduction to Business Create a 5-to-7 slide .docxResource Ch. 9 of Introduction to Business Create a 5-to-7 slide .docx
Resource Ch. 9 of Introduction to Business Create a 5-to-7 slide .docx
karlhennesey
 
Resource Ch. 9 of Introduction to Business Complete the table in .docx
Resource Ch. 9 of Introduction to Business Complete the table in .docxResource Ch. 9 of Introduction to Business Complete the table in .docx
Resource Ch. 9 of Introduction to Business Complete the table in .docx
karlhennesey
 
Resource Ch. 3 of ManagementIdentify a time in your life wh.docx
Resource Ch. 3 of ManagementIdentify a time in your life wh.docxResource Ch. 3 of ManagementIdentify a time in your life wh.docx
Resource Ch. 3 of ManagementIdentify a time in your life wh.docx
karlhennesey
 
Resource Significant Health Care Event Paper Grading Criteria.docx
Resource Significant Health Care Event Paper Grading Criteria.docxResource Significant Health Care Event Paper Grading Criteria.docx
Resource Significant Health Care Event Paper Grading Criteria.docx
karlhennesey
 
Resource Ch. 3 of Financial AccountingComplete Exercises E3.docx
Resource Ch. 3 of Financial AccountingComplete Exercises E3.docxResource Ch. 3 of Financial AccountingComplete Exercises E3.docx
Resource Ch. 3 of Financial AccountingComplete Exercises E3.docx
karlhennesey
 
Resource University of Phoenix Material Appendix AIdentify.docx
Resource University of Phoenix Material Appendix AIdentify.docxResource University of Phoenix Material Appendix AIdentify.docx
Resource University of Phoenix Material Appendix AIdentify.docx
karlhennesey
 
Resource The Threat of Bioterrorism VideoWrite a 700 to 850-w.docx
Resource The Threat of Bioterrorism VideoWrite a 700 to 850-w.docxResource The Threat of Bioterrorism VideoWrite a 700 to 850-w.docx
Resource The Threat of Bioterrorism VideoWrite a 700 to 850-w.docx
karlhennesey
 
Resource Ch. 14 of Introduction to Psychology Create an 8 to 12 s.docx
Resource Ch. 14 of Introduction to Psychology Create an 8 to 12 s.docxResource Ch. 14 of Introduction to Psychology Create an 8 to 12 s.docx
Resource Ch. 14 of Introduction to Psychology Create an 8 to 12 s.docx
karlhennesey
 

More from karlhennesey (20)

Resources Assigned readings, ERRs, the Internet,and other resources.docx
Resources Assigned readings, ERRs, the Internet,and other resources.docxResources Assigned readings, ERRs, the Internet,and other resources.docx
Resources Assigned readings, ERRs, the Internet,and other resources.docx
 
Resource Review Documenting the Face of America Roy Stryker and.docx
Resource Review Documenting the Face of America Roy Stryker and.docxResource Review Documenting the Face of America Roy Stryker and.docx
Resource Review Documenting the Face of America Roy Stryker and.docx
 
Resource Review Thelma Golden--How Art Gives Shape to Cultural C.docx
Resource Review Thelma Golden--How Art Gives Shape to Cultural C.docxResource Review Thelma Golden--How Art Gives Shape to Cultural C.docx
Resource Review Thelma Golden--How Art Gives Shape to Cultural C.docx
 
Resource Review Representational Cityscape, and Ch. 3 of Oxfo.docx
Resource Review Representational Cityscape, and Ch. 3 of Oxfo.docxResource Review Representational Cityscape, and Ch. 3 of Oxfo.docx
Resource Review Representational Cityscape, and Ch. 3 of Oxfo.docx
 
Resource Part 2 of Terrorism TodayYou work on a national se.docx
Resource Part 2 of Terrorism TodayYou work on a national se.docxResource Part 2 of Terrorism TodayYou work on a national se.docx
Resource Part 2 of Terrorism TodayYou work on a national se.docx
 
Resources Appendix A, The Home Depot, Inc. Annual Report in Fun.docx
Resources Appendix A, The Home Depot, Inc. Annual Report in Fun.docxResources Appendix A, The Home Depot, Inc. Annual Report in Fun.docx
Resources Appendix A, The Home Depot, Inc. Annual Report in Fun.docx
 
Resources Annotated Bibliography document. Research five websites t.docx
Resources Annotated Bibliography document. Research five websites t.docxResources Annotated Bibliography document. Research five websites t.docx
Resources Annotated Bibliography document. Research five websites t.docx
 
Resources American History, Primary Source Investigator;Cente.docx
Resources American History, Primary Source Investigator;Cente.docxResources American History, Primary Source Investigator;Cente.docx
Resources American History, Primary Source Investigator;Cente.docx
 
Resource University of Phoenix Material Data SetDownload the.docx
Resource University of Phoenix Material Data SetDownload the.docxResource University of Phoenix Material Data SetDownload the.docx
Resource University of Phoenix Material Data SetDownload the.docx
 
Resource Ch. 6 & 7 of Financial AccountingComplete Brief Ex.docx
Resource Ch. 6 & 7 of Financial AccountingComplete Brief Ex.docxResource Ch. 6 & 7 of Financial AccountingComplete Brief Ex.docx
Resource Ch. 6 & 7 of Financial AccountingComplete Brief Ex.docx
 
Resource Films on DemandCrime and Punishment”Experiment Res.docx
Resource Films on DemandCrime and Punishment”Experiment Res.docxResource Films on DemandCrime and Punishment”Experiment Res.docx
Resource Films on DemandCrime and Punishment”Experiment Res.docx
 
Resource Managing Environmental Issues Simulation(or research a.docx
Resource Managing Environmental Issues Simulation(or research a.docxResource Managing Environmental Issues Simulation(or research a.docx
Resource Managing Environmental Issues Simulation(or research a.docx
 
Resource Ch. 9 of Introduction to Business Create a 5-to-7 slide .docx
Resource Ch. 9 of Introduction to Business Create a 5-to-7 slide .docxResource Ch. 9 of Introduction to Business Create a 5-to-7 slide .docx
Resource Ch. 9 of Introduction to Business Create a 5-to-7 slide .docx
 
Resource Ch. 9 of Introduction to Business Complete the table in .docx
Resource Ch. 9 of Introduction to Business Complete the table in .docxResource Ch. 9 of Introduction to Business Complete the table in .docx
Resource Ch. 9 of Introduction to Business Complete the table in .docx
 
Resource Ch. 3 of ManagementIdentify a time in your life wh.docx
Resource Ch. 3 of ManagementIdentify a time in your life wh.docxResource Ch. 3 of ManagementIdentify a time in your life wh.docx
Resource Ch. 3 of ManagementIdentify a time in your life wh.docx
 
Resource Significant Health Care Event Paper Grading Criteria.docx
Resource Significant Health Care Event Paper Grading Criteria.docxResource Significant Health Care Event Paper Grading Criteria.docx
Resource Significant Health Care Event Paper Grading Criteria.docx
 
Resource Ch. 3 of Financial AccountingComplete Exercises E3.docx
Resource Ch. 3 of Financial AccountingComplete Exercises E3.docxResource Ch. 3 of Financial AccountingComplete Exercises E3.docx
Resource Ch. 3 of Financial AccountingComplete Exercises E3.docx
 
Resource University of Phoenix Material Appendix AIdentify.docx
Resource University of Phoenix Material Appendix AIdentify.docxResource University of Phoenix Material Appendix AIdentify.docx
Resource University of Phoenix Material Appendix AIdentify.docx
 
Resource The Threat of Bioterrorism VideoWrite a 700 to 850-w.docx
Resource The Threat of Bioterrorism VideoWrite a 700 to 850-w.docxResource The Threat of Bioterrorism VideoWrite a 700 to 850-w.docx
Resource The Threat of Bioterrorism VideoWrite a 700 to 850-w.docx
 
Resource Ch. 14 of Introduction to Psychology Create an 8 to 12 s.docx
Resource Ch. 14 of Introduction to Psychology Create an 8 to 12 s.docxResource Ch. 14 of Introduction to Psychology Create an 8 to 12 s.docx
Resource Ch. 14 of Introduction to Psychology Create an 8 to 12 s.docx
 

Recently uploaded

Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 

Recently uploaded (20)

Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 

Page 1 of 2 Updated on 2020-01-23 CPS 151Spring 2020 .docx

  • 1. Page 1 of 2 Updated on: 2020-01-23 CPS 151/Spring 2020 In-class Assignment 1 Textbook reference: See discussion of how to insert (add) and remove (delete) an item in a partially filled array while maintaining order of existing items (subsections 6.3.6, 6.3.7). The concept of a partially filled array is discussed in subsection 6.1.3. The program is supposed to: a) allocate storage for an array of integers of a size specified by the user b) fill the array partially with increasing values (number of values chosen by the user) c) check that the item at a user specified index position can be removed, maintaining order of other elements d) check that a new item can be added at a user specified index position, also maintaining order of other elements.
  • 2. Study the startup code and complete the program. You need to complete the validity checks for index position specified by the user. Note that only existing items may be removed, but a new item could be added right after the existing items in the list. Sample output from startup code Page 2 of 2 Updated on: 2020-01-23 Sample Output (successful insert/delete) Sample output with invalid delete/insert positions final static Scanner cin = new Scanner(System.in); static int currentSize; // number of values actually in the intList static int[] intList; // reference to the partially filled array storage
  • 3. /** * @param args the command line arguments */ public static void main(String[] args) { out.println("CPS 151 ICA 1 by _________________________"); setup(); printList(intList, "nOriginal List"); checkDeletion(); checkInsertion(); printList(intList, "nFinal List"); out.println("nGoodbye"); } // end main private static void checkDeletion() { // Checking deletion int position = getInt("nPosition to delete from: ");
  • 4. // check validity of position if (2 + 2 == 5) { shiftUp(position); currentSize--; printList(intList, "nList after deletion"); } else { out.println("Invalid delete position, no changes made"); } // end if } // end method private static void checkInsertion() { // Checking insertion int value = getInt("nValue to insert: "); int position = getInt("At what position? "); // check validity of position if (2 + 2 == 5) { shiftDown(position);
  • 5. intList[position] = value; currentSize++; printList(intList, "nList after insertion"); } else { out.println("Invalid insert position, no changes made"); } // end if } // end method // fills array with increasing values private static void fillArrayInc(final int startValue, final int howMany) { // Validity check if (howMany < 1 || howMany > intList.length) { terminate("fillArrayInc: illegal argument, howMany = " + howMany); } for (int k = 0; k < howMany; k++) { intList[k] = startValue + k;
  • 6. } currentSize = howMany; } //end setSequenced // prints partially filled array with a legend private static void printList(final int[] arr, final String legend) { out.println(legend); for (int k = 0; k < currentSize; k++) { out.print(" " + arr[k]); } out.println(); } // end printList // move items from pos+1:currentSize-1 one position up (lower subscripts) private static void shiftUp(int pos) { // Write the code
  • 7. } // end shiftUp // move items from pos:currentSize-1 one position down (higher subscripts) private static void shiftDown(int pos) { // Write the code } // end shiftDown private static void setup() { int maxSize, initSize; maxSize = getInt("Enter the maximum size: "); intList = new int[maxSize]; initSize = getInt("Enter the starting size: "); if (initSize > maxSize) { terminate("starting size cannot be greater than maximum size"); } fillArrayInc(100, initSize);
  • 8. } // end method private static int getInt(String prompt) { out.print(prompt); return cin.nextInt(); } // end method private static void terminate(String message) { out.println("Error: " + message); exit(0); } // end terminate