Results 1 to 6 of 6
Thread: External method calls
- 11-07-2011, 06:31 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
External method calls
Hi, i'm very new at Java and have just starting to learn about if/else and connecting classes and objects together.
Here is the 2 classes i need help with.
Artist
and SongJava Code:public class Artist { private String name; private Song song; public Artist(String name) { this.name = name; } public String getName() { return this.name; } public void setName(String newName) { this.name = newName; } public Song getSongs() { System.out.println(this.song.listSongs()); } public void addSong(String song) { songs.storeSong(song); } }
In the method:Java Code:import java.util.ArrayList; public class Song { private ArrayList<String> songs; private double lengthOfSong; public Song(String songName, double lengthOfSong) { songs = new ArrayList<String>(); this.lengthOfSong = lengthOfSong; } public int getNumberOfSongs() { return songs.size(); } public void storeSong(String song) { songs.add(song); } public void showSong(int songNumber) { if(songNumber < 0) { System.out.println("You need to use a positive number!"); System.out.println(); } else if(songNumber < getNumberOfSongs()) { System.out.println(songs.get(songNumber)); System.out.println(); } else { System.out.println("You have tried a invalid number."); System.out.println("There is total " + songs.size() + " numbers available"); System.out.println(); } } public double getLengthOfSong() { return this.lengthOfSong; } public void listSongs() { for(String song : songs) { System.out.println(song); } } }
would i like to call to the method listSongs in class Song, but it says "void type is not allowed here" and i guess because to listSongs-method is the type void,Java Code:public Song getSongs() { System.out.println(this.song.listSongs()); }
but if i change to String for example, it says that i need a return-statement which i dont want :P so can someone help this poor beginner :)?
- 11-07-2011, 06:41 PM #2
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
Re: External method calls
System.out.println() takes a string as a parameter. So if you place a call to a method in it, that method needs to return a string and not be a void method.
If you're calling a method in the same class that you're in, you don't need to use "this". You can just use "listSongs()".
Your method getSongs(), just needs to call listSongs() to print, as you already have the print statement in listSongs().
So it can be written:
But if that is all the code getSongs() is going to have, then you don't need that method as it's just doing the same as listSongs?Java Code:public Song getSongs() { listSongs(); }Last edited by FallenBlade; 11-07-2011 at 06:44 PM.
- 11-07-2011, 06:54 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
- 11-07-2011, 07:05 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
Re: External method calls
Ah I see.
Ok, then I imagine this would work?
Again, you don't need to call "this" as the variable name "song" should just work. And you don't need a println statement in both methods, as you have it in listSongs.Java Code:public Song getSongs() { song.listSongs(); }
- 11-07-2011, 08:09 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
- 11-07-2011, 08:40 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
Re: External method calls
By saying "public Song" the "Song" part means you need to return a Song object from the method. If you don't want to do that change the "Song" to void. Making it:
But where ever you are calling "getSongs()" in the rest of your code, you could just call "song.listSongs()".Java Code:public void getSongs() { song.listSongs(); }
Similar Threads
-
Is Logging All Method Calls Possible In Eclipse?
By ckh in forum EclipseReplies: 0Last Post: 02-06-2009, 09:37 PM -
Controlling method calls
By bugger in forum New To JavaReplies: 2Last Post: 01-04-2008, 01:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks