call on other methods, won't compile

luna621
luna621 used Ask the Experts™
on
Thank you to object who answered my first question, but I have another one.  I'm a very lost beginner and I'm having trouble calling on other methods to do a task.  I need to have a method that should invoke "removeLongestFromScrapBin" private helper method in order to remove the longest scrap from the scrap bin. Likewise, it should invoke the "addToScrapBin" method to insert any remaining piece of the scrap back into the scrap bin.  I need help writing the cutLongestSrcap method.  Thank you!!  Here's my code:

import java.io.*;
import java.lang.*;
import java.util.*;

/**
 *   ScrapBin -- This class models the bin of log scraps in the "saw mill"
 *   simulation described in Programming Assignment #4. Using an array
 *   implementation, the scrap bin stores lumber scraps in order from longest to
 *   shortest. Various methods, which are described more thoroughly below,
 *   allow one to add a scrap to the scrap bin, and simulate the cutting of the
 *   longest scrap in the scrap bin (which involves both removing the longest
 *   scrap from the scrap bin, and (potentially) adding a shorter scrap
 *   back to the scrap bin).
 */
public class ScrapBin {

/**
 *   Class constructor--creates an empty array of scraps, and initializes
 *   numScraps and totalScrapLength instance variables.
 *   @param NONE
 *   @return a new instance of ScrapBin
 *   @worth  DO NOT MODIFY THIS METHOD!
 */
public ScrapBin(int maxScraps) {
  scraps = new int[maxScraps];
  numScraps = 0;
  totalScrapLength = 0;
}


/**
 *   addToScrapBin--adds a scrap to the scrap bin, inserting it such that
 *   the scraps are in order from longest to shortest.
 *   @param logLength, the length of the scrap to be inserted
 *   @return NOTHING (but scraps array and instance vars should be updated appropriately)
  */
public void addToScrapBin(int[] logLength) {

int max, temp;

for (int i = 0; i < logLength.length-1; i++) {
      max = i;
      for (int j = i+1; j < logLength.length; j++) {
            if (logLength[j] > logLength[max]) {
                  max = j;
            }

/**
 * Swap the values
 */

      temp = logLength[max];
      logLength[max] = logLength[i];
      logLength[i] = temp;
      }
}


for (int m = 1; m < logLength.length; m++) {
      int k = logLength[m];
      int index = m;
/**
 * Shift larger values to the right.
 */

       while (index > 0 && logLength[index-1] > k) {
             logLength[index] = logLength[index-1];
             index--;
       }
       logLength[index] = k;
      }
}


/**
 *   removeLongestFromScrapBin--private helper method that removes the longest
 *   scrap (located at scraps[0]) from the scrap bin, shuffling all other scraps
 *   forward.
 *   @param  None
 *   @return length of (longest) scrap removed, or 0 if the scraps array is empty
 *   @note   This method will be used by the "cutLongestScrap" method
 */
private int removeLongestFromScrapBin() {

      System.arraycopy(scraps, 1, scraps, 0, numScraps-1);
      numScraps--;

return scraps[0];
}

/**
 *   cutLongestScrap--simulates the cutting of the longest scrap by removing that
 *   scrap (located at scraps[0]) from the scrap bin, cutting what's needed
 *   from it, and potentially re-inserting any piece that remains back into the
 *   scrap bin
 *   @param  lengthNeeded, the length of the piece to be cut from the longest
 *           scrap.
 *   @return length of longest scrap that was cut, or 0 if no scrap bin is empty
 *           and hence no scrap is available to be cut
 *   @note   This method should invoke the "removeLongestFromScrapBin" private
 *           helper method in order to remove the longest scrap from the scrap
 *           bin. Likewise, it should invoke the "addToScrapBin" method to
 *           insert any remaining piece of the scrap back into the scrap bin.
 */
public int cutLongestScrap(int lengthNeeded) {
>>>>>>>>need help here!!!!<<<<<<<<<<<<<<<<<
return scraps[0];
}

/**
 *   getLongestScrap--"getter" method for the longest scrap in the scrap bin
 *   (does not actually remove that scrap from the scrap bin)
 *   @param  NONE
 *   @return the length of the longest scrap currently in the scrap bin
 *   @worth  DO NOT MODIFY THIS METHOD!
 */
public int getLongestScrap() {
  if (numScraps == 0)
    return 0;
  else return scraps[0];
}

/**
 *   getTotalScrapLength--"getter" method for the total length of all scraps
 *   presently in the scrap bin
 *   @param  NONE
 *   @return the total length of the all scraps currently in the scrap bin
 *   @worth  DO NOT MODIFY THIS METHOD!
 */
public int getTotalScrapLength() {
  return totalScrapLength;
}

/**
 *   printScraps--pretty-prints to the terminal the current contents of the
 *   scraps array, i.e., the scrap lengths, in order from longest to shortest
 *   @param  NONE
 *   @return NOTHING
 *   @worth  DO NOT MODIFY THIS METHOD!
 */
public void printScraps() {
  for (int i = 0; i < numScraps; i++)
    System.out.println("Length of scrap #" + (i+1) + ": " + scraps[i]);
}

//INSTANCE VARIABLES (Hint: You will not need to define any others)
private int[] scraps; //the array of scraps
private int numScraps;  //number of scraps currently in scraps array
private int totalScrapLength;  //total length of all scraps currently in scraps array

}
 
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
>> private int removeLongestFromScrapBin() {

This function is called like this,

int x = removeLongestFromScrapBin();

However, this function is declared private so it can only be called from within this class but more specifically it can only be called from within this instance of this class as each instance of this class has its own copy of this method.

Commented:
/**
*   cutLongestScrap--simulates the cutting of the longest scrap by removing that
*   scrap (located at scraps[0]) from the scrap bin, cutting what's needed
*   from it, and potentially re-inserting any piece that remains back into the
*   scrap bin
*   @param  lengthNeeded, the length of the piece to be cut from the longest
*           scrap.
*   @return length of longest scrap that was cut, or 0 if no scrap bin is empty
*           and hence no scrap is available to be cut
*   @note   This method should invoke the "removeLongestFromScrapBin" private
*           helper method in order to remove the longest scrap from the scrap
*           bin. Likewise, it should invoke the "addToScrapBin" method to
*           insert any remaining piece of the scrap back into the scrap bin.
*/
public int cutLongestScrap(int lengthNeeded) {
//>>>>>>>>need help here!!!!<<<<<<<<<<<<<<<<<
return scraps[0];
}

as the comments explain, let's say u wanted to remove a scrap of length 40, and the longest scrap u have in scrap[0] is 100.. what u need to do is first call the removeLongestFromScrapBin method which will return the longest scrap length(i.e. 100) , u would then deduct the lengthNeeded (ie. 100 - 40 = 60) and then call addToScrapBin to put the remaining 60 length scarp in the appropriate place.. so the method calls should be

public int cutLongestScrap(int lengthNeeded) {
int longestScarp = removeLongestFromScrapBin();
int remainingScrap = longestScrap - lengthNeeded;
addToScrapBin(remaininScrap);
return scraps[0];
}

if u noticed, your addToScrapBin method is not done properly.. it should take input of scrap length and see where it can insert it into the exisitng scrap array.. you should not do anything with the logLength array
vemul
Commented:
your addToScrapBin method should be as follows..

/**
*   addToScrapBin--adds a scrap to the scrap bin, inserting it such that
*   the scraps are in order from longest to shortest.
*   @param logLength, the length of the scrap to be inserted
*   @return NOTHING (but scraps array and instance vars should be updated appropriately)
 */
public void addToScrapBin(int logLength) {

     int pos;

     for (int i = 0; i < scraps.length; i++) {
          if (scraps[i] < logLength){
               pos = i;
               break;
          }
     }

     // now we have the position where the scrap has to be inserted into the array
     // so shift the remaining elements by 1
     System.arraycopy(scraps, pos, scraps, pos+1, scraps.length-1 - pos);
     scraps[pos] = logLength;
}
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Author

Commented:
When I compiled the program I got the following error:

variable pos might not have been initialized

Am I suppose to set pos = 0??  This is the method:

public void addToScrapBin(int logLength) {

     int pos;

         for (int i = 0; i < scraps.length; i++) {
              if (scraps[i] < logLength){
                   pos = i;
                   break;
              }
         }

         // Shift the remaining elements by 1
         System.arraycopy(scraps, pos, scraps, pos+1, scraps.length-1 - pos);
         scraps[pos] = logLength;

}

Commented:
yes, u can do that..

int pos = 0;

Author

Commented:
How would you update instance variables numScraps and totalScrapLength?
>> How would you update instance variables numScraps and totalScrapLength?

That depends on how and where these variables are declared.

>> private int[] scraps;
>> private int numScraps;  
>> private int totalScrapLength;

Since these are declared private they can only be accessed from within this instance of this class. Inside the class they can be altered just like any other variable. I.E.

numScraps = 5;

To access them from another class you must build set and get methods into this class. I.E.

public int getNumScraps()
{
      return numScraps;
}

Commented:
>> How would you update instance variables numScraps and totalScrapLength?

since these are modified only when u add or remove a scrap,they should be handled over there..

u already have a numScraps-- in your removeXX code..

add a numScraps++ in your addXX code when u a add a new scrap..

also I think u would need to use numScraps as the range setter when u iterate over scraps array
ie. in these scenarios

for(int i = 0; i < numScraps; i++)

instead of
for(int i = 0; i < scraps.length; i++)

hth
vemul

Author

Commented:
The problem is still there.  This is what I have to do, here are links to view the programs:

http://www2.hawaii.edu/~flam/ScrapBin.htm
http://www2.hawaii.edu/~flam/SawMill.htm
http://www2.hawaii.edu/~flam/LogOrder.htm
http://www2.hawaii.edu/~flam/logtest.txt

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

1.) run SawMill.java (which calls upon LogOrder.java & ScrapBin.java) *I'm only modifing the ScrapBin.java*

2.) read from logtest.txt

3.) the output should be

AppAccelerator(tm) 1.2.010 for Java (JDK 1.2),
Copyright (c) 1997-1999 Inprise Corporation. A
**WELCOME TO THE SAW MILL SIMULATION**
Please enter an input file for the simulation:
Would you like to run the simulation in 'verbo
Thank you. Reading in file. . .
Performing simulation. . .
Printing statistics. . .
Number of log orders processed: 10
Number of log pieces cut      : 35
Total length of log pieces cut: 138
Number of pieces cut of. . .  :
. . .length 1: 9
. . .length 2: 6
. . .length 3: 4
. . .length 4: 1
. . .length 5: 3
. . .length 6: 4
. . .length 7: 5
. . .length 8: 1
. . .length 9: 1
. . .length 10: 1
. . .length 11: 0
. . .length 12: 0
. . .length 13: 0
. . .length 14: 0
. . .length 15: 0
. . .length 16: 0
. . .length 17: 0
. . .length 18: 0
. . .length 19: 0
. . .length 20: 0

*** this is where I run into problems... ***

Average length of cut piece   : 3.942857
Total length of wasted lumber : 17 <<-- I get 0 over here!
List of wasted scrap lengths  :
Length of scrap #1: 13
Length of scrap #2: 2
Length of scrap #3: 1
Length of scrap #4: 1

I appreciate all your help! :)

 

>> *** this is where I run into problems... ***

What problems???

If you expect to get meaningfull answers you need to ask more specific questions.

Author

Commented:
Sorry.  Somehow, my instance variables (numScraps and totalScrapLength are not updating...  

"Total length of wasted lumber : 17 <<-- I get 0 over here!"

What that means is I'm suppose to get 17 as the total length of wasted lumber, but when I run my program, that number is zero.  Everything else in my program works as it should.  A comment from vemul said I should add numScraps++ to my addToScrapBin method.  I tried that, but it didn't work either.  Any ideas?  Thank you!
>> Total length of wasted lumber : 17 <<-- I get 0 over here!

This is the total length of wasted lumber. numScraps is only how many scraps were left over. To obtain how much lumber was wasted,

int wasted = 0, x;

for( x = 0; x < scraps.length; x++ )
      wasted += scraps[x];

System.out.println( "Total length of wasted lumber : " + wasted );
P.S. why is numScraps needed? Can't you just use scraps.length or is numScraps used in resizing scraps?

>> Total length of wasted lumber : 17 <<-- I get 0 over here!

The reason you are getting a zero here is because you are displaying totalScrapLength, which is not being updated in your addScrapToBin() function.

Try this,

numScraps++;
totalScrapLength = 0;

for( x = 0; x < scraps.length; x++ )
     totalScrapLength += scraps[x];

Commented:
in addition to Exceter's comments, I would like to add this too..
>>
private int removeLongestFromScrapBin() {

     System.arraycopy(scraps, 1, scraps, 0, numScraps-1);
     numScraps--;

return scraps[0];
>>

by doing so, this is returning the value after the array is recreated and not before.. this causes a lot of problems..

u should modify it to this..

private int removeLongestFromScrapBin() {
  int value = scraps[0];
     System.arraycopy(scraps, 1, scraps, 0, numScraps-1);
     numScraps--;

return value;

also in the whole program, u are requried to write only 3 methods.. I am sure u can do it with some debugging.. add a few System.out.println() statements wherever necessary and see what might be going wrong..

also one more thing, u have to handle cases where numScraps = 0 and also if the logLength to be removed is greater than any scrap available..
I tested it with code changes and u should be able to get desired output..
vemul

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial