Java Using Objects

Ace your homework & exams now with Quizwiz!

How do you create and display a frame?

...

How do you create and display a graphic applet?

...

How do you create and display a graphic as a app?

...

How do you draw a line?

...

How do you use the getHeight method in the java.awt.Rectangle class?

...

How do you use the getX method in the java.awt.Rectangle class?

...

How do you use the length, replace, toLowerCase, toUpperCase methods in the java.lang.String 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?

...

How to you use the getY method in the java.awt.Rectangle class?

...

What is the difference between single and double precision?

...

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.

What html code is required to include an applet in a web page?

<body>.... <applet code="MyApplet.class" width="300" height="400"> </applet> .... </body>

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.

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.

Expression

A combination of variables, literals, operators and/or methods is called in expression.

Methods

A method consists of a sequence of instructions that can access the internal data of an object.

Mutator Method

A mutator method changes the internal data of a method.

What is an identifier

A name of a variable, method or class.

Method Parameters

A parameter is an input to a method.

What is a Test Program

A test program verifies that methods behave as expected.

Accessor Method

An accessor method does not change the internal data of its implicit parameter.

What are Applets?

Applets are java programs that run within a browser.

Floating point numbers

As opposed to integers, floating point numbers can have fractional parts. Ex: 1.3 786.345

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.

You draw an ellipse by specifying a _______

Bounding box

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.

What is an important part of a creating a test program?

Determining the expected result in advance.

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 are the names of the two classes used to draw ellipses?

Ellipse2D.Float and Ellipse2D.Double

The paintComponent method is called _______

Every time the component needs to be painted or repainted.

Passing a newly constructed object to a method

Ex: System.out.println(new Rectangle()); What does this do?

How to fill an drawn shape

Ex: g2.fill(circle);

Can't assign an uninitialize variable to another variable

Ex: int height; width = height;

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

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.

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.

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.

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.

What are java integers?

Integers are whole numbers. Ex: 1, 333, 789

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.

What is the java.awt class?

It is the java abstract windowing class

Show JFrame Example

JFrame = new JFrame; frame.setSize(300, 400); frame.setTitle("My Title"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);

What color model does java use?

Java uses the RGB color model Ex: Color magenta = new Color(255, 0, 255); Constructs a color object with maximum red, not green and maximum blue.

Draw Line Example

Line2D.Double segment = new Point2D.Double(x1, y1);

Number Literals in Java

Number Type Comment 6 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

Objects

Objects are entities in your program that you manipulate by calling methods. You manipulate an object by calling one of its 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)

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);

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.

The Rectangle Object

Rectangle(x, y, width, height) x and y are the coordinates of the top left corner

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.

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.

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)

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.

Method implicit parameters

The implicit parameter of a method is the object on which the method is invoked. All other parameters are explicit parameters.

Object Reference

The memory location of an object.

Construction

The process of creating a new onject is called construction.

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.

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.

What is the type of an object

The type of an object is class.

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.

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 to contruct an object

Use the new operator, followed by a class name and parameters, to construct new objects.

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.

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

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 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

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.

Can you have multiple applets in an html file?

Yes. Simply add a separate applet tag for each applet.

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 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 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.

Common Error: Trying to invoke a constructor like a method.

box.Rectangle(20, 35, 20, 30);//error = cant reinitialize The remedy is to make a new object and overwrite the current one stored by box. box = new Rectangle(20, 35, 20, 30);

Drawing Text Example

g2.drawText("Message", 50, 100);

How to set the color for drawing on an object

g2.setColor(Color.RED);

What are the commonly used methods in the Rectangle class in java.awt.Rectangle?

getX, getY, getHeight, getWidth, setSize, translate

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;

What import statements are needed for drawing graphics?

import java.awt.Graphics;// to use graphics methods import 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

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 import statements are required to draw Frames?

import javax.swing.JFrame;

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.

Syntax for importing a Class from a Package

import packageName.ClassName; Ex: import java.awt.Rectangle

When you set a new color in the graphics context ...

it is used for subsequent drawing operations.

In what class is the setTitle method, used to set the title of a frame, stored?

java.awt.Frame

What are commonly used methods in the java.lang.String class?

length, replace, toLowerCase, toUpperCase

Syntax for Object Creation

new ClassName(parameters) Example: Rectangle box = new Rectangle(5, 10, 20, 30);

What is a commonly used method of the javax.swing.JComponent class?

paintComponent

How to use the Graphics2D class

put the following statement at the beginning of the paintComponent method: Graphics2D g2 = (Graphics2D) g;

What is the difference between the java.awt.Frame class and the javax.swing.JFrame class?

the JFrame class extends the Frame class

What class and import statement is used to draw rectangles?

the Rectangle class import java.awt.Rectangle;

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 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;

Variable Declaration Syntax

typeName variableName = value; or typeName variableName;

Syntax: Assignment

variableName = value; ex: double width = 10.0;


Related study sets

mgmt100- Chapter 8 Management & Leadership page302-329)

View Set

ap world final 2016 ashmans quiz

View Set

Chapter 18: Nursing Management of the Newborn 1-4

View Set

Entrepreneurship Unit 1: What Is Entrepreneurship?

View Set

intro Digital forensics Final study

View Set

Introduction to Coordinate Systems

View Set