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
1 change: 1 addition & 0 deletions .idea/.name

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

2 changes: 1 addition & 1 deletion .idea/Java-Assignment-010.iml

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

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.

20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
6. All the instance methods for the class **Student**

```java
class Student{
private String name;
private int rollNo;
class Student{ // Class name is Student
private String name; // String type instance variable
private int rollNo; // Int data type instance variable

Student(String s, int r)
Student(String s, int r) // Constructor, where the instance variables get set
{
name = s;
rollNo = r;
name = s; // Constructor instance variable String for student name
rollNo = r; // Constructor instance variable int for student roll call ID
}

void methodForDisplay()
void methodForDisplay() // The only instance method, shows student name and number
{
System.out.println(name+"'s Roll No: "+rollNo);
}

public static void main(String[] args) {
Student obj1=new Student("Rambo",21);
Student obj1=new Student("Rambo",21); // Here's the student object
obj1.methodForDisplay();
}
}
Expand All @@ -36,7 +36,9 @@ 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.


* Answer - A static method can be accessed without creating an object of the class. I'm unsure, but I think a public method can ONLY be accessed by objects.
*
## 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
94 changes: 94 additions & 0 deletions src/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import java.util.Scanner;

public class Dog {
private String name;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Collecting dog names
System.out.println("Enter name for Dog 1:");
String nameOfDog1 = scanner.nextLine();
System.out.println("Enter name for Dog 2:");
String nameOfDog2 = scanner.nextLine();
System.out.println("Enter name for Dog 3:");
String nameOfDog3 = scanner.nextLine();

// Dog objects with details from the image
String breed1 = "Bulldog";
String size1 = "Large";
String color1 = "Light grey";
int age1 = 5;

String breed2 = "Beagle";
String size2 = "Large";
String color2 = "Orange";
int age2 = 6;

String breed3 = "German Shepherd";
String size3 = "Large";
String color3 = "White & Orange";
int age3 = 6;

Dog dog1 = new Dog(nameOfDog1, breed1, size1, color1, age1);
Dog dog2 = new Dog(nameOfDog2, breed2, size2, color2, age2);
Dog dog3 = new Dog(nameOfDog3, breed3, size3, color3, age3);

// The dogs have to eat, right?
Food food = new Food(40);

// These strings seem like the easy way to work with the methods required.
dog1.eat(food);
dog1.run();
dog1.sleep();

dog2.eat(food);
dog2.run();
dog2.sleep();

dog3.eat(food);
dog3.run();
dog3.sleep();
}

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

public void eat(Food food) {
food.setWeight(food.getWeight() - 1);
String message = this.getName() + " is eating.";
System.out.println(message);
}

public void run() {
System.out.println(this.getName() + " is running!");
}

public void sleep() {
System.out.println(this.getName() + " is sleeping soundly.");
}

public String getName() {
return this.name;
}

public static class Food {
private int weight;

public Food(int weight) {
this.weight = weight;
}

// Setter method to modify weight
public void setWeight(int newWeight) {
this.weight = newWeight;
}

// Getter method for biscuits remaining
public int getWeight() {
return this.weight;

}
}
}