Java Reference
In-Depth Information
//Method to output count
public static void
printCount()
{
//write the definition
}
//Method to increment count
//Postcondition: count++
public static int
incrementCount()
{
//write the definition
}
}
a.
Write a Java statement that increments the value of
count
by
1
.
b.
Write a Java statement that outputs the value of
count
.
c.
Write the definitions of the methods and the constructors of the
class
MyClass
as described in its definition.
d.
Write a Java statement that declares
myObject1
to be a
MyClass
object
and initializes its instance variable
x
to
5
.
e.
Write a Java statement that declares
myObject2
to be a
MyClass
object
and initializes its instance variable
x
to
7
.
f.
Which of the following statements are valid? (Assume that
myObject1
and
myObject2
are as declared in parts d and e.)
myObject1.printCount();
//Line 1
myObject1.printX();
//Line 2
MyClass.printCount();
//Line 3
MyClass.printX();
//Line 4
MyClass.count++;
//Line 5
g.
Assume that
myObject1
and
myObject2
are as declared in parts d
and e. After you have written the definition of the methods of the
class
MyClass
, what is the output of the following Java code?
myObject1.printX();
myObject1.incrementCount();
MyClass.incrementCount();
myObject1.printCount();
myObject2.printCount();
myObject2.printX();
myObject1.setX(14);
myObject1.incrementCount();
myObject1.printX();
myObject1.printCount();
myObject2.printCount();
Search WWH ::
Custom Search