Question

I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However,...

I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs two sets of dashed lines. What am I getting wrong? Is it the deleteRepeats() method?:

public class CharArrayProject_3
{
private char[] array;
privateint length;
privateintnumberOfRepeats;

public CharArrayProject_3( char[] arr )
{
length = arr.length;
array = new char[ length ];
numberOfRepeats = 0;
for( int k = 0; k < arr.length; k++ )
{ array[k] = arr[k]; }
}

public void deleteRepeats()
{
int j = 0;//for next element
for (inti=0; i < length-1; i++){
if (array[i] == array[i+1]){
array[j++] = array[i];
numberOfRepeats++;
}
}
array[j++] = array[length-1];
}
}

public String toString()
{
String result;
for (char value : array)
{
result += value;
}
String result = "\nNumber of Repeats: " + numberOfRepeats;
return result;
}
}

public class CharArrayProject_3_Driver
{
public static void main( String args[] )
{
char a[] = { 'h','o','l','l','y',
'c','l','e','m','e','n','c','e' };
CharArrayProject_3 hello = new CharArrayProject_3( a );
hello.toString();
hello.deleteRepeats();
System.out.println();
hello.toString();
System.out.print( "\n-----------------------------\n" +
"-----------------------------\n");
char b[] = { 'a','b','c','b','b',
'c','a','d','a','d','c' };
hello = new CharArrayProject_3( b );
hello.toString();
hello.deleteRepeats();
System.out.println();
hello.toString();
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

the only modification I made to get it to work was to add the hello.toString() line into a print statement. The toString function returns a string but does not print the string itself.

only you know if the code does the right thing.

I've boldened the modifications

please drop a comment if there's a problem

//--------------------------------------------

public class CharArrayProject_3_Driver {

    public static void main(String args[]) {

        char a[] = { 'h', 'o', 'l', 'l', 'y', 'c', 'l', 'e', 'm', 'e', 'n', 'c', 'e' };

        CharArrayProject_3 hello = new CharArrayProject_3(a);

        System.out.println(hello.toString());

        

        hello.deleteRepeats();

        System.out.println(hello.toString());

        

        System.out.print("\n-----------------------------\n" + "-----------------------------\n");

        char b[] = { 'a', 'b', 'c', 'b', 'b', 'c', 'a', 'd', 'a', 'd', 'c' };

        hello = new CharArrayProject_3(b);

        System.out.println(hello.toString());

        hello.deleteRepeats();

        System.out.println();

        System.out.println(hello.toString());

    }

}

//-----OUTPUT------

Number of Repeats: 0

Number of Repeats: 1

-----------------------------
-----------------------------

Number of Repeats: 0


Number of Repeats: 1

Add a comment
Know the answer?
Add Answer to:
I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass...

    I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass { public static void main(String[] args) { int a[] = {3, 2, 5, 6, 1}; InsertionSortClass insertion = new InsertionSortClass(); System.out.print("The original list : "); System.out.println(); insertion.printArray(a); System.out.println(); System.out.println("The list after insertionSort : "); System.out.println(); insertion.insertionSort(a); } } package DriverClass; public interface SortADTInterface { public void insertionSort(int arr[]); public void printArray(int arr[]); } package DriverClass; public class InsertionSortClass implements SortADTInterface{ public void insertionSort(int[] arr)...

  • I'm attempting to convert ch object array to double array public void calculateSalesPrice() { double salePrice[]...

    I'm attempting to convert ch object array to double array public void calculateSalesPrice() { double salePrice[] = new double[ch.length]; int p = 0; for (int i = 0; i < ch.length; i += 3) { p = i + 1; for(int j = 0;j } } error message:items.java:16: error: cannot find symbol for(int j = 0;j ^ symbol: variable length location: class Object items.java:17: error: array required, but Object found salePrice[j] = (double full program import java.io.File; import java.util.Scanner; import...

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • 10. Which of the following correcthy milites elements each with value 0? super Bass to com...

    10. Which of the following correcthy milites elements each with value 0? super Bass to com I inttl superBass { 0, 0, 0, 0, 0 ); int(1 superBass = new int [5] inttl superBass = new int (5) for (int i = 0; i < super Bass.length superBass ) = 0; +) (A) (B) I only II only I and II only I and III only I. II, and III (D) (E) 17. Consider the following Java program public class...

  • To the HighArray class in the highArray.java, add a method called getMax() that returns the value...

    To the HighArray class in the highArray.java, add a method called getMax() that returns the value of the highest key in the array, or -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. // highArray.java class HighArray { private long[] a; private int nElems;    public HighArray(int max)    { a = new long[max];    nElems = 0; } public boolean find(long searchKey) { int...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT