0
\$\begingroup\$

Problem Statement:

Create a class diagram and Java code for the following system and scenario, taking into account the possibility of future extensions. "The system is a command line utility that prints a short 'quote of the day' on the user's terminal when run. To begin with the quote is selected randomly from a set of hard-coded strings within the program itself, but that might change later on -- the quotes might be based on the user's history, the time of day, the date, etc.. Scenario:

  1. User types "java QuoteOfTheDay" on the command line.
  2. System prints out a quote of the day, with an attribution.

This is my code for the following:

package quoteOfTheDay;
import java.util.Random;

public class QuoteOfTheDay {

    public static void main(String[] args) {

        System.out.println();

        Random rand = new Random();
        int randomInt = rand.nextInt(20);

        switch (randomInt) {
        //Print a quote depending upon the generated random number..
        }
    }
}

Is this the right approach? Am I missing some things from the problem requirements? I am open for any kind of suggestions.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ I recommend that you actually write a complete solution and ask for a review then. \$\endgroup\$
    – user29120
    Commented Nov 4, 2014 at 17:21
  • \$\begingroup\$ that is my complete code. only thing i commented out is the hard coded Quotes.. \$\endgroup\$
    – SouvikMaji
    Commented Nov 4, 2014 at 17:25
  • \$\begingroup\$ We don't need you to provide 20 quotes, but you could easily post a completely working program that chooses from three quotes. That would be more reasonable than posting a skeleton that just prints a newline and exits. \$\endgroup\$ Commented Nov 4, 2014 at 20:30

2 Answers 2

4
\$\begingroup\$

Your code contains a magic number, and the implied use of the number in the switch statement.

int randomInt = rand.nextInt(20);

The magic number here is 20. What is that? The number of quotes? Does this mean that you have the switch statement:

switch(randomInt) {
    case 0:
        ....
    case 1:
        ....
    case 19:
        ....
}

I believe so. So, the magic number is a hard-coded number of quotes you support. A simpler solution, that is more managable, is to store your quotes in an array, and the only hard-coded content is the actual quotes:

private static final String[] QUOTES = {
    "Life isn't about getting and having, it's about giving and being. –Kevin Kruse",
    "Whatever the mind of man can conceive and believe, it can achieve. –Napoleon Hill",
    "Strive not to be a success, but rather to be of value. –Albert Einstein",
    "Two roads diverged in a wood, and I—I took the one less traveled by, And that has made all the difference.  –Robert Frost",
    "I attribute my success to this: I never gave or took any excuse. –Florence Nightingale",
    "You miss 100% of the shots you don’t take. –Wayne Gretzky",
    "I’ve missed more than 9000 shots in my career. I’ve lost almost 300 games. 26 times I’ve been trusted to take the game winning shot and missed. I’ve failed over and over and over again in my life. And that is why I succeed. –Michael Jordan",
    "The most difficult thing is the decision to act, the rest is merely tenacity. –Amelia Earhart",
    "Every strike brings me closer to the next home run. –Babe Ruth",
    ....
};

Now you have hard-coded Quotes, and you can add and change them at will, and the QUOTES array changes along with them.

How do you use this? Well, your main method becomes really simple then:

public static void main(String[] args) {

    System.out.println();
    Random rand = new Random();
    System.out.println(QUOTES[rand.nextInt(QUOTES.length)]);

}

Note that there's no longer any magic numbers. You get the number of quotes from the actual number of quotes, not from a hard-coded value.

if you have 10 quotes it will work fine, if you have 1000 it will work equally well...

Now, if you take in to consideration the comments from @tim, you will also be able to extend the actual String quotes in to Quote and Attribution Objects, and make the program more object-oriented.

\$\endgroup\$
4
\$\begingroup\$

Am I missing some things from the problem requirements?

I would say that you are:

Create a class diagram and Java code for the following system and scenario, taking into account the possibility of future extensions.

You don't really have classes (and thus also didn't really take into account future extensions).

I would think that the expectation is something like this:

Create classes Quote, Person (for the attribution, maybe also an Attribution class), a class containing a collection of Quotes, from which you can retrieve items via index, an Output class with methods for print quote and print attribution.

That way, it would be easy to add a GUI for output, to add additional attribution information, add a date for the quote, etc.

Misc

There isn't really much to your code to review, but a couple of points:

  • you should not have all your code in the main method. The main method should only start your program, not perform any actions itself.
  • package names should be all lowercase
  • extract magic numbers to fields (but if you actually change your code as I suggested above, you would not need it at all)
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.