Last modified: January 26, 2024
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.
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 theTools
menu and selectReplace...
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
andPX_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.
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
Add the following attributes and update the constructors accordingly:
myFavoriteNumber
that stores an int value.myFavoriteColor
that stores a reference to a String.isSleeping
that stores true or false.Add the following methods and write the appropriate code:
ageInDogYears
that returns the age of this person in dog years (1 human year = 7 dog years).getFavoriteColor
that returns this person's favorite color.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.
sleep
that changes the state of this person to sleeping.wakeUp
that changes the state of this person to awake.Replace the code in your Driver class to make it create a Person object with the following attributes
Then make another Person with the following attributes:
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:
printInfo2()
method on Mr. Jones.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
.PX_LastName_FirstName_Person.java
and PX_LastName_FirstName_Driver.java
. You should submit those two files below.You must Sign In to submit to this assignment
Dark Mode
Outline