A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing


  • 5 years ago

I have a class in Java I created called Date. What I want to do is extend a subclass SpanishDate from this class and the only different in this subclass is that I want it to change the values in monthArray[] to the Spanish month names. My questions are a) what do I need to do to my Date class to make it extendable in this manner? and b) what should the subclass look like? I mean would it only contain the new monthArray[] variable? Or would I have to create a whole new method?

  • This Question is Closed
  1. jagatuba
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Here is the code in my Date class It compiles and works as it should. I have remove comments to save space, but if you would rather view it with the comments left in just let me know: import java.util.Scanner; public class Date { static String[] monthArray = {"January","February", "March","April","May","June","July","August", "September","October","November","December"}; public static void main(String[] args) { String dateIn = ""; String dateOut = ""; String monthOut = ""; int monthIn = 0; String day = ""; String year = ""; Scanner scan = new Scanner(System.in);//Create Scanner instance dateIn = scan.nextLine(); String delims = "/"; String[] parsedDate = dateIn.split(delims); monthIn = Integer.parseInt(parsedDate[0]); day = parsedDate[1]; year = parsedDate[2]; monthOut = monthArray[monthIn - 1]; dateOut = monthOut + " " + day + ", " + year; System.out.println("The converted date is " + dateOut); } }

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Okay, first, there is no need to double space the code, makes it harder to read, unless this was printed on paper so that you can make changes. Next, since everything is in one method, i.e. main, you really don't get much benefit out of inheritance. What I would do is break this out into it's own class and create some methods. For example, your main should go something like this. import java.Util.scanner; public class ConvertDate { Date myDate = new Date(); // Read in date Scanner scan = new Scanner(System.in); String dateIn = scan.nextLine(); // Pass date in with the format DD/MM/YYYY myDate.setNumericDate(dateIn); // Get the date out in the format DD <Month Name> YYYY String dateOut = myDate.getStringDate(); System.out.println("The converted date is " + dateOut); } Now you will have a class with methods, rather than just one big file. So now you can extend Date with a function called getSpanishStringDate(). The way your program is currently setup, with everything in one method, if you make changes you would replace the current method which basically means rewriting the complete file. Break it up a little like this and you'll gain the ability to reuse code that you have written before. Just make a function in the date class called getMonthString(), then your inherited class can call getSpanishMonthString() or you could set a flag in the date and have your getMonthString return the month name depending on whether the flag is set to Spanish, English, Swedish, etc. Good luck, hopefully this gets you started.

  3. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...


  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.