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
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...
Re: How to call a master constructor from a minor constructor?
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
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);
}
}
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.
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.
Re: How to call a master constructor from a minor constructor?
Quote:
Originally Posted by
Junky
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!