Lesson
20 Exercise
Matthew
Walker
Balcar
3A 1/30/13
1.
At any time after several TvShow objects have been instantiated, how would you find out
how
many shows were instantiated? (Don’t use an object to do this.)
2.
Would the code inside the numberOfShows
method still be correct if numberOfShows
were
non-static? If not, why?
3.
Suppose the code inside the numberOfShows
method is replaced with the following
line:
return
y;
Is
this legal? If not, why?
4.
Write code that will print the data member actor2. Do this without instantiating any
objects.
5.
Is the following code legal? If not, why?
TvShow.setActor1(“Jimmy
Stewart”);
6.
Create an instance of TvShow called chrs (pass in the String
“Cheers”) and use it to
access
and print the class variable numShows.
7.
Give the output of the following:
System.out.println(TvShow.x);
TvShow
chrs = new TvShow(“Cheers”);
System.out.println(TvShow.x);
System.out.println(chrs.x);
TvShow
hc = new TvShow(“History Channel”);
hc.x
= 160;
System.out.println(TvShow.x);
System.out.println(hc.x);
8.
Is the following a legal declaration of a class variable? If not, why?
static
public char ch = ‘K’;
9.
Write code that will cause the variable zxb
to be a static state
variable. The variable zxb is
a
double.
10.
Write code that will cause sn to be a constant static
class member. The constant sn should
be
initialized as an empty String.
11.
What is the significance of the word Math
when we use Math.pow(3.2, 4.001)?
Math is the
class variable
12.
Class variables are also called Static
State variables.
13.
Assuming that the appropriate static
import has been done, rewrite the
following code
without
using the class name of the static methods.
double
xop = Math.pow(Math.sqrt(x - zv), 3.1);
14. What
are the two primary reasons for using the key-word, static?
1.
you can call a method without creating an object
2. they are shared by all objects
No comments:
Post a Comment