In Java 1. Assume that we have defined a class named MyClass. MyClass has a defa
ID: 3721728 • Letter: I
Question
In Java
1. Assume that we have defined a class named MyClass. MyClass has a default constructor. Please write the code snippet to declare a variable called myClass1 and assign a new object to it.
2. Write a class named MyClass with the following members:
a private instance variable of double type called "value";
a default constructor that give a default value 0 to the variable "value";
a parameterized constructor that assign a parameter to the "value" instance variable as its initial value;
3. Write a class called Coordinates with:
1. two instance variables: x and y as double
2. a default constructor that initialize x and y as 0
3. corresponding mutators and accessors for both x and y
4. Assume that we have a class called MyClass. MyClass has a (non-static) method called giveMeAValue() that will give an arbitrary integer value when called. Assume that we have created an object named myClass1 of the MyClass type.
Write the code to print a line of text to the screen like:
I have a value which is XX!
Replace XX with the value that the giveMeAValue will return.
5. State whether the following are primitive types or reference types:
-double
-string
-char
-integer
-scanner
-int
6. Assume we have a method called in the main method like this:
System.out.printf("The num or words is %d.", numOfWords(text));
Please write the definition line (aka, signature of method, first line of a method definition) of the method numOfWords. Make the best guess of the types.
Hint: static or non-static?
7. Please check correct statements about wrapper classes.
Question 7 options:
- Some advanced Java data structures only works with objects so wrapper classes have to be used to replace the - primitive types.
- Wrapper class can carry useful methods related to the corresponding wrapper class.
- To check if two wrapper class variables has same values, you should use .equals() or .compareTo() rather than ==
- Wrapper class variables can be used in most expressions same way as their primitive type counterparts.
- Some advanced Java data structures only works with objects so wrapper classes have to be used to replace the - primitive types.
- Wrapper class can carry useful methods related to the corresponding wrapper class.
- To check if two wrapper class variables has same values, you should use .equals() or .compareTo() rather than ==
- Wrapper class variables can be used in most expressions same way as their primitive type counterparts.
Explanation / Answer
1.
MyClass myClass1 = new MyClass();
2. class MyClass{
private double value;
public MyClass() { value = 0.0;}
}
public MyClass(double value) { this.value = value; }
3.
class Coordinates {
private double x,y;
public Coordinates() {
x=0.0;
y = 0.0; }
3.
public void setX(double x) {
this.x = x; }
public double getX() { return x; }
public void setY(double y) {
this.y = y; }
public double getY() { return y; }
4.
System.out.println("I have a value which is "+myClass1.giveMeAValue()+"!");
5. State whether the following are primitive types or reference types:
-double -- primitive
-string -- reference
-char -- primitive
-integer -- reference
-scanner -- primitive
-int -- primitive
6.
public static int numOfWords(String text)
{ int words = 0;
for(int i=0;i<text.length;i++)
if(text[i] == " "|| text[i] == " " ) words++;
}
return words; }
It should be a static function.
7.
- Some advanced Java data structures only works with objects so wrapper classes have to be used to replace the - primitive types.
- Wrapper class can carry useful methods related to the corresponding wrapper class.
Do ask if any doubt. Please upvote.
- Some advanced Java data structures only works with objects so wrapper classes have to be used to replace the - primitive types.
- Wrapper class can carry useful methods related to the corresponding wrapper class.
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.