using objects
DESCRIPTION
Using Objects. Java Primitive Data Types. primitive. integral. boolean. floating point. byte char short int long float double. Begin with lowercase letters. Java Reference Types. Reference types:. 1. String 2. Class name. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/1.jpg)
Using Objects
![Page 2: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/2.jpg)
Java Primitive Data Types
primitive
integral floating point
byte char short int long float double
boolean
Begin with lowercase letters
![Page 3: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/3.jpg)
Java Reference Types
Reference types:
1. String
2. Class name
![Page 4: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/4.jpg)
• int n = greeting.length();
• String bigRiver = river.toUpperCase();
String Methods
![Page 5: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/5.jpg)
What is: Java Syntax??
![Page 6: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/6.jpg)
Variables
int total;
int count, temp, result;
Multiple variables can be created in one declaration
data type
variable name
![Page 7: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/7.jpg)
What Does a Variable Declaration Do?
int ageOfDog;
4 bytes for ageOfDog
![Page 8: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/8.jpg)
initial value in the declaration
Variables
int sum = 0;int base = 32, max = 149;
![Page 9: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/9.jpg)
Assignment
ageOfDog = 10;
![Page 10: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/10.jpg)
Variable Declarations and Assignment of Values
VARIABLE DECLARATIONS
ASSIGNMENT STATEMENTS
![Page 11: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/11.jpg)
•Rules for identifiers in Java?
Identifiers
![Page 12: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/12.jpg)
package movetester;import java.awt.Rectangle; public class MoveTester{ public static void main(String[] args) { Rectangle box = new Rectangle(5, 10, 20, 30); // Move the rectangle where x becomes 20 and y becomes 35 box.translate(15, 25); // Print information about the moved rectangle System.out.println(box.getX()); System.out.print("y: "); System.out.println(box.getY()); System.out.println("Expected: 35"); }}
![Page 13: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/13.jpg)
Packages
•package areatester;
![Page 14: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/14.jpg)
We work with Objects
A fundamental entity in Java.
Objects are “things”
![Page 15: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/15.jpg)
java.lang.Object: the root of java classes
![Page 16: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/16.jpg)
Class Object
Class encapsulates objects
◦work together (behavior)
Objects have properties:
![Page 17: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/17.jpg)
HomeWork P2.1 - Programming Exercise
![Page 18: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/18.jpg)
Graphical User Interfaces
GUI Objects
![Page 19: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/19.jpg)
frame
Content pane used to display objects in a frame
Graphical User Interfaces (GUI): Demonstrates the object concep0t!
![Page 20: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/20.jpg)
01: import javax.swing.JFrame;02: 03: public class EmptyFrameViewer04: {05: public static void main(String[] args)06: {07: JFrame frame = new JFrame();08: 09: frame.setSize(300, 400);10: frame.setTitle("An Empty Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);12: frame.setVisible(true);14: }15: }
Example: Frame Viewer
![Page 21: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/21.jpg)
frame = new JFrame (name); (creates a class instance)
Jframe frame1. Point to object
![Page 22: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/22.jpg)
See Sun site forgreater detail
Sun.java
![Page 23: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/23.jpg)
1. Using the example in your text book on page 64; Section 2.13:
a. Create your own FaceViewer Class with a main method that will show a JComponent.
b. Adjust your Frame to be squarec. The frame title must contain your name.
2. Using the FaceComponent example as a guide (page 64; Section 2.13):
a. Create an Animal Viewer and draw an animal of your choice.
b. Draw a string that describes the animalc. Set colors as desired
Lab 2:Graphical Fun
![Page 24: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/24.jpg)
Lab Completion
Output◦Two classes - tested◦Upload to the appropriate dropbox
![Page 25: Using Objects](https://reader035.vdocuments.us/reader035/viewer/2022062805/56814cf8550346895dba0640/html5/thumbnails/25.jpg)
Object Lessons?