Upcoming SlideShare
×

# Quote of the Day Program Step By Step

165
-1

Published on

Quote of the Day program displays a different quote each day of the year as the quote of the day. The program picks a random quote from an array of quotes based on day of the year; and keeps showing that selected quote for the whole day and change to a new one the next day.

Published in: Education, Technology
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
165
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
14
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Quote of the Day Program Step By Step

1. 1. https://www.facebook.com/Oxus20 CONTENTS Problem Statement .................................................... 3 Solution ............................................................. 3 Code Explanation ..................................................... 5 Package and Import ................................................. 5 Class .............................................................. 6 Method MAIN ........................................................ 6 Array .............................................................. 6 Loop ............................................................... 7 Calendar.DAY_OF_YEAR ............................................... 8 Conclusion ........................................................... 9 Page 1 of 9
2. 2. https://www.facebook.com/Oxus20 RECOMMENDATION AND GUIDANCE Programming is fun and easy if and only if you concentrate and consider the Problem Solving process which consists of a sequence of sections that fit together depending on the type of problem to be solved. These are:  Understand the problem  Generating possible solutions  Refine the Solutions and select the best solution(s).  Implement and code the best selected solution  Test the code The process is only a guide for problem solving. It is useful to have a structure to follow to make sure that nothing is overlooked and ignored. Abdul Rahman Sherzad Special Thanks goes to Nooria Esmaelzadeh for her support taking minute of the session and drafting the idea together Page 2 of 9
3. 3. https://www.facebook.com/Oxus20 PROBLEM STATEMENT Write a program to display a different quote each day of the year as the quote of the day. The program should pick a random quote from an array of quotes you provide based on day of the year; and it must show that same picked and selected quote for the whole day and change to a new one the next day. NOTE: Remember and keep in mind the already picked and selected quotes should not pick again until the next year. SOLUTION Do you think it is simple or complex? REMEMBER "Every problem has a solution, Tom Hanks"  To be honest it is quite easy and simple. Followings ingredients are required solving the problem efficiently:  Array: An array is needed to store the quotes  Loop: Using loop can help initialize the dummy quotes for the demonstration and testing purpose inside the array.  Calenday.DAY_OF_YEAR: The constant DAY_OF_YEAR of the class Calendar returns day of year ranges from 1 to 365 (366 for the leap years). Page 3 of 9
4. 4. https://www.facebook.com/Oxus20 Following is the complete code of the "Quote of the Day" java application program: /* * Copyright (c) 2013, OXUS20 and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ import java.util.Calendar; /** * Display Quote Of The Day * This class picks a quote from an array of quotes based on day of the year. */ public class QuoteOfTheDay { public static void main(String[] args) { // Array quotes[] to store the quotes String quotes[] = new String[366]; // Loop initialize dummy quotes inside array quotes[] for (int i = 0; i < quotes.length; i++) { quotes[i] = "Dummy quote of the day " + (i + 1); } // Construct Calendar object "c" Calendar c = Calendar.getInstance(); // quote_index represent day of the year int quote_index = c.get(Calendar.DAY_OF_YEAR); // quotes[quote_index] returns day of the year quote String quote_of_the_day = quotes[quote_index]; // Display the code the screen System.out.println(quote_of_the_day); } } Page 4 of 9
5. 5. https://www.facebook.com/Oxus20 CODE EXPLANATION Let's break down the above code of "Quote of the Day" application program into pieces for the purpose of explanation as follow: PACKAGE AND IMPORT Package = directory: Java classes can be grouped together in packages. A package name is the same as the directory (folder) name which contains the .java files. To import classes into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement, if there is one. Here's how you would import the Calendar class from the java.util package. import java.util.Calendar; From now on you can refer to the Calendar class by its simple name as follow: Calendar c = Calendar.getInstance(); Otherwise if you did not import the Calendar class you had to use following method in order to access the Calendar class: java.util.Calendar c = java.util.Calendar.getInstance(); Page 5 of 9
6. 6. https://www.facebook.com/Oxus20 CLASS Java class is nothing but a template for object you are going to create or it is a blueprint. When we create class in java the first step is keyword class and then name of the class or identifier we can say i.e. "QuoteOfTheDay". public class QuoteOfTheDay { } The curly braces symbols demonstrates the class body { }; and between this all things related to the class i.e. property and method will come here. METHOD MAIN In Java, you need to have a method named main () in at least one class. The following is what must appear in a real Java program. public static void main(String[] args) { } In the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class's main () method. The main () method then calls all the other methods required to run your application. ARRAY An array is a container object that holds a fixed number of values of a single type. Array length is fixed after creation. Page 6 of 9
7. 7. https://www.facebook.com/Oxus20 In simple words it is a programming construct which helps replacing following: String String String String quote1 quote2 quote3 quote4 = = = = "Dummy "Dummy "Dummy "Dummy quote quote quote quote of of of of the the the the day day day day 1"; 2"; 3"; 4"; String quote5 = "Dummy quote of the day 5"; With following: String quotes[] = new String[366]; quotes[0] = "Dummy quote of the day quotes[1] = "Dummy quote of the day quotes[2] = "Dummy quote of the day quotes[3] = "Dummy quote of the day 1"; 2"; 3"; 4"; quotes[4] = "Dummy quote of the day 5"; LOOP There may be a situation when we need to execute a block of code several number of times, and is often referred to as a loop. In simple words it is a programming construct which helps replacing following: String quotes[] = new String[366]; quotes[0] = "Dummy quote of the day quotes[1] = "Dummy quote of the day quotes[2] = "Dummy quote of the day quotes[3] = "Dummy quote of the day 1"; 2"; 3"; 4"; quotes[4] quotes[5] quotes[6] quotes[7] quotes[8] 5"; 6"; 7"; 8"; 9"; = = = = = "Dummy "Dummy "Dummy "Dummy "Dummy quote quote quote quote quote of of of of of the the the the the day day day day day quotes[365] = "Dummy quote of the day 365"; With following: String quotes[] = new String[366]; for (int i = 0; i < quotes.length; i++) { quotes[i] = "Dummy quote of the day " + (i + 1); } Page 7 of 9
8. 8. https://www.facebook.com/Oxus20 CALENDAR.DAY_OF_YEAR As it is already mentioned the DAY_OF_YEAR is a constant of the Calendar class and returns the day of the year ranges from 0 to 365 (for the leap years 366). Therefore, we can use this as index of the array quotes [] in order to display the quote for the current day of the year. Next day the next quote will be displayed. // Construct Calendar object "c" Calendar c = Calendar.getInstance(); // quote_index represent day of the year int quote_index = c.get(Calendar.DAY_OF_YEAR); // quotes[quote_index] returns day of the year quote String quote_of_the_day = quotes[quote_index]; // Display the code the screen System.out.println(quote_of_the_day); Page 8 of 9
9. 9. https://www.facebook.com/Oxus20 CONCLUSION Good programmers don't have to be geniuses but they should be smart, inventive and creative and a flare in problem solving. In addition, good programmers passionate about technology, programs as a hobby and learn new technologies on his/her own. The concept of "Quote of the Day" for the year can be customized to be used as follow:  Monthly Quote of the Day: To display the quote according the day of the month.  Weekly Quote of the Day: To display the quote based on day of the week and reset back next week.  Hourly Quote of the Day: To display the quote each hour  Minutely Quote of the Day: To display different quote each minute Even can be used with different concept accomplishing different tasks and idea; for example, the same concept can be customized to be used for the banner advertisement, different themes and backgrounds monthly, weekly, daily, hourly, minutely and/or even secondly. Page 9 of 9