Hello! I'm a college student who is taking a beginners Java programming class. For the first time, I really need some assistance figuring out the latest assignment, which involves static methods. I have 5 such assignments, but I only need helping figuring out 1, because then I can do the other 4 on my own. Plus, the textbook (Java Software Solutions by Lewis & Loftus) didn't explain the concepts very well and I am quite lost...

To make things easier I'll post the assignment here, but can you only give me advice on how to get started? I'd greatly appreciate any help!!!

Assignment:
Part1
Write a method named replace that will accept 3 parameters: the original String, the search String, and the replace String. The method should return a new String that has all of the occurrences of the search String replaced by the replace String. For example, replace("Blue shoes are good shoes.", "shoe", "car") would return the String "Blue cars are good cars." The method header must be:
public static String replace (String original, String s, String r)

Part2
Write a Java program named Tester.java that contains a main method. Inside the main method, write statements that will call the methods from LabWork.java. You should try calling each method several times to make sure the method works for all possibilities.

I think the structure for LabWork.java should begin like this, but what to put in the body is where I'm having trouble:

import java.util.Scanner;
public class LabWork
{
     public static String replace (String original, String s, String r)
     {
          // body

     } // end replace method
} // end class

Recommended Answers

All 12 Replies

Something like this maybe???

String faceName = "";

       for(int i = 0; i < faceName.length(); i++){
         if(r.substring(0) == original){
            faceName += s;
         }
         else{
            faceName += r.substring(0);
            }
      } //end for

Btw... I think we are in the same java class. I'm just as confused about this lab as you are. : )

Very interesting Clem999 - I'm gonna play around with your code and see if I can come up with anything. As for the suggestions by Jocamps, I know how to work with strings in general, but the problem I'm having is structuring them in a static method and then invoking them in the tester program. I'll still review the sites you suggested just in case there was something I missed. Thank you very much for your help so far! I'll be back if I'm either able to figure it out or have more questions!

Your welcome, I hope the code works. If it is the same lab, could you help me with the lotsOfStars method... I'm drawing a black at what to put in the body. :(

Write a method named lotsOfStars that will accept two integers: the first integer indicates the number of lines to print, the second the number of asterisks per line. The method should print a rectangular arrangement of asterisks.

public static void lotsOfStars( int l, int a )

THANK YOU!

Hmm yes we must be in the same class because I have that problem too. Are you in Dr. Steiner's 172 class (section M01)? Perhaps we can get together during the Thursday lab and figure stuff out. I'm usually able to figure out the entire assignment after I've seen some examples, so all I need to do is see a fully functioning static method and tester program, and from there I can do the other 4, including the lotsOfStars one. If I manage to figure it out before the deadline I can show you how to do it.

Yep... in that class all right. I could go to the lab tomorrow (I've got nothing better to do). But I can only go until around 3:00-ish. I don't like the Kate editor though, but I really CANNOT fail this lab. I absolutely hate computer programming so any help would be greatly appreciated.

In 118B at 2:30 then? :)

Ok that sounds good - hopefully the TA's are able to help us! Yeah Kate is a bit awkward to use compared to JGrasp. Btw I've come up with something like this for the first assignment: (my idea is to assign variables to the parameters in order to work with them within the class... I'm not sure if this will work, but it's the best I've come up with so far)

import java.util.Scanner;

public class myExample
{
     public static String replace (String original, String s, String r)
     {
	
          String originalText = original;
          String wordToChange = s;
          String newWord = r;
          String newText = original.replaceAll("s","r");
          Return String newText = original.replaceAll("s","r");
					
     } // end replace method
} // end class

Please note that this doesn't compile! I get an error saying the return line is expecting a ;

Haha. Pretty amusing that two people from the exact same class ended up here, assuming that is really the case. Apologize for freeposting/upping this thread but that is really funny.

how on earth did you guys get the 4th and 5th part of the lab assignment?

If you want to talk about your lab you're probably better off meeting with your teacher or TA than discussing it here. But if you're going to discuss it here, post the problem and ask specific questions about what you don't understand so that anyone on the site can help you.

how on earth did you guys get the 4th and 5th part of the lab assignment?

During Lab Dr. Steiner explained that since void methods don't have any return values, in this assignment we need to put a println statement within the method itself, and only call it (don't print) in the test program. If you need any more help contact me and I can give you some assistence because I finished all 5 parts.

I was able to solve the problem I was having and I want to explain it the best I can for anyone else having the same problem.

The code necessary for the assignment above is as follows:

public static String replace (String original, String s, String r)
	{

		String newText = original.replaceAll(s,r);
		return newText;

	} // end replace

As for the tester program, simply put in:

System.out.println("Original sentence: Good night world. New sentence: " + nameOfClass.replace("Good night world", "night", "morning"));

Which will print:

Original sentence: Good night world. New sentence: Good morning world

Note: Do not give the String the same variable name as the method! My CS instructor told me that it doesn't always work and could lead to errors that take up too much time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.