------------------------------------- tester -----------------------------------
ID: 440168 • Letter: #
Question
-------------------------------------
tester
-------------------------------------
import java.util.Scanner;
public class SequenceTester {
/**
* @param args
*/
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
System.out.print("How many elements in your SortedSequence?:");
int numElements = keyb.nextInt();
SortedSequence ss = new SortedSequence(numElements);
System.out.println("Here is your sequence so far:" + ss);
System.out.println(" Adding 6 items to the list...");
for(int i = 0; i<6; i++){
int newElement = keyb.nextInt();
ss.add(newElement);
System.out.println("Here is your sequence so far:" + ss);
}
System.out.println(" Enter 2 numbers to search for in the sequence...");
for(int i = 0; i<2; i++){
int search = keyb.nextInt();
if(ss.isFound(search))
System.out.println(search + " is present in the sequence!");
else
System.out.println("Sorry. " + search + " could not be found.");
}
}
}
-------------------------------------------------
class
------------------------------------------------
public class SortedSequence {
int[] data;
int manyItems;
/**
* Default constructor. Creates a SortedSequence of size
* 10 with 0 items.
*/
public SortedSequence(){
//a special call to the single-argument constructor
this(10);
}
/**
* Single-argument constructor.
* @param siz
e - initial size of the data array.
*/
public SortedSequence(int size){
//a special call to the single-argument constructor
data = new int[10];
manyItems = 0;
}
public void add(int element){
//Only proceed if the array is not full.
if(manyItems < data.length){
//find the first index that is larger than the
//incoming element.
int index = 0;
while(index < manyItems && data[index] < element){
index++;
}
//Move all of the elements to the right of the index
//down one. Start at the end or else we will overwrite values
//by mistake.
for(int i = manyItems-1; i>=index; i--){
data[i+1] = data[i];
}
//now put the element in the place that we found.
data[index] = element;
//show that we have a new element added
manyItems++;
}
}
/**
* A method to figure out if a value is in the sequence or not.
* @param findMe - the value to find.
* @return true if the value is in the sequence, false otherwise
*/
public boolean isFound(int findMe){
boolean found = false;
//could be done with a for loop but i prefer a while
int index = 0;
while(!found && index < manyItems){
if(data[index] == findMe){
found = true;
}
index++;
}
return found;
}
/**
* @return String representation of a SortedSequence.
*/
public String toString(){
String s = "Sequence[";
for(int i = 0; i<manyItems; i++){
if(i == manyItems - 1) //special case for last element
s += data[i];
else
s += data[i] + ",";
}
s += "]";
return s;
}
}
----------------------------------------------------------
Explanation / Answer
There was some problems in your code,one was however the input size yo were asking the user to enter only 6 integers,i corrected it and generalized to n.One more thing when you have more than one class in a file make only main class as public,otherwise you will get the error.
here is the remove method code , full code is in last.
public void remove(int element){
if (isFound(element) ){
// find index in the array
int index = -1;
for ( int i = 0 ; i < manyItems ;i++){
if ( data[i] == element ){
index = i;
break;
}
}
// index must be non-sero because isFound us true
int temp = data[index]; /// removed element
for ( int i = index + 1 ; i < manyItems;i++){
data[i-1] = data[i];
}
manyItems--;
}
else {
System.out.println("Not Found ,Can't remove");
}
}
Here is the fulll code,running on my system.
import java.util.Scanner;
public class SequenceTester {
/**
* @param args
*/
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
System.out.print("How many elements in your SortedSequence?:");
int numElements = keyb.nextInt();
SortedSequence ss = new SortedSequence(numElements);
System.out.println("Here is your sequence so far:" + ss);
System.out.println(" Adding " + numElements + " items to the list...");
for(int i = 0; i<numElements; i++){
int newElement = keyb.nextInt();
ss.add(newElement);
System.out.println("Here is your sequence so far:" + ss);
}
System.out.println(" Enter 2 numbers to search for in the sequence...");
for(int i = 0; i<2; i++){
int search = keyb.nextInt();
if(ss.isFound(search))
System.out.println(search + " is present in the sequence!");
else
System.out.println("Sorry. " + search + " could not be found.");
}
//remove tester code;
System.out.println("Going to Remove 4");
ss.remove(4);
System.out.println("Here is your sequence " + ss);
}
}
class SortedSequence {
int[] data;
int manyItems;
int max_size;
/**
* Default constructor. Creates a SortedSequence of size
* 10 with 0 items.
*/
public SortedSequence(){
//a special call to the single-argument constructor
this(10);
}
/**
* Single-argument constructor.
* @param siz e - initial size of the data array.
*/
public SortedSequence(int size){
//a special call to the single-argument constructor
data = new int[size];
max_size = size;
manyItems = 0;
}
public void add(int element){
//Only proceed if the array is not full.
if(manyItems < data.length){
//find the first index that is larger than the
//incoming element.
int index = 0;
while(index < manyItems && data[index] < element){
index++;
} //Move all of the elements to the right of the index
//down one. Start at the end or else we will overwrite values
//by mistake.
for(int i = manyItems-1; i>=index; i--){
data[i+1] = data[i];
}
//now put the element in the place that we found.
data[index] = element;
//show that we have a new element added
manyItems++;
}
}
public void remove(int element){
if (isFound(element) ){
// find index in the array
int index = -1;
for ( int i = 0 ; i < manyItems ;i++){
if ( data[i] == element ){
index = i;
break;
}
}
// index must be non-sero because isFound us true
int temp = data[index]; /// removed element
for ( int i = index + 1 ; i < manyItems;i++){
data[i-1] = data[i];
}
manyItems--;
}
else {
System.out.println("Not Found ,Can't remove");
}
}
/**
* A method to figure out if a value is in the sequence or not.
* @param findMe - the value to find.
* @return true if the value is in the sequence, false otherwise
*/
public boolean isFound(int findMe){
boolean found = false;
//could be done with a for loop but i prefer a while
int index = 0;
while(!found && index < manyItems){
if(data[index] == findMe){
found = true;
}
index++;
}
return found;
}
/**
* @return String representation of a SortedSequence.
*/
public String toString(){
String s = "Sequence[";
for(int i = 0; i<manyItems; i++){
if(i == manyItems - 1) //special case for last element
s += data[i];
else
s += data[i] + ",";
}
s += "]";
return s;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.