0% found this document useful (0 votes)
28 views

1.00 Tutorial 4

This document provides an agenda for a tutorial covering static methods and data members, recursion, and scope and access. It includes examples of writing a Student class with constructors and methods, using static data members to track the number of students created, and examples of recursion and issues with variable scope. Key points are: - The tutorial will cover administrative issues, static methods/data members, recursion, and scope/access. - Examples are provided for writing Student and StudentTest classes to demonstrate constructors and calculating average age. - Static data members can be used to track the number of students created. - Recursion examples include calculating factorials, Fibonacci numbers, and finding minimum in an array

Uploaded by

yekych
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

1.00 Tutorial 4

This document provides an agenda for a tutorial covering static methods and data members, recursion, and scope and access. It includes examples of writing a Student class with constructors and methods, using static data members to track the number of students created, and examples of recursion and issues with variable scope. Key points are: - The tutorial will cover administrative issues, static methods/data members, recursion, and scope/access. - Examples are provided for writing Student and StudentTest classes to demonstrate constructors and calculating average age. - Static data members can be used to track the number of students created. - Recursion examples include calculating factorials, Fibonacci numbers, and finding minimum in an array

Uploaded by

yekych
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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 dont 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

Lets 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);
Whats wrong with the above segment of
code?
Scope and Access (2)

package Package1;
public class A { If I have following code
public int a; somewhere in the same package
public A(int i) {a = i;}} (Package1), what line will give
me trouble? What if the
package Package1; following code is in a different
public class B { package?
int b;
public B(int i) {b=i;}}
System.out.println((new A(5)).a);
package Package1; System.out.println((new
public class C { B(5)).b);
private int c; System.out.println((new
public C(int i) {c = i;}} C(5)).c);
Scope and Access (3)
package Package1;
public class A {
public int a; If I have following code
public A(int i) {a = i;}} somewhere in the Package2,
what line will give me trouble?
package Package2; What if the following code is in
public class B { Package1?
int b;
public B(int i) {b=i;}} System.out.println((new A(5)).a);
System.out.println((new
package Package2;
B(5)).b);
public class C {
private int c; System.out.println((new
public C(int i) {c = i;}} C(5)).c);

You might also like