Skip to content
Open

10 #44

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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Main/Main.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
53 changes: 53 additions & 0 deletions Main/src/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
public class Dog {

private String breed;
private String size;
private String color;
private int age;

public Dog(String breed, String size, String color, int age) {
this.breed = breed;
this.size = size;
this.color = color;
this.age = age;
}

public void eat() {
System.out.println("The " + breed + " is eating.");
}

public void run() {
System.out.println("The " + breed + " is running.");
}

public void sleep() {
System.out.println("The " + breed + " is sleeping.");
}

public String name() {
return breed;
}

public static void main(String[] args) {

Dog Dog1Object = new Dog("Bulldog", "large", "light gray", 5);
Dog Dog2Object = new Dog("Beagle", "large", "orange", 6);
Dog Dog3Object = new Dog("German Shepherd", "large", "white & orange", 6);

// Testing the methods for Dog1Object
System.out.println("Dog1: " + Dog1Object.name());
Dog1Object.eat();
Dog1Object.run();
Dog1Object.sleep();

System.out.println("Dog2: " + Dog2Object.name());
Dog2Object.eat();
Dog2Object.run();
Dog2Object.sleep();

System.out.println("Dog3: " + Dog3Object.name());
Dog3Object.eat();
Dog3Object.run();
Dog3Object.sleep();
}
}
52 changes: 33 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,47 @@
6. All the instance methods for the class **Student**

```java
// Class name: Student
class Student{
private String name;
private int rollNo;

Student(String s, int r)
{
name = s;
rollNo = r;
}

void methodForDisplay()
{
System.out.println(name+"'s Roll No: "+rollNo);
}

public static void main(String[] args) {
Student obj1=new Student("Rambo",21);
obj1.methodForDisplay();
}
// Instance variables/fields:
// - name: String
private String name;
// - rollNo: int
private final int rollNo;

// Constructor: initializes name and rollNo
// Constructor Parameters:
// - s: String (for name)
// - r: int (for rollNo)
Student(String s, int r)
{
name = s; // Setting name to the value of s
rollNo = r; // Setting rollNo to the value of r
}

// Instance method for displaying student's name and roll number
void methodForDisplay()
{
System.out.println(name+"'s Roll No: "+rollNo);
}

public static void main(String[] args) {
// Creating a Student object:
// - name: "Rambo"
// - rollNo: 21
Student obj1=new Student("Rambo",21);
// Calling the instance method to display student information
obj1.methodForDisplay();
}
}

```

## Part 2 - Public VS Static

* 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 methods can only be accessed my objects, whereas static methods can be accessed without creating an object of the class
## 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.
Expand Down