Welcome to Dream.In.Code
Become a Java Expert!

Join 150,429 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,077 people online right now. Registration is fast and FREE... Join Now!




need help wit this "method" problem

 
Reply to this topicStart new topic

need help wit this "method" problem, using series of method to rewrite the song

spit-king
16 Apr, 2008 - 05:27 AM
Post #1

New D.I.C Head
*

Joined: 1 Mar, 2008
Posts: 23

im new to java and i really need ur help guys!
question is:
Using a series of methods, output the popular song "My Bonnie Lies Over the Ocean". However, instead of using "Bonnie", allow the users to enter a name of their choice.

Lyrics:
My Bonnie lies over the ocean
My Bonnie lies over the sea
My Bonnie lies over the ocean
Oh bring back my Bonnie to me
Bring back, bring back
Bring back my Bonnie to me, to me
Bring back, bring back
Bring back my Bonnie to me
Last night as I lay on my pillow
Last night as I lay on my bed
Last night as I lay on my pillow
I dreamed that my Bonnie was dead
Bring back, bring back
Bring back my Bonnie to me, to me
Bring back, bring back
Bring back my Bonnie to me
Oh blow ye the winds o'er the ocean
And blow ye the winds o'er the sea
Oh blow ye the winds o'er the ocean
And bring back my Bonnie to me
Bring back, bring back
Bring back my Bonnie to me, to me
Bring back, bring back
Bring back my Bonnie to me
The winds have blown over the ocean
The winds have blown over the sea
The winds have blown over the ocean
And brought back my Bonnie to me
Bring back, bring back
Bring back my Bonnie to me, to me
Bring back, bring back
Bring back my Bonnie to me

this is what i have so far:
/ The "Lyrics" class.
public class Lyrics
{
public static void main (String[] args)
{
string name;
System.out.println ("enter the name of the user");
name = ReadLib.readString ();

System.out.println ("my" + name + "bonnie lies over the ocean");

} // end of main method

public static double (
} // end of Lyrics class

this is all i have im not sure about the whole;
so can you guys plz help?! plzzzzzz~~~~
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Need Help Wit This "method" Problem
16 Apr, 2008 - 05:56 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
System.out.println ("my" + name + "bonnie lies over the ocean"); should be System.out.println ("my" + name + " lives over the ocean");
After that you don't have to think much.
User is offlineProfile CardPM
+Quote Post

spit-king
RE: Need Help Wit This "method" Problem
16 Apr, 2008 - 12:40 PM
Post #3

New D.I.C Head
*

Joined: 1 Mar, 2008
Posts: 23

but how do u use method to do solve the question? plz help
can u jus do one verse for me
User is offlineProfile CardPM
+Quote Post

pbl
RE: Need Help Wit This "method" Problem
16 Apr, 2008 - 01:20 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(spit-king @ 16 Apr, 2008 - 01:40 PM) *

but how do u use method to do solve the question? plz help
can u jus do one verse for me


1licca explained it to you.
I guess you didn't worked very hard on the problem smile.gif

OK one solution would be....
java

import java.util.Scanner;

/** A class which hold the Lyric in an array of String */
public class Lyrics
{
String[] song = {
"My Bonnie lies over the ocean",
"My Bonnie lies over the sea",
"My Bonnie lies over the ocean",
"Oh bring back my Bonnie to me",
"Bring back, bring back",
"Bring back my Bonnie to me, to me",
"Bring back, bring back"
};

// method that returns the song
String[] getSong() {
return song;
}

// static method to do the job
public static void main (String[] args)
{
// pickup by what replacing "Bonnie"
String name;
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the name of the user: ");
name = scan.next();
// Get an instance of Lyrics
Lyrics lyrics = new Lyrics();
// Get the song lines
String[] str = lyrics.getSong();
// lopp thru all lines
for(int i = 0; i < str.length; i++) {
// replacing "Bonnie" by the name I have scanned
System.out.println(str[i].replaceAll("Bonnie", name));
}
} // end of main method
} // end of Lyrics class


That way, if you decide to put your song in a text file only the getSong() method will have to be changed. It will have to open the file and put every line in a String array.

User is online!Profile CardPM
+Quote Post

spit-king
RE: Need Help Wit This "method" Problem
16 Apr, 2008 - 02:03 PM
Post #5

New D.I.C Head
*

Joined: 1 Mar, 2008
Posts: 23

thank you so much for the answer!!!
just one more question
can you put the whole lyrics in here?:
String[] song = {
"My Bonnie lies over the ocean",
"My Bonnie lies over the sea",
"My Bonnie lies over the ocean",
"Oh bring back my Bonnie to me",
"Bring back, bring back",
"Bring back my Bonnie to me, to me",
"Bring back, bring back"
};
and my school doesnt use scanner so what do i do with this:
Scanner scan = new Scanner(System.in);
User is offlineProfile CardPM
+Quote Post

pbl
RE: Need Help Wit This "method" Problem
16 Apr, 2008 - 02:28 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(spit-king @ 16 Apr, 2008 - 03:03 PM) *

can you put the whole lyrics in here?:


I guess that if you cut & paste the code from this topic you can also cut&paste the lyric
you will just have to add the " before every line
and add a " after every line plus a "," but for the last one

QUOTE(spit-king @ 16 Apr, 2008 - 03:03 PM) *

and my school doesnt use scanner so what do i do with this:
Scanner scan = new Scanner(System.in);


The Scanner object is part of Java, it is used to scan input... in your case the console
so don't worry we won't use your school scanner... would be hard to insert it into your PC
just make sure to put the line
CODE

import java.util.Scanner;

at the beginning of your code as in the code I wrote for you

Before you used
CODE

name = ReadLib.readString ();

but I do not have your ReadLib library so I used a standard Java one which is Scanner


User is online!Profile CardPM
+Quote Post

spit-king
RE: Need Help Wit This "method" Problem
16 Apr, 2008 - 03:36 PM
Post #7

New D.I.C Head
*

Joined: 1 Mar, 2008
Posts: 23

thank you so much for your help man!!!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month