Two Laptop Bag*
The moose likes Java in General and the fly likes Bad code - Help me fix it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Bad code - Help me fix it" Watch "Bad code - Help me fix it" New topic
Author

Bad code - Help me fix it

Mark Phylus
Greenhorn

Joined: Mar 28, 2004
Posts: 18
I am having trouble implementig a method that formats the length in seconds of a song as a String in the mm d format

I can't figure out where in my Song.java to put this code line

public String displayLength()

And how to add this method in my SongTest.java

public static void testdisplayLength(Song s)

Here are the two classes:



Testing class:

[ September 13, 2005: Message edited by: Mark Phylus ]
[ September 13, 2005: Message edited by: Michael Ernest ]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

I tried cleaning this up -- mostly removing a distracting amount of double-spacing -- and it's just way too messy for me to wade through.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Is this a homework assignment, or did someone give half-complete code for you to complete?
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Mark Phylus:
I am having trouble implementig a method that formats the length in seconds of a song as a String in the mm d format

I can't figure out where in my Song.java to put this code line

public String displayLength()

And how to add this method in my SongTest.java

public static void testdisplayLength(Song s)

Here are the two classes:



Testing class:

[ September 13, 2005: Message edited by: Mark Phylus ]

[ September 13, 2005: Message edited by: Michael Ernest ]


Hey,

Did you ever get this figured out? Because I'm doing the same problem now and I was just hoping if you got the code all figured out. If so, could you help me out? Like on the converting seconds to mm:ss format.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Welcome to JavaRanch, Michael Nguyen.

I think he was hoping you had a solution.

Both of you: please give more details, and explain what goes wrong and what you have done about it.
[ September 06, 2008: Message edited by: Campbell Ritchie ]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
I think that the original poster's homework was due almost three years ago; it's unlikely he's still working on it. But you never know.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Originally posted by Mike Simmons:
. . . due almost three years ago . . .


I never noticed that. But maybe he got an extension.
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Campbell Ritchie:
Welcome to JavaRanch, Michael Nguyen.

I think he was hoping you had a solution.

Both of you: please give more details, and explain what goes wrong and what you have done about it.

[ September 06, 2008: Message edited by: Campbell Ritchie ]
got

I'm supposed to:
A. Implement a method that formats the length in seconds of a song as a String in the mm:ss format.
public String displayLength()

B. Using the established pattern in SongTest.java, write a method
public static void testdisplayLength(Song s)
that formats and prints the length of Song s. Be sure to pick several well-chosen songs to test by calling this method from run.

I think I got part A of the question but I'm not sure how to implement part B of the problem. Any help would be great! Below is the code I have so far.

Thanks

*Code for Song.java*

public class Song {

public final static int MINLENGTH = 120, MAXLENGTH = 600, DEFAULTLENGTH = 180;

private String title, artist;

private int sec;

private int min;

private int length;

//Pre: 120 < l < 600.

// Otherwise, an IllegalArgumentException is thrown

//Post: a Song object has been created

public Song(String t, String a, int l) {
checkIfValid(l);
title = t; // checking for valid arguments
artist = a; // is done later in this lab
length = l;

}

private static void checkIfValid(int l) throws IllegalArgumentException {
if((l <= MINLENGTH) || (l >= MAXLENGTH)) {
throw new IllegalArgumentException("illegal song length");
}
}

//Post: this object's title has been returned

public String getTitle() {
return title;

}

//Post: this object's artist has been returned

public String getArtist() {
return artist;

}

//Post: this object's length has been returned

public int getLength() {
return length;

}

public String toString() {
return title + " by " + artist + " : " + length + "sec";

}

public Song(String title) {
//call another constructor whose parameter list is String, String, int
//the word "this" must be used here
this(title,new String("Unknown"),DEFAULTLENGTH);

}

public Song(String title, int length) {
this(title,new String("Unknown"),length);

}

public String displayLength() {
min = length/60;
sec = length%60;
if(sec < 10) {
return min + ":" + 0 + sec;
}
else {
return min + ":" + sec;
}
}
}

*Code for SongTest.java*

public class SongTest {

public static void main(String[] args) {

SongTest test = new SongTest();
test.run();

}

private void run() {
Song s1;
Song s2;
Song s3;
String t1 = new String("title1");
String t2 = new String("t2");
String a1 = new String("artist1");
String a2 = new String("a2");
System.out.println("\nTesting improved constructor:");
s1 = testConstructor(t1,a1, 24); //illegal
s1 = testConstructor(t1, a1, 765); //illegal
s1 = testConstructor(t1, a1, 248); //legal
s1 = testConstructor(t1, a1, 600); //illegal
s2 = testConstructor(t1);
s3 = testConstructor("t1", 234);

System.out.println("\nConstructing songs:");
s1 = new Song(t1,a1,325);
System.out.println(s1.toString());

try {
s1 = new Song(t2,a2,-23);
}

catch(IllegalArgumentException e) {
System.out.println(e);
System.out.println("Song not created");
}
System.out.println(s1);

Song song1 = new Song(t1,a1,200);
Song song2 = new Song(t1,a1,200);
System.out.println(song1 + " == " + song2 + " is " + (song1 == song2));

}

//Pre: t, a, and l form a legal song. If not, an IllegalArgumentException is
// caught and null returned
//Post: A new Song object is created, printed and returned.
private Song testConstructor(String t, String a, int l) {
Song song = null; //must be initialized for the return statement
try {
song = new Song(t,a,l); //try to construct a new Song
} catch(IllegalArgumentException e) //if an Exception is thrown, catch it
{
System.out.println(e); //print the Exception
return null; //return null reference
}
System.out.println(song + " created"); //print and return the new Song
return song;

}

private static Song testConstructor(String t) {
Song song;
try {
song = new Song(t);
} catch(IllegalArgumentException e) //catch the exception
{
System.out.println(e); //print the exception
return null; //return null reference & exit
}
System.out.println(song + " created"); //print newly created song
return song; //return the new Song
}

private Song testConstructor(String t, int l) {
Song song;
try {
song = new Song(t,l);
} catch(IllegalArgumentException e) //catch the exception
{
System.out.println(e); //print the exception
return null; //return null reference & exit
}
System.out.println(song + " created"); //print newly created song
return song; //return the new Song
}
}
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Michael Nguyen:
got

I'm supposed to:
A. Implement a method that formats the length in seconds of a song as a String in the mm:ss format.
public String displayLength()

B. Using the established pattern in SongTest.java, write a method
public static void testdisplayLength(Song s)
that formats and prints the length of Song s. Be sure to pick several well-chosen songs to test by calling this method from run.

I think I got part A of the question but I'm not sure how to implement part B of the problem. Any help would be great! Below is the code I have so far.

Thanks


private String title, artist;

private int sec;

private int min;

private int length;

//Pre: 120 < l < 600.

// Otherwise, an IllegalArgumentException is thrown

//Post: a Song object has been created

public Song(String t, String a, int l) {
checkIfValid(l);
title = t; // checking for valid arguments
artist = a; // is done later in this lab
length = l;

}

private static void checkIfValid(int l) throws IllegalArgumentException {
if((l <= MINLENGTH) || (l >= MAXLENGTH)) {
throw new IllegalArgumentException("illegal song length");
}
}

//Post: this object's title has been returned

public String getTitle() {
return title;

}

//Post: this object's artist has been returned

public String getArtist() {
return artist;

}

//Post: this object's length has been returned

public int getLength() {
return length;

}

public String toString() {
return title + " by " + artist + " : " + length + "sec";

}

public Song(String title) {
//call another constructor whose parameter list is String, String, int
//the word "this" must be used here
this(title,new String("Unknown"),DEFAULTLENGTH);

}

public Song(String title, int length) {
this(title,new String("Unknown"),length);

}

public String displayLength() {
min = length/60;
sec = length%60;
if(sec < 10) {
return min + ":" + 0 + sec;
}
else {
return min + ":" + sec;
}
}
}

*Code for SongTest.java*

public class SongTest {

public static void main(String[] args) {

SongTest test = new SongTest();
test.run();

}

private void run() {
Song s1;
Song s2;
Song s3;
String t1 = new String("title1");
String t2 = new String("t2");
String a1 = new String("artist1");
String a2 = new String("a2");
System.out.println("\nTesting improved constructor:");
s1 = testConstructor(t1,a1, 24); //illegal
s1 = testConstructor(t1, a1, 765); //illegal
s1 = testConstructor(t1, a1, 248); //legal
s1 = testConstructor(t1, a1, 600); //illegal
s2 = testConstructor(t1);
s3 = testConstructor("t1", 234);

System.out.println("\nConstructing songs:");
s1 = new Song(t1,a1,325);
System.out.println(s1.toString());

try {
s1 = new Song(t2,a2,-23);
}

catch(IllegalArgumentException e) {
System.out.println(e);
System.out.println("Song not created");
}
System.out.println(s1);

Song song1 = new Song(t1,a1,200);
Song song2 = new Song(t1,a1,200);
System.out.println(song1 + " == " + song2 + " is " + (song1 == song2));

}

//Pre: t, a, and l form a legal song. If not, an IllegalArgumentException is
// caught and null returned
//Post: A new Song object is created, printed and returned.
private Song testConstructor(String t, String a, int l) {
Song song = null; //must be initialized for the return statement
try {
song = new Song(t,a,l); //try to construct a new Song
} catch(IllegalArgumentException e) //if an Exception is thrown, catch it
{
System.out.println(e); //print the Exception
return null; //return null reference
}
System.out.println(song + " created"); //print and return the new Song
return song;

}

private static Song testConstructor(String t) {
Song song;
try {
song = new Song(t);
} catch(IllegalArgumentException e) //catch the exception
{
System.out.println(e); //print the exception
return null; //return null reference & exit
}
System.out.println(song + " created"); //print newly created song
return song; //return the new Song
}

private Song testConstructor(String t, int l) {
Song song;
try {
song = new Song(t,l);
} catch(IllegalArgumentException e) //catch the exception
{
System.out.println(e); //print the exception
return null; //return null reference & exit
}
System.out.println(song + " created"); //print newly created song
return song; //return the new Song
}
}


[LIST][LIST][/CODE]
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Michael Nguyen:
got

I'm supposed to:
A. Implement a method that formats the length in seconds of a song as a String in the mm:ss format.
public String displayLength()

B. Using the established pattern in SongTest.java, write a method
public static void testdisplayLength(Song s)
that formats and prints the length of Song s. Be sure to pick several well-chosen songs to test by calling this method from run.

I think I got part A of the question but I'm not sure how to implement part B of the problem. Any help would be great! Below is the code I have so far.

Thanks

*Code for Song.java*



*Code for SongTest.java*

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

I think I got part A of the question but I'm not sure how to implement part B of the problem. Any help would be great! Below is the code I have so far.


Well, can you tell us exactly what are you having problems with? And why? What have you tried so far? And why didn't they work?

If you are very exact, you will likely get some hints towards the solution. If you just throw "code over the wall", it is generally not very productive.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Thank you for the e-mail, but we don't send answers via e-mail. Henry has already told you, please provide more details about what has gone wrong and about what you have done. Then we shall better be able to help.
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Henry Wong:


Well, can you tell us exactly what are you having problems with? And why? What have you tried so far? And why didn't they work?

If you are very exact, you will likely get some hints towards the solution. If you just throw "code over the wall", it is generally not very productive.

Henry


I've made the method public String displayLength()in the Song.java class to format the length of a song in mm:ss format. But I'm having trouble implementing the public static void testdisplayLength(Song s) I got it to print out the length of the song in mm:ss format but now I just found out that if i exclude the public static void testdisplayLength(Song s) I still get it to output in mm:ss format, but I need to implement the method public static void testdisplayLength(Song s) How can I implement public static void testdisplayLength(Song s) in SongTest.java to pick a song to test and format its length to mm:ss. I hope this is what your looking for.

The method in Song.java




The method in SongTest.java

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Please show us what you plan to put in the testDisplayLength() method. Apart from there being a few spelling errors (check carefully about d against D) which might cause problems, what goes in that method can be very simple.
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Campbell Ritchie:
Please show us what you plan to put in the testDisplayLength() method. Apart from there being a few spelling errors (check carefully about d against D) which might cause problems, what goes in that method can be very simple.


This is what I had.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Far too complicated. You can lose about � of that.

But that won't print the song's length method; the println() method calls something else which you can look up here.
[ September 07, 2008: Message edited by: Campbell Ritchie ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Generally, for a method called testDisplayLength(), it can be presumed that you did it to test the displayLength() method that you wrote eariler. How to you test the displayLength() method, if you don't actually call the displayLength() method?

Henry
[ September 07, 2008: Message edited by: Henry Wong ]
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Henry Wong:
Generally, for a method called testDisplayLength(), it can be presumed that you did it to test the displayLength() method that you wrote eariler. How to you test the displayLength() method, if you don't actually call the displayLength() method?

Henry

[ September 07, 2008: Message edited by: Henry Wong ]


Yeah thaTS what im having trouble with...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Originally posted by Michael Nguyen:


Yeah thaTS what im having trouble with...


Well, you *wrote* the displayLength() method. Don't you know how to call your own method?

Henry
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Henry Wong:


Well, you *wrote* the displayLength() method. Don't you know how to call your own method?

Henry


yeah, for example i have



to call that. but i have to implement the


and i can't get this to work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
No, you won't get the method you wrote in reply to my post to work.

You seem to have written the contents for the testDisplayLength method and forgotten where you put it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Now I am confused. When the songs are called s2 and s3, you can call the methods fine. But when you rename it to s, and pass it as a parameter, you have to do it differently? Why can't you call the displayLength() method the same way?

Henry
[ September 07, 2008: Message edited by: Henry Wong ]
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Henry Wong:
Now I am confused. When the songs are called s2 and s3, you can call the methods fine. But when you rename it to s, and pass it as a parameter, you have to do it differently? Why can't you call the displayLength() method the same way?

Henry

[ September 07, 2008: Message edited by: Henry Wong ]


Yeah I have to make a method public static void testdisplayLength(Song s) that formats and prints the length of Song s. And I'm supposed to call this method from the method run().
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Originally posted by Michael Nguyen:


Yeah I have to make a method public static void testdisplayLength(Song s) that formats and prints the length of Song s. And I'm supposed to call this method from the method run().


So do it.... What is stopping you? Obviously, you did it already -- as inline code. Why can't you re-code the 2 lines into the method? It's just a cut-n-paste with a variable name change.

Henry
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Henry Wong:


So do it.... What is stopping you? Obviously, you did it already -- as inline code. Why can't you re-code the 2 lines into the method? It's just a cut-n-paste with a variable name change.

Henry


Sorry im kinda new at this..but what do you mean
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Originally posted by Michael Nguyen:


Sorry im kinda new at this..but what do you mean


Wow, you got me. I am completely out of hints...

I can't figure out another way of explaining it -- short of actually doing it for you, which is not allowed on the ranch.


The only thing I can say is... You did it already. You just have to do it in the right place. And I don't know of another way to explain how.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
You wrote a method which didn't work in a posting.
In the same posting you actually wrote some statements which would have made a good body for the method which didn't work and all you had to do was put one of those lines in the method and change "s2" to "s".

And you didn't put the two together.
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Campbell Ritchie:
You wrote a method which didn't work in a posting.
In the same posting you actually wrote some statements which would have made a good body for the method which didn't work and all you had to do was put one of those lines in the method and change "s2" to "s".

And you didn't put the two together.


Thanks I finally figured it out....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Well done . . . Please show us your new method.
Michael Nguyen
Greenhorn

Joined: Sep 05, 2008
Posts: 12
Originally posted by Campbell Ritchie:
Well done . . . Please show us your new method.


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Now I hope you understand what I meant by "just a cut-n-paste with a variable name change" and why I ran "out of hints"...

Great that you finally figured it out though...
Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bad code - Help me fix it
 
Similar Threads
exam lab gc question
How many Objects are elligible for Gabbage Collection at Line 17?
Garbage collection
Garbage Collection
Strings