patternjavaMinor
Random Quote in java
Viewed 0 times
randomquotejava
Problem
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:
This is my code for the following:
Class Containing the main method
Quote Class containng the quotes.
```
package quoteOfTheDay;
public class Quote {
public static final String QUOTES[] = {
"Be yourself; everyone else is already taken.― Oscar Wilde",
"A room without books is like a body without a soul. ― Marcus Tullius Cicero",
"Be the change that you wish to see in the world. ― Mahatma Gandhi",
"If you tell the truth, you don't have to remember anything. ― Mark Twain",
"If you want to know what a man's like, take a good look at how he treats his inferiors, not his equals.― J.K. Rowling",
"To live is the rarest thing in the world. Most people exist, that is all.― Oscar Wilde",
"Without music, life would be a mistake. ― Friedrich Nietzsche",
"Always forgive your enemies, nothing annoys them so much. ― Oscar Wilde",
"Life isn't about getting and having, it's about giving and being. –Kevin Kruse",
"Whatever the mind of man can
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:
- User types "java QuoteOfTheDay" on the command line.
- System prints out a quote of the day, with an attribution.
This is my code for the following:
Class Containing the main method
package quoteOfTheDay;
import java.util.Random;
public class QuoteOfTheDay {
public static void main(String[] args) {
System.out.println();
Random rand = new Random();
System.out.println(Quote.QUOTES[rand.nextInt(Quote.QUOTES.length)]);
}
}Quote Class containng the quotes.
```
package quoteOfTheDay;
public class Quote {
public static final String QUOTES[] = {
"Be yourself; everyone else is already taken.― Oscar Wilde",
"A room without books is like a body without a soul. ― Marcus Tullius Cicero",
"Be the change that you wish to see in the world. ― Mahatma Gandhi",
"If you tell the truth, you don't have to remember anything. ― Mark Twain",
"If you want to know what a man's like, take a good look at how he treats his inferiors, not his equals.― J.K. Rowling",
"To live is the rarest thing in the world. Most people exist, that is all.― Oscar Wilde",
"Without music, life would be a mistake. ― Friedrich Nietzsche",
"Always forgive your enemies, nothing annoys them so much. ― Oscar Wilde",
"Life isn't about getting and having, it's about giving and being. –Kevin Kruse",
"Whatever the mind of man can
Solution
The using of a public field should be avoided and instead you should use a method to get the value.
If you take into account that the class will be extended (like written in the problem statement), I would suggest that you add a
Also you should make your String array
A
You should always name your variables and methods in a meaningful easily readable way. So I have renamed
If you take into account that the class will be extended (like written in the problem statement), I would suggest that you add a
getRandomQuote() method to the Quote class. Also you should make your String array
QUOTES private. I don't know what the conventions for the array braces [] are , but I would write it private static final String[] QUOTES A
Random should be created once and then used as often one needs.So I would suggest to create it at the constructor. private Random random;
public Quotes(){
random = new Random();
}
public String getRandomQuote(){
// return your random quote here
}You should always name your variables and methods in a meaningful easily readable way. So I have renamed
rand to random.Code Snippets
private Random random;
public Quotes(){
random = new Random();
}
public String getRandomQuote(){
// return your random quote here
}Context
StackExchange Code Review Q#68940, answer score: 6
Revisions (0)
No revisions yet.