Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//Dog class
class Dog {
//Private Variables/Fields for Data Members
private String breed;
private String size;
private String color;
private int age;
//Constructor
private boolean isHungry;

Dog(String breed, String size, String color, int age) {
this.breed = breed;
this.size = size;
this.color = color;
this.age = age;
//Constructor
this.isHungry = true; //Dog is hungry
}

//Method to /set/ dog's name
void setName(String name) {
this.name = name;
}

//Method to /get/ dog's name
String getName() {
return name;
}

//4 Methods for eat, run, sleep, and name

//Method for dog to eat
void eat() {
if (isHungry) {
System.out.println(getName() + " is eating.");
isHungry = false; //Dog already ate
} else {
System.out.println(getName() + " is not hungry right now.");
}
}

//Method for dog to run
void run() {
System.out.println(getName() + " is running.");
}

//Method for dog to sleep
void sleep() {
System.out.println(getName() + " is sleeping.");
}

//Main Method: Dog objects and their behavior
public static void main(String[] args) {
//Three Dog objects
Dog bulldog = new Dog("Bulldog", "Large", "Light Grey", 5);
bulldog.setName("Buddy");

Dog beagle = new Dog("Beagle", "Large", "Orange", 6);
beagle.setName("Max");

Dog germanShepherd = new Dog("German Shepherd", "Large", "White and Orange", 6);
germanShepherd.setName("Rocky");

//Performing actions on the dogs
bulldog.eat();
beagle.run();
germanShepherd.sleep();
}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Java-Assignment-010 - Classes and Objects

## Part 1 - Analyze
* Add Comments to the Code below and label the following:
* Add Comments to the Code below and label the following using comments:
1. Class name
2. All **instance variables/fields** and their data-types
3. The **Constructor** and the **Constructor Parameters**
Expand Down Expand Up @@ -36,9 +36,11 @@ class Student{

* Read the W3Schools page on class methods: [W3Schools Java Class Methods](https://www.w3schools.com/java/java_class_methods.asp)
* In your own words, write a few sentences below explaining the difference between static and public methods in relation to a class.
* ---------------------
* Public can only be accessed by objects.
* Static can be accsed without needing to creat an object.

## Part 3 - Dogs

* View the image below, and from the image, construct a Java file **Dog** that mirrors the diagrammed class and the 3 dog objects.
![Dog Class](images/ClassVSObject.png)

Expand Down
26 changes: 26 additions & 0 deletions Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//i. Class name
class Student {
//ii. Instance variables/fields and their data-types
private String name;
private int rollNo;

//iii. Constructor and the Constructor Parameters
Student(String s, int r) {
//v. Where the instance variables value gets set and what its values are
name = s;
rollNo = r;
}

//vi. All the instance methods for the class Student
void methodForDisplay() {
System.out.println(name + "'s Roll No: " + rollNo);
}

//iv. Where a Student object gets created
public static void main(String[] args) {
Student obj1 = new Student("Rambo", 21); //Student object creation
obj1.methodForDisplay();
}
}