public class XClass { private int u; private double w; public XClass() { } publi
ID: 3608949 • Letter: P
Question
public class XClass { private int u; private double w; public XClass() { } public XClass(int a, double b) { } public void func() { } public void print() { } } XClass x = new XClass(10, 20.75);a. How many members does class XClasshave?
b. How many private members does class XClasshave?
c. How many constructors does class XClasshave?
d. Write the definition of the member func so thatu is set to 10 and w is set to15.3.
e. Write the definition of the member printthat prints the contents of u andw.
f. Write the definition of the default constructor of theclassXClass so that the instance variables areinitialized to 0.
g. Write the definition of the constructor with the parametersof the classXClass so that the instance variableu is initialized to the value ofa and the instance variable w isinitialized to the value of b.
h. Write a Java statement that prints the values of theinstance variables of x.
i. Write a Java statement that creates the XClass object t and initializes theinstance variables of t to 20 and 35.0respectively.
public class XClass { private int u; private double w; public XClass() { } public XClass(int a, double b) { } public void func() { } public void print() { } } XClass x = new XClass(10, 20.75);
a. How many members does class XClasshave?
b. How many private members does class XClasshave?
c. How many constructors does class XClasshave?
d. Write the definition of the member func so thatu is set to 10 and w is set to15.3.
e. Write the definition of the member printthat prints the contents of u andw.
f. Write the definition of the default constructor of theclassXClass so that the instance variables areinitialized to 0.
g. Write the definition of the constructor with the parametersof the classXClass so that the instance variableu is initialized to the value ofa and the instance variable w isinitialized to the value of b.
h. Write a Java statement that prints the values of theinstance variables of x.
i. Write a Java statement that creates the XClass object t and initializes theinstance variables of t to 20 and 35.0respectively.
Explanation / Answer
a. How many members does class XClass have? Answer : 6b. How many private members does class XClass have? Answer : 2
c. How many constructors does class XClass have? Answer : 2
d. Write the definition of the member func so that u is set to10 and w is set to 15.3 Answer : public void func() { u = 10 ; w = 15.3;
}
e. Write the definition of the member print that prints thecontents of u and w.
Answer :
public void print() { u = 10 ; w = 15.3;
System.out.println("u : " + u);
System.out.println("w: " + w);
} f. Write the definition of the default constructor of theclass XClass so that the instance variables are initialized to0(zero).
Anwer :
public XClass() { u = 0 ; w = 0; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.