Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: 2 Questions about Inheritance

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2 Questions about Inheritance

    Hi!

    a quiz code is attached as a pdf file.

    There are 2 questions I'm not shure.

    1) Line ... is the first line of a mutator method that is inherited by the subclass.

    Answer: 17 ?

    2) Line ... is the first line of a method in the super class that is not inherited by the subclass.


    Answer 31 ?

    Am I right with my guesses?

    Thank you in advance!

    JackforJava
    Attached Files Attached Files

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 Questions about Inheritance

    Please post the code(wrapped in code tags)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 Questions about Inheritance

    public class Month {
    	private int name;
    	private int days;
     
    public Month(int name) {
    	setMonth(name);
    }
     
    public int getName() {
    	return(name);
    }
     
    public int getDays() {
    	return(days);
    }
     
    public void setMonth(int name) {
    	this.name = name;
    	if (name == 9 || name == 4 || name == 6 || name == 11)
    		days = 30;
    	else if(name == 2)
    		days = 28;
    	else
    		days = 31;
    }
     
    public String toString() {
    	return(this.convertToString() + " has " + days + " days in it.");
    }
     
    private String convertToString() {
    	switch (name) {
    		case 1: return "January";
    		case 2: return "February";
    		case 3: return "March";
    		case 4: return "April";
    		case 5: return "May";
    		case 6: return "June";
    		case 7: return "July";
    		case 8: return "August";
    		case 9: return "September";
    		case 10: return "October";
    		case 11: return "November";
    		case 12: return "December";
     
    		default: return "Invalid month";
    	}
    }
    }
    public class SchoolMonth extends Month {
    	private boolean holidays; // are there any holidays in this month?
    	private char semester; // 'F'all, 'S'pring, s'U'mmer
     
    	public SchoolMonth(int name) {
                super(name);
                setSemester();
                setHolidays();
            }
     
     
    	public boolean containsHolidays() {
    		return holidays;
    	}
     
     
    public void setSemester(){
    if (getName() > 7) // Aug - Dec
    semester = 'F';
    else if(getName() > 4) // May - July
    semester = 'U';
    else
    semester = 'S'; // Jan - April
    }
     
     
    public void setHolidays() {
    if (getName() != 3 && getName() !=4 && getName() != 6 && getName() !=8)
    holidays = true;
    }
     
    public String toString(){
    return (super.toString() + " It is in the " + semester + "semester.");
    }
     
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 Questions about Inheritance

    The code does not use the @Override annotation so it is not easy to see what methods are being overridden.

    Also the lines are not numbered so its hard to see where the lines are you are referring to.
    What is on line 17 and 31?

    Also the posted code has lost its indentations at setSemester() ff. making it harder to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 Questions about Inheritance

    Please look at the pdf file.
    Attached Files Attached Files

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 Questions about Inheritance

    Sorry, I'd rather look at text posted in this thread and not have to download a file and open it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 Questions about Inheritance


  8. #8
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 Questions about Inheritance

    Please, have a look I've posted now 2 images.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 Questions about Inheritance

    Can you copy the statements at lines 17 and 31 and post them with comments on why you chose them for the answers?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 Questions about Inheritance

    1. public class Month {
    2. private int name;
    3. private int days;
    4. ...
    5. public Month(int name) {
    6. setMonth(name);
    7. }
    8. ...
    9. public int getName() {
    10. return(name);
    11. }
    12. ...
    13. public int getDays() {
    14. return(days);
    15. }
    16. ...
    17. public void setMonth(int name) {
    18. this.name = name;
    19. if (name == 9 || name == 4 || name == 6 || name == 11)
    20. days = 30;
    21. else if(name == 2)
    22. days = 28;
    23. else
    24. days = 31;
    25. }
    26. ...
    27. public String toString() {
    28. return(this.convertToString() + " has " + days + " days in it.");
    29. }
    30. ...
    31. private String convertToString() {
    32. switch (name) {
    33. case 1: return "January";
    34. case 2: return "February";
    35. case 3: return "March";
    36. case 4: return "April";
    37. case 5: return "May";
    38. case 6: return "June";
    39. case 7: return "July";
    40. case 8: return "August";
    41. case 9: return "September";
    42. case 10: return "October";
    43. case 11: return "November";
    44. case 12: return "December";
    45. ...
    46. default: return "Invalid month";
    47. }
    48. }
    49. }
    50. public class SchoolMonth extends Month {
    51. private boolean holidays; // are there any holidays in this month?
    52. private char semester; // 'F'all, 'S'pring, s'U'mmer
    53. ...
    54. public SchoolMonth(int name) {
    55. super(name);
    56. setSemester();
    57. setHolidays();
    58. }
    59. ...
    60. public boolean containsHolidays() {
    61. return holidays;
    62. }
    63. ...
    64. public void setSemester(){
    65. if (getName() > 7) // Aug - Dec
    66. semester = 'F';
    67. else if(getName() > 4) // May - July
    68. semester = 'U';
    69. else
    70. semester = 'S'; // Jan - April
    71. }
    72. ...
    73. public void setHolidays() {
    74. if (getName() != 3 && getName() !=4 && getName() != 6 && getName() !=8)
    75. holidays = true;
    76. }
    77. ...
    78. public String toString(){
    79. return (super.toString() + " It is in the " + semester +
    80. "semester.");
    81. }
    82. ...
    83. }


    --- Update ---

    The questions:.

    1) Line (?) is the first line of a mutator method that is inherited by the subclass.

    2) Line (?) is the first line of a method in the super class that is not inherited by the subclass.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2 Questions about Inheritance

    Make a list of the methods in the super class and indicate which are inherited by the subclass and which are not inherited by the subclass. Also flag the mutator methods.

    When you have that list you will be able to answer the questions.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 Questions about Inheritance

    It should be:
    1) setMonth() - Line 18
    2) convertToString() - Line 32

  13. #13
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: 2 Questions about Inheritance

    By any chance this exam is the Java SE 7 (number) certification? This a access modifier, private, interface @ override. Parent class and its methods are overwritten in the subclass. May share the same values used when that method is invoked or shared by other classes of that package. I didn't see the exam. Just saw the book on it.

Similar Threads

  1. Inheritance
    By vasanthjayaraman in forum Java Theory & Questions
    Replies: 5
    Last Post: July 10th, 2013, 12:28 PM
  2. List of my Java3D Questions, and Proguard questions
    By Zachary1234 in forum Java SE APIs
    Replies: 0
    Last Post: November 16th, 2012, 09:40 PM
  3. Help with inheritance
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2012, 12:07 AM
  4. Inheritance questions....
    By smellyhole85 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 06:11 PM
  5. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM