Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ariel/inheritance #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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 .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<classpathentry kind="src" path="3. Getters and Setters"/>
<classpathentry kind="src" path="4. Static"/>
<classpathentry kind="src" path="6. UML"/>
<classpathentry kind="src" path="5.Inheritance"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions 2. Inside Classes/examples/CharacterPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package examples;

public class CharacterPractice {

private String name;
private String species;

public CharacterPractice(String name, String species) {
this.name = name;
this.species = species;
}

public static void main(String[]args) {
CharacterPractice bilbo = new CharacterPractice("Bilbo", "Hobbit");
CharacterPractice legolas = new CharacterPractice("Legolas", "Elf");
}
}
34 changes: 34 additions & 0 deletions 2. Inside Classes/examples/DuckPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package examples;

public class DuckPractice {

// member variable: data
private String name;
private int lifeExpectancy;
private String favoriteFood;

public DuckPractice(String name, int lifeExpectancy, String favoriteFood) {
super();
this.name = name;
this.lifeExpectancy = lifeExpectancy;
this.favoriteFood = favoriteFood;
}

// methods: functionality
void waddle() {
String message = String.format("%s is waddling.", this.name);
System.out.println(message);
lifeExpectancy ++;
}

void quack() {
System.out.println("quack quack");
}

@Override
public String toString() {
String message = String.format("%s likes %s. Life expectancy is %d", name, favoriteFood, lifeExpectancy);
return message.toString();
}

}
17 changes: 17 additions & 0 deletions 2. Inside Classes/examples/PondPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package examples;

public class PondPractice {
public static void main(String[] args) {
String me = "june";

//<type> <name> = new <type>()
DuckPractice mobyDuck = new DuckPractice("Moby", 90/3, "celery");

mobyDuck.quack();
mobyDuck.waddle();
mobyDuck.waddle();

System.out.println(mobyDuck);

}
}
13 changes: 13 additions & 0 deletions 5.Inheritance/examples/ExtraRandom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package examples;

import java.util.Random;

public class ExtraRandom extends Random {

public String nextLetter() {
int letterStartAt = 97;
int randomInt = new Random().nextInt(26) + letterStartAt;

return "" + (char)randomInt;
}
}
11 changes: 11 additions & 0 deletions 5.Inheritance/examples/Random.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package examples;

public class Random {

public static void main(String []arg) {
ExtraRandom extraRandom = new ExtraRandom();

System.out.println(extraRandom.nextLetter());
}

}