Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
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 mmd 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 ]
Originally posted by Mike Simmons:
. . . due almost three years ago . . .
gotOriginally 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 ]
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
}
}
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*
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.
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
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.
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 ]
Originally posted by Michael Nguyen:
Yeah thaTS what im having trouble with...
Originally posted by Henry Wong:
Well, you *wrote* the displayLength() method. Don't you know how to call your own method?
Henry
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 ]
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().
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
Originally posted by Michael Nguyen:
Sorry im kinda new at this..but what do you mean
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.
Originally posted by Campbell Ritchie:
Well done. . . Please show us your new method.
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|