Java代写:OOP编程入门

OOP的入门级别作业,实验可以实例化的类。

CS 117: Instantiable classes lab

Just do these exercises and ask questions when you have them. You need not hand anything in.

I. Creating a Student

  1. Create a new BlueJ project called student. From the “Edit” menu, choose “Add Class From File.” Go to S:\Shared_Faculty_Files\Ondich\ and select the file named Student.java.

  2. Look at the Java source code for Student.java. What does this class do?

  3. In the top right corner of the Student.java window, click on the bar that says “Implementation”. On the drop down box that appears, click “Interface”. BlueJ will then automatically generate the javadoc for this class. What information does it contain? Scroll through it and look at it. Notice the detailed information it contains on author, version, and documentation for each method. How does BlueJ figure all this out? What purpose does a javadoc serve?

  4. Click on the “Interface” bar to change it back to “Implementation”.

  5. Compile the Student class.

  6. Go back to the main BlueJ window and create an instance of the Student class using the right click menu and the new Student() option. After you name the new student object (“student_1” is the default) and say OK, you should see a red rectangle with rounded corners in the bottom of the BlueJ window. This represents an instance of the Student class. Note that BlueJ’s rectangle and near-rectangle notation corresponds nicely with the class/instance drawings in Wu.

  7. Double-click on the new instance. An “object inspector” window will open and show you the object’s data members and their values. (For example, the data member age should have value 18.) Where did these values come from?

  8. Right click the instance and select “int getAge().” When you do this, you are actually invoking the getAge instance method on the particular Student instance that your created two steps ago.

  9. Use the appropriate instance method of the object to change the value of its firstName member. Don’t forget to put the double quotes on the String. If you still have the object inspector window open, you should see the new name there. You can also use the getFirstName method to see the new name.

  10. Use the appropriate method of the object to change the value of its familyName member. Inspect the object to see if the change worked.

  11. Use the appropriate method of the object to change the value of its age member. Inspect the object to see if the change worked.

  12. Create another Student object using the right click menu and the

    new Student(anIdNumber,aFirstName,aFamilyName,anAge)

    option. You are creating an instance of the class and calling the 4-parameter constructor rather than the default 0-parameter constructor. Give the members some values as the object is created. Inspect the new object. Note that you can have more than one object inspector open at once.

  13. Create yet another object using the 4-parameter . See what happens when you key in the parameters incorrectly (e.g. key in the first name or the family name without quotes, or the age with quotes).

  14. Change the value of the familyName member of this object. Then use the getFamilyName method to find out what its family name is now.

  15. From the “Edit” menu, choose “Add Class From File.” Go to the “T” disk drive at the top, then go into the “javalibs” folder, then go into the “dmusican” folder. Click on the file named StudentInfo.java.

  16. Try to compile StudentInfo.java. What kind of error do you get?

  17. Go into Student.java, and change the line that says

    private String firstName;

    to say

    public String firstName;

  18. Recompile Student, then recompile StudentInfo. StudentInfo now compiles successfully. Why? What is the difference between a private and a public data value?

  19. Change firstName back to private in Student. Recompile.

  20. Modify the main method in StudentInfo so that it asks a user for an idNumber, firstName, familyName, and age for three different students, each of which gets stored in an individual student object. Output a summary of each student back to the user.

II. Creating a Car

  1. In a new project, create a class called Car. Within this class, you should have data members that represent year manufactured, miles driven, manufacturer name, and model name. You should provide methods for updating these data values, namely:

    void setYearManufactured(int year);
    void setMilesDriven(int miles);
    void setManufacturerName(String name);
    void setModelName(String name);

    You should also provide methods for obtaining these data values, namely:

    int getYearManufactured();
    int getMilesDriven();
    String getManufacturerName();
    String getModelName();

    Make appropriate use of the private and public visibility modifiers, and make sure to include a constructor.

  2. Check the javadoc that BlueJ generates for your class. Add comments to your program appropriately so that the javadoc contains useful information.

  3. It turns out that a car might be sold to a recent immigrant from Canada. Therefore, you want to be able to report the distance driven in kilometers instead of miles. Add a method called “getKilometersDiven” that returns the distance driven in kilometers instead of miles. (Useful information: 1 mile = 1.6094 kilometers.)

kamisama wechat
KamiSama