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 4 of 4

Thread: How to develop a class MonthDate to print out the following output ?

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to develop a class MonthDate to print out the following output ?

    How to develop a class MonthDate to print out the following output ? I know one of the method is to use toString, but how?


    Code:

    import java.util.Arrays;
    public class SortMonthDate {
    public static void main(String[] args) {
    MonthDate[] mdates = new MonthDate[5];
    mdates[0] = new MonthDate(22, 7);
    mdates[1] = new MonthDate(25, 8);
    mdates[2] = new MonthDate(25, 6);
    mdates[3] = new MonthDate(28, 9);
    mdates[4] = new MonthDate(5, 9);
    print(mdates);
    Arrays.sort(mdates);
    print(mdates);
    }

    static <T> void print(T[] a) { // generic method
    for (T t : a) {
    System.out.printf("%s ", t);
    }
    System.out.println();
    }
    }


    Expected Output:
    22/7 25/8 25/6 28/9 5/9
    28/9 5/9 25/8 22/7 25/6

  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: How to develop a class MonthDate to print out the following output ?

    What is the current output?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to develop a class MonthDate to print out the following output ?

    Here is what I have tried :

    public class MonthDate {

    public String toString() {
    return String.format("%02d/%02d", this.day, this.month);
    }
    }

    And I have no idea to continue

  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: How to develop a class MonthDate to print out the following output ?

    Here is what I have tried
    Can you copy and post here what is printed out when the code executes so we can see it?
    Add comments explaining what is wrong with it.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    --- Update ---

    Also posted here: https://coderanch.com/t/707969/java/...e-dd-mm-format

    http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Output of 2 identical two-dimensional arrays print their tables differently
    By SamJava_the_Hut in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 19th, 2019, 09:52 AM
  2. Want to print RESULT word before output
    By naveensrikan in forum Other Programming Languages
    Replies: 5
    Last Post: June 1st, 2014, 01:41 PM
  3. Cannot print boolean value from class!
    By adnan.alvee in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 15th, 2013, 06:12 AM
  4. [SOLVED] How print " in output?
    By Purple01 in forum Java Theory & Questions
    Replies: 9
    Last Post: November 27th, 2012, 08:54 AM
  5. [SOLVED] how to print for-loop output to JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 13th, 2010, 02:43 PM

Tags for this Thread