• jagatuba
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?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
  • jagatuba
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); } }
  • anonymous
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 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.

Looking for something else?

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