1

The following method is supposed to create two random String variables and print them. At this point, they are supposed to be identical.

public static void main(String[] args){
    ScalesSolution s1 = new ScalesSolution(10);
    s1.println();
    ScalesSolution s2 = new ScalesSolution(s1.getSol());
    s2.println();
}

The ScalesSolution class:

import java.util.*;

public class ScalesSolution
{
private String scasol;
//Creates a new scales solution based on a string parameter
//The string parameter is checked to see if it contains all zeros and ones
//Otherwise the random binary string generator is used (n = length of parameter)
public ScalesSolution(String s)
{
    boolean ok = true;
    int n = s.length();
    for(int i=0;i<n;++i)
    {
        char si = s.charAt(i);
        if (si != '0' && si != '1') ok = false;
    }
    if (ok)
    {
        scasol = s;
    }
    else
    {
        scasol = RandomBinaryString(n);
    }
}
private static String RandomBinaryString(int n)
{
    String s = new String();
    //Code goes here
    //Create a random binary string of just ones and zeros of length n

    return(s);
}
public ScalesSolution(int n) 
{
    scasol = RandomBinaryString(n); 
}
//This is the fitness function for the Scales problem
//This function returns -1 if the number of weights is less than
//the size of the current solution
public double ScalesFitness(ArrayList<Double> weights)
{
    if (scasol.length() > weights.size()) return(-1);
    double lhs = 0.0,rhs = 0.0;
    int n = scasol.length();

    //Code goes here
    //Check each element of scasol for a 0 (lhs) and 1 (rhs) add the weight wi
    //to variables lhs and rhs as appropriate

    return(Math.abs(lhs-rhs));
}

public void smallChange(int n)
{
    Random rand = new Random();
    int p = (rand.nextInt(n) - 1);

//      Checks if p < 0.  If so, then p will not have 1 subtracted from it.
    if(p < 0){
        p = (rand.nextInt(n));
    }

    String x = new String();

    x = scasol.substring(0, p);
        if (scasol.charAt(p) == '0')
            scasol.replace('0', '1');
        else if (scasol.charAt(p) == '1')
            scasol.replace('1', '0');
            scasol = x;
}//End smallChange()

public String getSol(){

    return(scasol);

}//End getSol()

//Display the string without a new line
public void print()
{
    System.out.print(scasol);
}

//Display the string with a new line
public void println()
{
    print();
    System.out.println();
}


}

The output is blank - what am I doing wrong?

Thanks.

0

3 Answers 3

5

You need to write the part that's mentioned here

String s = new String();
//Code goes here
//Create a random binary string of just ones and zeros of length n

return(s);
Sign up to request clarification or add additional context in comments.

3 Comments

Of course it reaches this code. This code is the part of the exercise he was meant to write. The constructor from int will call the method private static String RandomBinaryString(int n) which will do nothing because he's been asked to implement it.
Yes, but it seems he's making the mistake of thinking that new ScalesSolution(10) is calling the String constructor. Perhaps that could be pointed out to him.
I think you just did. Odds are good he didn't get this far without hearing about constructors, types, and operator overloading.
4

Right now your RandomBinaryString code consists of a new string and a comment:

private static String RandomBinaryString(int n)
{
    String s = new String();
    //Code goes here
    //Create a random binary string of just ones and zeros of length n

    return(s);
}

This means you'll always get a blank string back when you call RandomBinaryString. When the "code goes here" is there, things will most likely work better. :)

Comments

0

Well, what is printed depends on the definition of scasol. From the code above, scasol is going to contain the return value of RandomBinaryString() which, taken as shown, always returns an empty String. There's a comment that says more code goes here -- presumably that code doesn't exist yet?

Can you explain what you expected to be printed?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.