-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassesCode.java
More file actions
35 lines (30 loc) · 1.05 KB
/
ClassesCode.java
File metadata and controls
35 lines (30 loc) · 1.05 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
public class ClassesCode {
// A simple class representing a Dog
static class Dog {
// Attributes (fields) of the Dog class
String name;
int age;
// Constructor to initialize a Dog object
Dog(String name, int age) {
this.name = name;
this.age = age;
}
// Method to make the dog bark
void bark() {
System.out.println(name + " says: Woof Woof! ==> ");
}
// Method to get the dog's age in human years
int getAgeInHumanYears() {
return age * 7;
}
}
public static void main(String[] args) {
// Creating an instance (object) of the Dog class
Dog myDog = new Dog("Buddy ==> ", 3);
// Accessing attributes and methods of the Dog object
System.out.println("My dog's name is: " + myDog.name);
System.out.println("My dog is " + myDog.age + " years old.");
myDog.bark();
System.out.println("My dog's age in human years is: " + myDog.getAgeInHumanYears());
}
}