Click here to Skip to main content
15,881,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
+ displayHistoricalFigure():void

Write a method, getHistoricalPeriod that returns the period in which this British historical figure was born.

1. Print out a sentence summarising this historical figure as shown in the expected output below. Youshouldcall the getHistoricalPeriodmethodforpartoftheoutput.

What I have tried:

Java
public class BritishHistoricalFigure{

      private String name;
      private int birthYear;
      private String occupation;
      private int yearOfDeath;
   
    /* replacement for default constructor*/
   public BritishHistoricalFigure(){
   }
      /*constructor method*/
   public BritishHistoricalFigure(String n, int bY, String o, int yOD){
      occupation = o;
      birthYear = bY;
      diedInYear = dIY;
      name =n;
   
   
   }

   public int getBirthYear(){
      return birthYear;
   }

   public String getOccupation(){
      return occupation;
   }

   public int getdiedInYear(){
      return diedInYear;
   }

   public void setOccupation(String O){
      occupation = O;
   }

   public String getHistoricalPeriod(){
      if (yearOfDeath < 1485){
      return "Pre-Tudor";
      }
      else if (yearOfDeath> 1485 || yearOfDeath <1603){
      return "Tudor";
      }
      else if (yearOfDeath >1603 || yearOfDeath <1714){  
      return "Stuart";
      }
      else if (yearOfDeath >1714 || yearOfDeath < 1837){  
      return "Georgian";
      }
      else if (yearOfDeath >1837 ||yearOfDeath < 1901){
      return "Victorian";
      }
      else if(yearOfDeath > 1901 || yearOfDeath< 1910){
      return "Edwardian";
      }
      else{
         return "Windsor";
      }
   }
   public static void displayHistoricalFigure(){
      if(birthYear >=1564 && yearOfDeath <=1616){
         System.out.print
      Sytem.out.println("William Shakespear, "+ birthYear+ " - "+ diedInYear+ ,  )
   }
}
Posted
Updated 11-Dec-22 22:36pm
v2

Look at the line closely:
Java
Sytem.out.println("William Shakespear, "+ birthYear+ " - "+ diedInYear+ , )
It should be System not Sytem
It should have a semicolon at the end
It has sp[uriopus "+ ," at the end of the string concatenation.

But the line above is wrong as well!
It needs "()" to show it's a method call.
It needs a semicolon at the end.

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And do yourself a favour: indent your code! It makes it a whole load more readable ...
 
Share this answer
 
Comments
CPallini 12-Dec-22 2:21am    
5.
You could also simplify this method:
Java
 public String getHistoricalPeriod(){
   if (yearOfDeath < 1485){
   return "Pre-Tudor";
   }
   else if (yearOfDeath> 1485 || yearOfDeath <1603){
   return "Tudor";
   }
   else if (yearOfDeath >1603 || yearOfDeath <1714){
   return "Stuart";
   }
   else if (yearOfDeath >1714 || yearOfDeath < 1837){
   return "Georgian";
   }
   else if (yearOfDeath >1837 ||yearOfDeath < 1901){
   return "Victorian";
   }
   else if(yearOfDeath > 1901 || yearOfDeath< 1910){
   return "Edwardian";
   }
   else{
      return "Windsor";
   }
}

to
Java
public String getHistoricalPeriod(){
    if (yearOfDeath < 1485){
        return "Pre-Tudor";
    }
    else if (yearOfDeath < 1603){ // you already know it is greater than 1485 from the previous test
        return "Tudor";
    }
    else if (yearOfDeath < 1714){  // as above
        return "Stuart";
    }
    else if (yearOfDeath < 1837){  
        return "Georgian";
    }
    else if (yearOfDeath < 1901){
        return "Victorian";
    }
    else if(yearOfDeath < 1910){
        return "Edwardian";
    }
    else{
        return "Windsor";
    }
}
 
Share this answer
 
Comments
Richard Deeming 14-Dec-22 6:41am    
You've changed the behaviour of his code to be correct; the original would return "Tudor" for any year after 1485. :)
Richard MacCutchan 14-Dec-22 6:53am    
I suppose I should have made that clear, but ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900