Java Lab

As I had mentioned in the previous posts about my AP Computer Science course that in this class we mainly focus on learning JAVA language.  It is very difficult to me since it is a completely different language. We had already covered Unit 1 and Unit 2 which is about one of the Java data type called “primitive”.  After doing all the practices in those units, we always have labs, which is challenging exercises. Below is one of the labs that I had done in Unit 2, which is about determined a person’s horoscope. 

The lab is very messy and confusing with all the dates, the months, and the zodiac signs. It is not the best idea to just read the instruction and go straight to programming. I find it more reliable to use to paper and plan out about what I’m going to program; it is more organized to not mess up. Though in Unit 2 we only covered primitive data types, which means we need to write lots of codes to achieve our goal. Thereby the below example had included Array, which is not a primitive data type but a reference data type in order to make it more convenient and more organized. I spent many times to write and tweak the codes. So, by leaving a comment next to the code are very useful to remind of what had previously done. 

// You can copy this code into any Java compiler to run it but I would recommend BlueJ 

// There are also examples of the outputs down below

import java.util.Scanner;
public class Lab3
{
 public static void main(String args[]){
 Scanner scan = new Scanner(System.in);
 
 //ask the user for inputs
 System.out.println("On what day of the month were you born? (1 to 31)");
 int day = scan.nextInt();
 System.out.println("In which month were you born? (1 to 12)");
 int month = scan.nextInt();
 
 //create lists for the horoscopes, days, and months in string 
 String hoArray[] = {"Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"};

 String dayArray[] = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eigth", "nineth", "tenth","eleven", "twelveth", 
 "thirdteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eigteenth", "nineteeth", "twentieth", "twenty-first", "twenty-second", 
 "twenty-third", "twenty-fourth", "twenty-fifth", "twenty-sixth", "twenty-seventh", "twenty-eigth", "twenty-nineth", "thirtieth", "thirty-first"};

 String monthArray[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October","November", "December"};
 
 //check the date for the right zodiac sign
 if ((day > 20 && day < 32) && month == 3 || (day < 20 && day < 32) && month == 4){
 //21 March - 19 April
 System.out.println("Your Zodiac sign is: " + hoArray[0]);//Aries
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);//minus one since array start at zero
 System.out.println("Horoscope: courageous and determined but impatient and agressive");
 }
 if ((day > 19 && day < 32) && month == 4 || (day < 21 && day < 32) && month == 5){
 //20 April - 20 May
 System.out.println("Your Zodiac sign is: " + hoArray[1]);//Taurus
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: reliable and patient but stubborn and possessive");
 }
 if ((day > 20 && day < 32) && month == 5 || (day < 21 && day < 32) && month == 6){
 //21 May - 20 June
 System.out.println("Your Zodiac sign is: " + hoArray[2]);//Gemini
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: gentle and curious but nervous and indecisive");
 }
 if ((day > 20 && day < 32) && month == 6 || (day < 23 && day < 32) && month == 7){
 //21 June - 22 July
 System.out.println("Your Zodiac sign is: " + hoArray[3]);//Cancer
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: tenacious and loyal but moody and insecure");
 }
 if ((day > 22 && day < 32) && month == 7 || (day < 23 && day < 32) && month == 8){
 //23 July - 22 August
 System.out.println("Your Zodiac sign is: " + hoArray[4]);//Leo
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: creative and passionate but arrogant and stubborn");
 }
 if ((day > 22 && day < 32) && month == 8 || (day < 23 && day < 32) && month == 9){
 //23 August - 22 September
 System.out.println("Your Zodiac sign is: " + hoArray[5]);//Virgo
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: loyal and hardworking but worry and shy");
 }
 if ((day > 22 && day < 32) && month == 9 || (day < 23 && day < 32) && month == 10){
 //23 September - 22 October
 System.out.println("Your Zodiac sign is: " + hoArray[6]);//Libra
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: cooperative and gracious but indecisive and self-pity");
 }
 if ((day > 22 && day < 32) && month == 10 || (day < 22 && day < 32) && month == 11){
 //23 October - 21 November
 System.out.println("Your Zodiac sign is: " + hoArray[7]);//Scorpio
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: resourceful and a true friend but secretive and jealous");
 }
 if ((day > 23 && day < 32) && month == 11 || (day < 22 && day < 32) && month == 12){
 //22 November - 21 December
 System.out.println("Your Zodiac sign is: " + hoArray[8]);//Sagittarius
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: generous and great sense of humor but very impatient and promises more than can deliver");
 } 
 if ((day > 23 && day < 32) && month == 12 || (day < 20 && day < 32) && month == 1){
 //22 December - 19 January
 System.out.println("Your Zodiac sign is: " + hoArray[9]);//Capricorn
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: responsible and self-control but unforgiving and expecting the worst");
 }
 if ((day > 21 && day < 32) && month == 1 || (day < 19 && day < 32) && month == 2){
 //20 January - 18 February
 System.out.println("Your Zodiac sign is: " + hoArray[10]);//Aqaurius
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: progressive and independent but temperamental and uncompromising");
 }
 if ((day > 18 && day < 32) && month == 2 || (day < 21 && day < 32) && month == 3){
 //19 February - 20 March
 System.out.println("Your Zodiac sign is: " + hoArray[11]);//Pisces
 System.out.println("Your birthday is: " + monthArray[month-1] + " " + dayArray[day-1]);
 System.out.println("Horoscope: compassionate and wise but fearful and desire to escape reality");
 }
 else if (day > 31 || month > 12){//if day greater than 31 or month greater than 12; print error
 System.out.println("error");
 }
 }
}

Example of the outputs:

 

Leave a Reply

Your email address will not be published. Required fields are marked *