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
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@
6. All the instance methods for the class **Student**

```java
//class name is Student
class Student{
//Two instance variables, String name and int rollNo
private String name;
private int rollNo;

//Constructor Student, parameters are String s and int r
Student(String s, int r)
{
//Instance variable values are set here for both name = s, and rollNo = r
name = s;
rollNo = r;
}

//Instance method here for displaying the Student obj1
void methodForDisplay()
{
System.out.println(name+"'s Roll No: "+rollNo);
}

public static void main(String[] args) {
//Student Object is created here
Student obj1=new Student("Rambo",21);
//Instance method methodForDisplay is called in order to display name and roll number
obj1.methodForDisplay();
}
}
Expand All @@ -37,6 +44,10 @@ 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.

// The main difference between a static and a public method are that static methods can be called without created objects while public classes must be called by creating objects.
// Not calling a method correctly would result in a compile error.
// It is important to use the correct format for calling the certain method in order to have the code function properly and give the desired output.

## 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
11 changes: 11 additions & 0 deletions source code/source code.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="jdk" jdkName="21" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
76 changes: 76 additions & 0 deletions src/src/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
public class Dog {

private String name;
private String breed;
private String size;
private double weight;
private String color;
private int age;
private dogOwner owner;

Dog (String n, String b, String s, double w, String c, int a, dogOwner o){
name = n;
breed = b;
size = s;
weight = w;
color = c;
age = a;
owner = o;
}

void displayDog(){
System.out.println("Name: " + name);
System.out.println("Breed: " + breed);
System.out.println("Size: " + size);
System.out.println("Weight: " + weight + " lbs");
System.out.println("Color: " + color);
System.out.println("Age: " + age + " years");
}

public void eat(Food food){
System.out.println(name + " is eating " + food.getWeight() + " grams of " + food.getName());
food.consume(50);
}

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

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

public String theName(){
return name;
}

public dogOwner getOwner() {
return owner;
}

public static void main(String[] args){
dogOwner owner1 = new dogOwner ("Lebron James", "123-456-7890");
dogOwner owner2 = new dogOwner ("Shaquille O'neil", "111-111-1111");
dogOwner owner3 = new dogOwner ("Stephen Curry", "098-765-4321");

Dog dog1 = new Dog ("Max","Bulldog", "Large", 45.5, "Light Gray", 5, owner1);
Dog dog2 = new Dog ("Charlie", "Beagle", "Large", 35.0, "Orange", 6, owner2);
Dog dog3 = new Dog ("Ace", "German Shepherd", "Large", 85.0, "White and Orange", 6, owner3);

Food kibble = new Food ("Dog Kibble", 200);



dog1.displayDog();
dog1.eat(kibble);
System.out.println("Owner: " + dog1.getOwner().getName() + ", Phone Number: " + dog1.getOwner().getContactNumber());
System.out.println();
dog2.displayDog();
dog2.eat(kibble);
System.out.println("Owner: " + dog2.getOwner().getName() + ", Phone Number: " + dog2.getOwner().getContactNumber());
System.out.println();
dog3.displayDog();
dog3.eat(kibble);
System.out.println("Owner: " + dog3.getOwner().getName() + ", Phone Number: " + dog3.getOwner().getContactNumber());
}
}
27 changes: 27 additions & 0 deletions src/src/Food.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class Food {
private String name;
private double weight;

public Food(String name, double weight) {
this.name = name;
this.weight = weight;
}

public String getName(){
return name;
}

public double getWeight() {
return weight;
}

public void consume(double amount) {
if (amount > 0 && amount <= weight) {
weight -= amount;
System.out.println(name + " consumed. Remaining weight: " + weight + " grams.");
} else {
System.out.println("Invalid amount to consume.");
}
}
}

18 changes: 18 additions & 0 deletions src/src/dogOwner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class dogOwner {

private String name;
private String contactNumber;

public dogOwner(String ownerName, String ownerNumber){
name = ownerName;
contactNumber = ownerNumber;
}

public String getName() {
return name;
}

public String getContactNumber() {
return contactNumber;
}
}
11 changes: 11 additions & 0 deletions src/untitled.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="jdk" jdkName="22 (2)" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>