Java Using Objects
About this set
Created by:
ready2learn98 on May 12, 2011
Subjects:
Log in to favorite or report as inappropriate.
Order by
117 terms
Terms | Definitions |
|---|---|
Relationship between Java classes and objects | In Java you maipulate data items such as objects. You implement classes that describe the behavior of these objects. |
What is a Java Type? | A Java type specifies a set or values and the operations that can be carried out with the values. In Java, every value has a type. The type tells you what operations you can carry out with the values. |
What are java integers? | Integers are whole numbers. Ex: 1, 333, 789 |
Floating point numbers | As opposed to integers, floating point numbers can have fractional parts. Ex: 1.3 786.345 |
Number Literals | When a value such as 13 or 1.3 occurs in a Java program, it is called a number literal. Do not use commas when you write number literals in Java. For example, 13,000 must be written as 13000. The write numbers in expeoential notation in Java, exponentail notation Example: 1.3E-4 |
Why not always just use floating point numbers? | Integers have several advantages: Take less memory space, are processed faster and don't cause rounding errors. |
Number Literals in Java | Number Type Comment6 int an integer has no fractional part -6 int ints can be negative 0 int zero is an int 0.5 double 1.0 double 1E6 double num in exp not. 100,000 not valid |
Primitive | In Java, the number types (int double and the less commonly used types) are primitive types. They are not objects. They have not methods. |
Expression | A combination of variables, literals, operators and/or methods is called in expression. |
Identifiers for variables - naming RULES | Identifiers for variables, method, and classes are composed of letters, digits, the underscore character and th $ character but cannot start with a number. Can't use ? or %. Can't use reserved words. |
What is an identifier | A name of a variable, method or class. |
Identifier Naming CONVENTIONS | Variable and method names should start with a lower case letter but can follow with an upper case character, for example when usijng camel case (myVariable). Class names should start with an uppercase letter. You should not use the $ in names. It is intended for names that are auto generated by tools. |
Variable Declaration Syntax | typeName variableName = value; ortypeName variableName; |
Can't assign an uninitialize variable to another variable | Ex: int height;width = height; |
Syntax: Assignment | variableName = value;ex: double width = 10.0; |
Objects | Objects are entities in your program that you manipulate by calling methods. You manipulate an object by calling one of its methods. |
Methods | A method consists of a sequence of instructions that can access the internal data of an object. |
What is the type of an object | The type of an object is class. |
What is the purpose of the public interface of a class? | The public interface of a class specifies what you can do with it s objects. The hidden implementation describes how these actions are carried out. |
Parts of a class that are private | A class also decleares a private implemenation, describing the data inside its objects and the instructions for its methods. Those details are hidden from the programmers who use objects and call methods. |
What is method overloading | Occasionally, a class declares two methods with the same name and different parameter types. For example, the PrintStream class declares a second method, also called println,as public void println(int output) |
Method Parameters | A parameter is an input to a method. |
Method implicit parameters | The implicit parameter of a method is the object on which the method is invoked. All other parameters are explicit parameters. |
Method Return Values | The return value of a method is a result that the method has computed for use by the code that called it. |
Syntax for Object Creation | new ClassName(parameters)Example: Rectangle box = new Rectangle(5, 10, 20, 30); |
Passing a newly constructed object to a method | Ex: System.out.println(new Rectangle()); What does this do? |
Construction | The process of creating a new onject is called construction. |
How to contruct an object | Use the new operator, followed by a class name and parameters, to construct new objects. |
The Rectangle Object | Rectangle(x, y, width, height)x and y are the coordinates of the top left corner |
Common Error: Trying to invoke a constructor like a method. | box.Rectangle(20, 35, 20, 30);//error = cant reinitializeThe remedy is to make a new object and overwrite the current one stored by box. box = new Rectangle(20, 35, 20, 30); |
Accessor Method | An accessor method does not change the internal data of its implicit parameter. |
Mutator Method | A mutator method changes the internal data of a method. |
The API | The API (Application Programming Interface) documentation lists the classes and methods of the Java library |
Structure of the API Documenation | The API documenation starts out with a section that describes the purpose of the clalss. Then summary tables for the constructors and methods. |
In the API, what does the detailed description of a method show? | The action the method carries out. The parameters the method receives. The value it returns (if any) |
Syntax for importing a Class from a Package | import packageName.ClassName;Ex: import java.awt.Rectangle |
Why don't you have to import that System and String Classes | Because the System an String classes are in the java.lang package that is automically importated because it is so frequently used. |
What is a Test Program | A test program verifies that methods behave as expected. |
Fuctions of a Test Program | Provides a tester class. Suppies a main method. Inside the main method, constructs one or more objects. Applies methods to the object(s). Displays the result of method calls. Displays the values you expect to get. |
What is an important part of a creating a test program? | Determining the expected result in advance. |
Object Reference | The memory location of an object. |
Steps to show a frame | 1. Construct an object of the JFrame class. 2. Set the size of the frame. 3. Set the frame title (optional) 4. Set the default close operation. 4. Make the fram visible. |
Show JFrame Example | JFrame = new JFrame;frame.setSize(300, 400); frame.setTitle("My Title"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); |
What is the of a graphics component object | Whenever you want to show anything inside a frame, you have to construct a component object and add it to a frame. |
What import statements are required to draw Frames? | import javax.swing.JFrame; |
What is required to display a drawing in a frame? | In order to display a drawing in a frame, declare a class that extends the JComponent class. |
To show a frame you have to: | Construct a JFrame object, set its size, and make it visible. |
When you want to draw on a Frame you... | Create a class that extends the JComponent class and put drawing instructions inside a paintComponent method. |
The paintComponent method is called _______ | Every time the component needs to be painted or repainted. |
When is the paintComponent method called? | When the component is shown the first time or when the window is resized or when it is shown again after it was hidden. |
What does the graphic object store? | The graphic object stores the graphic state - the current color, font and so on, that are used for drawing operations. |
What is the Graphics class? | The Graphics class contains methods for drawing graphics |
What is the Graphics2D class? | The Graphics2D class extends the Graphics class and provides more advanced graphics methods than the Graphics class. |
What do you have to do to use the Graphics2D class?` | You have to 'Recover' the Graphics2D class by using a cast:Ex: Graphics2D g2 = (Graphics2D) g; |
How to use the Graphics2D class | put the following statement at the beginning of the paintComponent method: Graphics2D g2 = (Graphics2D) g; |
What import statements are needed for drawing graphics? | import java.awt.Graphics;// to use graphics methodsimport java.awt Graphics2D;// to use newer graphics methods import javax.swing.JComponent; //to draw on JFrames import javax.swing.JFrame; //to create JFrames import java.awt.Rectangle; // if drawing rectangles |
Steps required to display a drawing | import needed classes, construct a new frame, construct an object of your component class, add the component to the frame, make the frame visible. |
What are Applets? | Applets are java programs that run within a browser. |
What are the two advantages of applets? | They don't need separate component and viewer classes; you only implement a single class. More importantly, they run inside a browser. |
What are the differences in the outline for displaying an object in an applet? | You extent JApplet, not JComponent.You place the drawing inside a paint method, not inside a paintComponent method. |
What html code is required to include an applet in a web page? | <body>....<applet code="MyApplet.class" width="300" height="400"> </applet> .... </body> |
Can you have multiple applets in an html file? | Yes. Simply add a separate applet tag for each applet. |
What is the difference between single and double precision? | ... |
What are the names of the two classes used to draw ellipses? | Ellipse2D.Float and Ellipse2D.Double |
You draw an ellipse by specifying a _______ | Bounding box |
Statement to create an ellipse | Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width,height); // x and y coordinates are the top left corner. |
What import statement is needed to use the Ellipse2D class? | When using the Ellipse2D.Double, be sure to only import the Ellipse2D outer class.Example : import java.awt.geom.Ellipse2D |
How do you draw a circle? | Use the Ellipse2D.Doube method but set the witdth and height to the same value. (note that x and y value will not be center of circle but coordinates of left top of bounding box. |
How do you draw a line? | ... |
How do you draw text? | You use the drawString method of the Graphics2D class. You supply the text and the x and y coordinates of the basepoint of the first character in the string. |
Drawing Text Example | g2.drawText("Message", 50, 100); |
Draw Line Example | Line2D.Double segment = new Point2D.Double(x1, y1); |
Alternate way to Draw a Line | Point2D.Double from = new Point2D.Double(x1, y1);Point2D.Double to = new Point2D.Double(x2, y2); Line2D.Double segment = new Line2D.Double(from, to); |
What color model does java use? | Java uses the RGB color modelEx: Color magenta = new Color(255, 0, 255); Constructs a color object with maximum red, not green and maximum blue. |
Predefined Colors | For your convenience, a variety of colors has been defined in the Colors class. Ex: Color.Pink has RGB value of 255, 175, 175 |
When you set a new color in the graphics context ... | it is used for subsequent drawing operations. |
How to set the color for drawing on an object | g2.setColor(Color.RED); |
How to fill an drawn shape | Ex: g2.fill(circle); |
What is the java.awt class? | It is the java abstract windowing class |
What import statement is needed to use the Color object? | import java.awt.Color; //This imports the java.awt.Color class |
What import statement is required to use the getHeight, getWidth, setSize and SetVisible methods? | import java.awt.Component; //imports the java.awt.Component class |
What import statement is required to create frames? | import java.awt.Frame; |
In what class is the setTitle method, used to set the title of a frame, stored? | java.awt.Frame |
What package are the Ellipse2D.Double, Line2D.Double, and Point2D Classes in? | the java.awt.geom package. So, to use these objects, you must use: import java.awt.geom; |
What import statement is needed to be able to use the setColor method for drawing graphics? | import java.awt.Graphics;//imports the java.awt.Graphics class |
What methods can you use to draw shapes, strings and fill shapes? | the draw, drawString and fill methods of the java.awt.Graphics2D class. Must use the import statement:import java.awt.Graphics2D. |
What class and import statement is used to draw rectangles? | the Rectangle class import java.awt.Rectangle; |
What are the commonly used methods in the Rectangle class in java.awt.Rectangle? | getX, getY, getHeight, getWidth, setSize, translate |
How do you use the getX method in the java.awt.Rectangle class? | ... |
How to you use the getY method in the java.awt.Rectangle class? | ... |
How do you use the getHeight method in the java.awt.Rectangle class? | ... |
How do you use the setSize method in the java.awt.Rectangle class? | ... |
How do you use the translate method in the java.awt.Rectangle class? | ... |
What are commonly used methods in the java.lang.String class? | length, replace, toLowerCase, toUpperCase |
How do you use the length, replace, toLowerCase, toUpperCase methods in the java.lang.String class? | ... |
What is a commonly used method of the javax.swing.JComponent class? | paintComponent |
How do you create and display a frame? | ... |
How do you create and display a graphic as a app? | ... |
How do you create and display a graphic applet? | ... |
What is the difference between the java.awt.Frame class and the javax.swing.JFrame class? | the JFrame class extends the Frame class |
What is javax.swing.JComponent? | ... |
How do you use the method paintComponent in the class javax.swing.JComponent? | ... |
How do you use the setDefaultCloseOperation in the javax.swing.JComponent class? | ... |
R2.1 Explain the difference between an object and an object reference. | Objects are entities in your program that you manipulate by calling methods. An object reference is the memory location of an object. When a variable contains the memory location of an object, we say that it refers to an object. |
R2.2 Explain the diffence between an object and an object variable. | Objects are entities in your program that you manipulate by calling methods. Object variables store references to objects. |
R2.3 Explain the difference between an object and a class. | A class is a blueprint or a description of how to create an object. An object is an instantiated class that resides in memory. |
R2.4 Give the Java code for contruction an object ofclass Rectangle, and for declaring an object variable of class Rectangle. | publi class Rectangle{.....} Rectangle myRectangle = new Rectangle(x, y, width,height); |
What is the proper naming convention for objects?myObject? MyObject? myobject? | I think the answer to this is myObject or myobject would be ok. Object variables should start with lowercase letters per page 37. This is a convention, not a rule. myObject preferred. |
R2.5 Explain the difference between the use of the = symbol in Java and mathematics. | In Java, the = symbol is for assignment. |
R2.6 Give Java code for objects with the following descriptions: a. A rectangle with the center(100, 100) and all side lengths equal to 50. | Rectangle(x,y, width, height);Rectangle myRectangle = new Rectangle(75,75, 50, 50) |
R2.6 B Give Java code for an object with the description: A string with the contents "Hello, Dave" | String myString="Hello, Dave"; |
R2.8 | Rectangle square = new Rectangle(10, 20, 40, 40);Rectangle square = new Rectangle(20, 20, 40, 40); |
R2.9 Write Java statements that initialize two variables square1 and square2 to refer to the same square with center (20, 20) and the side length 40. | Rectangle square1 = new Rectangle(0,0, 40, 40);square2 = square1; |
R2.10 Write Java statements that initialize a string message with "Hello" and then change it to "HELLO". Use the toUpperCase method. | String myString = "Hello";myString = myString.ToUpperCase(); |
R2.11 Write Java statements that initialize a string message with "Hello" and then change it to "hello". Use the replace method. | myString = myString.replace("H", "h"); |
How do you use the replace method of the String class. | Ex1: myString = myString.replace("H", "h");Ex2: System.out.printn(myString.replace("issip", "our")); |
R2.12 Find the error in the following statement:double width = Rectangle(5,10, 15, 20).getWidth(); | ... |
First Time Here?
Welcome to Quizlet, a fun, free place to study. Try these flashcards, find others to study, or make your own.