In: Computer Science
First: Get a Date
Numerology has been used since ancient times to shed light on relationships, health, and global events. Each element in a birth date is believed to possess a special numerical significance. We are going to develop our own rudimentary numerology prediction program. The first thing to do is to ask the user to enter a birth date to process.
You will need to indicate to your user that they need to enter the month, day and year in that order separated by spaces and forward slashes, which look like this: /. An example would be 12 / 25 / 2112. The spaces must be present between the numeric and character input.
Second: Validate the Date
The important and time-consuming portion of this program is validating your input from the user. This means that you need to check to make sure the month is between 1 and 12, the day is appropriate for the month, and the year is between 1500 and 3000, inclusive.
By “the day is appropriate for the month and year”, this means that if a date of January 32nd is entered or a date of June 0th is entered, you should recognize and report that. This also means that you should check to see that if the year is a leap year, you should allow February 29th and if it's not a leap year, you should disallow February 29th .
Third: Crunch the Date
Then, you need to calculate a single digit from the birth date. For example, if your birth date is 3/19/1955 you add 3+1+9+1+9+5+5 to get 33. You would then break this apart and add 3+3 to get 6.
Once you have calculated the single-digit, PRINT it on the console (see the sample test runs).
Solution Suggestions
Goals
Points to Think About
Code in Java
import java.util.*;
public class Numberology {
public static int[] getDate(){
int[] arr = new int[3];
Scanner sc = new Scanner(System.in);
boolean date_is_correct = false;
System.out.println("Enter a date of birth in the following format MM<space>/<space>DD<space>/<space>YYYY");
while(!date_is_correct){
//store month day and year in the array index 0,1 and 2
String d = sc.nextLine();
int mon = 0;
int i=0;
//read till it is number
while(i<d.length()&&d.charAt(i)>='0'&&d.charAt(i)<='9'){
mon = mon*10+(d.charAt(i)-'0');
i++;
}
//check for slash
if(i+2>=d.length() || !d.substring(i,i+3).equals(" / ")){
System.out.println("Invalid");
continue;
}
i+=3;
int day = 0;
//read till it is number
while(i<d.length()&&d.charAt(i)>='0'&&d.charAt(i)<='9'){
day = day*10+(d.charAt(i)-'0');
i++;
}
//check for slash
if(i+2>=d.length() || !d.substring(i,i+3).equals(" / ")){
System.out.println("Invalid");
continue;
}
i+=3;
int year = 0;
//read till it is number
while(i<d.length()){
if(d.charAt(i)<'0'||d.charAt(i)>'9'){
System.out.println("Invalid");
continue;
}
year = year*10+(d.charAt(i)-'0');
i++;
}
arr[0]=mon;
arr[1]=day;
arr[2]=year;
if(!validateDate(arr)){
System.out.println("Invalid");
continue;
}else{
date_is_correct=true;
}
}
return arr;
}
public static boolean validateDate(int [] date){
//check valid month between 1 and 12
if(date[0]<1||date[0]>12)
return false;
//check for year between 1500 and 3000
if(date[2]<1500||date[2]>3000)
return false;
//check date boundaries
if(date[1]<1||date[1]>31)
return false;
//check appropriate day for month
if(date[0]==2){
boolean is_leap = false;
//This is feb, check for leap year
if(date[2]%4==0){
if(date[2]%100!=0 || date[2]%400==0)
is_leap=true;
}
//If not leap year but feb date is more than 28 then return false
if(!is_leap&&date[1]>28){
return false;
}else if(date[1]>29){
return false;
}
}else if(date[0]==4 || date[0]==6 || date[0]==9 || date[0]==11){
if(date[1]>30)
return false;
}
return true;
}
public static int crunch_Date(int [] date){
int val=0;
for(int i=0;i<3;i++){
int num=date[i];
while(num>0){
//extract the last digit and add to val
val+=num%10;
num=num/10;
}
}
//now we need to crunch val until it is single digit
while(val/10>0){
int num=val;
val=0;
//add all digits of val
while(num>0){
//extract the last digit and add to val
val+=num%10;
num=num/10;
}
}
return val;
}
public static void main(String[] args) {
int[] arr = getDate();
int val = crunch_Date(arr);
String forecast="";
switch(val){
case 1: forecast = "You are shy!";
break;
case 2: forecast = "You will have a good year in studies!";
break;
case 3: forecast = "You will make friends in this coming year!";
break;
case 4: forecast = "You are smart and intelligent!";
break;
case 5: forecast = "Be careful of dogs!";
break;
case 6: forecast = "Be careful of your friends!";
break;
case 7: forecast = "Next year will be lucky for you";
break;
case 8: forecast = "You will have a healthy next year!";
break;
case 9: forecast = "You will get married int 5 years!";
break;
}
System.out.println(val+ ": " +forecast);
}
}
Sample Input/Output
Note
I have added random forecasts, you can change them according to your wish :)