Vocab/OOP
Homework Assignments
- main method, tester methods
- Subclass constructor, super Keyword
- Overloading a method, same name different parameters
- Overriding a method, same signature of a method
- Abstract Class, Abstract Method
- Standard methods: toString(), equals(), hashCode()
- Late binding of object
- Polymorphism
- Accessor & Mutator methods
- Static methods, Class methods
- Wrapper Classes
- Comparing Objects
- HOMEWORK
public class toString
{
public static void main(String[] args)
{}}
Integer x = new Integer(10);
System.out.println(x.toString());
System.out.println(x.toString(90));
public class hashCode
{
public static void main(String[] args)
{}}
String str_New = "yay!";
System.out.println(str_New.hashCode());
//basic example
public class Person {
private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
import java.util.Scanner;
public class Main {
//static variables
private static String[] words = {"words", "to", "put", "in"};
private static String word = words[(int) (Math.random() * words.length)];
private static String asterisk = new String(new char[word.length()]).replace("\0","*");
private static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//create a while loop when count is less than 7 and
//when asterisk contains *
while (count < 7 && asterisk.contains("*")) {
//print guess any letter in the word
//print asterisk
//take in guess and scan it in (string guess and take in scan)
//hang(guess)
System.out.println("Guess any letter in the word");
System.out.println(asterisk);
String guess = sc.next();
hang(guess);
}
sc.close();
}}
public class friends
{
// instance variables
private String name;
private String email;
private String phoneNumber;
// Static counter variable
public static int personCounter = 0;
// static method to print out counter
public static void printPersonCounter() {
System.out.println("Person counter: " + personCounter);
}
// constructor
public friends(String initName, String initEmail, String initPhone)
{
name = initName;
email = initEmail;
phoneNumber = initPhone;
personCounter++;
}
// toString() method
public String toString()
{
return name + ": " + email + " " + phoneNumber;
}
// main method for testing
public static void main(String[] args)
{
// call the constructor to create a new person
friends p1 = new friends("Sana", "sana@gmail.com", "123-456-7890");
friends p2 = new friends("Jean", "jean@gmail.com", "404 899-9955");
friends.printPersonCounter();
}
}
//class methods
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
}
//methods declared within a class
- Why wrap int, double, etc?
- way to use primitive data types as objects
- primitive type and corresponding wrapper class
- Wrapper classes are used to convert any data type into an object. The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language.
public class wrappers
{
public static void main(String[] args)
{}}
// int x = 10;
// System.out.println(x.toString()); ------ doesn't work because x is a primitive data type, not a modifiable object
Integer x = new Integer(10);
//to get around this, you define an integer wrapper class
System.out.println(x.toString());
//x is now an object, not a primitive data type
Integer x = new Integer(15);
class Food {
//attributes
String cuisine;
String name;
int mealsConsumed;
//constructor
Food(String cuisine, String name, int mealsConsumed)
{
this.cuisine = cuisine;
this.name = name;
this.mealsConsumed = mealsConsumed;
}
}
//objects
Food Dal1 = new Food("Indian", "Mango Dal", 10);
Food Dal2 = new Food("Indian", "Moong Dal", 4);
System.out.println("Dal1 = Dal2 is " + Dal1.equals(Dal2));
//UNIT 2 HW
//goblin hacks
public class Goblin {
private String name;
private int HP;
private int DMG;
private double hitChance;
public String getName() {
return name;
}
public int getHP() {
return HP;
}
public int getDMG() {
return DMG;
}
public double getHitChance() {
return hitChance;
}
public boolean isAlive() {
if (this.HP > 0) {
return true;
} else {
return false;
}
}
public void setName(String newName) {
this.name = newName;
}
public void setHP(int newHP) {
this.HP = newHP;
}
public void takeDMG(int takenDamage) {
this.HP -= takenDamage;
}
public void setDMG(int newDMG) {
this.DMG = newDMG;
}
public void setHitChance(double newHitChance) {
this.hitChance = newHitChance;
}
}
import java.lang.Math;
public class Duel {
public static void attack(Goblin attackerGoblin, Goblin attackeeGoblin) {
System.out.println(attackerGoblin.getName() + " attacks " + attackeeGoblin.getName() + "!");
if (0.8 > attackerGoblin.getHitChance()) {
attackeeGoblin.takeDMG(attackerGoblin.getDMG());
System.out.println(attackerGoblin.getName() + " hits!");
System.out.println(attackeeGoblin.getName() + " takes " + attackerGoblin.getDMG() + " damage");
} else {
System.out.println(attackerGoblin.getName() + " misses...");
}
System.out.println(attackeeGoblin.getName() + " HP: " + attackeeGoblin.getHP());
System.out.println();
}
public static void fight(Goblin goblin1, Goblin goblin2) {
while (goblin1.isAlive() && goblin2.isAlive()) {
attack(goblin1, goblin2);
if (!goblin1.isAlive()) {
System.out.println(goblin1.getName() + " has perished");
break;
}
attack(goblin2, goblin1);
if (!goblin2.isAlive()) {
System.out.println(goblin2.getName() + " has perished");
break;
}
}
}
public static void main(String[] args) {
Goblin goblin1 = new Goblin();
goblin1.setName("jeffrey");
goblin1.setHP(12);
goblin1.setDMG(2);
goblin1.setHitChance(0.50);
Goblin goblin2 = new Goblin();
goblin2.setName("Gunther the great");
goblin2.setHP(4);
goblin2.setDMG(1);
goblin2.setHitChance(1);
fight(goblin1, goblin2);
}
}
Duel.main(null);