Java Programming: Defining Custom Classes and Objects - Prof. Brian F. Hanks, Study notes of Javascript programming

An overview of java programming concepts related to defining custom classes and creating objects. It includes discussions on jframes, instance methods, constructors, and creating a custom date class. The document also includes a lab exercise for creating a date class and testing it.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-lji-1
koofers-user-lji-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 22
Quiz Results:
Mean: 16.3
STD: 8.1
Median: 18
Max 23.5
Distribution: <10: 2 10-14.5: 2 15-19.5: 4 20+: 4
Chapter 4 - Being Classy
So far, we have looked at pre-defined Java capabilities and classes. You've been exposed
to variables, types, input, output, expressions, assignment, conditionals, loops, and some
standard classes and their methods (such as String, Scanner, File).
In this chapter, you'll learn about defining your own classes and creating your own
objects. That is, you will learn to define your own types.
Before we start defining our own classes, let's look at some Java for manipulating
windows.
But first, let's look at a program that displays a couple of windows:
TwoWindows.java
javax.swing.* (Java eXtension - what does swing stand for?)
1) Define and initialize two Jframes:
a. Jframe is a titled, bordered graphical window
b. single parameter - String for window title
2) Call instance methods on the two frames to set their size
a. What is an instance method?
3) Call instance method to make the windows visible
a. Jframe is invisible (not displayed) by default
A very simple class
Let's look at another program: BoxFun.java
- Creates 2 windows
- Pauses so I can move them
- calls paint method on the ColoredRectangles
pf3

Partial preview of the text

Download Java Programming: Defining Custom Classes and Objects - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 22

Quiz Results: Mean: 16. STD: 8. Median: 18 Max 23. Distribution: <10: 2 10-14.5: 2 15-19.5: 4 20+: 4 Chapter 4 - Being Classy So far, we have looked at pre-defined Java capabilities and classes. You've been exposed to variables, types, input, output, expressions, assignment, conditionals, loops, and some standard classes and their methods (such as String, Scanner, File). In this chapter, you'll learn about defining your own classes and creating your own objects. That is, you will learn to define your own types. Before we start defining our own classes, let's look at some Java for manipulating windows. But first, let's look at a program that displays a couple of windows: TwoWindows.java javax.swing.* (Java eXtension - what does swing stand for?)

  1. Define and initialize two Jframes: a. Jframe is a titled, bordered graphical window b. single parameter - String for window title
  2. Call instance methods on the two frames to set their size a. What is an instance method?
  3. Call instance method to make the windows visible a. Jframe is invisible (not displayed) by default A very simple class Let's look at another program: BoxFun.java
  • Creates 2 windows
  • Pauses so I can move them
  • calls paint method on the ColoredRectangles

BoxFun uses the class ColoredRectangle

  • but, ColoredRectangle is not a standard Java class
  • ColoredRectangle is a class that has been defined in another file
  • Where is it? o In same directory as BoxFun.java o Java automatically compiled it for us when we compiled BoxFun.java Let's look at ColoredRectangle First are instance variable declarations
  • These are used to save information about each rectangle
  • note that they are declared private o private means that these variables can only be accessed by statements included in the same class. Constructor
  • special method that is invoked when you create a new object o We've seen constructors before - whenever we used the keyword new.
  • Must have same name as the class name
  • Does not have a return type - implicitly returns an object of the type
  • Constructors are used to initialize new objects This constructor creates a window, makes it visible, and then initializes the variables used for the size, position, and color of the rectangle Look at page 148 instance method paint() paint is an instance method, which is a block of code that is executed when you call it by using its name. You must have an instance of an object to call the method. In this example, we have two ColoredRectangles. We can call the paint method individually on each one. Note that the method is invoked separately for each instance of a ColoredRectangle. When we call paint() on r1 it doesn't have any affect on r2, and vice versa. Lab Exercise: Write a Date class to store dates. This class should have three instance variable: one for the day of the month (1 to 31), one for the month (1 to 12), and one for the year (>0). Your class should have two methods: A constructor that allows you to create a new date. Your constructor can use any date you want.