Results 1 to 6 of 6
  1. #1
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default 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
    Java 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);
        }
    }
    and Song

    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);
            }
        }
    }
    In the method:

    Java Code:
       
     public Song getSongs()
        { 
            System.out.println(this.song.listSongs());
        }
    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,
    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 :)?

  2. #2
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    30
    Rep Power
    0

    Default 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:
    Java Code:
    public Song getSongs()
       { 
           listSongs();
       }
    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?
    Last edited by FallenBlade; 11-07-2011 at 07:44 PM.

  3. #3
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: External method calls

    Quote Originally Posted by FallenBlade View Post
    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:
    Java Code:
    public Song getSongs()
       { 
           listSongs();
       }
    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?
    I can't make it work with just
    Java Code:
    public Song getSongs()
       { 
           listSongs();
       }
    because its not in the same class, the getSongs-method is in the class Artist and the listSongs-method in the Song-class.

  4. #4
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    30
    Rep Power
    0

    Default Re: External method calls

    Ah I see.

    Ok, then I imagine this would work?

    Java Code:
    public Song getSongs()
       { 
           song.listSongs();
       }
    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.

  5. #5
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: External method calls

    Quote Originally Posted by FallenBlade View Post
    Ah I see.

    Ok, then I imagine this would work?

    Java Code:
    public Song getSongs()
       { 
           song.listSongs();
       }
    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.
    Now it comes a new problem, it says that its missing a return statement..Is the whole idea with this method wrong or why doesn't it work? :S
    Last edited by JultomteN; 11-07-2011 at 09:38 PM.

  6. #6
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    30
    Rep Power
    0

    Default 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:

    Java Code:
    public void getSongs()
       { 
           song.listSongs();
       }
    But where ever you are calling "getSongs()" in the rest of your code, you could just call "song.listSongs()".

Similar Threads

  1. Replies: 0
    Last Post: 02-06-2009, 10:37 PM
  2. Controlling method calls
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •