Michael Nguyen

Greenhorn
+ Follow
since Sep 05, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Michael Nguyen

Originally posted by Campbell Ritchie:
Well done . . . Please show us your new method.



15 years ago

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....
15 years ago

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
15 years ago

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().
15 years ago

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.
15 years ago

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...
15 years ago

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.

15 years ago

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

15 years ago

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*

15 years ago

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]
15 years ago

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
}
}
15 years ago

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
15 years ago