Free Response Practice Question (with solution)- Zodiac Sign Project


<-- Back to Mixed Number Project Next to Right-Angled Triangle Project -->





Zodiac Sign Project


Part 1: Date.java

Write the complete class definition for the Date class. The Date class consists of the following:


Additionally,


Part 2: Celebrity.java

Another class named Celebrity stores the name and the date of birth of the celebrity. Hence, the Celebrity class consists of the following:


Moreover,


Aquarius (January 20 - February 18)

Pisces (February 19 - March 20)

Aries (March 21 - April 19)

Taurus (April 20 - May 20)

Gemini (May 21 - June 20)

Cancer (June 21 - July 22)

Leo (July 23 - August 22)

Virgo (August 23 - September 22)

Libra (September 23 - October 22)

Scorpio (October 23 - November 21)

Sagittarius (November 22 - December 21)

Capricorn (December 22 - January 19)


Part 3: ZodiacRunner.java

Write the complete class definition for the runner class named ZodiacRunner class, that makes use of the Celebrity class. The main method of the ZodiacRunner class should do the following:



Solution:


Part 1: Date.java

    
public class Date {

    int date;
    int month;
    int year;
    
    public Date()
    {
        date =0;
        month=0;
        year=0;
    }
    
    public Date(int date, int month, int year)
    {
        this.date=date;
        this.month = month;
        this.year = year;
    }
    
    public int getDate()
    {
        return date;
    }
    
    public int getMonth()
    {
        return month;
    }
    
    public int getYear()
    {
        return year;
    }
}


Part 2:Celebrity.java


public class Celebrity {

    String name;
    Date dob;
    
    public Celebrity()
    {
        name=null;
        dob= null;
    }
    
    public Celebrity(String name, Date dob)
    {
        this.name = name;
        this.dob= dob;
    }
    
    public String getName()
    {
        return name;
    }
    
    public Date getDate()
    {
        return dob;
    }
    
    public void getZodiac()
    {
        String zodiac=null;
        if ((this.name != null) && (this.dob !=null))
        {
            int month = dob.getMonth();
            int date = dob.getDate();
            
            switch (month) {
            
            case 1:
                if ((date >= 1) && (date <= 19)) zodiac = "Capricorn";
                else if ((date >= 20) && (date <= 31)) zodiac= "Aquarius";
                break;
                
            case 2:
                if ((date >= 1) && (date <= 18)) zodiac= "Aquarius";
                else if ((date >= 19) && (date <= 29)) zodiac= "Pisces";
                break;
                
            case 3:
                if ((date >= 1) && (date <= 20)) zodiac= "Pisces";
                else if ((date >= 21) && (date <= 31)) zodiac= "Aries";
                break;
                
            case 4:
                if ((date >= 1) && (date <= 19)) zodiac= "Aries";
                else if ((date >= 20) && (date <= 31)) zodiac= "Taurus";
                break;
                
            case 5:
                if ((date >= 1) && (date <= 20)) zodiac= "Taurus";
                else if ((date >= 21) && (date <= 31)) zodiac= "Gemini";
                break;
                
            case 6:
                if ((date >= 1) && (date <= 20)) zodiac= "Gemini";
                else if ((date >= 21) && (date <= 31)) zodiac= "Cancer";
                break;
                
            case 7:
                if ((date >= 1) && (date <= 22)) zodiac= "Cancer";
                else if ((date >= 23) && (date <= 31)) zodiac= "Leo";
                break;
                
            case 8:
                if ((date >= 1) && (date <= 22)) zodiac= "Leo";
                else if ((date >= 23) && (date <= 31)) zodiac= "Virgo";
                break;
                
            case 9:
                if ((date >= 1) && (date <= 22)) zodiac= "Virgo";
                else if ((date >= 23) && (date <= 31)) zodiac= "Libra";
                break;
                
            case 10:
                if ((date >= 1) && (date <= 22)) zodiac= "Libra";
                else if ((date >= 23) && (date <= 31)) zodiac= "Scorpio";
                break;
                
            case 11:
                if ((date >= 1) && (date <= 21)) zodiac= "Scorpio";
                else if ((date >= 22) && (date <= 31)) zodiac= "Sagittarius";
                break;
                
            case 12:
                if ((date >= 1) && (date <= 21)) zodiac= "Sagittarius";
                else if ((date >= 22) && (date <= 31)) zodiac= "Capricorn";
                break;  
            }

            System.out.println("The zodiac sign is: "+ zodiac);
        }
        else
            System.out.println("Name and Date values are null.");
    }
}

Part 3: ZodiacRunner.java

    
public class ZodiacRunner {

    public static void main(String args[])
    {
        Celebrity genCelebrity = new Celebrity();
        genCelebrity.getZodiac();
        
        Celebrity oneCelebrity = new Celebrity("Sachin", new Date(2, 2, 1980));
        oneCelebrity.getZodiac();

        Celebrity twoCelebrity = new Celebrity("Juhi", new Date(3, 4, 1980));
        twoCelebrity.getZodiac();

    }
}




Java project files (with Runner code):


<-- Back to Mixed Number Project Next to Right-Angled Triangle Project -->