In this lab, we will practice with classes and inheritance in Java. The tutorials in the lab will be presented by the TA step-by-step. However, you are free to work at your own pace on the lab. The lab assignment questions will not be presented by the TA.
This tutorial will demonstrate how to create a class with instance variables and methods.
Create a new project called Lab7 and a new class inside the project called Person. This class should be public and it should not have a main method. Create a second class called TestPerson that has a main method.
Add to the Person class the instance variables firstName, lastName, and birthYear. All instance variables should be private.
A constructor is a special method that builds an object when new is called. If you do not create a constructor, Java automatically creates a constructor for you with no parameters which is called the default constructor. We will create the default constructor and our own constructor that takes in the values of the instance variables as parameters. The code is below. Eclipse will build a constructor for you. Select Source from the menu then Generate Constructor Using Fields...
Just click OK in the window that prompts for the fields to build a constructor for. Repeat the process again, but this time uncheck all fields to produce a default constructor.
The result should look like this. For now, ignore the super() call. The this. means change the instance variables of this object.
Since instance variables are private, we need methods to get or set the values of these variables. These are called getter/setter methods. These have a standard naming convention. For the variable firstName, the get method would be String getFirstName() and the set method would be void setFirstName(String fname). Notice that the get method has no parameters but returns a string (the first name), and the set method returns nothing but takes as a paramteter a string that represents what the first name should be set to. You could type these methods yourself, but Eclipse makes it easier for you. Under Source menu select Generate Getters and Setters.... Make sure all fields are checked (you want to generate get/set methods for all your instance variables), then click OK.
The code results for two of the instance variables:
You can create as many methods as you want in your class. We will add two more methods: int getAge() will calculate the user's age based on the current date (may be imprecise as only storing year) and String getFullName() which will return their full name. One special method is toString(). This method is used by Java whenever it has to convert your object into a string (like when it needs to print it). By defining this method, it is easier to see what objects contain rather than calling lots of get methods. The code is below. You will need to import java.util.Calendar and java.util.GregorianCalendar.
In the main method of the TestPerson class, we will create two new Person object instances. One uses the default constructor while the other uses the customer constructor with values "Joe", "Smith", 1990. The default constructor creates an "empty" object. We can use the set methods to set the first name, last name, and year to "Fred", "Walter", 1980 respectively. Finally, we use some of the getter methods to retrieve the data and show how toString() can print out an entire object however we decide to format it. The final code files are: Person.java and TestPerson.java.
Output:
Fred was born in: 1980 Info about Joe: First name: Joe Last name: Smith Year: 1990
In this tutorial, we will learn how we can use inheritance to define similar objects.
In your Lab7 project, create two new classes (no main methods) called Student and Instructor. The Student class should have instance variables major and gpa. The Instructor class should have instance variables salary and department. Create the appropriate getter/setter methods.
Inheritance is a method for extending existing classes. A new class inherits properties and methods from an existing class. In our example, the two new classes Student and Instructor can inherit from the existing class Person. This is beneficial as we can use the existing instance variables and methods in the class that we inherit from. To denote inheritance, we say class Student extends Person. Person is called the superclass, and Student and Instructor are called subclasses.
Create a default and overloaded constructor for both Student and Instructor. In this case, Eclipse will not automatically generate an overloaded constructor that has all fields of the subclass and all fields of the superclass. You will have to manually edit.
Polymorphism is when a method that is defined in both a superclass and subclass is dynamically selected based on the object type. Let's define a toString method for both new subclasses. Then, create a new class with a main method called TestInheritance.java with code as below. Note that the object reference is of type Person. When the toString method is called, the system calls the correct toString method based on the type of object not the type of the object reference. The final code files are: Person.java, Instructor.java, Student.java, TestPerson.java, TestInheritance.java.
toString method for Student:
toString method for Instructor:
main method in TestInheritance.java:
Output:
Printing student: First name: Amy Last name: Winters Year: 1990 Major: Computer Science GPA: 4.0 Printing instructor: First name: Steve Last name: Smith Year: 1960 Salary: 70000.0 Department: Unit 4
Starting in this lab, the labs are designed to construct the components of the simple game Asteroids. Each lab will build one part of each project.
We will begin with creating four classes for use with our asteroids game:
This is the superclass for all game objects. Instance variables:
Methods:
This class inherits from GameObject class and will represent our spaceship. New instance variables:
Methods:
This class inherits from GameObject class and will represent the asteroids that we will shoot. New instance variables:
Methods:
This is the main class. It has no instance variables. It has no methods. In the main method we will create and print three objects:
An example output when running the main class is:
Game object: Location: (0.0,0.0) Speed: (0.0,0.0) Size: (0.0,0.0) Ship: Location: (100.0,200.0) Speed: (5.0,5.0) Size: (30.0,30.0) Acceleration: (1.0,1.0) Num.Lives: 3 Asteroid: Location: (50.0,50.0) Speed: (-5.0,-10.0) Size: (60.0,60.0) Type: 3 Ship: Location: (100.0,200.0) Speed: (5.0,5.0) Size: (30.0,30.0) Acceleration: (1.0,1.0) Num.Lives: 3 Asteroid: Location: (50.0,50.0) Speed: (-5.0,-10.0) Size: (60.0,60.0) Type: 3 Exploding asteroid: Location: (50.0,50.0) Speed: (-5.0,-10.0) Size: (60.0,60.0) Type: 3 New asteroid: Location: (50.0,50.0) Speed: (-5.0,-10.0) Size: (40.0,40.0) Type: 2
For this lab assignment, each group should submit using Connect all the Java files for the questions. Please submit a zip containing all Java files.