Download Weeks 7

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CPSC150
Week 6 notes
Chapter 5 and other topics
toString
• Try to print any class
• Many print “garbage” (e.g., an address)
• All classes inherit from Object which has a
toString method that prints the address
• You can override Object’s toString method
• Modify Quiz 2 to use toString method
• Write a toString method for every class
you write
public class Student
{
private String name;
private double gpa;
// constructor and accessor and mutator methods
public String toString( )
{
return (“Student : “ + name + “ with “ + gpa
+ “GPA” + “\n”);
}
public void printStudent( )
{
System.out.println(this);
}
Now, rewrite Course method
JavaDoc Documentation
• View in BlueJ through Interface
• View on web through API documentation
(application programming interface):
– http://java.sun.com/j2se/1.5.0/docs/api/
• Write your own
/** Here is what my Class does
* can go over multiple lines */
JavaDoc: write your own
• Write in html (text IS html)
/** Here is my <b>important</b> part */
• Use tags:
@author your name
@version date or 1.0
@param name description
@return description
• For a full list of tags, see:
http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javadoc.html
Using the API
• Use API whenever you want to do something, but don’t
know how
Example: How do I use Random numbers?
• Look at API for Random
• Or, Look at documentation for classes:
– http://java.sun.com/j2se/1.5.0/search.html
– (note: search isn’t always that helpful)
• Or, look at: http://java.sun.com/j2se/1.5.0/docs/
– This has a more graphical representation than the
API; most people use the API
– Look at top description and each method summary
Random
• java.util.Random
• javadoc
• http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html
import java.util.Random;
import java.util.ArrayList;
public class MyNumbers
{
// use Integer wrapper because ArrayLists must have object types
ArrayList<Integer> nbrs = new ArrayList<Integer> ();
Random random = new Random();
public MyNumbers()
{ // don’t need to do anything
}
public void addNumber( )
{
nbrs.add(random.nextInt()); // nextInt from javadoc
}
public String toString( )
{
String toReturn = ““List of Numbers is: ”;
for (int i : nbrs)
Math Library
• in java.lang.Math
• java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html
• Lots of methods, all static.
public class MathDemo
{
public MathDemo() { // don’t need anything}
public double findDoubleMin(double a, double b)
{ return Math.min(a, b); } // no object needed
}
ArrayList Exercises
• Assume that a Book class exists that has a title
(type String), author (type String) and call number
(type int). Assume that Book has a toString method
and accessor and mutator methods for all three
fields.
• Write a Library class that has an ArrayList bookList.
Use generics. Create the following methods:
addBook, printBook, and removeBook. Use a
foreach loop for printBook. Just worry about title
and author for now.
• Write a method addBookBunch which accepts an
array of Books and adds each to the bookList.
More on Library
• Add a method addCallNbrs which assigns a call
number to each book in the library. The call
numbers should be sequential starting at 10 and
adding 10 for each book (so the first 5 call
numbers would be 10, 20, 30, 40, 50).
• Revise the earlier methods to include adding call
numbers. The call number should be reported to
the user in printBook, but addBook should
generate a call number, not request one from
the user.
More ArrayList exercises
• Rewrite your Course class from Quiz 2 so that, instead
of using two Student objects, you use an ArrayList to
hold all Students.
• Create a class FillCanvas that creates Circles at random
places (between 0 and 300 for both x and y) and of
random size (between 0 and 300). Have a private
getNewCircle method that creates one new Circle (but
does not draw it). Write a public Fill10Circles method
that puts 10 random circles in a circles ArrayList (by
calling getNewCircle 10 times).
• Write a method drawCircles that draws all of the circles
in the ArrayList circles.
• Write a method drawCirclesOneAtATime that draws the
circle, then erases it, then draws the next circle.