Skip Top Navigation Bar

Create Objects and Call Instance Methods

Grab a Piece of Paper!

Calculator Class Methods

The following is a diagram of a Calculator class.

Class Name: Calculator Class
Constructor:
  • Calculator(double oper1, double oper2)
Data/Attributes:
  • double operand1;
  • double operand2;
Methods/Behaviors:
  • double sum()
  • double sum(double oper1, double oper2)
  • double product()
  • double product(double oper1, double oper2)
  • double posDifference(double oper1, double oper2)
  • double quotient(double oper1, double oper2)

Use the Java Playground

  • Predict the output from running the body of main for testing.
  • Run the code to determine if your prediction was correct.
  • Call each of the other methods in the table above and print their values to the screen.

Dice Class Code

The following is a diagram of a Dice class.

Class Name: Dice Class
Constructors:
  • Dice()
  • Dice(int numSides)
Data/Attributes:
  • int numSides;
Methods/Behaviors:
  • int getNumSides()
  • void setNumSides(int sides)
  • int roll()

Use the Java Playground

  • Predict the output from running the body of main for testing.
  • Run the code to determine if your prediction was correct.
  • Add the following statements:
    • add statements to call the accessor method and print the number of sides.
    • add a statement to change the number of sides to 10.
    • add statements to roll the dice and print the value.
    • add a statement to use the toString method.
  • Run the code to test the additions you have mad to main

LocalDate Instance Methods

Practice calling methods with the LocalDate class.