-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassObject.java
More file actions
103 lines (85 loc) · 2.46 KB
/
ClassObject.java
File metadata and controls
103 lines (85 loc) · 2.46 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// simple first Hello World program //
public class ClassObject
{
int x; // instance variable
static int y; // class variable
private static class Point{
int x_coord;
int y_coord;
int n;
// default constructor
Point(){
this.x_coord = 10;
this.y_coord = 10;
}
// constructor setting x on user defined value, y on 10
// defining a class's (constructor) method more than once is called overloading
Point(int x_coord){
this.x_coord = x_coord;
this.y_coord = 10;
}
Point(int x_coord, int y_coord){
this.x_coord = x_coord;
this.y_coord = y_coord;
}
public void set_x_coord( int x_coord_2b_set ){
this.x_coord = x_coord_2b_set;
}
public int get_x_coord(){
// returns value of attribute x_coord of an object to the caller
return this.x_coord;
}
public long calculateFacultyIteratively(int n){
long facu = 1;
for ( int i = 1; i <= n; i++ )
{
facu *= i;
}
return facu;
}
public long calculateFacultyRecursively(int n){
System.out.println("Called with "+ n);
if ( n >= 1 )
{
return n * calculateFacultyRecursively( n - 1 );
}
else
{
return 1;
}
}
}
class Line{
Point point_one;
Point point_two;
}
public class PointExtended extends Point{
}
public static void main(String[] args)
{
// calling default constructor
Point p = new Point();
// print object p and its variables
System.out.println("Object p: " + p);
System.out.println("Default constructor x_coord: " + p.x_coord);
System.out.println("Default constructor y_coord: " + p.y_coord);
p.set_x_coord(2);
System.out.println("Default constructor x_coord: " + p.x_coord);
p.set_x_coord(99);
System.out.println("Default constructor x_coord: " + p.get_x_coord());
int val = 6;
System.out.println("Faculty of " + val + ": " + p.calculateFacultyIteratively(val));
long facu = p.calculateFacultyRecursively(val);
System.out.println("Faculty of " + val + ": " + facu);
// calling constructor with one parameter
Point p1 = new Point(23);
System.out.println("Object p1: " + p1);
System.out.println("Parameter constructor x_coord: " + p1.x_coord);
System.out.println("Default constructor y_coord: " + p1.y_coord);
// calling constructor with two parameters
Point p2 = new Point(42,12);
System.out.println("Object p2: " + p2);
System.out.println("Parameter constructor x_coord: " + p2.x_coord);
System.out.println("Parameter constructor y_coord: " + p2.y_coord);
}
}