Java programming chapter 1 chapter 2 Review

15 Просмотры
Издатель
What is an object?
An object is an entity that you can manipulate by calling one or more of its methods.

An object is a type of class.
For example

JFrame frame = new JFrame();

Rectangle box = new Rectangle(5,10,20,30);

Ellipse2D.Double head = new Ellipse2D.Double(5,10,100,100);

Explain the “new” operator.
“new” is a special operator which constructs object instances.

For example:

With Argument
Rectangle box = new Rectangle(5,10,30,50);
Circle c = new Circle(5);
Without arguments
JFrame frame = new JFrame();
Snowman mySnowman = new Snowman();
What is the difference between parameter and argument?
An argument is the instance passed to the method during runtime. The term parameter refers to any declaration within the parentheses following the method/function name in a method/function declaration or definition; the term argument refers to any expression within the parentheses of a method/function call.

a parameter is what appears in the definition of the method. An argument is the instance passed to the method during runtime.



Parameters

paramterArgument.PNG

double x, double y, double w, double h are called parameter.

Rectangle box = new Rectangle(5,10,30,50);

5, 10, 30, 50 are called Arguments

What does API stands for?
Application Programming Interface
The API document lists the classes and methods of the Java library.
Thereare 5,000 or so classes built-in to Java, and programmers have written hundreds of thousands if not millions of their
Java classes are grouped into packages. Use the import statements to use classes that are declared in other package.
Which String method would allow a programmer to delete all “-“to obtain the string "1234567890" from the input string "123-456-7890"?
String inputs ="562-692-0921";
String replacement = inputs.replaceAll("\\-", "");
out.println(inputs);
out.println(replacement);
What g2 methods allow a programmer to fill a triangle shape?
g2.fillPolygon(xpoints, ypoints, nPoints);

xpoints is an integer array which contains 3 coordinate of x points

ypoints is an integer array which contains 3 coordinate of y points

nPoints =3

What g2 methods allow a programmer to draw a pentagon shape?
g2.drawPolygon(xpoints, ypoints, nPoints);

xpoints is an integer array which contains 5 coordinate of x points

ypoints is an integer array which contains 5 coordinate of y points

nPoints =5



What g2 methods allow a programmer to fill a hexagon shape?
g2.fillPolygon(xpoints, ypoints, nPoints);

xpoints is an integer array which contains 6 coordinate of x points

ypoints is an integer array which contains 6 coordinate of y points

nPoints = 6

A String object river is initialized to “Mississippi”, Which String method allows a programmer to compute the length of the String?
river.length()

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

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



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



13.  Give instructions to draw a string consisting of the letter “V”.

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

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

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));
Категория
Язык программирования Java
Комментариев нет.