Results 1 to 6 of 6
- 12-13-2011, 08:17 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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
but i'm not sure if i've done it right...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; }
- 12-13-2011, 08:27 PM #2
Re: How to call a master constructor from a minor constructor?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-14-2011, 01:45 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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); } }
- 12-14-2011, 03:04 AM #4
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.
- 12-14-2011, 03:06 AM #5
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.
- 12-14-2011, 01:47 PM #6
Re: How to call a master constructor from a minor constructor?
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
-
What, if we call super() from parents default constructor?
By ezee in forum New To JavaReplies: 3Last Post: 11-01-2011, 08:24 AM -
Why do I need to call my constructor to get this to work?
By fresh83 in forum New To JavaReplies: 7Last Post: 10-28-2011, 07:26 AM -
Unable to call classe constructor from main
By serdimay in forum New To JavaReplies: 13Last Post: 08-30-2011, 10:38 AM -
Constructor call not calling
By Singing Boyo in forum New To JavaReplies: 5Last Post: 06-09-2009, 01:06 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM


4Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks