2. Consider the following definition of the class MyClass public class MyClass p
ID: 3750368 • Letter: 2
Question
2. Consider the following definition of the class MyClass public class MyClass peivale int x peivate static int count dcfault constructor public MyClass[ 1 constractor with a parameter public MyClass int a) methed to set the value of public void stXntal t public void prinX) pulic static veid primCoum). public static void incrememCoune method to output method to output count method to increment count a. Wrine a Java statement that increments the value of count by 1. b. Write a Java sanement that outpats the vallue of count. e. Write the 6 definitions of the constructons and the methods of the elass MyClas as describod in ins dcaitios d Wrise a Java statement that declares myoejectl to be a MyClass object and initializes its instance variable xo. e. Write aJava statement that declares myObject2 to be a MyClass object and initialies its instance variablex to 7 f Identify each of the following stalements as ciher valid oe ievalid (Ill why it is invalid Assume that myObjoctl and my Object2 are declared as in parts (d) and (c) my Objectl.My Class my Object2 sxiSOk MyClass printCouel MyClass.counExplanation / Answer
a) count++;
b) System.out.println(count);
c) MyClass(){
x = 0;
count = 0;
}
public MyClass(int a){
x = a;
}
public void setX(int a){
x = a;
}
public void printX(){
System.out.println(x);
}
public static void printCount(){
System.out.println(count);
}
public static void incrementCount(){
count++;
}
d) MyClass myObject1 = new MyClass(5);
e) MyClass myObject2 = new MyClass(7);
f) myObject1.Myclass(); is invalid as we can't call it's constructor using object
myObject2.setX(50); valid
MyClass.printCount(); valid
MyClass.printX(); not valid as the method is able to be called using its object only. Only static methods can be called using class Name.
MyClass.count++; It is valid if you are calling it from the class itself and not valid from the other class as the count is declared as private.
//any query, post in the comment section
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.