Kristofer Younger 3c741d9c85 Update 'README.md' | vor 6 Jahren | |
---|---|---|
.gitignore | vor 6 Jahren | |
README.md | vor 6 Jahren |
In the source code of class Picture, find the part that actually draws the picture. Change it so that the sun will be blue rather than yellow.
Add a second sun to the picture. To do this, pay attention to the field definitions close to the top of the class. You will find this code: private Square wall; private Square window; private Triangle roof; private Circle sun; You need to add a line here for the second sun. For example: private Circle sun2; Then write the appropriate code for creating the second sun.
(This means that this exercise might not be solved quickly. We do not expect everyone to be able to solve this at the moment. If you do – great. If you don’t, then don’t worry. Things will become clearer as you read on. Come back to this exercise later.)
Add a sunset to the single-sun version of Picture. That is: make the sun go down slowly. Remember: The circle has a method slowMoveVertical that you can use to do this.
If you added your sunset to the end of the draw method (so that the sun goes down automatically when the picture is drawn), change this now. We now want the sunset in a separate method, so that we can call draw and see the picture with the sun up, and then call sunset (a separate method!) to make the sun go down.
Create an object of class Student. You will notice that this time you are prompted not only for a name of the instance, but also for some other parameters. Fill them in before clicking Ok. (Remember that parameters of type String must be written in double quotes.)
Create some student objects. Call the getName method on each object. Explain what is happening.
Create an object of class LabClass. As the signature indicates, you need to specify the maximum number of students in that class (an integer).
Call the numberOfStudents method of that class. What does it do?
Look at the signature of the enrollStudent method. You will notice that the type of the expected parameter is Student. Make sure you have two or three students and a LabClass object on the object bench, then call the enrollStudent method of the LabClass object. With the input cursor in the dialog entry field, click on one of the student objects – this enters the name of the student object into the par- ameter field of the enrollStudent method (Figure 1.8). Click Ok, and you have added the student to the LabClass. Add one or more other students as well.
Call the printList method of the LabClass object. You will see a list of all the students in that class printed to the BlueJ terminal window (Figure 1.9).
Create three students with the following details:
Then enter all three into a lab and print a list to the screen.
Use the inspector on a LabClass
object to discover what fields it has.
Set the instructor, room, and time for a lab, and print the list to the terminal window to check that these new details appear.
In this chapter we have mentioned the data types int and String. Java has more predefined data types. Find out what they are and what they are used for. To do this, you can check Appendix B, or look it up in another Java book or in an online Java language manual. One such manual is at http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
What are the types of the following values?
What would you have to do to add a new field, for example one called name, to a circle object?
Write the signature for a method named send
that has one parameter of type String, and does not return a value.
Write the signature for a method named average that has two parameters, both of type int, and returns an int value.
Look at the book you are reading right now. Is it an object or a class? If it is a class, name some objects. If it is an object, name its class.
Can an object have several different classes? Discuss.