Blog o’ Will Cook for Food
Not just another Wordpress blog

May
06

5.6

1.

To scale the panel size to the image displayed, use the getPanelWidth() and getPanelHeight() functions

2.

Circle(double x, double y, double r, Color c)

double z = scanner.nextDouble()

void setRadius(double z)

3.

for detecting mouse motion, the method MouseMotionAdapter is used.

for detecting mouse clicking, the method MouseAdapter is used.

6.8

1.

to set up a timer, you must use the methods start and actionPerformed. The timer counts time from when it starts, and when its interval of time passes, it activates the actionPerformed method. The timer is used to do certain things at regular intervals.

2. A rapid display of successive frames allows us to see moving graphical images.

3. Flicker is caused by the speed of the display medium with large, fast moving images. Slowing down the image would reduce flicker.

4. The direction and velocity of an object can tell you where it will be placed after a period of time, and can be tracked with certain functions. You can also tell the circle to move according to its velocity and direction with void move().

Apr
24

2.7

1.

255, 255, 255

0,0,0

0,0,255

128,128,128.

2.

Frames are used for housing the elements of the GUI, panels show colors, text or images, and layout managers determine how everything in multiple panels and frames are laid out.

3.

A border layout arranges panels in 5 places, top, left, right, bottom, and center.

4.

pane.setLayout( new GridLayout(5, 5));

pane.add(panel1)

pane.add(panel2)

pane.add(panel3)

pane.add(panel25)

3.7

1.

drawRect (int 45, int 20, int 100, int 50)

drawLine (int 20, int 20, int 100, int 100)

drawOval (int 100, int 100, int 50, int 50)

drawLine (int 50, int 50, int 200, int 200)

drawLine (int 200, int 200, int 100, int 100)

drawLine (int 100, int 100, int 50, int 50)

2. To draw a filled blue rectangle on a red background, you need to set the background of the panel with g.setColor (Color.red);

g.drawRect (x, y, z, a);

g.setColor (Color.blue)

3.

The center point of a panel can be found with the getHeight and getWidth functions.

4. color, font style, font size.

4.10

1.

String inputName = JOption.Pane.showInputDialog(”Enter your name” , ” “);

2.

JOptionPane.showMessageDialog( “My name is Will” /n “My address is 1, Two st., Threed.”);

3. parseInt and parseDouble processes the String based number into a double or an integer.

4. By letting the message box set its own size, the message would fit perfectly and not get cut off accidentally.

Apr
15

1.

Recursive definitions are not circular because finding sum(n) is finding the sum of n + whatever comes before n, cycling down to 1.

2.

a recursive method must have a stopping state and a recursive step.

3.

4.

Using recursion allows a user to find large sums or the sum of a large series of numbers.

5.

2 x 5

2 x 4

2 x 3

2 x 2

2 x 1

1

6. This method prints n until n = 0

if n = 3, this would print

3

Feb
14

9.1.1

With an array, all of the variables can be processed as a group, instead of individually.

9.1.2

to access an item in an array, type

nameofarray[number_of_item]

9.1.3

Wages of the Week

5.23 = wages[0] – Monday

6.23 = wages[1] – Tuesday

6.83 = wages[2] – Wednesday

5.36 = wages[3] – Thursday

6.21 = wages[4] – Friday

9.2.1

a a[1] = 23

b a[a.length - 1] = 5-1 = 4

c a[2]-a[3] = 67-89 = -22

9.2.2

that number is out of bounds and will produce an error.

9.3.1

int x = 0

while (x < 5)

system.out.println(array[x])

x++

9.3.2

array goes from [0] to [5]

int x = 6

while (x > 0)

x–

system.out.print(array[x]

9.3.3

int index

index = 0

int i

i = -1

while (i <= 10){

i++

if xyz[i] < 0

index++

9.3.4

a. while i is less than the length of the array, increase i and find the absolute value of the element of the array corresponding to i.

b. while i is less than the length of the array, increase i. the string is the same value as the element a[i]

9.3.5

with length, you can easily find how many elements are in the array in total.

Feb
01

8.1

1. hypertext consists of nodes and links between them

2. hypermedia adds Guis, images, sound, annimation and appliactions to hypertext.

3. uniform resource locators, web addresses.

8.2

1. Hypertext markup language

2. they tell the computer how to show the text and website

3. <HTML>

<HEAD> <TITLE> YO </TITLE> </HEAD>

<BODY> SUP </BODY>

</HTML>

4. an HTML comment is <!– this –>

8.3

1. to display several lines of text without word wrap

2. to show text “as is”

3.

<H1> level 1 </H1>

<P ALIGN=CENTER> Text. </P ALIGN=CENTER>

<H2> level 2 </H2>

8.4

1. the following text will be italic

2. escape sequences allow special characters like < > and & to be displayed, instead of considered as code for HTML

8.5

1.Definition (displays terms and definitions), Ordered (lists things 1, 2, 3…), Nested (Lists inside other lists, sometimes a few levels deep)

2.

<UL>

<LI> Mother — Amanda

<OL>

<LI> Grandmother — Penny

<LI> Grandfather — Roy

</OL>

<LI> Father — Neil

<OL>

<LI> Grandfather — Don

<LI> Grandmother — Pat

</OL>

</UL>

8.6

1. <A>

2. an absolute path name is a path that specifies the exact position of the file in the computer’s directorial structure.

3. A relative path name is one that specifies the location of a target document

4. <A href=”http://www.worcesteracademy.org”>WA Website</A>

8.7

1. an inline image is a graphical image displayed when a user opens a page external images are links to images not shown until the link is clicked.

2. <IMG src=”somepic.jpg”>

3. <IMG src=”image.gif” ALIGN=CENTER HEIGHT=200 WIDTH=200>

4.Either by linking to the picture with text or showing a thumbnail of the picture

<A href=”image.gif”>Some picture</A>

<A href=”image.gif”><IMG src=”image.gif”></A>

8.8

1. To make a table, you use <TABLE> to start it, <CAPTION> for the title, <TR> makes a new row, <TH> defines a header cell, and <TD> defines a data cell.

2.

<TABLE>

<CAPTION ALIGN=CENTER>

A number table

</CAPTION>

<TR>

<TD> 1 </TD>

<TD> 2 </TD>

<TD> 3 </TD>

</TR>

<TR>

<TD> 4 </TD>

<TD> 5 </TD>

<TD> 6 </TD>

</TR>

<TR>

<TD> 7 </TD>

<TD> 8 </TD>

<TD> 9 </TD>

</TR>

</TABLE>

Jan
17

7.2.1

The structure of a query-controlled loop for processing repeated sets of inputs allows users to repeat a program without running it twice, simply by using a while command.

7.2.2

a simple line of code could be added to the method to make it repeat, either with a user option, or when an invalid string was entered.

7.3.1

menus can add organization programs as well as easily integrate loops. Menus help make the program more presentable as well.

7.3.2

menu-driven command loops let the program repeat easily until the user tells the program to stop. The structure is made to repeat after an input unless the user chooses the “Goodbye” option from the menu.

7.3.3

Finished

Dec
07

6.1.1

P—|–Q–| ! ((P || Q) && (P && Q))

true|true | false

true|false| true

false|true| true

false|false| true

6.1.2

a. true

b. false

c. true

d. false

6.1.4

Grouping, Method Selector, Unary plus, Unary minus, Not, Multiplication, Division, Remainder or Modulus, Addition, Subtraction, And, Or, Assignment Operators.

6.1.5

if (x < p || x > q)

System.out.println(”reject”);

6.2.1

a.

numbers to test = 0, 1, -1

b.

numbers to test = -5, 10, 110

6.2.2

With complete code coverage, each line of code in the program is executed at least once, so everything gets tested.

6.2.3

everything that tests a program in the same way belongs to the same equivalence class, so testing two integers that are both accepted in the code, like testing 5 and 8 when the number must be between 1 and 100, would be considered an equivalence class.

6.2.4

boundary conditions are the boundaries between equivalence classes, so for testing the aforementioned numbers, it would be plugging in 0, 1, and 2.

6.2.5

Extreme conditions are the limits of the validity in the code, so to test them in this, they would be 0, and 100.

6.2.6

The tested equivalence classes would be between 0 and 60, and between 60 and 100.

The boundary conditions to test would be 59, 60, and 61.

The extreme conditions would be 0, 1, 99, and 100.

6.3.1

|Before Noon|Monday|___________Result_________|

|True______|True|____Take the computer science quiz|

|True______|False|_______________Go to gym class|

|False______|True|______Throw a Frisbee in the quad|

|False______|False|______Throw a Frisbee in the quad|

6.3.2

Nested if statements are programs that have multiple solutions which require the first if statement to be true in order to consider the second. Multiway statements do not require the first if to be true to consider the second.

6.4.1

if (income > 10000)

rate = 0.10;

else if (income > 20000)

rate = 0.18;

else if (income > 50000)

rate 0.40;

else

rate = 0.0

This code is written is such a way that anything above 10,000 would result in a rate of 10%. The code says that anything greater than 20,000 would be 18%, but that is only if 20,000 was less than 10,000, which is false.

6.4.2

if (income > 50000)

rate 0.40;

else if (income > 20000)

rate = 0.18;

else if (income > 10000)

rate = 0.10;

else

rate = 0.0

6.5.1

a.

1 2 3 1 2 3 1 2 3…

b.

1 2 3 1 2 3 1 2 3 …

6.5.2

for (int x = 1; x<=25; x++){
if(x % 5 == 0 ){

System.out.print(x);
System.out.println(” “);
}
System.out.print(x + ” “);

Nov
05

5.1

1. An object is a runtime entity that contains data and responds to messages, while a class is a software package or template that describes the characteristics of similar objects. Classes are used with objects, but objects do not need to be used with classes.

2. When an object’s memory storage is no longer referenced by a variable, it is deleted, using the process “garbage collection”

3. The behavior, the state, and the identity of an object are three characteristics of objects that are important.

4. The server sends information to the client, in the form of messages.

5. The interface of a class is the list of methods supported by the server.

5.2

1. mutators are messages that change an object’s state. To access the objects state and check to see if the mutators worked correctly, accessors are used.

2. public and private are both visibility modifiers. When neither of them are used, the program basically labels everything as public. Private is used for most instance variables.

3. Constructors are used to initialize the instance variables of a newly instantiated object. They are activated by “new”.

4. toString obtains the string representation for the object

5. a variable can be assigned to another variable to represent the same object or value. An example would be s1 = s2, making s1 equal to s2.

6. in Java, primitive and reference type variables are handled differently in memory, an example of primitive is int, and an example of reference is String.

7. the null value, once assigned to a reference variable, causes the computer to reclaim the object’s memory during garbage collection.

8. when a program attempts to run a method with an obhject that is null, Java throws a null pointer exception.

String str = null;

System.out.println (str.length());

9. default constructors have empty parameter lists.

10. it provides a default constructor

11. constructors that expect another object of the same class can be used to chain constructors.

5.4

1. parameters listed in a method’s definition are formal parameters, while values passed to a method when invoked are called arguments or actual parameters.

2. the purpose of a parameter is the pass information to a method.

3.

public int getAverage(){

int sum;

int num1;

int num2;

sum = (int) Math.add(num1 + num2);

debug(”Sum:”, sum);

return sum;

4. local variables are temporary working storage for data in a method.

5.5

1. the lifetime of a variable is the period in which it can be used. Local variables and formal parameters exist for one execution of the method. Instance variables last for the lifetime of an object.

2. shadowing is dangerous because it increases the chances of getting a programming error.

3. a. the variables in the code are a, b, x, y, c, d.

b. a and b are both global variables. c and d are local variables.

c.  a and b do not expire, while c, d, x, and y are part of the method, and are no longer accessible when the method stops executing, so are wiped and renewed when the method starts again.

Oct
11

1 a.

i is equal to 1. while i is less than or equal to the variable ‘limit’, add one to i.

if i divided by 2 results in the remainder 0, print the value of i.

b.       Generate a new random number. The computer assigns a number between 1 and 10. Declare variable x to have a value of 0, and declare the variable “yourNumber”

While x is equal to 0, print out the lines

“I’m guessing a number between 1 and 10. Which number is it?”

The user inputs a number which is assigned to the variable “yourNumber”

If the variables’ values are the same, the computer will print

“That’s it!”

if not, the computer will print

“Sorry, try again.”

2.

a.

x = 0

while (x < 10){

System.out.println��(x * x)

System.out.println(x * x * x)

x++

x++

}

b.

x = 20

while (x > 0){

–x

–x

System.out.println(x)

}

4.8

1. opens a scanner on the file “myfile.txt”

4.9

1. a.
b.  number += 2; is not a valid statement

Sep
23

3.1

1. The vocabulary of a language is all of the symbols and words recognized by the language. Some examples of items in Java’s vocabulary are +, -, =, and 4.216

2. An example of a syntax rule in Java is that parentheses must always be arranged in the correct order: ( ), never ) (.

3. (x + y) * z means: add x to y and multiply the sum by z.

4. Programming languages need exact syntax and grammar, and are followed extremely literally, unlike English, where instructions can be more vague.

3.2

1. A double is a number variable that can use decimals. an int is an integer number variable, so it does not use decimals.

2. Syntax for non numeric data types must be defined before use. Numeric data types can be used as they are.

3. a.     2.35E1

b.    4.6E-2

4. a.    322100

b. 0.0556

5. One example of a string literal is “Enter first number to add: ” Another example is “The sum of the numbers is:”

6. A variable is called a variable because it does not have a fixed value; one is assigned  to it, making it variable.

7. scanner, double, int, and string (not in chapter 2)

8. double payRate =  $35.67

9. int a, b = 4, c

10. non-numeric data types cannot be stored in data type int, nor can numbers with decimals, such as 3.5235.

11. final double pound = kilogram * 2.2

12. x = 5, y = 10

a. x + y * 2         5 + 20 = 25

b. x-y*2             5 – 20 = -15

c. (x+y)*2        (15)*2 = 30

d. y % x             10/5 = 0

13.

a. a – * b + c: – and * are next to each other.

b. – (a + b) * c): c) has no open parentheses, only close parentheses.

c. (): there are no units to make this do anything.

14. x = 4.5 and y = 2

a. x / y                   4.5/2 = 2.25

b. y / x                   2 / 4.5 = .44

c. x % y                   remainder of 4.5 from 2 = .5

15.

a. x = z: valid

b. x = y * z: valid

c. z = x + y: syntax error

16. x = double, 4.5. y = int, 2.

a. (int) x * y = 9

b. (int) (x * y) = 9

17.     y = (int) x

18. x = “Wizard”    y = “Java”

a. y + x = “Java” + “Wizard”

b. y + y.length() + x = “Java” + “Java”.length() + “Wizard”

c. y + “\n” + x + “\n”

“Java”

“Wizard”

19. String myInfo = “Will Cook” + ”\n” + “19 Carter road” “\n” “783 0330″ “\n”

20. Methods are parameter based, and messages can correspond to them, but only if they have the same name as the method.

21. In a method’s signature, the type of value it returns is important for messages that are corrseponding to it. It’s name is important because a message’s name must be the same if they are to correspond. Also, the number and type of the parameters it expects are important for how the message corresponds to it.

22.

a. valid

b. valid

c. invalid – starts with a digit

d. valid

e. invalid – contains a “?”

23.

a. double circleDiameter

b. finaldouble incomeTaxDeduction

c. double drawRectangle

24. import x.y.z;      x is the overall name of the package, y is the name of a subsection in the package, and z is the name of a particular class in that subsection.

25. import x.y.* would make the computer load all the classes under the given subsection at once.

3.3

1. a 

System.out.print (”Enter your hourly wage: “);

wage = reader.nextDouble();

System.out.print (”your hourly wage is ” + wage + “.”);

System.out.print (”Enter your Social Security number: “);

ssn = reader.nextString();

System.out.print (”your Social Security number is ” + ssn + “.”);

2. nextInt retuns the first integer in the input line.

3. the program will return the object specified by the method.

3.4

1. an end of line comment includes all text following //, better for single lines of comments.

a multi line comment includes everything between an opening /* and a closing */, good for comments longer than one line.

2. begin most programs with comments about the purpose of the program.

certain segments of code should have comments preceding them to explain their functions.

3.5

1. syntax errors are checked for and detected during the compiling process. Run-time errors are found when trying to run the program, and logic errors can be found by the user when the program is run.

2. One example of a run-time error would be telling the program to divide by zero. This would keep the program from running. These errors are not detected during the compiling process because they can be written in ways that the program can understand, but not see as errors, and cannot be performed.

3.

a. run-time error

b. compile-time error

c. logic error