Document 11586313

advertisement
1.00 Tutorial 4
Agenda
•
•
•
•
Administrative Issues
Static Methods and Data Members
Recursion
Scope and Access
Quiz 1 & Psets
• Quiz 1 Review - 1.5 hours
• Quiz 1 will cover Lecture 1 to Lecture 11
• Problem set 3 is a difficult one, you have
done great and don’t worry about it too
much. Look forward. ☺
• Problem set 4 is due one week after the quiz 1.
Classes and Objects
-A Student Class
• Write a simple Student class with following
attributes, a String variable “name”, an int variable
“age”.
• Write two constructors
– The first constructor takes one argument – a String type
argument as the value for “name”, and assign the “age”
variable value “19”.
– The Second constructor takes two arguments - a String
argument as the value for “name” and a int argument as
the value for “age”.
• Write a print method to print out the attributes
StudentTest Class
•
•
Write a main class StudentTest
In the main method,
1. creates 3 student objects by using both
constructors,
2. print out their attributes, and
3. calculate the average age of the students.
Static Data Members
• If you are asked to add static data members
to your student class, what would be your
choice?
• Try to add a studentCount (type int) to
record how many students have been
created.
• What do you need to change in your
constructor accordingly?
Static Methods
• Can you create a static method called
“averageAge” in the Student class that
calculates the average age of two students?
What about an array of students?
• Hint, think about the method title first.
Use of Static Methods and Data Members
• Now go back to your StudentTest class, in
the main method, change your code so that
it will do following:
1. creates an array of students objects by using
both constructors,
2. print out # of students created
3. calculate the average age of the students by
calling the static method.
Recursion
• Let’s review factorial, fibonacci, and power
functions. What are the base condition for
each case? What are the incremental step
for each case (how does it approach to the
base condition)?
• Draw the recursion diagram for factorial(5),
and fibonacci(5).
Recursion (2)
• Now write a recursion method to find a
minimum number from an array of doubles.
• The method title looks like the following
double findMinimum (double d[])
• Test your method in a main class
Scope and Access (1)
for (int j = 0; j<5; j++)
{
int sum += j;
}
System.out.println(sum);
What’s wrong with the above segment of
code?
Scope and Access (2)
package Package1;
public class A {
public int a;
public A(int i) {a = i;}}
package Package1;
public class B {
int b;
public B(int i) {b=i;}}
package Package1;
public class C {
private int c;
public C(int i) {c = i;}}
If I have following code
somewhere in the same package
(Package1), what line will give
me trouble? What if the
following code is in a different
package?
System.out.println((new A(5)).a);
System.out.println((new
B(5)).b);
System.out.println((new
C(5)).c);
Scope and Access (3)
package Package1;
public class A {
public int a;
public A(int i) {a = i;}}
package Package2;
public class B {
int b;
public B(int i) {b=i;}}
package Package2;
public class C {
private int c;
public C(int i) {c = i;}}
If I have following code
somewhere in the Package2,
what line will give me trouble?
What if the following code is in
Package1?
System.out.println((new A(5)).a);
System.out.println((new
B(5)).b);
System.out.println((new
C(5)).c);
Download