Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8954301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:17:37+00:00 2026-06-15T14:17:37+00:00

I got a simple question. I have a class, which I use for purpose

  • 0

I got a simple question. I have a class, which I use for purpose of splitting a string in 2 years:

public class Period {

    int firstYear;
    int secondYear;

    Period () {

    }

    Period(String periode) {

        String [] periodeSplit = periode.split("-");
        this.firstYear = Integer.parseInt(periodeSplit[0]);
        this.secondYear = Integer.parseInt(periodeSplit[1]);
    }

    public String toString() {

        return  "Firstyear: " + this.firstYear + "\n" + "Secondyear: " + this.secondYear;
    }
}

I now want to extend this class, not splitting the data into 2 different ints but into 3 different ints. So besides the 2 already exisiting integer vars I want one extra. Whats the easiest way of doing this?

Your help is appreciated!

Kind regards,

Kipt Scriddy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:17:38+00:00Added an answer on June 15, 2026 at 2:17 pm

    I think it would be better (and quite easy) the create more general class that will be able to deal with any number of years you pass to it:

    public class Period {
    
        int[] years;
    
        Period() {
    
        }
    
        Period(String periode) {
    
            String[] periodeSplit = periode.split("-");
            years = new int[periodeSplit.length];
            for (int i = 0; i < periodeSplit.length; i++) {
                years[i] = Integer.parseInt(periodeSplit[i]);
            }
        }
    
        public String toString() {
            String result = "";
            for (int i = 0; i < years.length; i++) {
                result += "Year " + i + ":" + years[i] + "\n";
            }
            return result;
        }
    }
    

    If the original class really have to be extended than it can be done like this:

    class ExtendedPeriod extends Period {
        int thirdPart;
    
        ExtendedPeriod(String periode) {
            String[] periodeSplit = periode.split("-");
            this.firstYear = Integer.parseInt(periodeSplit[0]);
            this.secondYear = Integer.parseInt(periodeSplit[1]);
            this.thirdPart = Integer.parseInt(periodeSplit[1]);
        }
    
        public String toString() {
            return "Day: " + this.firstYear + "\n" + "Month: " + this.secondYear
                    + "\nYear: " + this.thirdPart;
        }
    }
    

    I would recommand to change variable names ‘firstYear’ and ‘secondYear’ to something different, like ‘firstPart’, ‘secondPart’ because for extendedPeriod they aren’t years anymore (I left them in my code so it would compile with yours but called the new int ‘thirdPart’). I don’t feel that this is the best use of inheritance but if that’s what’s needed. I also wanted to reuse toString from Period like this:

    public String toString2() {
            return super.toString() + "\nThird part: " + this.thirdPart;
        }
    

    but for it to have sense you would have to chagne toString method in Period not to call values ‘years’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

got a very simple question which I can't seem to find any answer for.
I've got a simple question for you :) I have the following Scala code,
Let's say we have very simple Java class MyClass . public class MyClass {
This is a simple question, I came across a scenario which got me thinking
I've got a simple question but it has got me confounded. The code below
I've got a very simple question around Rails MVC/application design with a focus on
I've got a relatively simple question about asp.net MVC models. I've got a model
Simple question but I got a headache to solve this game. Example regex. [a-zA-Z0-9\s]
Simple question: If I've got a generic Error.aspx page in my Shared folder (and
Pretty simple question. I've got some Polygons and GroundOverlays defined in KML. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.