APCS- Chp 2 notes

¡Supera tus tareas y exámenes ahora con Quizwiz!

java.awt

"abstract windowing tool kit" - contains many classes for drawing windows and graphical shapes

As in mathematics, the _ operator binds more strongly than the + operator

**: x + y *** 2 // means the sum of x and y * 2 (x + y) * 2 // multiplies the sum of x and y with 2

Rules of a identifier:

-Can be made up of letters, digits, and the underscore (_) character -Cannot start with a digit -Cannot use other symbols such as ? or % -Spaces are not permitted inside identifiers -You cannot use reserved words -They are case sensitive

Display the frame into which you add a component object:

-Construct a frame. -Construct an object of your component class: RectangleComponent component = new RectangleComponent(); -Add the component to the frame: frame.add(component); -Make the frame visible

To show a frame:

-Construct an object of the JFrame class: JFrame frame = new JFrame(); -Set the size of the frame: frame.setSize(300, 400); -If you'd like, set the title of the frame: frame.setTitle("An Empty Frame"); -Set the "default close operation": frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); -Make the frame visible: frame.setVisible(true);

process to implement a test prog:

-Provide a tester class. -Supply a main method. -Inside the main method, construct one or more objects. -Apply methods to the objects. -Display the results of the method calls. -Display the values that you expect to get.

Variables:

-Store vals -Can be used in place of the objects they store -the storage location in a comp. mem. sys., that has a: - type -name and -contents

Conventions:

-variable names start with a lowercase letter -class names start with an uppercase letter

What does the following statement print? : System.out.println(new Rectangle().getWidth());

0

What is the result of the call greeting.replace("World", "Dave").length()?

12

Specify red, green, blue between 0 and 255:

Color magenta = new Color(255, 0, 255);

___ use commas when you write numbers

DON'T

Must construct and draw the ellipse

Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width, height); g2.draw(ellipse);

package

collection of classes in a related purposes

The process of creating a new object is called ____

construction

The four values 5, 10, 20, and 30 are called the _____

construction parameters: new Rectangle(); // constructs a rectangle with its top left corner at the origin (0, 0), width 0, and height 0

Length method

counts the number of characters in a string

toUpperCase method

creates another String object that contains the characters of the original string, with lowercase letters converted to uppercase

Ellipse2D.Double

describes an ellipse, is a inner class of double

Object reference

describes the location of an object in memory

private implementation

describing the data inside its object and the instructions for its methods.

Accessor method:

does not change the state of its implicit parameter : double width = box.getWidth();

Which number type would you use for storing the area of a circle?

double

Call method ___ of the Graphics2D class to draw shapes, such as rectangles, ellipses, line segments, polygons, and arcs:

draw : Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box);

object

entity that you can manipulate in your programs (by calling methods)

Error unintalized var: int luckyNum; System.out.println(luckyNum);

error because you never initialized it Solution: initialize it b4 you use it or assign a val to it when you create it

Floating point numbers:

float, double , 1.3, 0.00013

to draw text:

g2.drawString("Message", 50, 100); // 50 is the base point, 100 in the base line

Color is used when drawing and filling shapes

g2.fill(rectangle); // filled with current color

Set color in graphics context

g2.setColor(magenta);

How do you change the value of the greeting variable to "Hello, Nina!"?

greeting = "Hello, Nina!";

Graphics2D class

has methods to draw shape objects

Graphics class

lets you manipulate the graphics state (such as current color)

Identifier:

name of a variable, method, or class

The __ operator makes a Rectangle object

new, It uses the parameters (in this case, 5, 10, 20, and 30) to initialize the data of the object then It returns the object.

java.swing

nickname for graphical user interface library in java. the "x" means that swing started out as a extension but was added to the standard library

Primitive type variables ≠:

object variables

in java when a var who's type is a class doesn't hold an ____. it merely holds the ____ of the ______.

object, location, object

A method name is ____ if a class has more than one method with the same name (but different parameter types)

overloaded: public void println(String output) public void println(int output)

Place drawing instructions inside the ______ method. That method is called whenever the component needs to be repainted.

paintComponent: public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { Drawing instructions go here } }

You can also use the return value as a ____ of another method:

parameter, System.out.println(*greeting.length()*);

When a floating-point number is multiplied or divided by 10, only the ____ of the decimal point changes; it "floats". This representation is related to the ______.

position,"scientific" notation : 1.3 × 10-4. 1.3E-4 // 1.3 × 10-4 written in Java

type

tells you what you can do with a val

Look at the API documentation of the String class. Which method would you use to obtain the string "hello, world!" from the string "Hello, World!"?

toLowerCase

Every value has a ___, these must match when you declare a variable

type: typeName var = val;

Method definition specifies:

types of explicit parameters, the acessor type, and return value

Exponetial notation

use E(n=power) : 1.3E-4.o;

double

use to process fraction, stands for double precision floating - point number.

in order to remember a object hold it in a ___

var

If method returns no value, the return type is declared as ___.

void

Camel case

when you name variable with a capital letter in the middle of the name, w/ lower case beginning the name

Parameter (explicit parameter):

Input to a method. Not all methods have explicit parameters. the stuff in the parentheses: System.out.println(*greeting*)

Method

Sequence of instructions that accesses the data of an object, manipulate objects by using these

Public Interface:

Specifies what you can do with the objects of a class

Mutator method:

changes the state of its implicit parameter: box.translate(15, 25);

Return value:

A result that the method has computed for use by the code that called it

How do you construct a square with center (100, 100) and side length 20?

Answer: new Rectangle(90, 90, 20, 20);

In the API documentation of the String class, look at the description of the trim method. What is the result of applying trim to the string " Hello, Space ! "? (Note the spaces in the string.)

Answer: "Hello, Space !" - only the leading and trailing spaces are trimmed.

What is the result of the call river.replace("p", "s")?

Answer: "Missississi".

Why is the expression 13.println() an error?

Answer: An int is not an object, and you cannot call a method on it.

How is the toUpperCase method defined in the String class?

Answer: As public String toUpperCase(), with no explicit parameter and return type String.

Why doesn't the MoveTester program print the width and height of the rectangle?

Answer: Because the translate method doesn't modify the shape of the rectangle.

After calling greeting2.toUpperCase(), what are the contents of greeting and greeting2?

Answer: Both variables still refer to the same string, and the string has not been modified. Recall that the toUpperCase method constructs a new string that contains uppercase characters, leaving the original string unchanged.

How can a program display two frames at once?

Answer: Construct two JFrame objects, set each of their sizes, and call setVisible(true) on each of them.

How do you draw a yellow square on a red background?

Answer: First fill a big red square, then fill a small yellow square inside: g2.setColor(Color.RED); g2.fill(new Rectangle(0, 0, 200, 200)); g2.setColor(Color.YELLOW); g2.fill(new Rectangle(50, 50, 100, 100));

Is it legal to call river.println()? Why or why not?

Answer: It is not legal. The variable river has type String. The println method is not a method of the String class.

How do you display a square frame with a title bar that reads "Hello, World!"?

Answer: Modify the EmptyFrameViewer program as follows: frame.setSize(300, 300); frame.setTitle("Hello, World!");

Is 12 = 12 a valid expression in the Java language?

Answer: No, the left-hand side of the = operator must be a variable.

What is the effect of the assignment greeting2 = greeting?

Answer: Now greeting and greeting2 both refer to the same String object.

Which of the following are legal identifiers? Greeting1 g void 101dalmatians Hello, World <greeting>

Answer: Only the first two are legal identifiers.

How can you print out the uppercase version of "Hello, World!"?

Answer: System.out.println(greeting.toUpperCase());

What are the implicit parameters, explicit parameters, and return values in the method call river.length()?

Answer: The implicit parameter is river. There is no explicit parameter. The return value is 11.

Which call to translate is needed to move the box rectangle so that its top-left corner is the origin (0, 0)?

Answer: box.translate(-5, -10), provided the method is called immediately after storing the new rectangle into box.

Give instructions to draw a circle with center (100, 100) and radius 25.

Answer: g2.draw(new Ellipse2D.Double(75, 75, 50, 50));

Give instructions to draw a string consisting of the letter "V".

Answer: g2.drawString("V", 0, 30);

What is the type of the values 1 and "1"?

Answer: int and String.

How can you compute the length of the string "Mississippi"?

Answer: river.length() or "Mississippi".length()

Suppose we had called box.translate(25, 15) instead of box.translate(15, 25). What are the expected outputs?

Answer: x: 30, y: 25

API:

Application Programming Interface, Lists classes and methods in the Java library

In order to display a drawing in a frame, define a class that extends the ____ class.

JComponent

To draw a line:

Line2D.Double segment = new Line2D.Double(x1, y1, x2, y2); g2.draw(segment); or Point2D.Double from = new Point2D.Double(x1, y1); Point2D.Double to = new Point2D.Double(x2, y2); Line2D.Double segment = new Line2D.Double(from, to); g2.draw(segment);

Give instructions to draw a letter "V" by drawing two line segments.

Line2D.Double segment1 = new Line2D.Double(0, 0, 10, 30); g2.draw(segment1); Line2D.Double segment2 = new Line2D.Double(10, 30, 20, 0); g2.draw(segment2);

Assignment operator is ___ used to symbolize equality

NOT, it is used to change the val or a variable

A Rectangle object is ___ a rectangular shape - it is an:

NOT, object that contains a set of numbers that describe the rectangle

Numbers are ___objects; numbers types are ____

NOT, primitive types

Implicit parameter:

The object on which a method is invoked: *System.out*.println(greeting)

Applets

are programs that run inside a web browser

Replace method

carries out a search and replace operation: River.replace("issipp", "our") // constructs a new string ("Missouri")

Use a ___ to recover the Graphics2D object from the Graphics parameter:

cast: public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; . . . } }

import statement for ellipses:

import java.awt.geom.Ellipse2D;

draw a basic rectangle

public class RectangleComponent extends JComponent 10: { 11: public void paintComponent(Graphics g) 12: { 13: // Recover Graphics2D 14: Graphics2D g2 = (Graphics2D) g; 15: 16: // Construct a rectangle and draw it 17: Rectangle box = new Rectangle(5, 10, 20, 30); 18: g2.draw(box);

The new operator returns a ____ to a new object

reference

Multiple object variables can refer to the ___ object:

same: Rectangle box = new Rectangle(5, 10, 20, 30); Rectangle box2 = box; box2.translate(15, 25);

Integers

short, int, long, 13

A graphical application

shows information inside a frame window.

Class

specifies the methods you ca use on its objects, set of objects with the same behavior


Conjuntos de estudio relacionados

Hardware and Software Module 11 Quiz

View Set

Chapter 5 Cost-Volume-Profit Relationships

View Set

Chapter 41 Management of Patients with musculoskeletal disorders

View Set

Information Systems Project Mgmt - Chapter 7 Quiz

View Set

NASM Chapter 7 Concepts and Program Designs

View Set

22B Smlouvy příkazní, zprostředkovatelské, komisní a o obchodním zastoupení (pojmové náležitosti - jejich rozbor)

View Set

exam 3 questions class, ati, nursing.com

View Set