Tino Intro To Java

Last modified: January 26, 2024

Lab 1a Person

In a previous lesson, we used a Person class to demonstrate the components of a class.

In this lab, you will enhance the Person class to support more features.

Recreate the Person Class

First, create a new BlueJ project called "Lab1A_Person"

Then, create a new java file using the format PX_LastName_FirstName_Person where X is your period.

For example, if your name is Michael Wang and you are in 2nd period, you would name the file P2_Wang_Michael_Person. It is important to always use the correct naming conventions given in lab instructions as you will lose points for not naming files correctly.

Next, copy and paste the code below into the file you just created:

public class PX_LastName_FirstName_Person {

    int myAge;          // Age in years
    double myHeight;    // Height in meters
    boolean isStudent;  // True if enrolled at any school, false otherwise
    String myName;      // Person's name

    public PX_LastName_FirstName_Person() {        
        myAge = 17;
        myHeight = 1.53;
        isStudent = false;
        myName = "Frances";
    }

    public PX_LastName_FirstName_Person(int age, double height, boolean student, String name) {
        myAge = age;
        myHeight = height;
        isStudent = student;
        myName = name;
    }

    public double heightToInches() {
        // 1 meter = 39.97 inches
        double answer = myHeight * 39.97;
        return answer;
    }

    public String getName() {
        return myName;
    }

    public void printInfo() {
        String response;
        response = "Hello, my name is " + myName;
        response = response + " and I'm " + myAge + " years old!";
        if (isStudent == true) {
            response += " I am a student.";
        }
        else {
            response += " I am not a student.";
        }
        System.out.println(response);
    }

    public void increaseAge(int amount) {
        myAge = myAge + amount;
    }
}

Please change all occurrences of PX_LastName_FirstName_Person to match your actual period and name. While in the editor, go to the Tools menu and select Replace... to use the find and replace feature to accomplish this task easily.

Then, create a Driver file using the format PX_LastName_FirstName_Driver and copy and paste the following code:

public class PX_LastName_FirstName_Driver {
    public static void main(String[] args) {
        PX_LastName_FirstName_Person joe;
        joe = new PX_LastName_FirstName_Person();
        joe.printInfo();
        joe.increaseAge(5);
        joe.printInfo();

        PX_LastName_FirstName_Person bob = new PX_LastName_FirstName_Person(18, 1.72, true, "Bob");
        bob.printInfo();
        String answer = bob.getName();
        System.out.println( "Hi, I'm " + answer );
        System.out.println( "My height in inches is " + bob.heightToInches() );
    }
}

Please change all occurrences of PX_LastName_FirstName_Person and PX_LastName_FirstName_Driver to match your actual period and name!

Then compile both files and run the Driver in BlueJ

If you run into errors while compiling, here are some possibilities.

  • Did you misspell anything, including upper/lower case?
  • Did you forget any braces or semi-colons?
  • Does the file name match your class name?
  • Did you change PX_LastName_FirstName to match your name and period everywhere?

If you did everything correctly, the output should be:

Hello, my name is Frances and I'm 17 years old! I am not a student.
Hello, my name is Frances and I'm 22 years old! I am not a student.
Hello, my name is Bob and I'm 18 years old! I am a student.
Hi, I'm Bob
My height in inches is 68.7484

Upgrade the Person class

Add the following attributes and update the constructors accordingly:

  • An instance variable named myFavoriteNumber that stores an int value.
  • An instance variable named myFavoriteColor that stores a reference to a String.
  • An instance variable named isSleeping that stores true or false.

Add the following methods and write the appropriate code:

  • A method named ageInDogYears that returns the age of this person in dog years (1 human year = 7 dog years).
  • A method named getFavoriteColor that returns this person's favorite color.
  • A method named printInfo2 that prints this person's name, age, and favorite color like this:
    • My name is Bob. I am 19 years old and my favorite color is blue.
  • A method named sleep that changes the state of this person to sleeping.
  • A method named wakeUp that changes the state of this person to awake.
  • A method that prints the sleep state of this person.
    • If they are sleeping it should print "I am sleeping....zzzz...."
    • otherwise it should print "I am awake!"

Replace the code in your Driver class to make it create a Person object with the following attributes

  • name is "Happy Sparky"
  • is a student
  • 14 years old
  • favorite number is 314
  • favorite color is "Green"
  • awake

Then make another Person with the following attributes:

  • name is "Mr. Jones"
  • is not a student
  • 24 years old
  • favorite number is 2
  • favorite color is "Magenta"
  • awake

Write code in your DRIVER ONLY that produces the following output (based on the values of your Person objects):

My name is Mr. Jones. I am 24 years old and my favorite color is Magenta.
Hi there, Mr. Jones! My name is Happy Sparky.
My favorite color is Green.
In dog years, I'm 98 years old!
... fast forward 15 years ...
Now I'm 203 in dog years!
I am awake!
I am sleeping....zzzz....

Notes:

  • The printing should not be hardcoded. The name, favorite color, age and other values should change if your Person object is initialized with different values.
  • Call the appropriate methods in the driver to get or set the values of the instance variables of your Person object as needed. For example, when it says "... fast forward 15 years ...", you should call a method to increase age by 15 before printing the new age in dog years.
  • For the first line of output, you should call the printInfo2() method on Mr. Jones.

Submission

  • Make sure you have named your classes correctly using the format PX_LastName_FirstName_Person and PX_LastName_FirstName_Driver where X is your period. For example, if your name is Michael Wang and you are in 2nd period, the classes should be named P2_Wang_Michael_Person and P2_Wang_Michael_Driver.
  • Make sure you changed all references in your code accordingly so the code will still compile.
  • If you changed the class names in BlueJ, the file names should have been automatically changed to match.
  • Look in the file explorer/finder inside your project folder and find the corresponding java files named in the format PX_LastName_FirstName_Person.java and PX_LastName_FirstName_Driver.java. You should submit those two files below.
  • You can drag and drop or click on the Browse button and select both files so they are both submitted in a single submission.

You must Sign In to submit to this assignment

Dark Mode

Outline