-4

So this project requires me to "make up" a secret agent, and by doing this by randomized events- like birthday, gender, name, city etc... I've been having trouble with my birthday portion of this code..and I think I have to make a temporary variable but I'm not sure... and I read through this in a linear fashion and it looks okay from int->string with a boolean in there. Any help??

public static Agent GetRandomAgent()
{
    Agent randomAgent = new Agent();

    WordList maleName = new WordList("MaleNames.txt");
    WordList femaleName = new WordList("FemaleNames.txt");
    WordList cityBorn = new WordList("Cities.txt");
    WordList cityNow = new WordList ("Cities.txt");
    WordList major = new WordList ("Majors.txt");

    //Setter methods
    //Sets the gender of the agent generated 


    double setGender = (Math.random()); 

    Random r = new Random();

    if(Math.random()*100 > 50)
    {
        randomAgent.gender = "female";
    }
    else //if not female, then male
    {
        randomAgent.gender = "male";
    }

    public String setBirthday()
    //randomAgent.birthday = new String getBirthday; 
    {
        int birthdayYear = 1900 + (int)(Math.random() * ((2012 - 1900) + 1));
        int birthdayMonth = 1 + (int)(Math.random() * ((12 - 1) + 1));

        switch(birthdayMonth) {
        //set for 30-day months: April(4), June(6), September(9), November(11)
        case 4:
        case 6:
        case 9:
        case 11:
            birthdayDay = 1 + (int)(Math.random() * ((30 - 1) + 1));
            break;

        case 2:
            if (isLeapYear(birthdayYear)) {
              birthdayDay = 1 + (int)(Math.random() * ((29 - 1) + 1));
            } else {
              birthdayDay = 1 + (int)(Math.random() * ((28 - 1) + 1));
            }
            break;

        //Set for 31-day months: January(1), March(3), May(5), July(7), August(8), October(10), December(12)
          default:
            birthdayDay = 1 + (int)(Math.random() * ((31 - 1) + 1));
          break;
        }

        //Convert the random integers back into Strings (easier to use)
        String year = Integer.toString(birthdayYear);
        String month = Integer.toString(birthdayMonth);
        String day = Integer.toString(birthdayDay);

        //Account to make sure there is a zero before the date (clarity)
        if (birthdayMonth < 10) {
            month = "0" + birthdayMonth;
        }

        if (birthdayDay < 10) {
            day = "0" + birthdayDay;
        }

        String suffix; 
        if ((day.equals("01") || (day.equals("21") || (day.equals("31")))))
        {
            suffix = "st";
        }

        if ((day.equals("02") || (day.equals("22"))))
        {
            suffix = "nd";
        }

        if (day == "03" || day == "23")
        {
                    suffix = "rd";
        }
        else
        { 
            suffix = "th";
        }
        return this.birthday = (day + suffix + " " + month + " of " + year); 
      }

      public static boolean isLeapYear;
      int year; 
      {
        Calendar rightNow = Calendar.getInstance();
        rightNow.set(Calendar.YEAR, year);
        int numberOfDays = rightNow.getActualMaximum(Calendar.DAY_OF_YEAR);

        if (numberOfDays > 365) {
            return true;
        }
        return false;
      }
Lukas
  • 2,111
  • 2
  • 14
  • 8
  • oh oh and the only problem so far, is that the method header is wrong .... – Lukas Dec 09 '13 at 19:10
  • 1
    Better you add java tag – Ismail Sahin Dec 09 '13 at 19:10
  • and also I make the random agent.. using randomAgent, ..so i think somewhere in there i need to have a variable randomAgent.birthday... or this.birthday..something like this – Lukas Dec 09 '13 at 19:11
  • 1
    What does "having trouble" mean? Is there a specific compiler error you are getting? – OldProgrammer Dec 09 '13 at 19:14
  • Tell us what the problem is specifically instead of expecting us to also figure that out. – Radiodef Dec 09 '13 at 19:15
  • yes! well String is underlined , as is the ()... and it says : Syntax error on token "String", @ expected – Lukas Dec 09 '13 at 19:15
  • 1
    "The method header is wrong": The first thing I'd do is take a look at your method signature for `setBirthday()` and use that to fix `isLeapYear` – Chris Forrence Dec 09 '13 at 19:15
  • @Lukas But _which_ String? Under year? month? The method signature for setBirthday()? – Chris Forrence Dec 09 '13 at 19:17
  • Dont compare String with `==`, use [`String#equals()`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Smit Dec 09 '13 at 19:18
  • Chris, what do you mean by that? Should it not be a string? or should i change the title? I'm unsure... – Lukas Dec 09 '13 at 19:18
  • The method signature! – Lukas Dec 09 '13 at 19:19
  • 1
    The method signature for `isLeapYear` should probably be `public static boolean isLeapYear(int year) {`. – Radiodef Dec 09 '13 at 19:19
  • In that case, @Lukas, there's more to your *.java file than displayed. Creating a class and adding those two methods reveal no issues with the `setBirthday()` method signature (although there are other issues) – Chris Forrence Dec 09 '13 at 19:21
  • does it help maybe if i show you this as an example? this part of the code works! it's also under a constructor : i'll post it under Agent constructor (or a new question) :) – Lukas Dec 09 '13 at 19:24
  • 1
    If the underline is shown for "String" in the setBirthday declaration then just add whatever is above it to the code in the OP. It sounds like there is a missing brace or something like that. In cases like that the syntax error will "trickle down" to whatever comes after. – Radiodef Dec 09 '13 at 19:26
  • Sorry chris, I can only post every 20 minutes, so I'll fiddle around with it for awhile ..and Radiodef I will definitely have a look at that! – Lukas Dec 09 '13 at 19:28
  • And please don't post a new question! You can [edit](http://stackoverflow.com/posts/20478699/edit) your question to add additional information, however. – Chris Forrence Dec 09 '13 at 19:30
  • ^ Ah right, forgot about that... – Lukas Dec 09 '13 at 19:32

1 Answers1

2

There were a few things wrong with the code:

  1. The reason you were getting an error with the setBirthday() method signature was because GetRandomAgent() hadn't been closed yet. Please note the ending brace after setting randomAgent.birthday.
  2. birthdayDay was not initialized in setBirthday().
  3. The method signature for isLeapYear(int) was incorrect.
  4. As Smit said, you need to compare Strings with .equals() instead of ==.

Keeping all that in mind...

import java.util.Calendar;
import java.util.Random;

public class Agent
{

    String gender;
    String birthday;

    public static Agent GetRandomAgent()
    {
        Agent randomAgent = new Agent();

        WordList maleName = new WordList("MaleNames.txt");
        WordList femaleName = new WordList("FemaleNames.txt");
        WordList cityBorn = new WordList("Cities.txt");
        WordList cityNow = new WordList("Cities.txt");
        WordList major = new WordList("Majors.txt");

        //Setter methods
        //Sets the gender of the agent generated 
        double setGender = (Math.random());

        Random r = new Random();

        if(Math.random() * 100 > 50)
        {
            randomAgent.gender = "female";
        }
        else //if not female, then male
        {
            randomAgent.gender = "male";
        }
        randomAgent.birthday = randomAgent.setBirthday();
    }

    public String setBirthday()
    {
        int birthdayYear = 1900 + (int) (Math.random() * ((2012 - 1900) + 1));
        int birthdayMonth = 1 + (int) (Math.random() * ((12 - 1) + 1));
        int birthdayDay;
        switch(birthdayMonth)
        {
            //set for 30-day months: April(4), June(6), September(9), November(11)
            case 4:
            case 6:
            case 9:
            case 11:
                birthdayDay = 1 + (int) (Math.random() * ((30 - 1) + 1));
                break;

            case 2:
                if(isLeapYear(birthdayYear))
                {
                    birthdayDay = 1 + (int) (Math.random() * ((29 - 1) + 1));
                }
                else
                {
                    birthdayDay = 1 + (int) (Math.random() * ((28 - 1) + 1));
                }
                break;

            //Set for 31-day months: January(1), March(3), May(5), July(7), August(8), October(10), December(12)
            default:
                birthdayDay = 1 + (int) (Math.random() * ((31 - 1) + 1));
                break;
        }

        //Convert the random integers back into Strings (easier to use)
        String year = Integer.toString(birthdayYear);
        String month = Integer.toString(birthdayMonth);
        String day = Integer.toString(birthdayDay);

        //Account to make sure there is a zero before the date (clarity)
        if(birthdayMonth < 10)
        {
            month = "0" + birthdayMonth;
        }

        if(birthdayDay < 10)
        {
            day = "0" + birthdayDay;
        }

        String suffix;
        if((day.equals("01") || (day.equals("21") || (day.equals("31")))))
        {
            suffix = "st";
        }

        if((day.equals("02") || (day.equals("22"))))
        {
            suffix = "nd";
        }

        if(day.equals("03") || day.equals("23"))
        {
            suffix = "rd";
        }
        else
        {
            suffix = "th";
        }
        return this.birthday = (day + suffix + " " + month + " of " + year);
    }

    public static boolean isLeapYear(int year)
    {
        Calendar rightNow = Calendar.getInstance();
        rightNow.set(Calendar.YEAR, year);
        int numberOfDays = rightNow.getActualMaximum(Calendar.DAY_OF_YEAR);

        if(numberOfDays > 365)
        {
            return true;
        }
        return false;
    }
}
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64