Yeah, this is homework, but I have spent many hours and have asked many other students at my school to help and so far we haven't solved my problem. I am trying to write a program to figure out the first fit, best fit, and worst fit. I have randomly read in five values and had the user input the value that we are checking for. However, when I run the program it only looks at the first value. (So I assume the problem is in the for loops). Any help would be appreciated.
Here is my code:

import java.util.*;
public class MemorySim{


ArrayList <Integer> myMemoryHoles;
private int value;
private int Max;
private int Min;


public static void main(String[] args) {


MemorySim memorySim = new MemorySim();
memorySim.setMemoryHoles( new ArrayList<Integer>());
ArrayList<Integer> myMemoryHoles2 = memorySim.getMemoryHoles();


Random intRandom = new Random();


for (int i = 0; i < 6; i++) {
myMemoryHoles2.add(intRandom.nextInt(1000));
}


System.out.println("The random values of MB in the ArrayList are: ");


Iterator <Integer> it = memorySim.getMemoryHoles().iterator();


while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println();


//Use the Collections sort() to order the elements


Collections.sort(memorySim.getMemoryHoles());
System.out.println("The ordered values of MB in the ArrayList are: ");
Iterator <Integer>i = memorySim.getMemoryHoles().iterator();


while(i.hasNext())
{
System.out.println(i.next());
}
//Store this Array as a variable I can use for best fit
System.out.println();


/*
//Use the Collections reverse() to reverse order the elements
Collections.reverse(memorySim.getMemoryHoles());
System.out.println("The reverse ordered values of MB in the ArrayList are: ");
Iterator <Integer> iter = memorySim.getMemoryHoles().iterator();
while(iter.hasNext())
{
System.out.println(iter.next());
}
*/


System.out.println();
//Print out the maximum and minimum values
System.out.println("The maximum value in the ArrayList is: ");
System.out.println( Collections.max( memorySim.getMemoryHoles()));


System.out.println();


System.out.println("The minimum value of MB in the ArrayList is: ");
System.out.println( Collections.min( memorySim.getMemoryHoles()));
//memorySim.getMemoryHoles()


memorySim.setMin ( Integer.parseInt(Collections.min(memorySim.getMemoryHoles()).toString()) );
memorySim.setMax ( Integer.parseInt(Collections.max(memorySim.getMemoryHoles()).toString()) );


Scanner input = new Scanner(System.in);


System.out.print("Enter number of MB needing space: ");
memorySim.value = input.nextInt();



memorySim.firstFit();
memorySim.worstFit();
memorySim.bestFit();


} //end of main()


private ArrayList<Integer> getMemoryHoles() {
return myMemoryHoles;
}


private void setMemoryHoles(ArrayList<Integer> arrayList) {
myMemoryHoles = arrayList;


}


public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getMax() {
return Max;
}
public void setMax(int max) {
Max = max;
}
public int getMin(){
return Min;
}
public void setMin(int min){
Min = min;
}


public void firstFit(){



int index;
for( j = 0; j < myMemoryHoles.size(); j++){


//for ( int index: myMemoryHoles){


if( value <= myMemoryHoles.get(j) ){


System.out.printf("First fit value: %d MB fits in %d MB hole", value, j);
System.out.println();


return;
}
else
{
System.out.printf("%d MB will have to wait for first fit.", value);
System.out.println();


return;
}


}
}
public void worstFit(){


//for (k = 0; k < myMemoryHoles.size(); k++){
for (int index : myMemoryHoles){
if( value <= Max){
System.out.printf("Worst fit for %d MB is in %d MB hole", value, index);
//System.out.printf("Worst fit for %d MB is in %d MB hole ", value, myMemoryHoles.get(i));
System.out.println();
return;
}


else{
System.out.printf("%d MB will have to wait for worst fit.", value);
System.out.println();
return;


}
}
}
public void bestFit(){
int m = 0;
for (int l = 0; l < myMemoryHoles.size(); l++){
int n = myMemoryHoles.get(l) - value;
if (n > 0){
m = n;
if (myMemoryHoles.get(l) - value > m){
m = myMemoryHoles.get(l);



System.out.printf("Best fit for %d MB is in %d MB hole", value, myMemoryHoles.get(l));
System.out.println();


return;
}//


else{
System.out.printf("%d MB will have to wait for best fit.", value);
System.out.println();
return;
}



}


}//close for loop
}//close bestFit
} //end of class

Edited 3 Years Ago by happygeek: fixed formatting

You need code tags, formatting, and a more precise question: Best fit, worst fit? Best fit and worst fit of what? What exactly is the problem? Here's your code with code tags and formatting. It's much easier to read, though it looks better in an IDE than on here.

import java.util.*;

public class MemorySim
{

    ArrayList<Integer> myMemoryHoles;
    private int value;
    private int Max;
    private int Min;

    public static void main(String[] args)
    {

        MemorySim memorySim = new MemorySim();
        memorySim.setMemoryHoles(new ArrayList<Integer>());
        ArrayList<Integer> myMemoryHoles2 = memorySim.getMemoryHoles();

        Random intRandom = new Random();

        for (int i = 0; i < 6; i++) {
            myMemoryHoles2.add(intRandom.nextInt(1000));
        }

        System.out.println("The random values of MB in the ArrayList are: ");

        Iterator<Integer> it = memorySim.getMemoryHoles().iterator();

        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println();

//Use the Collections sort() to order the elements

        Collections.sort(memorySim.getMemoryHoles());
        System.out.println("The ordered values of MB in the ArrayList are: ");
        Iterator<Integer> i = memorySim.getMemoryHoles().iterator();

        while (i.hasNext()) {
            System.out.println(i.next());
        }
//Store this Array as a variable I can use for best fit
        System.out.println();

        /*
        //Use the Collections reverse() to reverse order the elements
        Collections.reverse(memorySim.getMemoryHoles());
        System.out.println("The reverse ordered values of MB in the ArrayList are: ");
        Iterator <Integer> iter = memorySim.getMemoryHoles().iterator();
        while(iter.hasNext())
        {
        System.out.println(iter.next());
        }
         */

        System.out.println();
//Print out the maximum and minimum values
        System.out.println("The maximum value in the ArrayList is: ");
        System.out.println(Collections.max(memorySim.getMemoryHoles()));

        System.out.println();

        System.out.println("The minimum value of MB in the ArrayList is: ");
        System.out.println(Collections.min(memorySim.getMemoryHoles()));
//memorySim.getMemoryHoles()

        memorySim.setMin(Integer.parseInt(Collections.min(memorySim.getMemoryHoles()).toString()));
        memorySim.setMax(Integer.parseInt(Collections.max(memorySim.getMemoryHoles()).toString()));

        Scanner input = new Scanner(System.in);

        System.out.print("Enter number of MB needing space: ");
        memorySim.value = input.nextInt();


        memorySim.firstFit();
        memorySim.worstFit();
        memorySim.bestFit();

    } //end of main()

    private ArrayList<Integer> getMemoryHoles()
    {
        return myMemoryHoles;
    }

    private void setMemoryHoles(ArrayList<Integer> arrayList)
    {
        myMemoryHoles = arrayList;

    }

    public int getValue()
    {
        return value;
    }

    public void setValue(int value)
    {
        this.value = value;
    }

    public int getMax()
    {
        return Max;
    }

    public void setMax(int max)
    {
        Max = max;
    }

    public int getMin()
    {
        return Min;
    }

    public void setMin(int min)
    {
        Min = min;
    }

    public void firstFit()
    {


        int index;
        for (j = 0; j < myMemoryHoles.size(); j++) {

//for ( int index: myMemoryHoles){

            if (value <= myMemoryHoles.get(j)) {



                System.out.printf("First fit value: %d MB fits in %d MB hole", value, j);
                System.out.println();

                return;
            } else {
                System.out.printf("%d MB will have to wait for first fit.", value);
                System.out.println();

                return;
            }



        }
    }

    public void worstFit()
    {

//for (k = 0; k < myMemoryHoles.size(); k++){
        for (int index : myMemoryHoles) {
            if (value <= Max) {
                System.out.printf("Worst fit for %d MB is in %d MB hole", value, index);
//System.out.printf("Worst fit for %d MB is in %d MB hole ", value, myMemoryHoles.get(i));
                System.out.println();
                return;
            } else {
                System.out.printf("%d MB will have to wait for worst fit.", value);
                System.out.println();
                return;

            }
        }
    }

    public void bestFit()
    {
        int m = 0;
        for (int l = 0; l < myMemoryHoles.size(); l++) {
            int n = myMemoryHoles.get(l) - value;
            if (n > 0) {
                m = n;
                if (myMemoryHoles.get(l) - value > m) {
                    m = myMemoryHoles.get(l);


                    System.out.printf("Best fit for %d MB is in %d MB hole", value, myMemoryHoles.get(l));
                    System.out.println();

                    return;
                }//
                else {
                    System.out.printf("%d MB will have to wait for best fit.", value);
                    System.out.println();
                    return;
                }


            }

        }//close for loop
    }//close bestFit
} //end of class

Thank you for the code tags - how do I do that if I ever need to post again?
My problem is that I'm trying to come up with a memory simulation that simulations having holes in memory and using first fit (the first hole in memory large enough for the program), worst fit ( the largest hole in memory) and best fit (the hole closest in size of the program).

The problem is that when I run my program it only looks at the first of the five "holes" in memory and tells if the user-defined hole will fit it and doesn't go through the rest of the loop to find the answer.

Did you fix this? If so, any chance of posting the correct code? Thanks in advance.

@ACW1989

Please do not resurrect old threads. If you have any questions please ask. .... You are welcome to start your own threads.

Thread Closed.

This article has been dead for over six months. Start a new discussion instead.