SlideShare a Scribd company logo
Objectives In this lab you will review passing arrays to methods and partially filled arrays.
Requirements 1. Fill an array with data from an input file sampledata-io.txt (Attached) a.
Assume no more than 100 data items, terminated by -1 for sentinel b. Note that the sample data
file has other information after the sentinel; it should cause no problem c. Read and store data d.
Print the number of data stored in the array e. Add a method to reverse the array. Pass the
original array as the argument and return the reversed array. 2. Testing a. Invoke the reverse
method, print out the reversed array. 3. Inserting into the partially filled array a. Add a method to
insert a user inputted value into a specific location. b. Remember to check parameters for
validity. Also remember to check whether the array is full or not. c. Invoke the insert method,
prompt to user whether the insertion is successful or not. 4. Removing from the partially filled
array a. Add a method to remove the element at the specific location in the partially filled array.
b. Invoke the remove method, prompt to user whether the remove is successful or not. If the
remove is successful, print out the value of the element just removed. There are several
important points that are general requirements for labs in this course: Include a comment at the
beginning of each source file you submit that includes your name and the lab date. Names for
variables and other program components should be chosen to help convey the meaning of the
variable. Turn in an archive of the entire Eclipse project for each lab. Do not attempt to turn in
individual files, some course management systems mangle such files.
Solution
package myProject;
import java.util.*;
import java.io.File;
//Create a class Array Operation
public class ArrayOperation
{
//Initializes the counter to zero
int counter = 0;
//Method to read data from file
void readData(int numberArray[])
{
//File object created
File file = new File("D:/TODAY/src/myProject/data.txt");
//Handles exception
try
{
//Scanner object created
Scanner scanner = new Scanner(file);
//Checks for the data availability
while(scanner.hasNextInt())
{
//Reads a number from the file
int no = scanner.nextInt();
//Checks if the number is -1 then stop reading
if(no == -1)
break;
//Stores the number in the array
numberArray[counter++] = no;
}//End of while
}//End of try
//Catch block
catch(Exception e)
{
e.printStackTrace();
}//End of catch
//Displays number of elements present in the array
System.out.println("Numbers of data stored in array = " + counter);
}//End of method
//Method to display the array in reverse order
void displayReverse(int numberArray[])
{
System.out.println("Numbers in reverse order: ");
//Loops from end to beginning and displays the elements in reverse order
for(int i = counter - 1; i >= 0 ; i--)
System.out.print(numberArray[i] + " ");
}
//Displays the contents of the array
void displayArray(int numberArray[])
{
//Loops from beginning to end and displays the array contents
for(int c = 0; c < counter; c++)
System.out.print(numberArray[c] + " ");
}
//Returns the status of insertion operation
boolean insertSpecifiedLocation(int numberArray[])
{
//Initializes the status to false
boolean status = false;
//Loop variable
int c;
//Scanner class object created
Scanner scanner = new Scanner(System.in);
//Accepts the position and number for insertion
System.out.println(" Enter the poistion to insert a number: ");
int position = scanner.nextInt();
System.out.println("Enter the a number to insert at position: " + position);
int number = scanner.nextInt();
//Checks the validity of the position
if(position >= 0 && position < counter)
{
//Checks the overflow condition
if(counter > 99)
System.out.println("Error: Overflow Memory, Array is full");
else
{
//Loops from end of the array to the position specified.
//position - 1 because array index position starts from 0
for(c = counter - 1; c >= position - 1; c--)
//Shifting the values to next position till the position specified
numberArray[c + 1] = numberArray[c];
//Stores the number in the specified position. position - 1 because array index position
starts from 0
numberArray[position - 1] = number;
//Increase the length of the array by 1
counter++;
//Update the status to true for successful insertion
status = true;
}//end of else
}//End of if
//Displays error message
else
System.out.println("Error: Invalid Position");
//Returns the status
return status;
}//End of method
//Method removes a number from a specified position and returns the status
boolean removeSpecifiedLocation(int numberArray[])
{
//Initializes the status to false
boolean status = false;
//Loop variable
int c;
//scanner class object created
Scanner scanner = new Scanner(System.in);
//Accept the position to remove the number
System.out.println(" Enter the poistion to remove a number: ");
int position = scanner.nextInt();
//Checks the validity of the position
if(position >= 0 && position < counter)
{
//If the length is zero no more element to be deleted
if(counter == -1)
System.out.println("Error: Underflow Memory, Array is empty");
else
{
//Displays the removed element
System.out.println("Removed element: " + numberArray[position - 1]);
//Loops from the specified position to the length of the array.
//position - 1 because array index position starts from 0
for(c = position - 1; c < counter; c++)
//Shifting the next position value to the current position till the position specified
numberArray[c] = numberArray[c + 1];
//Decrease the length by 1
counter--;
//Update the status to true for successful deletion
status = true;
}//End of else
}//End of if
//Displays error message
else
System.out.println("Error: Invalid Position");
//Returns the status
return status;
}//End of method
//Method to display menu
void menu()
{
System.out.println(" 1) Display Reverse order");
System.out.println("2) Insert a number into a specified position");
System.out.println("3) Remove a number from specified position");
System.out.println("4) Exit");
}
//Main method to test
public static void main(String ss[])
{
//Creates an array of size 100
int numberArray [] = new int[100];
//status to check success of failure
boolean status;
//To store user choice
int ch;
//Scanner class object created
Scanner scanner = new Scanner(System.in);
//Creates ArrayOperation class object
ArrayOperation ao = new ArrayOperation();
//Call readData to read data from file
ao.readData(numberArray);
//Loops till user choice
do
{
//Calls menu method to display menu
ao.menu();
//Accepts user choice
System.out.println(" Enter your choice: ");
ch = scanner.nextInt();
switch(ch)
{
//To display in reverse order
case 1:
ao.displayReverse(numberArray);
break;
//To insert a number at specified position
case 2:
status = ao.insertSpecifiedLocation(numberArray);
//If the status is true Display the array
if(status)
{
System.out.println("Insertion successfull  After Insertion: ");
ao.displayArray(numberArray);
}
break;
//To Remove an element
case 3:
status = ao.removeSpecifiedLocation(numberArray);
//If the status is true Display the array
if(status)
{
System.out.println("Deletion successfull  After Deletion: ");
ao.displayArray(numberArray);
}
break;
//Exit the program
case 4:
System.out.println("Thank You");
System.exit(0);
default:
System.out.println("Invalid Choice");
} //End of switch
}while(true);
}//End of main method
}//End of class
Output:
Numbers of data stored in array = 8
1) Display Reverse order
2) Insert a number into a specified position
3) Remove a number from specified position
4) Exit
Enter your choice:
1
Numbers in reverse order:
66 45 56 80 50 30 20 10
1) Display Reverse order
2) Insert a number into a specified position
3) Remove a number from specified position
4) Exit
Enter your choice:
2
Enter the poistion to insert a number:
12
Enter the a number to insert at position: 12
66
Error: Invalid Position
1) Display Reverse order
2) Insert a number into a specified position
3) Remove a number from specified position
4) Exit
Enter your choice:
2
Enter the poistion to insert a number:
3
Enter the a number to insert at position: 3
88
Insertion successfull
After Insertion:
10 20 88 30 50 80 56 45 66
1) Display Reverse order
2) Insert a number into a specified position
3) Remove a number from specified position
4) Exit
Enter your choice:
3
Enter the poistion to remove a number:
56
Error: Invalid Position
1) Display Reverse order
2) Insert a number into a specified position
3) Remove a number from specified position
4) Exit
Enter your choice:
3
Enter the poistion to remove a number:
1
Removed element: 10
Deletion successfull
After Deletion:
20 88 30 50 80 56 45 66
1) Display Reverse order
2) Insert a number into a specified position
3) Remove a number from specified position
4) Exit
Enter your choice:
3
Enter the poistion to remove a number:
4
Removed element: 50
Deletion successfull
After Deletion:
20 88 30 80 56 45 66
1) Display Reverse order
2) Insert a number into a specified position
3) Remove a number from specified position
4) Exit
Enter your choice:
4
Thank You

More Related Content

Similar to Objectives In this lab you will review passing arrays to methods and.pdf

lecture12.ppt
lecture12.pptlecture12.ppt
lecture12.ppt
UmairMughal74
 
Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.
iammukesh1075
 
Arrays Fundamentals Unit II
Arrays  Fundamentals Unit IIArrays  Fundamentals Unit II
Arrays Fundamentals Unit II
Arpana Awasthi
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
Chapter 2
Chapter 2Chapter 2
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
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
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
Nurjahan Nipa
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
mohd_mizan
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Please the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdfPlease the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdf
admin463580
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
BackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
BackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
BackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
BackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
ssuseraef9da
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
BackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
ssuseraef9da
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
arwholesalelors
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
BackPack3
 

Similar to Objectives In this lab you will review passing arrays to methods and.pdf (20)

lecture12.ppt
lecture12.pptlecture12.ppt
lecture12.ppt
 
Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.
 
Arrays Fundamentals Unit II
Arrays  Fundamentals Unit IIArrays  Fundamentals Unit II
Arrays Fundamentals Unit II
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
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
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Please the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdfPlease the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 

More from f3apparelsonline

How do the microsporangium and microspore in seed plants differ from.pdf
How do the microsporangium and microspore in seed plants differ from.pdfHow do the microsporangium and microspore in seed plants differ from.pdf
How do the microsporangium and microspore in seed plants differ from.pdf
f3apparelsonline
 
Homework question with 2 partsA. Compare and contrast the geologic.pdf
Homework question with 2 partsA. Compare and contrast the geologic.pdfHomework question with 2 partsA. Compare and contrast the geologic.pdf
Homework question with 2 partsA. Compare and contrast the geologic.pdf
f3apparelsonline
 
Client Business Risk The risk that the client will fail to achieve it.pdf
Client Business Risk The risk that the client will fail to achieve it.pdfClient Business Risk The risk that the client will fail to achieve it.pdf
Client Business Risk The risk that the client will fail to achieve it.pdf
f3apparelsonline
 
Describe how removing water from a hydrophobic binding site or ligan.pdf
Describe how removing water from a hydrophobic binding site or ligan.pdfDescribe how removing water from a hydrophobic binding site or ligan.pdf
Describe how removing water from a hydrophobic binding site or ligan.pdf
f3apparelsonline
 
create a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdfcreate a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdf
f3apparelsonline
 
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdf
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdfCase Project 7-1 commen, diicrerne functions, arii price. wri.pdf
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdf
f3apparelsonline
 
Base your answer to questio n 25 on the map below and on your knowled.pdf
Base your answer to questio n 25 on the map below and on your knowled.pdfBase your answer to questio n 25 on the map below and on your knowled.pdf
Base your answer to questio n 25 on the map below and on your knowled.pdf
f3apparelsonline
 
As presented, the tree summation algorithm was always illustrated wi.pdf
As presented, the tree summation algorithm was always illustrated wi.pdfAs presented, the tree summation algorithm was always illustrated wi.pdf
As presented, the tree summation algorithm was always illustrated wi.pdf
f3apparelsonline
 
A person who quits a job in Los Angeles to look for work in Chicago i.pdf
A person who quits a job in Los Angeles to look for work in Chicago i.pdfA person who quits a job in Los Angeles to look for work in Chicago i.pdf
A person who quits a job in Los Angeles to look for work in Chicago i.pdf
f3apparelsonline
 
4. Which one of the following is a key disadvantage of a a. As a gene.pdf
4. Which one of the following is a key disadvantage of a a. As a gene.pdf4. Which one of the following is a key disadvantage of a a. As a gene.pdf
4. Which one of the following is a key disadvantage of a a. As a gene.pdf
f3apparelsonline
 
Who are the three agencies today that rate bondsHow do the three .pdf
Who are the three agencies today that rate bondsHow do the three .pdfWho are the three agencies today that rate bondsHow do the three .pdf
Who are the three agencies today that rate bondsHow do the three .pdf
f3apparelsonline
 
Which of the following is trueChimpanzees and hominids share a comm.pdf
Which of the following is trueChimpanzees and hominids share a comm.pdfWhich of the following is trueChimpanzees and hominids share a comm.pdf
Which of the following is trueChimpanzees and hominids share a comm.pdf
f3apparelsonline
 
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdfWhat does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
f3apparelsonline
 
What is the firm’s cost of preferred stock Please show work in Exce.pdf
What is the firm’s cost of preferred stock Please show work in Exce.pdfWhat is the firm’s cost of preferred stock Please show work in Exce.pdf
What is the firm’s cost of preferred stock Please show work in Exce.pdf
f3apparelsonline
 
What are the 2 major cytoskeletal classes that participate in cell d.pdf
What are the 2 major cytoskeletal classes that participate in cell d.pdfWhat are the 2 major cytoskeletal classes that participate in cell d.pdf
What are the 2 major cytoskeletal classes that participate in cell d.pdf
f3apparelsonline
 
Wanda is a 20 percent owner of Video Associates, which is treated as.pdf
Wanda is a 20 percent owner of Video Associates, which is treated as.pdfWanda is a 20 percent owner of Video Associates, which is treated as.pdf
Wanda is a 20 percent owner of Video Associates, which is treated as.pdf
f3apparelsonline
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
f3apparelsonline
 
To design an expert system, we must first identify a problem to be s.pdf
To design an expert system, we must first identify a problem to be s.pdfTo design an expert system, we must first identify a problem to be s.pdf
To design an expert system, we must first identify a problem to be s.pdf
f3apparelsonline
 
Translate the following into English. Let C denote an arbitrary coll.pdf
Translate the following into English. Let C denote an arbitrary coll.pdfTranslate the following into English. Let C denote an arbitrary coll.pdf
Translate the following into English. Let C denote an arbitrary coll.pdf
f3apparelsonline
 
The force on an object is F = - 21j. For the vector v = 2 i - 2j, fin.pdf
The force on an object is F = - 21j. For the vector v = 2 i - 2j, fin.pdfThe force on an object is F = - 21j. For the vector v = 2 i - 2j, fin.pdf
The force on an object is F = - 21j. For the vector v = 2 i - 2j, fin.pdf
f3apparelsonline
 

More from f3apparelsonline (20)

How do the microsporangium and microspore in seed plants differ from.pdf
How do the microsporangium and microspore in seed plants differ from.pdfHow do the microsporangium and microspore in seed plants differ from.pdf
How do the microsporangium and microspore in seed plants differ from.pdf
 
Homework question with 2 partsA. Compare and contrast the geologic.pdf
Homework question with 2 partsA. Compare and contrast the geologic.pdfHomework question with 2 partsA. Compare and contrast the geologic.pdf
Homework question with 2 partsA. Compare and contrast the geologic.pdf
 
Client Business Risk The risk that the client will fail to achieve it.pdf
Client Business Risk The risk that the client will fail to achieve it.pdfClient Business Risk The risk that the client will fail to achieve it.pdf
Client Business Risk The risk that the client will fail to achieve it.pdf
 
Describe how removing water from a hydrophobic binding site or ligan.pdf
Describe how removing water from a hydrophobic binding site or ligan.pdfDescribe how removing water from a hydrophobic binding site or ligan.pdf
Describe how removing water from a hydrophobic binding site or ligan.pdf
 
create a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdfcreate a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdf
 
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdf
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdfCase Project 7-1 commen, diicrerne functions, arii price. wri.pdf
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdf
 
Base your answer to questio n 25 on the map below and on your knowled.pdf
Base your answer to questio n 25 on the map below and on your knowled.pdfBase your answer to questio n 25 on the map below and on your knowled.pdf
Base your answer to questio n 25 on the map below and on your knowled.pdf
 
As presented, the tree summation algorithm was always illustrated wi.pdf
As presented, the tree summation algorithm was always illustrated wi.pdfAs presented, the tree summation algorithm was always illustrated wi.pdf
As presented, the tree summation algorithm was always illustrated wi.pdf
 
A person who quits a job in Los Angeles to look for work in Chicago i.pdf
A person who quits a job in Los Angeles to look for work in Chicago i.pdfA person who quits a job in Los Angeles to look for work in Chicago i.pdf
A person who quits a job in Los Angeles to look for work in Chicago i.pdf
 
4. Which one of the following is a key disadvantage of a a. As a gene.pdf
4. Which one of the following is a key disadvantage of a a. As a gene.pdf4. Which one of the following is a key disadvantage of a a. As a gene.pdf
4. Which one of the following is a key disadvantage of a a. As a gene.pdf
 
Who are the three agencies today that rate bondsHow do the three .pdf
Who are the three agencies today that rate bondsHow do the three .pdfWho are the three agencies today that rate bondsHow do the three .pdf
Who are the three agencies today that rate bondsHow do the three .pdf
 
Which of the following is trueChimpanzees and hominids share a comm.pdf
Which of the following is trueChimpanzees and hominids share a comm.pdfWhich of the following is trueChimpanzees and hominids share a comm.pdf
Which of the following is trueChimpanzees and hominids share a comm.pdf
 
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdfWhat does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
 
What is the firm’s cost of preferred stock Please show work in Exce.pdf
What is the firm’s cost of preferred stock Please show work in Exce.pdfWhat is the firm’s cost of preferred stock Please show work in Exce.pdf
What is the firm’s cost of preferred stock Please show work in Exce.pdf
 
What are the 2 major cytoskeletal classes that participate in cell d.pdf
What are the 2 major cytoskeletal classes that participate in cell d.pdfWhat are the 2 major cytoskeletal classes that participate in cell d.pdf
What are the 2 major cytoskeletal classes that participate in cell d.pdf
 
Wanda is a 20 percent owner of Video Associates, which is treated as.pdf
Wanda is a 20 percent owner of Video Associates, which is treated as.pdfWanda is a 20 percent owner of Video Associates, which is treated as.pdf
Wanda is a 20 percent owner of Video Associates, which is treated as.pdf
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
 
To design an expert system, we must first identify a problem to be s.pdf
To design an expert system, we must first identify a problem to be s.pdfTo design an expert system, we must first identify a problem to be s.pdf
To design an expert system, we must first identify a problem to be s.pdf
 
Translate the following into English. Let C denote an arbitrary coll.pdf
Translate the following into English. Let C denote an arbitrary coll.pdfTranslate the following into English. Let C denote an arbitrary coll.pdf
Translate the following into English. Let C denote an arbitrary coll.pdf
 
The force on an object is F = - 21j. For the vector v = 2 i - 2j, fin.pdf
The force on an object is F = - 21j. For the vector v = 2 i - 2j, fin.pdfThe force on an object is F = - 21j. For the vector v = 2 i - 2j, fin.pdf
The force on an object is F = - 21j. For the vector v = 2 i - 2j, fin.pdf
 

Recently uploaded

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 

Recently uploaded (20)

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

Objectives In this lab you will review passing arrays to methods and.pdf

  • 1. Objectives In this lab you will review passing arrays to methods and partially filled arrays. Requirements 1. Fill an array with data from an input file sampledata-io.txt (Attached) a. Assume no more than 100 data items, terminated by -1 for sentinel b. Note that the sample data file has other information after the sentinel; it should cause no problem c. Read and store data d. Print the number of data stored in the array e. Add a method to reverse the array. Pass the original array as the argument and return the reversed array. 2. Testing a. Invoke the reverse method, print out the reversed array. 3. Inserting into the partially filled array a. Add a method to insert a user inputted value into a specific location. b. Remember to check parameters for validity. Also remember to check whether the array is full or not. c. Invoke the insert method, prompt to user whether the insertion is successful or not. 4. Removing from the partially filled array a. Add a method to remove the element at the specific location in the partially filled array. b. Invoke the remove method, prompt to user whether the remove is successful or not. If the remove is successful, print out the value of the element just removed. There are several important points that are general requirements for labs in this course: Include a comment at the beginning of each source file you submit that includes your name and the lab date. Names for variables and other program components should be chosen to help convey the meaning of the variable. Turn in an archive of the entire Eclipse project for each lab. Do not attempt to turn in individual files, some course management systems mangle such files. Solution package myProject; import java.util.*; import java.io.File; //Create a class Array Operation public class ArrayOperation { //Initializes the counter to zero int counter = 0; //Method to read data from file void readData(int numberArray[]) { //File object created File file = new File("D:/TODAY/src/myProject/data.txt");
  • 2. //Handles exception try { //Scanner object created Scanner scanner = new Scanner(file); //Checks for the data availability while(scanner.hasNextInt()) { //Reads a number from the file int no = scanner.nextInt(); //Checks if the number is -1 then stop reading if(no == -1) break; //Stores the number in the array numberArray[counter++] = no; }//End of while }//End of try //Catch block catch(Exception e) { e.printStackTrace(); }//End of catch //Displays number of elements present in the array System.out.println("Numbers of data stored in array = " + counter); }//End of method //Method to display the array in reverse order void displayReverse(int numberArray[]) { System.out.println("Numbers in reverse order: "); //Loops from end to beginning and displays the elements in reverse order for(int i = counter - 1; i >= 0 ; i--) System.out.print(numberArray[i] + " "); } //Displays the contents of the array
  • 3. void displayArray(int numberArray[]) { //Loops from beginning to end and displays the array contents for(int c = 0; c < counter; c++) System.out.print(numberArray[c] + " "); } //Returns the status of insertion operation boolean insertSpecifiedLocation(int numberArray[]) { //Initializes the status to false boolean status = false; //Loop variable int c; //Scanner class object created Scanner scanner = new Scanner(System.in); //Accepts the position and number for insertion System.out.println(" Enter the poistion to insert a number: "); int position = scanner.nextInt(); System.out.println("Enter the a number to insert at position: " + position); int number = scanner.nextInt(); //Checks the validity of the position if(position >= 0 && position < counter) { //Checks the overflow condition if(counter > 99) System.out.println("Error: Overflow Memory, Array is full"); else { //Loops from end of the array to the position specified. //position - 1 because array index position starts from 0 for(c = counter - 1; c >= position - 1; c--) //Shifting the values to next position till the position specified
  • 4. numberArray[c + 1] = numberArray[c]; //Stores the number in the specified position. position - 1 because array index position starts from 0 numberArray[position - 1] = number; //Increase the length of the array by 1 counter++; //Update the status to true for successful insertion status = true; }//end of else }//End of if //Displays error message else System.out.println("Error: Invalid Position"); //Returns the status return status; }//End of method //Method removes a number from a specified position and returns the status boolean removeSpecifiedLocation(int numberArray[]) { //Initializes the status to false boolean status = false; //Loop variable int c; //scanner class object created Scanner scanner = new Scanner(System.in); //Accept the position to remove the number System.out.println(" Enter the poistion to remove a number: "); int position = scanner.nextInt();
  • 5. //Checks the validity of the position if(position >= 0 && position < counter) { //If the length is zero no more element to be deleted if(counter == -1) System.out.println("Error: Underflow Memory, Array is empty"); else { //Displays the removed element System.out.println("Removed element: " + numberArray[position - 1]); //Loops from the specified position to the length of the array. //position - 1 because array index position starts from 0 for(c = position - 1; c < counter; c++) //Shifting the next position value to the current position till the position specified numberArray[c] = numberArray[c + 1]; //Decrease the length by 1 counter--; //Update the status to true for successful deletion status = true; }//End of else }//End of if //Displays error message else System.out.println("Error: Invalid Position"); //Returns the status return status; }//End of method //Method to display menu void menu() { System.out.println(" 1) Display Reverse order");
  • 6. System.out.println("2) Insert a number into a specified position"); System.out.println("3) Remove a number from specified position"); System.out.println("4) Exit"); } //Main method to test public static void main(String ss[]) { //Creates an array of size 100 int numberArray [] = new int[100]; //status to check success of failure boolean status; //To store user choice int ch; //Scanner class object created Scanner scanner = new Scanner(System.in); //Creates ArrayOperation class object ArrayOperation ao = new ArrayOperation(); //Call readData to read data from file ao.readData(numberArray); //Loops till user choice do { //Calls menu method to display menu ao.menu(); //Accepts user choice System.out.println(" Enter your choice: "); ch = scanner.nextInt(); switch(ch)
  • 7. { //To display in reverse order case 1: ao.displayReverse(numberArray); break; //To insert a number at specified position case 2: status = ao.insertSpecifiedLocation(numberArray); //If the status is true Display the array if(status) { System.out.println("Insertion successfull After Insertion: "); ao.displayArray(numberArray); } break; //To Remove an element case 3: status = ao.removeSpecifiedLocation(numberArray); //If the status is true Display the array if(status) { System.out.println("Deletion successfull After Deletion: "); ao.displayArray(numberArray); } break; //Exit the program case 4: System.out.println("Thank You"); System.exit(0); default: System.out.println("Invalid Choice"); } //End of switch }while(true); }//End of main method }//End of class
  • 8. Output: Numbers of data stored in array = 8 1) Display Reverse order 2) Insert a number into a specified position 3) Remove a number from specified position 4) Exit Enter your choice: 1 Numbers in reverse order: 66 45 56 80 50 30 20 10 1) Display Reverse order 2) Insert a number into a specified position 3) Remove a number from specified position 4) Exit Enter your choice: 2 Enter the poistion to insert a number: 12 Enter the a number to insert at position: 12 66 Error: Invalid Position 1) Display Reverse order 2) Insert a number into a specified position 3) Remove a number from specified position 4) Exit Enter your choice: 2 Enter the poistion to insert a number: 3 Enter the a number to insert at position: 3 88 Insertion successfull After Insertion: 10 20 88 30 50 80 56 45 66 1) Display Reverse order 2) Insert a number into a specified position
  • 9. 3) Remove a number from specified position 4) Exit Enter your choice: 3 Enter the poistion to remove a number: 56 Error: Invalid Position 1) Display Reverse order 2) Insert a number into a specified position 3) Remove a number from specified position 4) Exit Enter your choice: 3 Enter the poistion to remove a number: 1 Removed element: 10 Deletion successfull After Deletion: 20 88 30 50 80 56 45 66 1) Display Reverse order 2) Insert a number into a specified position 3) Remove a number from specified position 4) Exit Enter your choice: 3 Enter the poistion to remove a number: 4 Removed element: 50 Deletion successfull After Deletion: 20 88 30 80 56 45 66 1) Display Reverse order 2) Insert a number into a specified position 3) Remove a number from specified position 4) Exit Enter your choice: