-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaClasses_ObjectsE5.java
More file actions
41 lines (39 loc) · 1.18 KB
/
javaClasses_ObjectsE5.java
File metadata and controls
41 lines (39 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* from JAVA Classes/Objects: Exercise 5 ( https://www.w3schools.com/java/exercise.asp?filename=exercise_classes5 )
*
* question:
* Create and call a class constructor of MyClass
* Follow the comments to insert the missing parts of the code below:
*
* // Create a MyClass class
* public class _______ {
* int _; // Create a class attribute x
*
* // Create a class constructor for the MyClass class
* public _______() {
* x = _; // Set the initial value for the class attribute x to 5
* }
*
* public static void main(String[] args) {
* // Create an myObj object of class MyClass (This will call the constructor)
* MyClass _____ = new MyClass();
* // Print the value of x
* System.out.println(_____._);
* }
* }
*
*/
// Create a MyClass class
public class MyClass {
int x; // Create a class attribute x
// Create a class constructor for the MyClass class
public MyClass() {
x = 5; // Set the initial value for the class attribute x to 5
}
public static void main(String[] args) {
// Create an myObj object of class MyClass (This will call the constructor)
MyClass myObj = new MyClass();
// Print the value of x
System.out.println(myObj.x);
}
}