Results 1 to 6 of 6
Like Tree4Likes
  • 2 Post By KevinWorkman
  • 1 Post By Junky
  • 1 Post By KevinWorkman

Thread: How to call a master constructor from a minor constructor?

  1. #1
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default How to call a master constructor from a minor constructor?

    I'm writing a program where i want to call the master constructor from a minor constructor. I have wrote
    Java Code:
    public Track(String artist, String title, String album, int year,
    			Genre genre) {
    		// check parameters and assign data fields
    		assignValues(artist, title, album, year, genre);
    	}
    public Track(String artist, String title, String album, Genre genre) {
    		// TODO: put specified parameters alongside with default values
    		// for the unspecified ones into the master constructor
    		// (or into a higher tier minor constructor)
    		this.artist = DEFAULT_ARTIST;
    		this.title = DEFAULT_TITLE;
    		this.album = DEFAULT_TITLE;
    		this.genre = DEFAULT_GENRE;
    	}
    but i'm not sure if i've done it right...

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default Re: How to call a master constructor from a minor constructor?

    DarrylBurke and b.m like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Re: How to call a master constructor from a minor constructor?

    thnx for the link!!! I tried to understand that but i guess i'm missing sth here.
    i wrote a test method for the program but still i don't get the result i want.
    Can somebody tell me what i do wrong? I get null values in the end :S:S
    Java Code:
    import java.util.Calendar;
    import java.lang.Object;
    
    public class Song {
    
    	/**
    	 * enumeration for different music genres (just a few examples implemented
    	 * here)
    	 */
    	public enum Genre {
    		UNDEFINED, POP, ROCK, ALTERNATIVE, ELECTRONIC, INDUSTRIAL
    	}
    
    	/**
    	 * global constants
    	 */
    	// number of data fields (artist, title, album, year, genre)
    	private static final int NUMBER_OF_FIELDS = 5;
    	// max. length of artist-, title- and album-strings
    	private static final int MAX_STRING_LENGTH = 30;
    	// abbreviation character for strings exceeding max. length
    	private static final char ABBREVIATION_CHAR = '.';
    
    	/**
    	 * constant default values for all meta data fields
    	 */
    	private static final String DEFAULT_ARTIST = "";
    	private static final String DEFAULT_TITLE = "";
    	private static final String DEFAULT_ALBUM = "";
    	private static final int DEFAULT_YEAR = 0;
    	private static final Genre DEFAULT_GENRE = Genre.UNDEFINED;
    	
    	/**
    	 * data fields holding meta information on music tracks
    	 */
    	private String artist;
    	private String title;
    	private String album;
    	private int year;
    	private Genre genre;
    
    	/**
    	 * master constructor: takes all parameters, checks them for validity and
    	 * assigns them to the data fields
    	 */
    	public Song(String artist, String title, String album, int year,
    			Genre genre) {
    		// check parameters and assign data fields
    		assignValues(artist, title, album, year, genre);
    	}
    
    	/**
    	 * minor constructor: calls master constructor
    	 */
    	public Song(String artist, String title, String album, int year) {
    		// putting the specified parameters alongside with default value
    		// of genre into the master constructor
    		assignValues(artist, title, album, year, DEFAULT_GENRE);
    	}
    
    	/**
    	 * minor constructor: calls master constructor
    	 */
    	public Song(String artist, String title, String album, Genre genre) {
    		//put specified parameters alongside with default values
    		// for the unspecified ones into the master constructor
    		// (or into a higher tier minor constructor)
    		assignValues(artist, title, album, DEFAULT_YEAR, genre);
    	}
    
    	/**
    	 * minor constructor: calls master constructor
    	 */
    	public Song(String artist, String title, int year, Genre genre) {
    		//put specified parameters alongside with default values
    		// for the unspecified ones into the master constructor
    		// (or into a higher tier minor constructor)
    		assignValues(artist, title, DEFAULT_ALBUM, year, genre);
    	}
    
    	/**
    	 * minor constructor: calls master constructor
    	 */
    	public Song(String artist, String title, String album) {
    		//put specified parameters alongside with default values
    		// for the unspecified ones into the master constructor
    		// (or into a higher tier minor constructor)
    		assignValues(artist, title, album, DEFAULT_YEAR, DEFAULT_GENRE);
    	}
    
    	/**
    	 * minor constructor: calls master constructor
    	 */
    	public Song(String artist, String title, int year) {
    		//put specified parameters alongside with default values
    		// for the unspecified ones into the master constructor
    		// (or into a higher tier minor constructor)
    		assignValues(artist, title, DEFAULT_ALBUM, year, DEFAULT_GENRE);
    	}
    
    	/**
    	 * minor constructor: calls master constructor
    	 */
    	public Song(String artist, String title, Genre genre) {
    		//put specified parameters alongside with default values
    		// for the unspecified ones into the master constructor
    		// (or into a higher tier minor constructor)
    		assignValues(artist, title, DEFAULT_ALBUM, DEFAULT_YEAR, genre);
    	}
    
    	/**
    	 * minor constructor: calls master constructor
    	 */
    	public Song(String artist, String title) {
    		// put specified parameters alongside with default values
    		// for the unspecified ones into the master constructor
    		// (or into a higher tier minor constructor)
    		assignValues(artist, title, DEFAULT_ALBUM, DEFAULT_YEAR, DEFAULT_GENRE);
    		
    	}
    
    	
    	/**
    	 * validity check: checks string length and year number for validity and
    	 * applies changes if necessary before assigning them to the data fields
    	 */
    	private void assignValues(String artist, String title, String album,
    			int year, Genre genre) {
    	        // check String parameters and shorten the
    		// if necessary (trimText-method)
    		if (artist.length()>10){
    			artist = transfuse(artist, ABBREVIATION_CHAR , 10);
    		}
    		if (title.length()>10){
    			title = transfuse(title, ABBREVIATION_CHAR , 10);
    		}
    		if (album.length()>10){
    			album = transfuse(album, ABBREVIATION_CHAR , 10);
    		}
    
    
    		//check year not to be smaller than DEFAULT_YEAR or
    		// larger than current year
    		Calendar cal = Calendar.getInstance();
    		int currentYear = cal.get(java.util.Calendar.YEAR);
    		if(year<DEFAULT_YEAR && year>currentYear){
    			System.out.println("Year not valid");
    		}
    
    		//assign genre parameter without any validity check
    		genre = DEFAULT_GENRE;
    	}
    	
    	
    	
    	/**
    	 * overloads original method from java.lang.object
    	 * 
    	 * @return single string representation of all data fields
    	 */
    	public String toString() {
    		return artist + " | " + title + " | " + album + " | " + year + " | "
    				+ genre;
    	}
    
    	/**
    	 * trims a string to a specified length and appends an abbreviation character
    	 */
    	private static String transfuse(String text, char c, int length) {
    		// implement functionality
    		// transfuse("long text parameter", '.', 10);
    		// --> "long text."
    		 if (text != null && text.length() > length && c == '.')
    			    text = text.substring(0, length-1) + '.';
    			  return text;
    	}
    	
    	public static void main(String args[]){
    		Song p = new Song("Queens","We will rock you");
    		System.out.println(p);
    		}
    
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: How to call a master constructor from a minor constructor?

    I have no idea what a master constructor or a minor constructor is.

    You can have a constructor that does nothing.
    You can have a constructor that initialises instance variables.
    You can have a constructor that calls a method.
    You can have a constructor that calls another constructor in the same class.
    You can have a constructor that calls a constructor in the parent class. (which is does by default)

    I have no idea what your problem is. If you are getting error messages then copy and paste the full message and indicate on which line they occur.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: How to call a master constructor from a minor constructor?

    I just had a look at your assignValues method. Your problem is that the parameters and instance variables have the same names.
    b.m likes this.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default Re: How to call a master constructor from a minor constructor?

    Quote Originally Posted by Junky View Post
    I just had a look at your assignValues method. Your problem is that the parameters and instance variables have the same names.
    b.m, and the solution to that is in the link I gave you!
    b.m likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: 11-01-2011, 08:24 AM
  2. Replies: 7
    Last Post: 10-28-2011, 07:26 AM
  3. Unable to call classe constructor from main
    By serdimay in forum New To Java
    Replies: 13
    Last Post: 08-30-2011, 10:38 AM
  4. Constructor call not calling
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-09-2009, 01:06 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:10 AM

Posting Permissions

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