Unit 2 Objects
Documenting Learnings using Objects in Java
public class Tennis { // class creation
public void printPlayers(){ // simple method
System.out.println("Nadal");
System.out.println("Federer");
System.out.println("Djokovic");
}
public static void main(String[] args){ // main class that runs
Tennis myObject = new Tennis(); // CREATING AN OBJECT FROM TENNIS CLASS
myObject.printPlayers(); // Use dot notation to reference methods/attributes from the class that the object is initialized from
}
}
Tennis.main(null);
public class TennisTwo extends Tennis{ // extending Tennis class
public TennisTwo(){ // TennisTwo now has all of Tennis's methods
super();
}
public static void main(String[] args){ // main class that runs
TennisTwo myObjectTwo = new TennisTwo(); // CREATING AN OBJECT FROM TENNISTWO CLASS
myObjectTwo.printPlayers(); // Use dot notation to reference methods/attributes from the INHERITED class
}
}
TennisTwo.main(null);
HACK - Console Menu
Click Here to Go to Hack
Objects Used
- Makes Object from Scanner Class to obtain inputs / menu selection from User
- Use System Class, to call static methods System.out.print and System.out.println to output to console
- User Math Class, to call static method Math.random() to generate random number