A class is a blueprint or template that defines the properties (attributes) and behaviors (methods) that all objects of that type can have.
Here's how to create a simple Person class in jshell:
class Person {
String name;
int age;
}A constructor helps initialize a new Person object with specific values:
class Person {
String name;
int age;
Person(String n, int a) {
name = n;
age = a;
}
}Let's create some Person objects using our class:
jshell> Person person1 = new Person("Alice", 25)
person1 ==> Person@7852e922
jshell> Person person2 = new Person("Bob", 30)
person2 ==> Person@4e25154fYou can access the properties of a Person object using dot notation:
jshell> person1.name
$4 ==> "Alice"
jshell> person1.age
$5 ==> 25Let's add some methods to our Person class:
class Person {
String name;
int age;
Person(String n, int a) {
name = n;
age = a;
}
void sayHello() {
System.out.println("Hello, my name is " + name);
}
void haveBirthday() {
age = age + 1;
System.out.println("Happy Birthday! " + name + " is now " + age);
}
}Now we can use these methods with our Person objects:
jshell> person1.sayHello()
Hello, my name is Alice
jshell> person1.haveBirthday()
Happy Birthday! Alice is now 26Try creating your own Person class with:
- Create the basic class structure
- Add properties (name and age)
- Create a constructor
- Create two Person objects
- Access their properties
- Add and use methods
- Forgetting to initialize variables in the constructor
- Not using proper capitalization (class names should start with a capital letter)
- Forgetting to use
newwhen creating objects - Trying to access private variables directly
- Always test your class by creating objects and using their methods
- Use meaningful names for variables and methods
- Keep your code organized and properly indented
- Remember that each object is independent and has its own set of values
You can use these jshell commands to verify your work:
/vars- shows all variables/methods Person- shows all methods in the Person class/list- shows all code entered
Here's a more complete Person class:
class Person {
String name;
int age;
// Constructor
Person(String n, int a) {
name = n;
age = a;
}
// Method to print person details
void printDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
// Method to check if person is adult
boolean isAdult() {
return age >= 18;
}
// Method to have birthday
void haveBirthday() {
age = age + 1;
System.out.println("Happy Birthday! " + name + " is now " + age);
}
}Try using this extended example:
jshell> Person person = new Person("John", 17)
jshell> person.printDetails()
jshell> System.out.println(person.isAdult())
jshell> person.haveBirthday()
jshell> System.out.println(person.isAdult())This guide should give you a solid foundation for understanding how to create and use classes in Java. Remember to practice by creating your own variations of the Person class and experimenting with different properties and methods.
Maybe add a method that sets and gets a person's "chirality" (left-handed or right-handed) or "handedness" (ambidextrous, left-handed, right-handed). What type is variable would be best for this? What would be the default value?
You have to add a veriable declaration for the handedness. And add a getter and setter for the handedness. Be sure to add a method to print out the handedness.
Save it in a file using /save Person.jsh and also do the trinity if github commands,
git add .
git commit -m "Add Person class work"
git pushClasses and Objects are an essential part of object-oriented programming in Java.
Be sure to do commit of all this work to your GitHub repository.
touch finished.txt
git add .
git commit -m "Finished"
git push