Question

In: Computer Science

Write a Java program that will add the digits of a person’s birth date to obtain...

  • Write a Java program that will add the digits of a person’s birth date to obtain a single digit to generate a numerology number.
  • Write one separate method for each of the following tasks (it goes w/o saying that you will have a main() method along with these):
    • date validating
    • date crunching

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 .

  • Allow the user to input the entire date before you validate it.
  • You should continuously re-prompt the user for a date until a valid date is entered (you should use a 'while' loop for this).
  • If one part of the date fails (month, day, or year), re-prompt for the entire date again. Do not simply re-prompt for the offending part of the date. Output appropriate error messages for the incorrect dates. See the example runs for sample error messages.

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).

  • You will need to be careful because it is possible to have to “fold” the number more than just once, as in the above example. For example, if the birth date was 7/5/1942, you would add up the individual digits 7+5+1+9+4+2 to get 28, then you would need to add 2 + 8 to get 10. This is still a two-digit number, and needs to be added again – 1 + 0 – to get a single-digit result of 1.
  • You can simplify the process by adding the month day and year together first (i.e., 7+5+1942, to get 1954) and then breaking down and adding that result (1+9+5+4), which results in 19, which needs to be broken down again to get 1+9 which results in 10, which then results in 1+0, or 1. You should never have to crunch a number more than 3 times and will usually only need to add the digits once.

Solution Suggestions

  • It might actually be easier to work backward solving this program, like this, checking each step as you complete it:
    • Then add the code to take a date and crunch it down to a single digit. For simply testing this code, don’t worry about validating the date or input format yet; declare three variables (for example month, date, year), read in the date to process from the user, and then output the final single-digit number.
    • Finally, add code to validate the date that you read from the keyboard before crunching it. Validation will need to include tests for the month, day, and year as well as for using forward slashes between those pieces of information.
    • Make sure that your program continuously prompts the user for a date until a valid date is entered.
    • Make sure to allow the user to enter the entire date before validating it. In other words, don't just validate the month, then validate the day and then validate the year. Allow the entire date to be entered, then validate it and output an appropriate error message, repeating input if necessary.

Goals

  • Use most of the concepts in Java that we have learned so far: if/else, while, methods.
    • arrays can be used but not required.
  • Use only the data type int for numbers in this program; there’s no need to calculate floating-point numbers.
  • Solve a moderately complex Java program using the techniques you’ve learned so far.

Points to Think About

  • Validate to make sure that the year is between 1500 and 3000, inclusive.
  • Validate to make sure that the month is between 1 and 12, inclusive.
  • Validate to make sure that the day is appropriate for the month (at least >=1 and <= 28, 29, 30 or 31 depending on which month and year it is).
  • Validate that the user uses forward slashes (/) between the month, day, and year.

Solutions

Expert Solution

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 :)


Related Solutions

Write a program that prompts the user to enter a person’s date of birth in numeric...
Write a program that prompts the user to enter a person’s date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Follow similar conventions for the invalid values of month and year. (Note that your program...
In this exercise, you’ll write a program that accepts a person’s birth date from the console...
In this exercise, you’ll write a program that accepts a person’s birth date from the console and displays the person’s age in years. To make that easier to do, we’ll give you a class that contains the code for accepting the birth date. The console output for the program should look something like this: f Welcome to the age calculator. j Enter the month you were born (1 to 12) : 5 Enter the day of the month you were...
Write a complete java program to get input of a person’s age and their years of...
Write a complete java program to get input of a person’s age and their years of current USA citizenship. Tell them if they are eligible to run for US House of Representatives, US Senate, or President. At first, have the program just run once and give the answer for the given inputs. Give the answer in a nice format and be clear which offices the person can run for. Write good and complete pseudo code. Next, put that program in...
Write a complete java program to get input of a person’s age and their years of...
Write a complete java program to get input of a person’s age and their years of current USA citizenship. Tell them if they are eligible to run for US House of Representatives, US Senate, or President. At first, have the program just run once and give the answer for the given inputs. Give the answer in a nice format and be clear which offices the person can run for. Write good and complete pseudo code. Next, put that program in...
Write a java program that will ask for a Star Date and then convert it into...
Write a java program that will ask for a Star Date and then convert it into the corresponding Calendar date. without using array and methods.
Write a java program that uses a stack to reverse an integer of three digits. The...
Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse. - Your program must include the Stack class, the Reverse class, and the Test class. - In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse. - Class Reverse must use the Stack class to reverse the...
This is a Java program Problem Description Write an application that inputs five digits from the...
This is a Java program Problem Description Write an application that inputs five digits from the user, assembles the individual digits into a five-digit integer number (the first digit is for one’s place, the second digit is for the ten’s place, etc.) using arithmetic calculations and prints the number. Assume that the user enters enough digits. Sample Output Enter five digits: 1 2 3 4 5 The number is 54321 Problem-Solving Tips The input digits are integer, so you will...
in .java Write a program that reads an integer with 3 digits and prints each digit...
in .java Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, ... Enter an integer of exactly 3 digits(e.g. 538): 319 9 1 3 Hint: consider what you get from these operations: 319%10 319/10 31%10
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and...
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT